-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathflowable_core.go
More file actions
732 lines (651 loc) · 24.3 KB
/
Copy pathflowable_core.go
File metadata and controls
732 lines (651 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// internal methods for flowable.go
package activities
import (
"context"
"errors"
"fmt"
"log/slog"
"slices"
"sync/atomic"
"time"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/log"
"go.temporal.io/sdk/temporal"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"github.com/PeerDB-io/peerdb/flow/connectors"
connmysql "github.com/PeerDB-io/peerdb/flow/connectors/mysql"
connpostgres "github.com/PeerDB-io/peerdb/flow/connectors/postgres"
"github.com/PeerDB-io/peerdb/flow/connectors/utils/monitoring"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/internal"
"github.com/PeerDB-io/peerdb/flow/model"
"github.com/PeerDB-io/peerdb/flow/otel_metrics"
"github.com/PeerDB-io/peerdb/flow/shared"
"github.com/PeerDB-io/peerdb/flow/shared/exceptions"
)
type PeerType string
const (
Source PeerType = "source"
Destination PeerType = "destination"
)
func heartbeatRoutine(
ctx context.Context,
message func() string,
) func() {
counter := 0
return shared.Interval(
ctx,
15*time.Second,
func() {
counter += 1
activity.RecordHeartbeat(ctx, fmt.Sprintf("heartbeat #%d: %s", counter, message()))
},
)
}
func (a *FlowableActivity) getTableNameSchemaMapping(ctx context.Context, flowName string) (map[string]*protos.TableSchema, error) {
rows, err := a.CatalogPool.Query(ctx, "select table_name, table_schema from table_schema_mapping where flow_name = $1", flowName)
if err != nil {
return nil, err
}
var tableName string
var tableSchemaBytes []byte
tableNameSchemaMapping := make(map[string]*protos.TableSchema)
if _, err := pgx.ForEachRow(rows, []any{&tableName, &tableSchemaBytes}, func() error {
tableSchema := &protos.TableSchema{}
if err := proto.Unmarshal(tableSchemaBytes, tableSchema); err != nil {
return err
}
tableNameSchemaMapping[tableName] = tableSchema
return nil
}); err != nil {
return nil, fmt.Errorf("failed to deserialize table schema proto: %w", err)
}
return tableNameSchemaMapping, nil
}
func (a *FlowableActivity) applySchemaDeltas(
ctx context.Context,
config *protos.FlowConnectionConfigs,
options *protos.SyncFlowOptions,
schemaDeltas []*protos.TableSchemaDelta,
) error {
filteredTableMappings := make([]*protos.TableMapping, 0, len(schemaDeltas))
for _, tableMapping := range options.TableMappings {
if slices.ContainsFunc(schemaDeltas, func(schemaDelta *protos.TableSchemaDelta) bool {
return schemaDelta.SrcTableName == tableMapping.SourceTableIdentifier &&
schemaDelta.DstTableName == tableMapping.DestinationTableIdentifier
}) {
filteredTableMappings = append(filteredTableMappings, tableMapping)
}
}
if len(schemaDeltas) > 0 {
if err := a.SetupTableSchema(ctx, &protos.SetupTableSchemaBatchInput{
PeerName: config.SourceName,
TableMappings: filteredTableMappings,
FlowName: config.FlowJobName,
System: config.System,
Env: config.Env,
Version: config.Version,
}); err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to execute schema update at source: %w", err))
}
}
return nil
}
func syncCore[TPull connectors.CDCPullConnectorCore, TSync connectors.CDCSyncConnectorCore, Items model.Items](
ctx context.Context,
a *FlowableActivity,
config *protos.FlowConnectionConfigs,
options *protos.SyncFlowOptions,
srcConn TPull,
normRequests chan<- NormalizeBatchRequest,
syncingBatchID *atomic.Int64,
syncState *atomic.Pointer[string],
adaptStream func(*model.CDCStream[Items]) (*model.CDCStream[Items], error),
pull func(TPull, context.Context, shared.CatalogPool, *otel_metrics.OtelManager, *model.PullRecordsRequest[Items]) error,
sync func(TSync, context.Context, *model.SyncRecordsRequest[Items]) (*model.SyncResponse, error),
) (*model.SyncResponse, error) {
flowName := config.FlowJobName
ctx = context.WithValue(ctx, shared.FlowNameKey, flowName)
logger := internal.LoggerFromCtx(ctx)
tblNameMapping := make(map[string]model.NameAndExclude, len(options.TableMappings))
for _, v := range options.TableMappings {
tblNameMapping[v.SourceTableIdentifier] = model.NewNameAndExclude(v.DestinationTableIdentifier, v.Exclude)
}
if err := srcConn.ConnectionActive(ctx); err != nil {
return nil, temporal.NewNonRetryableApplicationError("connection to source down", "disconnect", nil)
}
batchSize := options.BatchSize
if batchSize == 0 {
batchSize = 250_000
}
lastOffset, err := func() (model.CdcCheckpoint, error) {
if myConn, isMy := any(srcConn).(*connmysql.MySqlConnector); isMy {
return myConn.GetLastOffset(ctx, config.FlowJobName)
} else {
dstConn, err := connectors.GetByNameAs[TSync](ctx, config.Env, a.CatalogPool, config.DestinationName)
if err != nil {
return model.CdcCheckpoint{}, fmt.Errorf("failed to get destination connector: %w", err)
}
defer connectors.CloseConnector(ctx, dstConn)
return dstConn.GetLastOffset(ctx, config.FlowJobName)
}
}()
if err != nil {
return nil, a.Alerter.LogFlowError(ctx, flowName, err)
}
logger.Info("pulling records...", slog.Any("LastOffset", lastOffset))
consumedOffset := atomic.Int64{}
consumedOffset.Store(lastOffset.ID)
channelBufferSize, err := internal.PeerDBCDCChannelBufferSize(ctx, config.Env)
if err != nil {
return nil, fmt.Errorf("failed to get CDC channel buffer size: %w", err)
}
recordBatchPull := model.NewCDCStream[Items](channelBufferSize)
recordBatchSync := recordBatchPull
if adaptStream != nil {
var err error
if recordBatchSync, err = adaptStream(recordBatchPull); err != nil {
return nil, err
}
}
tableNameSchemaMapping, err := a.getTableNameSchemaMapping(ctx, flowName)
if err != nil {
return nil, err
}
startTime := time.Now()
syncState.Store(shared.Ptr("syncing"))
errGroup, errCtx := errgroup.WithContext(ctx)
errGroup.Go(func() error {
return pull(srcConn, errCtx, a.CatalogPool, a.OtelManager, &model.PullRecordsRequest[Items]{
FlowJobName: flowName,
SrcTableIDNameMapping: options.SrcTableIdNameMapping,
TableNameMapping: tblNameMapping,
LastOffset: lastOffset,
ConsumedOffset: &consumedOffset,
MaxBatchSize: batchSize,
IdleTimeout: internal.PeerDBCDCIdleTimeoutSeconds(
int(options.IdleTimeoutSeconds),
),
TableNameSchemaMapping: tableNameSchemaMapping,
OverridePublicationName: config.PublicationName,
OverrideReplicationSlotName: config.ReplicationSlotName,
RecordStream: recordBatchPull,
Env: config.Env,
InternalVersion: config.Version,
})
})
hasRecords := !recordBatchSync.WaitAndCheckEmpty()
logger.Info("current sync flow has records?", slog.Bool("hasRecords", hasRecords))
if !hasRecords {
// wait for the pull goroutine to finish
if err := errGroup.Wait(); err != nil {
// don't log flow error for "replState changed" and "slot is already active"
if !(temporal.IsApplicationError(err) || shared.IsSQLStateError(err, pgerrcode.ObjectInUse)) {
_ = a.Alerter.LogFlowError(ctx, flowName, err)
}
if temporal.IsApplicationError(err) {
return nil, err
} else {
return nil, fmt.Errorf("failed in pull records when: %w", err)
}
}
logger.Info("no records to push")
dstConn, err := connectors.GetByNameAs[TSync](ctx, config.Env, a.CatalogPool, config.DestinationName)
if err != nil {
return nil, fmt.Errorf("failed to recreate destination connector: %w", err)
}
defer connectors.CloseConnector(ctx, dstConn)
syncState.Store(shared.Ptr("updating schema"))
if err := dstConn.ReplayTableSchemaDeltas(ctx, config.Env, flowName, options.TableMappings, recordBatchSync.SchemaDeltas); err != nil {
return nil, fmt.Errorf("failed to sync schema: %w", err)
}
return nil, a.applySchemaDeltas(ctx, config, options, recordBatchSync.SchemaDeltas)
}
var res *model.SyncResponse
errGroup.Go(func() error {
dstConn, err := connectors.GetByNameAs[TSync](ctx, config.Env, a.CatalogPool, config.DestinationName)
if err != nil {
return fmt.Errorf("failed to recreate destination connector: %w", err)
}
defer connectors.CloseConnector(ctx, dstConn)
syncBatchID, err := dstConn.GetLastSyncBatchID(errCtx, flowName)
if err != nil {
return err
}
syncBatchID += 1
syncingBatchID.Store(syncBatchID)
logger.Info("begin pulling records for batch", slog.Int64("SyncBatchID", syncBatchID))
if err := monitoring.AddCDCBatchForFlow(errCtx, a.CatalogPool, flowName, monitoring.CDCBatchInfo{
BatchID: syncBatchID,
RowsInBatch: 0,
BatchEndlSN: 0,
StartTime: startTime,
}); err != nil {
return a.Alerter.LogFlowError(ctx, flowName, err)
}
res, err = sync(dstConn, errCtx, &model.SyncRecordsRequest[Items]{
SyncBatchID: syncBatchID,
Records: recordBatchSync,
ConsumedOffset: &consumedOffset,
FlowJobName: flowName,
TableMappings: options.TableMappings,
StagingPath: config.CdcStagingPath,
Script: config.Script,
TableNameSchemaMapping: tableNameSchemaMapping,
Env: config.Env,
Version: config.Version,
})
if err != nil {
return a.Alerter.LogFlowError(ctx, flowName, fmt.Errorf("failed to push records: %w", err))
}
for _, warning := range res.Warnings {
a.Alerter.LogFlowWarning(ctx, flowName, warning)
}
logger.Info("finished pulling records for batch", slog.Int64("SyncBatchID", syncBatchID))
return nil
})
syncStartTime := time.Now()
if err := errGroup.Wait(); err != nil {
// don't log flow error for "replState changed" and "slot is already active"
var applicationError *temporal.ApplicationError
if !((errors.As(err, &applicationError) && applicationError.Type() == "desync") || shared.IsSQLStateError(err, pgerrcode.ObjectInUse)) {
_ = a.Alerter.LogFlowError(ctx, flowName, err)
}
if temporal.IsApplicationError(err) {
return nil, err
} else {
return nil, fmt.Errorf("[cdc] failed to pull records: %w", err)
}
}
syncState.Store(shared.Ptr("bookkeeping"))
syncDuration := time.Since(syncStartTime)
lastCheckpoint := recordBatchSync.GetLastCheckpoint()
logger.Info("batch synced", slog.Any("checkpoint", lastCheckpoint))
if err := srcConn.UpdateReplStateLastOffset(ctx, lastCheckpoint); err != nil {
return nil, a.Alerter.LogFlowError(ctx, flowName, err)
}
if err := monitoring.UpdateNumRowsAndEndLSNForCDCBatch(
ctx, a.CatalogPool, flowName, res.CurrentSyncBatchID, uint32(res.NumRecordsSynced), lastCheckpoint,
); err != nil {
return nil, a.Alerter.LogFlowError(ctx, flowName, err)
}
if err := monitoring.UpdateLatestLSNAtTargetForCDCFlow(ctx, a.CatalogPool, flowName, lastCheckpoint.ID); err != nil {
return nil, a.Alerter.LogFlowError(ctx, flowName, err)
}
if res.TableNameRowsMapping != nil {
if err := monitoring.AddCDCBatchTablesForFlow(
ctx, a.CatalogPool, flowName, res.CurrentSyncBatchID, res.TableNameRowsMapping,
); err != nil {
return nil, err
}
}
a.Alerter.LogFlowInfo(ctx, flowName, fmt.Sprintf("stored %d records into intermediate storage for batch %d in %v",
res.NumRecordsSynced, res.CurrentSyncBatchID, syncDuration.Truncate(time.Second)))
a.OtelManager.Metrics.CurrentBatchIdGauge.Record(ctx, res.CurrentSyncBatchID)
syncState.Store(shared.Ptr("updating schema"))
if err := a.applySchemaDeltas(ctx, config, options, res.TableSchemaDeltas); err != nil {
return nil, err
}
if recordBatchSync.NeedsNormalize() {
parallel, err := internal.PeerDBEnableParallelSyncNormalize(ctx, config.Env)
if err != nil {
return nil, err
}
var done chan struct{}
if !parallel {
done = make(chan struct{})
}
syncState.Store(shared.Ptr("normalizing"))
select {
case normRequests <- NormalizeBatchRequest{BatchID: res.CurrentSyncBatchID, Done: done}:
case <-ctx.Done():
return res, nil
}
if done != nil {
select {
case <-done:
case <-ctx.Done():
return res, nil
}
}
}
return res, nil
}
func (a *FlowableActivity) getPostgresPeerConfigs(ctx context.Context) ([]*protos.Peer, error) {
optionRows, err := a.CatalogPool.Query(ctx, `
SELECT p.name, p.options, p.enc_key_id
FROM peers p
WHERE p.type = $1 AND EXISTS(SELECT * FROM flows f WHERE p.id = f.source_peer)`, protos.DBType_POSTGRES)
if err != nil {
return nil, err
}
return pgx.CollectRows(optionRows, func(row pgx.CollectableRow) (*protos.Peer, error) {
var peerName string
var encPeerOptions []byte
var encKeyID string
if err := optionRows.Scan(&peerName, &encPeerOptions, &encKeyID); err != nil {
return nil, err
}
peerOptions, err := internal.Decrypt(ctx, encKeyID, encPeerOptions)
if err != nil {
return nil, err
}
var pgPeerConfig protos.PostgresConfig
unmarshalErr := proto.Unmarshal(peerOptions, &pgPeerConfig)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return &protos.Peer{
Name: peerName,
Type: protos.DBType_POSTGRES,
Config: &protos.Peer_PostgresConfig{PostgresConfig: &pgPeerConfig},
}, nil
})
}
// replicateQRepPartition replicates a QRepPartition from the source to the destination.
func replicateQRepPartition[TRead any, TWrite StreamCloser, TSync connectors.QRepSyncConnectorCore, TPull connectors.QRepPullConnectorCore](
ctx context.Context,
a *FlowableActivity,
config *protos.QRepConfig,
partition *protos.QRepPartition,
runUUID string,
stream TWrite,
outstream TRead,
pullRecords func(
TPull,
context.Context, *protos.QRepConfig,
*protos.QRepPartition,
TWrite,
) (int64, int64, error),
syncRecords func(TSync, context.Context, *protos.QRepConfig, *protos.QRepPartition, TRead) (int64, shared.QRepWarnings, error),
) error {
ctx = context.WithValue(ctx, shared.FlowNameKey, config.FlowJobName)
logger := log.With(internal.LoggerFromCtx(ctx), slog.String(string(shared.FlowNameKey), config.FlowJobName))
dstConn, err := connectors.GetByNameAs[TSync](ctx, config.Env, a.CatalogPool, config.DestinationName)
if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to get qrep destination connector: %w", err))
}
defer connectors.CloseConnector(ctx, dstConn)
done, err := dstConn.IsQRepPartitionSynced(ctx, &protos.IsQRepPartitionSyncedInput{
FlowJobName: config.FlowJobName,
PartitionId: partition.PartitionId,
})
if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to get fetch status of partition: %w", err))
}
if done {
logger.Info("no records to push for partition " + partition.PartitionId)
activity.RecordHeartbeat(ctx, "no records to push for partition "+partition.PartitionId)
return nil
}
if err := monitoring.UpdateStartTimeForPartition(ctx, a.CatalogPool, runUUID, partition, time.Now()); err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to update start time for partition: %w", err))
}
logger.Info("replicating partition", slog.String("partitionId", partition.PartitionId))
var rowsSynced int64
errGroup, errCtx := errgroup.WithContext(ctx)
errGroup.Go(func() error {
srcConn, err := connectors.GetByNameAs[TPull](ctx, config.Env, a.CatalogPool, config.SourceName)
if err != nil {
stream.Close(err)
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to get qrep source connector: %w", err))
}
defer connectors.CloseConnector(ctx, srcConn)
numRecords, numBytes, err := pullRecords(srcConn, errCtx, config, partition, stream)
if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("[qrep] failed to pull records: %w", err))
}
a.OtelManager.Metrics.FetchedBytesCounter.Add(ctx, numBytes)
if err := monitoring.UpdatePullEndTimeAndRowsForPartition(
errCtx, a.CatalogPool, runUUID, partition, numRecords,
); err != nil {
logger.Error(err.Error())
}
return nil
})
errGroup.Go(func() error {
var warnings shared.QRepWarnings
var err error
rowsSynced, warnings, err = syncRecords(dstConn, errCtx, config, partition, outstream)
if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to sync records: %w", err))
}
for _, warning := range warnings {
a.Alerter.LogFlowWarning(ctx, config.FlowJobName, warning)
}
return context.Canceled
})
if err := errGroup.Wait(); err != nil && err != context.Canceled {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, err)
}
if rowsSynced > 0 {
logger.Info(fmt.Sprintf("pushed %d records", rowsSynced))
if err := monitoring.UpdateRowsSyncedForPartition(ctx, a.CatalogPool, rowsSynced, runUUID, partition); err != nil {
return err
}
}
return monitoring.UpdateEndTimeForPartition(ctx, a.CatalogPool, runUUID, partition)
}
// replicateXminPartition replicates a XminPartition from the source to the destination.
func replicateXminPartition[TRead any, TWrite any, TSync connectors.QRepSyncConnectorCore](
ctx context.Context,
a *FlowableActivity,
config *protos.QRepConfig,
partition *protos.QRepPartition,
runUUID string,
stream TWrite,
outstream TRead,
pullRecords func(
*connpostgres.PostgresConnector,
context.Context, *protos.QRepConfig,
*protos.QRepPartition,
TWrite,
) (int64, int64, int64, error),
syncRecords func(TSync, context.Context, *protos.QRepConfig, *protos.QRepPartition, TRead) (int64, shared.QRepWarnings, error),
) (int64, error) {
ctx = context.WithValue(ctx, shared.FlowNameKey, config.FlowJobName)
logger := internal.LoggerFromCtx(ctx)
logger.Info("replicating xmin")
errGroup, errCtx := errgroup.WithContext(ctx)
startTime := time.Now()
var currentSnapshotXmin int64
var rowsSynced int64
errGroup.Go(func() error {
srcConn, err := connectors.GetByNameAs[*connpostgres.PostgresConnector](ctx, config.Env, a.CatalogPool, config.SourceName)
if err != nil {
return fmt.Errorf("failed to get qrep source connector: %w", err)
}
defer connectors.CloseConnector(ctx, srcConn)
var pullErr error
var numRecords int64
var numBytes int64
numRecords, numBytes, currentSnapshotXmin, pullErr = pullRecords(srcConn, ctx, config, partition, stream)
if pullErr != nil {
logger.Warn(fmt.Sprintf("[xmin] failed to pull records: %v", pullErr))
return a.Alerter.LogFlowError(ctx, config.FlowJobName, pullErr)
}
// The first sync of an XMIN mirror will have a partition without a range
// A nil range is not supported by the catalog mirror monitor functions below
// So I'm creating a partition with a range of 0 to numRecords
partitionForMetrics := partition
if partition.Range == nil {
partitionForMetrics = &protos.QRepPartition{
PartitionId: partition.PartitionId,
Range: &protos.PartitionRange{
Range: &protos.PartitionRange_IntRange{
IntRange: &protos.IntPartitionRange{Start: 0, End: numRecords},
},
},
}
}
if err := monitoring.InitializeQRepRun(
ctx, logger, a.CatalogPool, config, runUUID, []*protos.QRepPartition{partitionForMetrics}, config.ParentMirrorName,
); err != nil {
return err
}
if err := monitoring.UpdateStartTimeForPartition(ctx, a.CatalogPool, runUUID, partition, startTime); err != nil {
return fmt.Errorf("failed to update start time for partition: %w", err)
}
a.OtelManager.Metrics.FetchedBytesCounter.Add(ctx, numBytes)
if err := monitoring.UpdatePullEndTimeAndRowsForPartition(
errCtx, a.CatalogPool, runUUID, partition, numRecords,
); err != nil {
logger.Error(err.Error())
return err
}
return nil
})
errGroup.Go(func() error {
dstConn, err := connectors.GetByNameAs[TSync](ctx, config.Env, a.CatalogPool, config.DestinationName)
if err != nil {
return fmt.Errorf("failed to get qrep destination connector: %w", err)
}
defer connectors.CloseConnector(ctx, dstConn)
var warnings shared.QRepWarnings
rowsSynced, warnings, err = syncRecords(dstConn, ctx, config, partition, outstream)
if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to sync records: %w", err))
}
for _, warning := range warnings {
a.Alerter.LogFlowWarning(ctx, config.FlowJobName, warning)
}
return context.Canceled
})
if err := errGroup.Wait(); err != nil && err != context.Canceled {
return 0, a.Alerter.LogFlowError(ctx, config.FlowJobName, err)
}
if rowsSynced > 0 {
err := monitoring.UpdateRowsSyncedForPartition(ctx, a.CatalogPool, rowsSynced, runUUID, partition)
if err != nil {
return 0, err
}
logger.Info(fmt.Sprintf("pushed %d records", rowsSynced))
}
if err := monitoring.UpdateEndTimeForPartition(ctx, a.CatalogPool, runUUID, partition); err != nil {
return 0, err
}
return currentSnapshotXmin, nil
}
func (a *FlowableActivity) maintainReplConn(
ctx context.Context, flowName string, srcConn connectors.CDCPullConnectorCore, syncDone <-chan struct{},
) error {
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := srcConn.ReplPing(ctx); err != nil {
return a.Alerter.LogFlowError(ctx, flowName, fmt.Errorf("connection to source down: %w", err))
}
case <-syncDone:
return nil
case <-ctx.Done():
return nil
}
}
}
func (a *FlowableActivity) startNormalize(
ctx context.Context,
config *protos.FlowConnectionConfigs,
batchID int64,
) error {
logger := internal.LoggerFromCtx(ctx)
dstConn, err := connectors.GetByNameAs[connectors.CDCNormalizeConnector](
ctx,
config.Env,
a.CatalogPool,
config.DestinationName,
)
if errors.Is(err, errors.ErrUnsupported) {
return monitoring.UpdateEndTimeForCDCBatch(ctx, a.CatalogPool, config.FlowJobName, batchID)
} else if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName, fmt.Errorf("failed to get normalize connector: %w", err))
}
defer connectors.CloseConnector(ctx, dstConn)
tableNameSchemaMapping, err := a.getTableNameSchemaMapping(ctx, config.FlowJobName)
if err != nil {
return fmt.Errorf("failed to get table name schema mapping: %w", err)
}
logger.Info("normalizing batch", slog.Int64("SyncBatchID", batchID))
res, err := dstConn.NormalizeRecords(ctx, &model.NormalizeRecordsRequest{
FlowJobName: config.FlowJobName,
Env: config.Env,
TableNameSchemaMapping: tableNameSchemaMapping,
TableMappings: config.TableMappings,
SoftDeleteColName: config.SoftDeleteColName,
SyncedAtColName: config.SyncedAtColName,
SyncBatchID: batchID,
Version: config.Version,
})
if err != nil {
return a.Alerter.LogFlowError(ctx, config.FlowJobName,
exceptions.NewNormalizationError(fmt.Errorf("failed to normalize records: %w", err)))
}
if _, dstPg := dstConn.(*connpostgres.PostgresConnector); dstPg {
if err := monitoring.UpdateEndTimeForCDCBatch(ctx, a.CatalogPool, config.FlowJobName, batchID); err != nil {
return fmt.Errorf("failed to update end time for cdc batch: %w", err)
}
}
logger.Info("normalized batches", slog.Int64("StartBatchID", res.StartBatchID), slog.Int64("EndBatchID", res.EndBatchID))
return nil
}
// Suitable to be run as goroutine
func (a *FlowableActivity) normalizeLoop(
ctx context.Context,
logger log.Logger,
config *protos.FlowConnectionConfigs,
syncDone <-chan struct{},
normalizeRequests <-chan NormalizeBatchRequest,
normalizingBatchID *atomic.Int64,
normalizeWaiting *atomic.Bool,
) {
defer normalizeWaiting.Store(false)
for {
normalizeWaiting.Store(true)
select {
case req := <-normalizeRequests:
normalizeWaiting.Store(false)
retryInterval := time.Minute
retryLoop:
for {
normalizingBatchID.Store(req.BatchID)
if err := a.startNormalize(ctx, config, req.BatchID); err != nil {
_ = a.Alerter.LogFlowError(ctx, config.FlowJobName, err)
for {
// update req to latest normalize request & retry
select {
case req = <-normalizeRequests:
case <-syncDone:
logger.Info("[normalize-loop] syncDone closed before retry")
return
case <-ctx.Done():
logger.Info("[normalize-loop] context closed before retry")
return
default:
time.Sleep(retryInterval)
retryInterval = min(retryInterval*2, 5*time.Minute)
continue retryLoop
}
}
} else if req.Done != nil {
close(req.Done)
}
a.OtelManager.Metrics.LastNormalizedBatchIdGauge.Record(ctx, req.BatchID, metric.WithAttributeSet(attribute.NewSet(
attribute.String(otel_metrics.FlowNameKey, config.FlowJobName),
)))
break
}
case <-syncDone:
logger.Info("[normalize-loop] syncDone closed")
return
case <-ctx.Done():
logger.Info("[normalize-loop] context closed")
return
}
}
}