Compare commits
2 Commits
refactor/e
...
refactor/r
| Author | SHA1 | Date | |
|---|---|---|---|
|
f65e67e02f
|
|||
|
93b302db8e
|
@@ -1,77 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
_ "github.com/microsoft/go-mssqldb"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func connectToSqlServer() (*sql.DB, error) {
|
|
||||||
db, err := sql.Open("sqlserver", config.App.SourceDbUrl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to connect to sqlserver: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err := db.PingContext(ctx); err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to ping sqlserver: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return db, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func connectToPostgres() (*pgxpool.Pool, error) {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
pool, err := pgxpool.New(ctx, config.App.TargetDbUrl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to connect to postgres: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := pool.Ping(ctx); err != nil {
|
|
||||||
pool.Close()
|
|
||||||
return nil, fmt.Errorf("Unable to ping postgres: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return pool, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func connectToDatabases() (*sql.DB, *pgxpool.Pool, error) {
|
|
||||||
var sourceDbErr, targetDbErr error
|
|
||||||
var sourceDb *sql.DB
|
|
||||||
var targetDb *pgxpool.Pool
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
wg.Go(func() {
|
|
||||||
sourceDb, sourceDbErr = connectToSqlServer()
|
|
||||||
if sourceDbErr != nil {
|
|
||||||
log.Error("Unable to connect to source db: ", sourceDbErr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
wg.Go(func() {
|
|
||||||
targetDb, targetDbErr = connectToPostgres()
|
|
||||||
if targetDbErr != nil {
|
|
||||||
log.Error("Unable to connect to target db: ", targetDbErr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
if sourceDbErr != nil || targetDbErr != nil {
|
|
||||||
return nil, nil, errors.New("Unable to connect to databases")
|
|
||||||
}
|
|
||||||
|
|
||||||
return sourceDb, targetDb, nil
|
|
||||||
}
|
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/loaders"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/loaders"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/transformers"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/transformers"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
@@ -95,10 +96,10 @@ func processMigrationJobs(
|
|||||||
targetDb dbwrapper.DbWrapper,
|
targetDb dbwrapper.DbWrapper,
|
||||||
jobs []config.Job,
|
jobs []config.Job,
|
||||||
maxParallelWorkers int,
|
maxParallelWorkers int,
|
||||||
) []JobResult {
|
) []models.JobResult {
|
||||||
if len(jobs) == 0 {
|
if len(jobs) == 0 {
|
||||||
log.Info("No migration jobs configured")
|
log.Info("No migration jobs configured")
|
||||||
return []JobResult{}
|
return []models.JobResult{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if maxParallelWorkers <= 0 {
|
if maxParallelWorkers <= 0 {
|
||||||
@@ -111,7 +112,7 @@ func processMigrationJobs(
|
|||||||
|
|
||||||
log.Infof("Starting migration with %d parallel worker(s)", maxParallelWorkers)
|
log.Infof("Starting migration with %d parallel worker(s)", maxParallelWorkers)
|
||||||
|
|
||||||
chJobResults := make(chan JobResult, len(jobs))
|
chJobResults := make(chan models.JobResult, len(jobs))
|
||||||
chJobs := make(chan config.Job, len(jobs))
|
chJobs := make(chan config.Job, len(jobs))
|
||||||
var wgJobs sync.WaitGroup
|
var wgJobs sync.WaitGroup
|
||||||
|
|
||||||
@@ -151,7 +152,7 @@ func processMigrationJobs(
|
|||||||
close(chJobResults)
|
close(chJobResults)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var finalResults []JobResult
|
var finalResults []models.JobResult
|
||||||
for res := range chJobResults {
|
for res := range chJobResults {
|
||||||
finalResults = append(finalResults, res)
|
finalResults = append(finalResults, res)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
type JobResult struct {
|
|
||||||
JobName string
|
|
||||||
StartTime time.Time
|
|
||||||
Duration time.Duration
|
|
||||||
RowsRead int64
|
|
||||||
RowsLoaded int64
|
|
||||||
RowsFailed int64
|
|
||||||
Error error
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/extractors"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl/table_analyzers"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -27,11 +26,11 @@ func processMigrationJob(
|
|||||||
transformer etl.Transformer,
|
transformer etl.Transformer,
|
||||||
loader etl.Loader,
|
loader etl.Loader,
|
||||||
job config.Job,
|
job config.Job,
|
||||||
) JobResult {
|
) models.JobResult {
|
||||||
localCtx, cancel := context.WithCancel(ctx)
|
localCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
result := JobResult{
|
result := models.JobResult{
|
||||||
JobName: job.Name,
|
JobName: job.Name,
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
}
|
}
|
||||||
@@ -86,6 +85,7 @@ func processMigrationJob(
|
|||||||
}
|
}
|
||||||
|
|
||||||
chJobErrors := make(chan custom_errors.JobError, job.QueueSize)
|
chJobErrors := make(chan custom_errors.JobError, job.QueueSize)
|
||||||
|
chExtractorErrors := make(chan custom_errors.ExtractorError, job.QueueSize)
|
||||||
chLoadersErrors := make(chan custom_errors.LoaderError, job.QueueSize)
|
chLoadersErrors := make(chan custom_errors.LoaderError, job.QueueSize)
|
||||||
chPartitions := make(chan models.Partition, job.QueueSize)
|
chPartitions := make(chan models.Partition, job.QueueSize)
|
||||||
chBatchesRaw := make(chan models.Batch, job.QueueSize)
|
chBatchesRaw := make(chan models.Batch, job.QueueSize)
|
||||||
@@ -105,6 +105,15 @@ func processMigrationJob(
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
go custom_errors.ExtractorErrorHandler(
|
||||||
|
localCtx,
|
||||||
|
job.Retry,
|
||||||
|
job.MaxPartitionErrrors,
|
||||||
|
chExtractorErrors,
|
||||||
|
chPartitions,
|
||||||
|
chJobErrors,
|
||||||
|
&wgActivePartitions,
|
||||||
|
)
|
||||||
go custom_errors.LoaderErrorHandler(
|
go custom_errors.LoaderErrorHandler(
|
||||||
localCtx,
|
localCtx,
|
||||||
job.Retry,
|
job.Retry,
|
||||||
@@ -120,14 +129,14 @@ func processMigrationJob(
|
|||||||
|
|
||||||
for range maxExtractors {
|
for range maxExtractors {
|
||||||
wgExtractors.Go(func() {
|
wgExtractors.Go(func() {
|
||||||
extractors.Consume(
|
extractor.Exec(
|
||||||
localCtx,
|
localCtx,
|
||||||
extractor,
|
|
||||||
job.SourceTable,
|
job.SourceTable,
|
||||||
sourceColTypes,
|
sourceColTypes,
|
||||||
job.BatchSize,
|
job.BatchSize,
|
||||||
chPartitions,
|
chPartitions,
|
||||||
chBatchesRaw,
|
chBatchesRaw,
|
||||||
|
chExtractorErrors,
|
||||||
chJobErrors,
|
chJobErrors,
|
||||||
&wgActivePartitions,
|
&wgActivePartitions,
|
||||||
&rowsRead,
|
&rowsRead,
|
||||||
@@ -181,6 +190,8 @@ func processMigrationJob(
|
|||||||
log.Debugf("wgActivePartitions is empty (%v)", job.Name)
|
log.Debugf("wgActivePartitions is empty (%v)", job.Name)
|
||||||
close(chPartitions)
|
close(chPartitions)
|
||||||
log.Debugf("chPartitions is closed (%v)", job.Name)
|
log.Debugf("chPartitions is closed (%v)", job.Name)
|
||||||
|
close(chExtractorErrors)
|
||||||
|
log.Debugf("chExtractorErrors is closed (%v)", job.Name)
|
||||||
|
|
||||||
wgExtractors.Wait()
|
wgExtractors.Wait()
|
||||||
log.Debugf("wgExtractors is empty (%v)", job.Name)
|
log.Debugf("wgExtractors is empty (%v)", job.Name)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -15,6 +15,8 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||||
|
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -16,6 +16,10 @@ github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZ
|
|||||||
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
||||||
|
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||||
|
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||||
|
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
package custom_errors
|
package custom_errors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtractorError struct {
|
type ExtractorError struct {
|
||||||
@@ -14,3 +20,100 @@ type ExtractorError struct {
|
|||||||
func (e *ExtractorError) Error() string {
|
func (e *ExtractorError) Error() string {
|
||||||
return e.Msg
|
return e.Msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExtractorErrorHandler(
|
||||||
|
ctx context.Context,
|
||||||
|
retryConfig config.RetryConfig,
|
||||||
|
maxPartitionErrors int,
|
||||||
|
chErrorsIn <-chan ExtractorError,
|
||||||
|
chPartitionsOut chan<- models.Partition,
|
||||||
|
chJobErrorsOut chan<- JobError,
|
||||||
|
wgActivePartitions *sync.WaitGroup,
|
||||||
|
) {
|
||||||
|
definitiveErrors := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
|
||||||
|
case err, ok := <-chErrorsIn:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err.Partition.RetryCounter >= retryConfig.Attempts {
|
||||||
|
wgActivePartitions.Done()
|
||||||
|
definitiveErrors++
|
||||||
|
jobError := JobError{
|
||||||
|
ShouldCancelJob: false,
|
||||||
|
Msg: fmt.Sprintf("Partition %v reached max retries (%d)", err.Partition.Id, retryConfig.Attempts),
|
||||||
|
Prev: &err,
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case chJobErrorsOut <- jobError:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if maxPartitionErrors > 0 && definitiveErrors >= maxPartitionErrors {
|
||||||
|
fatalError := JobError{
|
||||||
|
ShouldCancelJob: true,
|
||||||
|
Msg: fmt.Sprintf("Partition error limit reached (%d)", maxPartitionErrors),
|
||||||
|
Prev: &err,
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case chJobErrorsOut <- fatalError:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
jobError := JobError{
|
||||||
|
ShouldCancelJob: false,
|
||||||
|
Msg: fmt.Sprintf("Temporal error in partition %v (retries: %d)", err.Partition.Id, err.Partition.RetryCounter),
|
||||||
|
Prev: &err,
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case chJobErrorsOut <- jobError:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newPartition := err.Partition
|
||||||
|
newPartition.RetryCounter++
|
||||||
|
|
||||||
|
delay := computeBackoffDelay(
|
||||||
|
newPartition.RetryCounter,
|
||||||
|
retryConfig.BaseDelayMs,
|
||||||
|
retryConfig.MaxDelayMs,
|
||||||
|
retryConfig.MaxJitterMs,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err.HasLastId {
|
||||||
|
newPartition.ParentId = err.Partition.Id
|
||||||
|
newPartition.Id = uuid.New()
|
||||||
|
newPartition.Range.Min = err.LastId
|
||||||
|
newPartition.Range.IsMinInclusive = false
|
||||||
|
}
|
||||||
|
|
||||||
|
requeueWithBackoff(ctx, delay, func() {
|
||||||
|
select {
|
||||||
|
case chPartitionsOut <- newPartition:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
package extractors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Consume(
|
|
||||||
ctx context.Context,
|
|
||||||
extractor etl.Extractor,
|
|
||||||
tableInfo config.SourceTableInfo,
|
|
||||||
columns []models.ColumnType,
|
|
||||||
batchSize int,
|
|
||||||
chPartitionsIn <-chan models.Partition,
|
|
||||||
chBatchesOut chan<- models.Batch,
|
|
||||||
chErrorsOut chan<- custom_errors.JobError,
|
|
||||||
wgActivePartitions *sync.WaitGroup,
|
|
||||||
rowsRead *int64,
|
|
||||||
) {
|
|
||||||
indexPrimaryKey := slices.IndexFunc(columns, func(col models.ColumnType) bool {
|
|
||||||
return strings.EqualFold(col.Name(), tableInfo.PrimaryKey)
|
|
||||||
})
|
|
||||||
|
|
||||||
if indexPrimaryKey == -1 {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case chErrorsOut <- custom_errors.JobError{
|
|
||||||
ShouldCancelJob: true,
|
|
||||||
Msg: "Primary key not found in provided columns",
|
|
||||||
}:
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case partition, ok := <-chPartitionsIn:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsReadResult, err := extractWithRetries(
|
|
||||||
ctx,
|
|
||||||
extractor,
|
|
||||||
tableInfo,
|
|
||||||
columns,
|
|
||||||
batchSize,
|
|
||||||
partition,
|
|
||||||
indexPrimaryKey,
|
|
||||||
chBatchesOut,
|
|
||||||
)
|
|
||||||
wgActivePartitions.Done()
|
|
||||||
|
|
||||||
if rowsReadResult > 0 {
|
|
||||||
atomic.AddInt64(rowsRead, rowsReadResult)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
var jobError *custom_errors.JobError
|
|
||||||
if errors.As(err, &jobError) {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case chErrorsOut <- *jobError:
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case chErrorsOut <- custom_errors.JobError{ShouldCancelJob: false, Msg: err.Error(), Prev: err}:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
package extractors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
func extractWithRetries(
|
|
||||||
ctx context.Context,
|
|
||||||
extractor etl.Extractor,
|
|
||||||
tableInfo config.SourceTableInfo,
|
|
||||||
columns []models.ColumnType,
|
|
||||||
batchSize int,
|
|
||||||
partition models.Partition,
|
|
||||||
indexPrimaryKey int,
|
|
||||||
chBatchesOut chan<- models.Batch,
|
|
||||||
) (int64, error) {
|
|
||||||
var totalRowsRead int64
|
|
||||||
delay := time.Duration(time.Second * 1)
|
|
||||||
currentParitition := partition
|
|
||||||
|
|
||||||
for {
|
|
||||||
rowsRead, err := extractor.Exec(
|
|
||||||
ctx,
|
|
||||||
tableInfo,
|
|
||||||
columns,
|
|
||||||
batchSize,
|
|
||||||
currentParitition,
|
|
||||||
indexPrimaryKey,
|
|
||||||
chBatchesOut,
|
|
||||||
)
|
|
||||||
totalRowsRead += rowsRead
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
return totalRowsRead, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var exError *custom_errors.ExtractorError
|
|
||||||
if errors.As(err, &exError) {
|
|
||||||
currentParitition.RetryCounter++
|
|
||||||
|
|
||||||
if currentParitition.RetryCounter > 3 {
|
|
||||||
return totalRowsRead, &custom_errors.JobError{
|
|
||||||
Msg: fmt.Sprintf("Partition %v reached max retries", exError.Partition.Id),
|
|
||||||
Prev: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if exError.HasLastId {
|
|
||||||
currentParitition.ParentId = exError.Partition.Id
|
|
||||||
currentParitition.Id = uuid.New()
|
|
||||||
currentParitition.Range.Min = exError.LastId
|
|
||||||
currentParitition.Range.IsMinInclusive = false
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(delay)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
return totalRowsRead, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
package extractors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/convert"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
func errorFromLastPartitionRow(
|
|
||||||
lastRow models.UnknownRowValues,
|
|
||||||
indexPrimaryKey int,
|
|
||||||
partition models.Partition,
|
|
||||||
previousError error,
|
|
||||||
) error {
|
|
||||||
lastIdRawValue := lastRow[indexPrimaryKey]
|
|
||||||
|
|
||||||
lastId, ok := convert.ToInt64(lastIdRawValue)
|
|
||||||
if !ok {
|
|
||||||
currentPartition := partition
|
|
||||||
currentPartition.RetryCounter = 3
|
|
||||||
return &custom_errors.ExtractorError{
|
|
||||||
Partition: currentPartition,
|
|
||||||
HasLastId: true,
|
|
||||||
Msg: fmt.Sprintf("Couldn't cast last id value as int: %s", previousError.Error()),
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return &custom_errors.ExtractorError{
|
|
||||||
Partition: partition,
|
|
||||||
HasLastId: true,
|
|
||||||
LastId: lastId,
|
|
||||||
Msg: previousError.Error(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendBatch(ctx context.Context, chBatchesOut chan<- models.Batch, batch models.Batch) error {
|
|
||||||
select {
|
|
||||||
case chBatchesOut <- batch:
|
|
||||||
return nil
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func flush(
|
|
||||||
ctx context.Context,
|
|
||||||
partition *models.Partition,
|
|
||||||
batchSize int,
|
|
||||||
batchRows []models.UnknownRowValues,
|
|
||||||
chBatchesOut chan<- models.Batch,
|
|
||||||
) error {
|
|
||||||
if len(batchRows) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
batch := models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows}
|
|
||||||
batchRows = make([]models.UnknownRowValues, 0, batchSize)
|
|
||||||
return sendBatch(ctx, chBatchesOut, batch)
|
|
||||||
}
|
|
||||||
@@ -3,13 +3,20 @@ package extractors
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/convert"
|
||||||
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
dbwrapper "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/etl"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MssqlExtractor struct {
|
type MssqlExtractor struct {
|
||||||
@@ -64,7 +71,35 @@ func buildExtractQueryMssql(
|
|||||||
return sbQuery.String()
|
return sbQuery.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mssqlEx *MssqlExtractor) Exec(
|
func errorFromLastRow(
|
||||||
|
lastRow models.UnknownRowValues,
|
||||||
|
indexPrimaryKey int,
|
||||||
|
partition models.Partition,
|
||||||
|
previousError error,
|
||||||
|
) *custom_errors.ExtractorError {
|
||||||
|
lastIdRawValue := lastRow[indexPrimaryKey]
|
||||||
|
|
||||||
|
lastId, ok := convert.ToInt64(lastIdRawValue)
|
||||||
|
if !ok {
|
||||||
|
currentPartition := partition
|
||||||
|
currentPartition.RetryCounter = 3
|
||||||
|
return &custom_errors.ExtractorError{
|
||||||
|
Partition: currentPartition,
|
||||||
|
HasLastId: true,
|
||||||
|
Msg: fmt.Sprintf("Couldn't cast last id value as int: %s", previousError.Error()),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return &custom_errors.ExtractorError{
|
||||||
|
Partition: partition,
|
||||||
|
HasLastId: true,
|
||||||
|
LastId: lastId,
|
||||||
|
Msg: previousError.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mssqlEx *MssqlExtractor) ProcessPartition(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tableInfo config.SourceTableInfo,
|
tableInfo config.SourceTableInfo,
|
||||||
columns []models.ColumnType,
|
columns []models.ColumnType,
|
||||||
@@ -72,50 +107,171 @@ func (mssqlEx *MssqlExtractor) Exec(
|
|||||||
partition models.Partition,
|
partition models.Partition,
|
||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int64, error) {
|
) (int, error) {
|
||||||
query := buildExtractQueryMssql(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive)
|
query := buildExtractQueryMssql(tableInfo, columns, partition.HasRange, partition.Range.IsMinInclusive)
|
||||||
|
|
||||||
var queryArgs []any
|
var queryArgs []any
|
||||||
if partition.HasRange {
|
if partition.HasRange {
|
||||||
queryArgs = append(queryArgs, sql.Named("min", partition.Range.Min), sql.Named("max", partition.Range.Max))
|
queryArgs = append(queryArgs,
|
||||||
|
sql.Named("min", partition.Range.Min),
|
||||||
|
sql.Named("max", partition.Range.Max),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rowsRead := 0
|
||||||
rows, err := mssqlEx.db.Query(ctx, query, queryArgs...)
|
rows, err := mssqlEx.db.Query(ctx, query, queryArgs...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
batchRows := make([]models.UnknownRowValues, 0, batchSize)
|
batchRows := make([]models.UnknownRowValues, 0, batchSize)
|
||||||
var rowsRead int64 = 0
|
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
values, err := rows.Values()
|
rowValues := make([]any, len(columns))
|
||||||
if err != nil {
|
scanArgs := make([]any, len(columns))
|
||||||
if len(batchRows) == 0 {
|
|
||||||
return rowsRead, err
|
for i := range rowValues {
|
||||||
|
scanArgs[i] = &rowValues[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := flush(ctx, &partition, batchSize, batchRows, chBatchesOut); err != nil {
|
if err := rows.Scan(scanArgs...); err != nil {
|
||||||
return rowsRead, err
|
if len(batchRows) == 0 {
|
||||||
|
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
lastRow := batchRows[len(batchRows)-1]
|
lastRow := batchRows[len(batchRows)-1]
|
||||||
return rowsRead, errorFromLastPartitionRow(lastRow, indexPrimaryKey, partition, err)
|
|
||||||
|
select {
|
||||||
|
case chBatchesOut <- models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows, RetryCounter: 0}:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return rowsRead, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowsRead, errorFromLastRow(lastRow, indexPrimaryKey, partition, err)
|
||||||
}
|
}
|
||||||
rowsRead++
|
rowsRead++
|
||||||
|
|
||||||
batchRows = append(batchRows, values)
|
batchRows = append(batchRows, rowValues)
|
||||||
if len(batchRows) >= batchSize {
|
if len(batchRows) >= batchSize {
|
||||||
if err := flush(ctx, &partition, batchSize, batchRows, chBatchesOut); err != nil {
|
select {
|
||||||
return rowsRead, err
|
case chBatchesOut <- models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows, RetryCounter: 0}:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return rowsRead, ctx.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
batchRows = make([]models.UnknownRowValues, 0, batchSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
if errors.Is(err, ctx.Err()) {
|
||||||
|
return rowsRead, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(batchRows) > 0 {
|
||||||
|
lastRow := batchRows[len(batchRows)-1]
|
||||||
|
return rowsRead, errorFromLastRow(lastRow, indexPrimaryKey, partition, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(batchRows) > 0 {
|
||||||
|
select {
|
||||||
|
case chBatchesOut <- models.Batch{Id: uuid.New(), PartitionId: partition.Id, Rows: batchRows, RetryCounter: 0}:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return rowsRead, ctx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := flush(ctx, &partition, batchSize, batchRows, chBatchesOut); err != nil {
|
return rowsRead, nil
|
||||||
return rowsRead, err
|
}
|
||||||
}
|
|
||||||
|
func (mssqlEx *MssqlExtractor) Exec(
|
||||||
return rowsRead, rows.Err()
|
ctx context.Context,
|
||||||
|
tableInfo config.SourceTableInfo,
|
||||||
|
columns []models.ColumnType,
|
||||||
|
batchSize int,
|
||||||
|
chPartitionsIn <-chan models.Partition,
|
||||||
|
chBatchesOut chan<- models.Batch,
|
||||||
|
chErrorsOut chan<- custom_errors.ExtractorError,
|
||||||
|
chJobErrorsOut chan<- custom_errors.JobError,
|
||||||
|
wgActivePartitions *sync.WaitGroup,
|
||||||
|
rowsRead *int64,
|
||||||
|
) {
|
||||||
|
indexPrimaryKey := slices.IndexFunc(columns, func(col models.ColumnType) bool {
|
||||||
|
return strings.EqualFold(col.Name(), tableInfo.PrimaryKey)
|
||||||
|
})
|
||||||
|
|
||||||
|
if indexPrimaryKey == -1 {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chJobErrorsOut <- custom_errors.JobError{
|
||||||
|
ShouldCancelJob: true,
|
||||||
|
Msg: "Primary key not found in provided columns",
|
||||||
|
}:
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case partition, ok := <-chPartitionsIn:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsReadResult, err := mssqlEx.ProcessPartition(
|
||||||
|
ctx,
|
||||||
|
tableInfo,
|
||||||
|
columns,
|
||||||
|
batchSize,
|
||||||
|
partition,
|
||||||
|
indexPrimaryKey,
|
||||||
|
chBatchesOut,
|
||||||
|
)
|
||||||
|
|
||||||
|
if rowsReadResult > 0 {
|
||||||
|
atomic.AddInt64(rowsRead, int64(rowsReadResult))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
var exError *custom_errors.ExtractorError
|
||||||
|
var jobError *custom_errors.JobError
|
||||||
|
if errors.As(err, &exError) {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chErrorsOut <- *exError:
|
||||||
|
}
|
||||||
|
} else if errors.As(err, &jobError) {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chJobErrorsOut <- *jobError:
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case chErrorsOut <- custom_errors.ExtractorError{Partition: partition, Msg: err.Error()}:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
wgActivePartitions.Done()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
|
||||||
@@ -50,7 +51,7 @@ func buildExtractQueryPostgres(sourceDbInfo config.SourceTableInfo, columns []mo
|
|||||||
return fmt.Sprintf(`SELECT %s FROM "%s"."%s" ORDER BY "%s" ASC`, sbColumns.String(), sourceDbInfo.Schema, sourceDbInfo.Table, sourceDbInfo.PrimaryKey)
|
return fmt.Sprintf(`SELECT %s FROM "%s"."%s" ORDER BY "%s" ASC`, sbColumns.String(), sourceDbInfo.Schema, sourceDbInfo.Table, sourceDbInfo.PrimaryKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (postgresEx *PostgresExtractor) Exec(
|
func (postgresEx *PostgresExtractor) ProcessPartition(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tableInfo config.SourceTableInfo,
|
tableInfo config.SourceTableInfo,
|
||||||
columns []models.ColumnType,
|
columns []models.ColumnType,
|
||||||
@@ -58,14 +59,14 @@ func (postgresEx *PostgresExtractor) Exec(
|
|||||||
partition models.Partition,
|
partition models.Partition,
|
||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int64, error) {
|
) (int, error) {
|
||||||
query := buildExtractQueryPostgres(tableInfo, columns)
|
query := buildExtractQueryPostgres(tableInfo, columns)
|
||||||
|
|
||||||
if partition.HasRange {
|
if partition.HasRange {
|
||||||
return 0, errors.New("Batch config not yet supported")
|
return 0, errors.New("Batch config not yet supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
var rowsRead int64 = 0
|
rowsRead := 0
|
||||||
rows, err := postgresEx.db.Query(ctx, query)
|
rows, err := postgresEx.db.Query(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
|
||||||
@@ -108,3 +109,17 @@ func (postgresEx *PostgresExtractor) Exec(
|
|||||||
|
|
||||||
return rowsRead, nil
|
return rowsRead, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (postgresEx *PostgresExtractor) Exec(
|
||||||
|
ctx context.Context,
|
||||||
|
tableInfo config.SourceTableInfo,
|
||||||
|
columns []models.ColumnType,
|
||||||
|
batchSize int,
|
||||||
|
chPartitionsIn <-chan models.Partition,
|
||||||
|
chBatchesOut chan<- models.Batch,
|
||||||
|
chErrorsOut chan<- custom_errors.ExtractorError,
|
||||||
|
chJobErrorsOut chan<- custom_errors.JobError,
|
||||||
|
wgActivePartitions *sync.WaitGroup,
|
||||||
|
rowsRead *int64,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|||||||
@@ -74,10 +74,6 @@ func (mssqlTr *MssqlTransformer) ProcessBatch(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rowValues == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, task := range transformationPlan {
|
for _, task := range transformationPlan {
|
||||||
val := rowValues[task.Index]
|
val := rowValues[task.Index]
|
||||||
if val == nil {
|
if val == nil {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Extractor interface {
|
type Extractor interface {
|
||||||
Exec(
|
ProcessPartition(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tableInfo config.SourceTableInfo,
|
tableInfo config.SourceTableInfo,
|
||||||
columns []models.ColumnType,
|
columns []models.ColumnType,
|
||||||
@@ -18,7 +18,20 @@ type Extractor interface {
|
|||||||
partition models.Partition,
|
partition models.Partition,
|
||||||
indexPrimaryKey int,
|
indexPrimaryKey int,
|
||||||
chBatchesOut chan<- models.Batch,
|
chBatchesOut chan<- models.Batch,
|
||||||
) (int64, error)
|
) (int, error)
|
||||||
|
|
||||||
|
Exec(
|
||||||
|
ctx context.Context,
|
||||||
|
tableInfo config.SourceTableInfo,
|
||||||
|
columns []models.ColumnType,
|
||||||
|
batchSize int,
|
||||||
|
chPartitionsIn <-chan models.Partition,
|
||||||
|
chBatchesOut chan<- models.Batch,
|
||||||
|
chErrorsOut chan<- custom_errors.ExtractorError,
|
||||||
|
chJobErrorsOut chan<- custom_errors.JobError,
|
||||||
|
wgActivePartitions *sync.WaitGroup,
|
||||||
|
rowsRead *int64,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransformerFunc func(any) (any, error)
|
type TransformerFunc func(any) (any, error)
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "github.com/google/uuid"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type UnknownRowValues = []any
|
type UnknownRowValues = []any
|
||||||
|
|
||||||
@@ -25,3 +29,13 @@ type Partition struct {
|
|||||||
HasRange bool
|
HasRange bool
|
||||||
RetryCounter int
|
RetryCounter int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type JobResult struct {
|
||||||
|
JobName string
|
||||||
|
StartTime time.Time
|
||||||
|
Duration time.Duration
|
||||||
|
RowsRead int64
|
||||||
|
RowsLoaded int64
|
||||||
|
RowsFailed int64
|
||||||
|
Error error
|
||||||
|
}
|
||||||
|
|||||||
58
scripts/backoff-test/main.go
Normal file
58
scripts/backoff-test/main.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/cenkalti/backoff/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleRetry() {
|
||||||
|
// Define an operation function that returns a value and an error.
|
||||||
|
// The value can be any type.
|
||||||
|
// We'll pass this operation to Retry function.
|
||||||
|
operation := func() (string, error) {
|
||||||
|
// An example request that may fail.
|
||||||
|
resp, err := http.Get("http://httpbin.org/get")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// If we are being rate limited, return a RetryAfter to specify how long to wait.
|
||||||
|
// This will also reset the backoff policy.
|
||||||
|
if resp.StatusCode == 429 {
|
||||||
|
seconds, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
return "", backoff.RetryAfter(int(seconds))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// In case of non-retriable error, return Permanent error to stop retrying.
|
||||||
|
// For this HTTP example, client errors are non-retriable.
|
||||||
|
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
|
||||||
|
return "", backoff.Permanent(errors.New("bad request"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return successful response.
|
||||||
|
return "hello", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := backoff.Retry(context.TODO(), operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Operation is successful after retries.
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
// Output: hello
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ExampleRetry()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user