-
Notifications
You must be signed in to change notification settings - Fork 107
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
initial load for queues #1675
Merged
Merged
initial load for queues #1675
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
426f410
kafka initial load
serprex 1719056
consolidate kafka lua pool init
serprex ab60ed3
e2e
serprex 1f084e7
pubsub
serprex ec35652
fix pubsub race
serprex 2b338d2
fix script for initial load
serprex 3badb8d
rename wgCtx/wgErr
serprex 26e5f96
Merge branch 'main' into queues-initial-load
serprex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package connkafka | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/twmb/franz-go/pkg/kgo" | ||
lua "github.com/yuin/gopher-lua" | ||
|
||
"github.com/PeerDB-io/peer-flow/generated/protos" | ||
"github.com/PeerDB-io/peer-flow/model" | ||
"github.com/PeerDB-io/peer-flow/pua" | ||
) | ||
|
||
func (*KafkaConnector) SetupQRepMetadataTables(_ context.Context, _ *protos.QRepConfig) error { | ||
return nil | ||
} | ||
|
||
func (c *KafkaConnector) SyncQRepRecords( | ||
ctx context.Context, | ||
config *protos.QRepConfig, | ||
partition *protos.QRepPartition, | ||
stream *model.QRecordStream, | ||
) (int, error) { | ||
startTime := time.Now() | ||
numRecords := atomic.Int64{} | ||
schema := stream.Schema() | ||
|
||
queueCtx, queueErr := context.WithCancelCause(ctx) | ||
pool, err := c.createPool(queueCtx, config.Script, config.FlowJobName, queueErr) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer pool.Close() | ||
|
||
Loop: | ||
for { | ||
select { | ||
case qrecord, ok := <-stream.Records: | ||
if !ok { | ||
c.logger.Info("flushing batches because no more records") | ||
break Loop | ||
} | ||
|
||
pool.Run(func(ls *lua.LState) []*kgo.Record { | ||
items := model.NewRecordItems(len(qrecord)) | ||
for i, val := range qrecord { | ||
items.AddColumn(schema.Fields[i].Name, val) | ||
} | ||
record := &model.InsertRecord[model.RecordItems]{ | ||
BaseRecord: model.BaseRecord{}, | ||
Items: items, | ||
SourceTableName: config.WatermarkTable, | ||
DestinationTableName: config.DestinationTableIdentifier, | ||
CommitID: 0, | ||
} | ||
|
||
lfn := ls.Env.RawGetString("onRecord") | ||
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. doesn't cdc have to do something similar as well? 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. Yes, but code is mixed up with tracking LSN/counts |
||
fn, ok := lfn.(*lua.LFunction) | ||
if !ok { | ||
queueErr(fmt.Errorf("script should define `onRecord` as function, not %s", lfn)) | ||
return nil | ||
} | ||
|
||
ls.Push(fn) | ||
ls.Push(pua.LuaRecord.New(ls, record)) | ||
err := ls.PCall(1, -1, nil) | ||
if err != nil { | ||
queueErr(fmt.Errorf("script failed: %w", err)) | ||
return nil | ||
} | ||
|
||
args := ls.GetTop() | ||
results := make([]*kgo.Record, 0, args) | ||
for i := range args { | ||
kr, err := lvalueToKafkaRecord(ls, ls.Get(i-args)) | ||
if err != nil { | ||
queueErr(err) | ||
return nil | ||
} | ||
if kr != nil { | ||
if kr.Topic == "" { | ||
kr.Topic = record.GetDestinationTableName() | ||
} | ||
results = append(results, kr) | ||
} | ||
} | ||
ls.SetTop(0) | ||
numRecords.Add(1) | ||
return results | ||
}) | ||
|
||
case <-queueCtx.Done(): | ||
break Loop | ||
} | ||
} | ||
|
||
if err := pool.Wait(queueCtx); err != nil { | ||
return 0, err | ||
} | ||
if err := c.client.Flush(queueCtx); err != nil { | ||
return 0, fmt.Errorf("[kafka] final flush error: %w", err) | ||
} | ||
|
||
if err := c.FinishQRepPartition(ctx, partition, config.FlowJobName, startTime); err != nil { | ||
return 0, err | ||
} | ||
return int(numRecords.Load()), nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
seems like a lot of the code below is shared between pub-sub and Kafka, can we unify it?
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.
Also share quite a bit between cdc/qrep. It'll be a mess of conditions. Would be better as an isolated PR