@@ -44,7 +44,7 @@ func (sh *sourcesHandler) getStatusInternal(ctx context.Context, db *sql.DB, job
44
44
filters , filterParams := sqlFilters (jobRunId , filter )
45
45
46
46
sqlStatement := fmt .Sprintf (
47
- `SELECT
47
+ `SELECT
48
48
source_id,
49
49
destination_id,
50
50
task_run_id,
@@ -96,7 +96,7 @@ func (*sourcesHandler) IncrementStats(ctx context.Context, tx *sql.Tx, jobRunId
96
96
failed_count
97
97
) values ($1, $2, $3, $4, $5, $6, $7)
98
98
on conflict(db_name, job_run_id, task_run_id, source_id, destination_id)
99
- do update set
99
+ do update set
100
100
in_count = "rsources_stats".in_count + excluded.in_count,
101
101
out_count = "rsources_stats".out_count + excluded.out_count,
102
102
failed_count = "rsources_stats".failed_count + excluded.failed_count,
@@ -116,7 +116,7 @@ func (sh *sourcesHandler) AddFailedRecords(ctx context.Context, tx *sql.Tx, jobR
116
116
return nil
117
117
}
118
118
row := tx .QueryRow (`INSERT INTO rsources_failed_keys_v2 (id, job_run_id, task_run_id, source_id, destination_id)
119
- VALUES ($1, $2, $3, $4, $5)
119
+ VALUES ($1, $2, $3, $4, $5)
120
120
ON CONFLICT (job_run_id, task_run_id, source_id, destination_id, db_name) DO UPDATE SET ts = NOW()
121
121
RETURNING id` , ksuid .New ().String (), jobRunId , key .TaskRunID , key .SourceID , key .DestinationID )
122
122
var id string
@@ -196,8 +196,8 @@ func (sh *sourcesHandler) GetFailedRecords(ctx context.Context, jobRunId string,
196
196
r.id,
197
197
r.record_id,
198
198
r.code
199
- FROM "rsources_failed_keys_v2_records" r
200
- JOIN "rsources_failed_keys_v2" k ON r.id = k.id %[1]s
199
+ FROM "rsources_failed_keys_v2_records" r
200
+ JOIN "rsources_failed_keys_v2" k ON r.id = k.id %[1]s
201
201
ORDER BY r.id, r.record_id ASC %[2]s` ,
202
202
filters , limit )
203
203
@@ -291,8 +291,8 @@ func (sh *sourcesHandler) GetFailedRecordsV1(ctx context.Context, jobRunId strin
291
291
k.destination_id,
292
292
r.id,
293
293
r.record_id
294
- FROM "rsources_failed_keys_v2_records" r
295
- JOIN "rsources_failed_keys_v2" k ON r.id = k.id %[1]s
294
+ FROM "rsources_failed_keys_v2_records" r
295
+ JOIN "rsources_failed_keys_v2" k ON r.id = k.id %[1]s
296
296
ORDER BY r.id, r.record_id ASC %[2]s` ,
297
297
filters , limit )
298
298
@@ -430,7 +430,7 @@ func (sh *sourcesHandler) doCleanupTables(ctx context.Context) error {
430
430
}
431
431
before := time .Now ().Add (- config .GetDuration ("Rsources.retention" , defaultRetentionPeriodInHours , time .Hour ))
432
432
if _ , err := tx .ExecContext (ctx , `delete from "rsources_stats" where job_run_id in (
433
- select lastUpdateToJobRunId.job_run_id from
433
+ select lastUpdateToJobRunId.job_run_id from
434
434
(select job_run_id, max(ts) as mts from "rsources_stats" group by job_run_id) lastUpdateToJobRunId
435
435
where lastUpdateToJobRunId.mts <= $1
436
436
)` , before ); err != nil {
@@ -444,7 +444,7 @@ func (sh *sourcesHandler) doCleanupTables(ctx context.Context) error {
444
444
JOIN "rsources_failed_keys_v2_records" r on r.id = k.id
445
445
GROUP BY k.job_run_id
446
446
) lastUpdateToJobRunId WHERE lastUpdateToJobRunId.mts <= $1
447
- )
447
+ )
448
448
),
449
449
deleted AS (
450
450
DELETE FROM "rsources_failed_keys_v2" WHERE id IN (SELECT id FROM to_delete) RETURNING id
@@ -487,7 +487,7 @@ func (sh *sourcesHandler) init() error {
487
487
return err
488
488
}
489
489
490
- if sh .sharedDB != nil {
490
+ if sh .config . ShouldSetupSharedDB && sh . sharedDB != nil {
491
491
if err := withAdvisoryLock (ctx , sh .sharedDB , lockID , func (_ * sql.Tx ) error {
492
492
sh .log .Debugf ("setting up rsources tables for shared db %s" , sh .config .SharedConn )
493
493
if err := setupTables (ctx , sh .sharedDB , "shared" , sh .log ); err != nil {
@@ -536,42 +536,42 @@ func migrateFailedKeysTable(ctx context.Context, tx *sql.Tx) error {
536
536
v_alphabet char array[62] := array[
537
537
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
538
538
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
539
- 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
540
- 'U', 'V', 'W', 'X', 'Y', 'Z',
541
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
539
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
540
+ 'U', 'V', 'W', 'X', 'Y', 'Z',
541
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
542
542
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
543
543
'u', 'v', 'w', 'x', 'y', 'z'];
544
544
i integer := 0;
545
545
begin
546
-
546
+
547
547
-- Get the current time
548
548
v_time := clock_timestamp();
549
-
549
+
550
550
-- Extract epoch seconds
551
551
v_seconds := EXTRACT(EPOCH FROM v_time) - v_epoch;
552
-
552
+
553
553
-- Generate a KSUID in a numeric variable
554
554
v_numeric := v_seconds * pow(2::numeric(50), 128) -- 32 bits for seconds and 128 bits for randomness
555
555
+ ((random()::numeric(70,20) * pow(2::numeric(70,20), 48))::numeric(50) * pow(2::numeric(50), 80)::numeric(50))
556
556
+ ((random()::numeric(70,20) * pow(2::numeric(70,20), 40))::numeric(50) * pow(2::numeric(50), 40)::numeric(50))
557
557
+ (random()::numeric(70,20) * pow(2::numeric(70,20), 40))::numeric(50);
558
-
558
+
559
559
-- Encode it to base-62
560
560
while v_numeric <> 0 loop
561
561
v_base62 := v_base62 || v_alphabet[mod(v_numeric, 62) + 1];
562
562
v_numeric := div(v_numeric, 62);
563
563
end loop;
564
564
v_base62 := reverse(v_base62);
565
565
v_base62 := lpad(v_base62, 27, '0');
566
-
566
+
567
567
return v_base62;
568
-
568
+
569
569
end $$ language plpgsql;` ); err != nil {
570
570
return fmt .Errorf ("failed to create ksuid function: %w" , err )
571
571
}
572
572
573
573
if _ , err := tx .ExecContext (ctx , `WITH new_keys AS (
574
- INSERT INTO "rsources_failed_keys_v2"
574
+ INSERT INTO "rsources_failed_keys_v2"
575
575
(id, job_run_id, task_run_id, source_id, destination_id, db_name)
576
576
SELECT ksuid(), t.* FROM (
577
577
SELECT DISTINCT job_run_id, task_run_id, source_id, destination_id, db_name from "rsources_failed_keys"
@@ -580,12 +580,12 @@ func migrateFailedKeysTable(ctx context.Context, tx *sql.Tx) error {
580
580
)
581
581
INSERT INTO "rsources_failed_keys_v2_records" (id, record_id, ts)
582
582
SELECT n.id, o.record_id::text, min(o.ts) FROM new_keys n
583
- JOIN rsources_failed_keys o
584
- on o.db_name = n.db_name
585
- and o.destination_id = n.destination_id
586
- and o.source_id = n.source_id
587
- and o.task_run_id = n.task_run_id
588
- and o.job_run_id = n.job_run_id
583
+ JOIN rsources_failed_keys o
584
+ on o.db_name = n.db_name
585
+ and o.destination_id = n.destination_id
586
+ and o.source_id = n.source_id
587
+ and o.task_run_id = n.task_run_id
588
+ and o.job_run_id = n.job_run_id
589
589
group by n.id, o.record_id
590
590
` ); err != nil {
591
591
return fmt .Errorf ("failed to migrate rsources_failed_keys table: %w" , err )
@@ -605,7 +605,7 @@ func migrateFailedKeysTable(ctx context.Context, tx *sql.Tx) error {
605
605
606
606
// TODO: Remove this after a few releases
607
607
func setupFailedKeysTableV0 (ctx context.Context , db * sql.DB , defaultDbName string , log logger.Logger ) error {
608
- sqlStatement := fmt .Sprintf (`create table "rsources_failed_keys" (
608
+ sqlStatement := fmt .Sprintf (`create table "rsources_failed_keys" (
609
609
id BIGSERIAL,
610
610
db_name text not null default '%s',
611
611
job_run_id text not null,
@@ -631,13 +631,13 @@ func setupFailedKeysTableV0(ctx context.Context, db *sql.DB, defaultDbName strin
631
631
}
632
632
633
633
func setupFailedKeysTable (ctx context.Context , db * sql.DB , defaultDbName string , log logger.Logger ) error {
634
- if _ , err := db .ExecContext (ctx , fmt .Sprintf (`create table "rsources_failed_keys_v2" (
634
+ if _ , err := db .ExecContext (ctx , fmt .Sprintf (`create table "rsources_failed_keys_v2" (
635
635
id VARCHAR(27) COLLATE "C",
636
636
db_name text not null default '%s',
637
637
job_run_id text not null,
638
638
task_run_id text not null,
639
639
source_id text not null,
640
- destination_id text not null,
640
+ destination_id text not null,
641
641
primary key (id),
642
642
unique (job_run_id, task_run_id, source_id, destination_id, db_name)
643
643
)` , defaultDbName )); err != nil {
@@ -648,7 +648,7 @@ func setupFailedKeysTable(ctx context.Context, db *sql.DB, defaultDbName string,
648
648
}
649
649
}
650
650
651
- if _ , err := db .ExecContext (ctx , `create table "rsources_failed_keys_v2_records" (
651
+ if _ , err := db .ExecContext (ctx , `create table "rsources_failed_keys_v2_records" (
652
652
id VARCHAR(27) COLLATE "C",
653
653
record_id text not null,
654
654
ts timestamp not null default NOW(),
0 commit comments