-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
574973c
commit d0bd081
Showing
3 changed files
with
77 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/artie-labs/reader/config" | ||
"github.com/artie-labs/reader/lib/kafkalib" | ||
"github.com/artie-labs/reader/lib/logger" | ||
"github.com/artie-labs/reader/lib/mtr" | ||
) | ||
|
||
func Run(ctx context.Context, cfg config.Settings, statsD *mtr.Client) { | ||
slog.Info("Kafka config", | ||
slog.Bool("aws", cfg.Kafka.AwsEnabled), | ||
slog.String("kafkaBootstrapServer", cfg.Kafka.BootstrapServers), | ||
slog.Any("publishSize", cfg.Kafka.GetPublishSize()), | ||
slog.Uint64("maxRequestSize", cfg.Kafka.MaxRequestSize), | ||
) | ||
kafkaWriter, err := kafkalib.NewBatchWriter(ctx, *cfg.Kafka) | ||
if err != nil { | ||
logger.Fatal("Failed to create kafka writer", slog.Any("err", err)) | ||
} | ||
|
||
db, err := sql.Open("postgres", NewConnection(cfg.PostgreSQL).String()) | ||
if err != nil { | ||
logger.Fatal("Failed to connect to postgres", slog.Any("err", err)) | ||
} | ||
defer db.Close() | ||
|
||
for _, table := range cfg.PostgreSQL.Tables { | ||
snapshotStartTime := time.Now() | ||
iter, err := LoadTable(db, table, statsD, cfg.Kafka.MaxRequestSize) | ||
if err != nil { | ||
logger.Fatal("Failed to create table iterator", slog.Any("err", err), slog.String("table", table.Name)) | ||
} | ||
|
||
var count int | ||
for iter.HasNext() { | ||
msgs, err := iter.Next() | ||
if err != nil { | ||
logger.Fatal("Failed to iterate over table", slog.Any("err", err), slog.String("table", table.Name)) | ||
} else if len(msgs) > 0 { | ||
if err = kafkaWriter.Write(msgs); err != nil { | ||
logger.Fatal("Failed to write messages to kafka", slog.Any("err", err), slog.String("table", table.Name)) | ||
} | ||
count += len(msgs) | ||
slog.Info("Scanning progress", slog.Duration("timing", time.Since(snapshotStartTime)), slog.Int("count", count)) | ||
} | ||
} | ||
|
||
slog.Info("Finished snapshotting", | ||
slog.Int("scannedTotal", count), | ||
slog.Duration("totalDuration", time.Since(snapshotStartTime)), | ||
) | ||
} | ||
} |
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