-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathmonitoring.go
More file actions
501 lines (454 loc) · 16.7 KB
/
Copy pathmonitoring.go
File metadata and controls
501 lines (454 loc) · 16.7 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
package monitoring
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strconv"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/log"
"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"
)
type CDCBatchInfo struct {
StartTime time.Time
BatchID int64
BatchEndlSN int64
RowsInBatch uint32
}
func InitializeCDCFlow(ctx context.Context, pool shared.CatalogPool, flowJobName string) error {
if _, err := pool.Exec(ctx,
`INSERT INTO peerdb_stats.cdc_flows(flow_name,latest_lsn_at_source,latest_lsn_at_target) VALUES($1,0,0) ON CONFLICT DO NOTHING`,
flowJobName,
); err != nil {
return fmt.Errorf("error while inserting flow into cdc_flows: %w", err)
}
return nil
}
func UpdateLatestLSNAtSourceForCDCFlow(ctx context.Context, pool shared.CatalogPool, flowJobName string,
latestLSNAtSource int64,
) error {
if _, err := pool.Exec(ctx,
"UPDATE peerdb_stats.cdc_flows SET latest_lsn_at_source=$1 WHERE flow_name=$2",
uint64(latestLSNAtSource), flowJobName,
); err != nil {
return fmt.Errorf("[source] error while updating flow in cdc_flows: %w", err)
}
return nil
}
func UpdateLatestLSNAtTargetForCDCFlow(ctx context.Context, pool shared.CatalogPool, flowJobName string,
latestLSNAtTarget int64,
) error {
if _, err := pool.Exec(ctx,
"UPDATE peerdb_stats.cdc_flows SET latest_lsn_at_target=$1 WHERE flow_name=$2",
uint64(latestLSNAtTarget), flowJobName,
); err != nil {
return fmt.Errorf("[target] error while updating flow in cdc_flows: %w", err)
}
return nil
}
func AddCDCBatchForFlow(ctx context.Context, pool shared.CatalogPool, flowJobName string,
batchInfo CDCBatchInfo,
) error {
if _, err := pool.Exec(ctx,
`INSERT INTO peerdb_stats.cdc_batches(flow_name,batch_id,rows_in_batch,batch_start_lsn,batch_end_lsn,
start_time) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING`,
flowJobName, batchInfo.BatchID, batchInfo.RowsInBatch, 0,
uint64(batchInfo.BatchEndlSN), batchInfo.StartTime,
); err != nil {
return fmt.Errorf("error while inserting batch into cdc_batch: %w", err)
}
return nil
}
// update num records and end-lsn for a cdc batch
func UpdateNumRowsAndEndLSNForCDCBatch(
ctx context.Context,
pool shared.CatalogPool,
flowJobName string,
batchID int64,
numRows uint32,
batchEndCheckpoint model.CdcCheckpoint,
) error {
if _, err := pool.Exec(ctx,
`UPDATE peerdb_stats.cdc_batches
SET rows_in_batch=$1, batch_end_lsn=$2, batch_end_lsn_text=$3, sync_time=NOW()
WHERE flow_name=$4 AND batch_id=$5`,
numRows, uint64(batchEndCheckpoint.ID), batchEndCheckpoint.Text, flowJobName, batchID,
); err != nil {
return fmt.Errorf("error while updating batch in cdc_batch: %w", err)
}
return nil
}
func UpdateEndTimeForCDCBatch(
ctx context.Context,
pool shared.CatalogPool,
flowJobName string,
batchID int64,
) error {
if _, err := pool.Exec(ctx,
`UPDATE peerdb_stats.cdc_batches
SET end_time = NOW()
WHERE flow_name = $1 AND batch_id <= $2 AND end_time IS NULL`,
flowJobName, batchID,
); err != nil {
return fmt.Errorf("error while updating batch in cdc_batch: %w", err)
}
return nil
}
func GetPendingNormalizeLagByFlow(
ctx context.Context,
pool shared.CatalogPool,
) (map[string]int64, error) {
// using catalog time to avoid clock skew with ScheduledTasks
rows, err := pool.Query(ctx,
`SELECT flow_name, (EXTRACT(EPOCH FROM (NOW() - MIN(sync_time))) * 1000000)::bigint
FROM peerdb_stats.cdc_batches
WHERE end_time IS NULL AND sync_time IS NOT NULL
GROUP BY flow_name`)
if err != nil {
return nil, fmt.Errorf("error while querying normalize lag: %w", err)
}
defer rows.Close()
result := make(map[string]int64)
for rows.Next() {
var flowName string
var lagMicroseconds int64
if err := rows.Scan(&flowName, &lagMicroseconds); err != nil {
return nil, fmt.Errorf("error while scanning normalize lag row: %w", err)
}
result[flowName] = lagMicroseconds
}
return result, rows.Err()
}
func AddCDCBatchTablesForFlow(
ctx context.Context,
pool shared.CatalogPool,
flowJobName string,
batchID int64,
tableNameRowsMapping map[string]*model.RecordTypeCounts,
otelManager *otel_metrics.OtelManager,
) error {
var recordAggregateMetrics bool
if enabled, err := internal.PeerDBMetricsRecordAggregatesEnabled(ctx, nil); err == nil && enabled {
recordAggregateMetrics = true
}
insertBatchTablesTx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("error while beginning transaction for inserting statistics: %w", err)
}
defer shared.RollbackTx(insertBatchTablesTx, internal.LoggerFromCtx(ctx))
type opWithValue struct {
op string
count int32
}
var syncedTablesCount int64
tableNameOperations := make(map[string][3]opWithValue, len(tableNameRowsMapping))
for destinationTableName, rowCounts := range tableNameRowsMapping {
inserts := rowCounts.InsertCount.Load()
updates := rowCounts.UpdateCount.Load()
deletes := rowCounts.DeleteCount.Load()
if inserts+updates+deletes > 0 {
syncedTablesCount++
}
if recordAggregateMetrics {
tableNameOperations[destinationTableName] = [3]opWithValue{
{op: otel_metrics.RecordOperationTypeInsert, count: inserts},
{op: otel_metrics.RecordOperationTypeUpdate, count: updates},
{op: otel_metrics.RecordOperationTypeDelete, count: deletes},
}
}
totalRows := inserts + updates + deletes
// Update the aggregated counts table
query := `
INSERT INTO peerdb_stats.cdc_table_aggregate_counts AS stats (
flow_name,
destination_table_name,
inserts_count,
updates_count,
deletes_count,
total_count,
latest_batch_id,
last_updated_at
)
VALUES (
$1, -- flow_name
$2, -- destination_table_name
$3, -- inserts_count
$4, -- updates_count
$5, -- deletes_count
$6, -- total_count
$7, -- latest_batch_id
NOW() -- last_updated_at
)
ON CONFLICT (flow_name, destination_table_name)
DO UPDATE
SET
inserts_count = stats.inserts_count + EXCLUDED.inserts_count,
updates_count = stats.updates_count + EXCLUDED.updates_count,
deletes_count = stats.deletes_count + EXCLUDED.deletes_count,
total_count = stats.total_count + EXCLUDED.total_count,
latest_batch_id = GREATEST(stats.latest_batch_id, EXCLUDED.latest_batch_id),
last_updated_at = NOW()
`
if _, err := insertBatchTablesTx.Exec(ctx, query,
flowJobName,
destinationTableName,
inserts,
updates,
deletes,
totalRows,
batchID,
); err != nil {
return fmt.Errorf("error while updating aggregate statistics in cdc_table_aggregate_counts: %w", err)
}
}
if err := insertBatchTablesTx.Commit(ctx); err != nil {
return fmt.Errorf("error while committing transaction for inserting and updating statistics: %w", err)
}
for destinationTableName, operations := range tableNameOperations {
for _, opAndCount := range operations {
otelManager.Metrics.RecordsSyncedPerTableCounter.Add(ctx, int64(opAndCount.count), metric.WithAttributeSet(attribute.NewSet(
attribute.String(otel_metrics.DestinationTableNameKey, destinationTableName),
attribute.String(otel_metrics.RecordOperationTypeKey, opAndCount.op),
)))
}
}
return nil
}
func InitializeQRepRun(
ctx context.Context,
logger log.Logger,
pool shared.CatalogPool,
config *protos.QRepConfig,
runUUID string,
partitions []*protos.QRepPartition,
parentMirrorName string,
) error {
flowJobName := config.GetFlowJobName()
tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("error while starting transaction to initialize qrep run: %w", err)
}
defer shared.RollbackTx(tx, logger)
if _, err := tx.Exec(ctx,
"INSERT INTO peerdb_stats.qrep_runs(flow_name,run_uuid,source_table,destination_table,parent_mirror_name)"+
" VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING",
flowJobName, runUUID, config.WatermarkTable, config.DestinationTableIdentifier, parentMirrorName,
); err != nil {
return fmt.Errorf("error while inserting qrep run in qrep_runs: %w", err)
}
for _, partition := range partitions {
if err := addPartitionToQRepRun(ctx, tx, flowJobName, runUUID, partition, parentMirrorName); err != nil {
return fmt.Errorf("unable to add partition to qrep run: %w", err)
}
}
return tx.Commit(ctx)
}
func UpdateStartTimeForQRepRun(ctx context.Context, pool shared.CatalogPool, runUUID string) error {
if _, err := pool.Exec(ctx,
"UPDATE peerdb_stats.qrep_runs SET start_time=$1, fetch_complete=true WHERE run_uuid=$2",
time.Now(), runUUID,
); err != nil {
return fmt.Errorf("error while updating start time for run_uuid %s in qrep_runs: %w", runUUID, err)
}
return nil
}
func UpdateEndTimeForQRepRun(ctx context.Context, pool shared.CatalogPool, runUUID string) error {
if _, err := pool.Exec(ctx,
"UPDATE peerdb_stats.qrep_runs SET end_time=$1, consolidate_complete=true WHERE run_uuid=$2",
time.Now(), runUUID,
); err != nil {
return fmt.Errorf("error while updating end time for run_uuid %s in qrep_runs: %w", runUUID, err)
}
return nil
}
func AppendSlotSizeInfo(
ctx context.Context,
pool shared.CatalogPool,
peerName string,
slotInfo *protos.SlotInfo,
) error {
if _, err := pool.Exec(ctx,
"INSERT INTO peerdb_stats.peer_slot_size"+
"(peer_name, slot_name, restart_lsn, redo_lsn, confirmed_flush_lsn, slot_size, wal_status) "+
"VALUES($1,$2,$3,$4,$5,$6,$7) ON CONFLICT DO NOTHING;",
peerName,
slotInfo.SlotName,
slotInfo.RestartLSN,
slotInfo.RedoLSN,
slotInfo.ConfirmedFlushLSN,
slotInfo.LagInMb,
slotInfo.WalStatus,
); err != nil {
return fmt.Errorf("error while upserting row for slot_size: %w", err)
}
return nil
}
func addPartitionToQRepRun(ctx context.Context, tx pgx.Tx, flowJobName string,
runUUID string, partition *protos.QRepPartition, parentMirrorName string,
) error {
if partition == nil {
internal.LoggerFromCtx(ctx).Info("cannot add nil partition to qrep run",
slog.String(string(shared.FlowNameKey), parentMirrorName))
return fmt.Errorf("cannot add nil partition to qrep run")
}
var rangeStart, rangeEnd *string
if partition.RangeOffloaded {
internal.LoggerFromCtx(ctx).Info(
"omitting partition_start/partition_end from qrep_partitions because it's offloaded",
slog.String("partitionId", partition.PartitionId))
} else if partition.Range != nil {
switch x := partition.Range.Range.(type) {
case *protos.PartitionRange_IntRange:
rangeStart, rangeEnd = new(strconv.FormatInt(x.IntRange.Start, 10)), new(strconv.FormatInt(x.IntRange.End, 10))
case *protos.PartitionRange_UintRange:
rangeStart, rangeEnd = new(strconv.FormatUint(x.UintRange.Start, 10)), new(strconv.FormatUint(x.UintRange.End, 10))
case *protos.PartitionRange_TimestampRange:
rangeStart, rangeEnd = new(x.TimestampRange.Start.AsTime().String()), new(x.TimestampRange.End.AsTime().String())
case *protos.PartitionRange_TidRange:
rangeStartValue, err := pgtype.TID{
BlockNumber: x.TidRange.Start.BlockNumber,
OffsetNumber: uint16(x.TidRange.Start.OffsetNumber),
Valid: true,
}.Value()
if err != nil {
return fmt.Errorf("unable to encode TID as string: %w", err)
}
rangeStart = new(rangeStartValue.(string))
rangeEndValue, err := pgtype.TID{
BlockNumber: x.TidRange.End.BlockNumber,
OffsetNumber: uint16(x.TidRange.End.OffsetNumber),
Valid: true,
}.Value()
if err != nil {
return fmt.Errorf("unable to encode TID as string: %w", err)
}
rangeEnd = new(rangeEndValue.(string))
case *protos.PartitionRange_ObjectIdRange:
rangeStart, rangeEnd = &x.ObjectIdRange.Start, &x.ObjectIdRange.End
case *protos.PartitionRange_StringRange:
rangeStart, rangeEnd = &x.StringRange.Start, &x.StringRange.End
case *protos.PartitionRange_NullRange:
// leave rangeStart and rangeEnd as nil
default:
return fmt.Errorf("unknown range type: %v", x)
}
} else if !partition.FullTablePartition && len(partition.ChildTableRanges) == 0 {
internal.LoggerFromCtx(ctx).Warn("[monitoring]: partition "+partition.PartitionId+" has nil range",
slog.String(string(shared.FlowNameKey), parentMirrorName))
}
var childTableBlockRangesJSON []byte
if len(partition.ChildTableRanges) > 0 {
internal.LoggerFromCtx(ctx).Info("Child table ranges detected")
var err error
childTableBlockRangesJSON, err = json.Marshal(partition.ChildTableRanges)
if err != nil {
return fmt.Errorf("failed to marshal child table block ranges: %w", err)
}
}
if _, err := tx.Exec(ctx,
`INSERT INTO peerdb_stats.qrep_partitions
(flow_name,run_uuid,partition_uuid,partition_start,partition_end,restart_count,parent_mirror_name,child_table_ranges)
VALUES($1,$2,$3,$4,$5,$6,$7,$8) ON CONFLICT(run_uuid,partition_uuid) DO UPDATE SET
restart_count=qrep_partitions.restart_count+1`,
flowJobName, runUUID, partition.PartitionId, rangeStart, rangeEnd, 0, parentMirrorName, childTableBlockRangesJSON,
); err != nil {
return fmt.Errorf("error while inserting qrep partition in qrep_partitions: %w", err)
}
return nil
}
func UpdateStartTimeForPartition(
ctx context.Context,
pool shared.CatalogPool,
runUUID string,
partition *protos.QRepPartition,
startTime time.Time,
) error {
if _, err := pool.Exec(ctx,
`UPDATE peerdb_stats.qrep_partitions SET start_time=$1 WHERE run_uuid=$2 AND partition_uuid=$3`,
startTime, runUUID, partition.PartitionId,
); err != nil {
return fmt.Errorf("error while updating qrep partition in qrep_partitions: %w", err)
}
return nil
}
func UpdatePullEndTimeAndRowsForPartition(ctx context.Context, pool shared.CatalogPool, runUUID string,
partition *protos.QRepPartition, rowsInPartition int64,
) error {
if _, err := pool.Exec(ctx,
`UPDATE peerdb_stats.qrep_partitions SET pull_end_time=$1,rows_in_partition=$2 WHERE run_uuid=$3 AND partition_uuid=$4`,
time.Now(), rowsInPartition, runUUID, partition.PartitionId,
); err != nil {
return fmt.Errorf("error while updating qrep partition in qrep_partitions: %w", err)
}
return nil
}
func UpdateEndTimeForPartition(ctx context.Context, pool shared.CatalogPool, runUUID string,
partition *protos.QRepPartition,
) error {
if _, err := pool.Exec(ctx,
`UPDATE peerdb_stats.qrep_partitions SET end_time=$1 WHERE run_uuid=$2 AND partition_uuid=$3`,
time.Now(), runUUID, partition.PartitionId,
); err != nil {
return fmt.Errorf("error while updating qrep partition in qrep_partitions: %w", err)
}
return nil
}
func UpdateRowsSyncedForPartition(ctx context.Context, pool shared.CatalogPool, rowsSynced int64, runUUID string,
partition *protos.QRepPartition,
) error {
if _, err := pool.Exec(ctx,
`UPDATE peerdb_stats.qrep_partitions SET rows_synced=$1 WHERE run_uuid=$2 AND partition_uuid=$3`,
rowsSynced, runUUID, partition.PartitionId,
); err != nil {
return fmt.Errorf("error while updating rows_synced in qrep_partitions: %w", err)
}
return nil
}
func DeleteMirrorStats(ctx context.Context, logger log.Logger, pool shared.CatalogPool, flowJobName string) error {
tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("error while starting transaction to delete metadata: %w", err)
}
defer shared.RollbackTx(tx, logger)
if _, err := tx.Exec(ctx, `DELETE FROM peerdb_stats.qrep_partitions WHERE parent_mirror_name = $1`, flowJobName); err != nil {
return fmt.Errorf("error while deleting qrep_partitions: %w", err)
}
if _, err := tx.Exec(ctx, `DELETE FROM peerdb_stats.qrep_runs WHERE parent_mirror_name = $1`, flowJobName); err != nil {
return fmt.Errorf("error while deleting qrep_runs: %w", err)
}
if _, err := tx.Exec(ctx, `DELETE FROM peerdb_stats.cdc_batches WHERE flow_name = $1`, flowJobName); err != nil {
return fmt.Errorf("error while deleting cdc_batches: %w", err)
}
if _, err := tx.Exec(ctx, `DELETE FROM peerdb_stats.cdc_table_aggregate_counts WHERE flow_name = $1`, flowJobName); err != nil {
return fmt.Errorf("error while deleting cdc_table_aggregate_counts: %w", err)
}
if _, err := tx.Exec(ctx, `DELETE FROM peerdb_stats.cdc_flows WHERE flow_name = $1`, flowJobName); err != nil {
return fmt.Errorf("error while deleting cdc_flows: %w", err)
}
return tx.Commit(ctx)
}
func AuditSchemaDelta(ctx context.Context, pool *pgxpool.Pool,
flowJobName string, rec *protos.TableSchemaDelta,
) error {
activityInfo := activity.GetInfo(ctx)
workflowID := activityInfo.WorkflowExecution.ID
runID := activityInfo.WorkflowExecution.RunID
if _, err := pool.Exec(ctx,
`INSERT INTO
peerdb_stats.schema_deltas_audit_log(flow_job_name,workflow_id,run_id,delta_info)
VALUES($1,$2,$3,$4)`,
flowJobName, workflowID, runID, rec); err != nil {
return fmt.Errorf("failed to insert row into table: %w", err)
}
return nil
}