-
Notifications
You must be signed in to change notification settings - Fork 248
perf(jdbc): optimize incremental sync with MapScanConcurrent #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from 3 commits
89488dc
18e2227
06020f6
94408cf
a65a316
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
|
|
||
| "github.com/datazip-inc/olake/constants" | ||
|
|
@@ -21,25 +22,15 @@ | |
| return fmt.Errorf("failed to build incremental condition: %s", err) | ||
| } | ||
|
|
||
| rows, err := p.client.QueryContext(ctx, incrementalQuery, queryArgs...) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to execute incremental query: %s", err) | ||
| } | ||
| defer rows.Close() | ||
| setter := jdbc.NewReader(ctx, incrementalQuery, func(ctx context.Context, query string, args ...any) (*sql.Rows, error) { | ||
| return p.client.QueryContext(ctx, query, args...) | ||
| }, queryArgs...) | ||
|
|
||
| for rows.Next() { | ||
| record := make(types.Record) | ||
| rowBytes, err := jdbc.MapScan(rows, record, p.dataTypeConverter, pgColumnSizer) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to scan record: %s", err) | ||
| } | ||
|
|
||
| if err := processFn(ctx, record, rowBytes); err != nil { | ||
| return fmt.Errorf("process error: %s", err) | ||
| } | ||
| if err := jdbc.MapScanConcurrent(setter, p.dataTypeConverter, processFn, pgColumnSizer); err != nil { | ||
| return fmt.Errorf("incremental process error: %s", err) | ||
| } | ||
|
|
||
| return rows.Err() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant line here, install go formatter in your IDE to avoid such mistakes... |
||
| return nil | ||
| } | ||
|
|
||
| func (p *Postgres) FetchMaxCursorValues(ctx context.Context, stream types.StreamInterface) (any, any, error) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,9 @@ func (o *Reader[T]) Capture(onCapture func(T) error) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
| if closer, ok := any(rows).(interface{ Close() error }); ok { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing this, we should just add Close() in iterable as we already use sql.Rows which needs closing so its better to have it in the interface by design rather than guessing |
||
| defer func() { _ = closer.Close() }() | ||
| } | ||
|
|
||
| for rows.Next() { | ||
| err := onCapture(rows) | ||
|
|
@@ -87,48 +90,6 @@ func normalizeDataTypeAndConvert(rawData any, colType *sql.ColumnType, converter | |
| return conv, nil | ||
| } | ||
|
|
||
| // TODO: Use MapScanConcurrent instead of MapScan for incremental as well | ||
| // | ||
| // MapScan scans the current row into dest and returns the row's source-DB byte | ||
| // size. columnSizer maps a SQL column type to a function that sizes one raw | ||
| // (pre-conversion) value of that column; the size is summed inline in the scan | ||
| // loop. NULL values carry no data and are skipped (0 bytes). | ||
| func MapScan(rows *sql.Rows, dest map[string]any, converter func(value interface{}, columnType string) (interface{}, error), columnSizer func(colType *sql.ColumnType) func(v any) int64) (int64, error) { | ||
| columns, colTypes, err := getColumnMetadata(rows) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| scanValues := make([]any, len(columns)) | ||
| for i := range scanValues { | ||
| scanValues[i] = new(any) // Allocate pointers for scanning | ||
| } | ||
|
|
||
| if err := rows.Scan(scanValues...); err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| var rowBytes int64 | ||
| for i, col := range columns { | ||
| rawData := *(scanValues[i].(*any)) // Dereference pointer before storing | ||
| // If rawData is nil, no byte is added | ||
| if rawData != nil { | ||
| rowBytes += columnSizer(colTypes[i])(rawData) | ||
| } | ||
| if converter != nil { | ||
| conv, err := normalizeDataTypeAndConvert(rawData, colTypes[i], converter) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| dest[col] = conv | ||
| } else { | ||
| dest[col] = rawData | ||
| } | ||
| } | ||
|
|
||
| return rowBytes, nil | ||
| } | ||
|
|
||
| // MapScanConcurrent scans rows concurrently using a producer/consumer pattern. | ||
| // columnSizer maps a SQL column type to a function that sizes one raw | ||
| // (pre-conversion) value of that column. Since column types are constant for the | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to handle closing the opened rows explicitly. JDBC backfills use defer tx.Rollback() which handles this but I don't think this is the case here...
Find out if there is a way to do this from here, but if we need to change the
jdbc.MapScanConcurrentthen we will need to test it thoroughly