1 Commits

4 changed files with 22 additions and 66 deletions

View File

@@ -19,15 +19,10 @@ func (e *ExtractorError) Error() string {
const maxRetryAttempts = 3 const maxRetryAttempts = 3
func extractorErrorHandler(chErrorsIn <-chan ExtractorError, chBatchesOut chan<- Batch, chJobErrorsOut chan<- JobError) { func extractorErrorHandler(chErrorsIn <-chan ExtractorError, chBatchesOut chan<- Batch, chGlobalErrorsOut chan<- error) {
for err := range chErrorsIn { for err := range chErrorsIn {
if err.RetryCounter >= maxRetryAttempts { if err.RetryCounter >= maxRetryAttempts {
jobError := JobError{ chGlobalErrorsOut <- fmt.Errorf("batch %v reached max retries (%d): %s", err.Id, maxRetryAttempts, err.Msg)
ShouldCancelJob: false,
Msg: fmt.Sprintf("batch %v reached max retries (%d)", err.Id, maxRetryAttempts),
Prev: &err,
}
chJobErrorsOut <- jobError
continue continue
} }

View File

@@ -23,18 +23,20 @@ func extractFromMssql(
chBatchesIn <-chan Batch, chBatchesIn <-chan Batch,
chChunksOut chan<- []UnknownRowValues, chChunksOut chan<- []UnknownRowValues,
chErrorsOut chan<- ExtractorError, chErrorsOut chan<- ExtractorError,
chJobErrorsOut chan<- JobError,
) { ) {
indexPrimaryKey := slices.IndexFunc(columns, func(col ColumnType) bool { indexPrimaryKey := slices.IndexFunc(columns, func(col ColumnType) bool {
return strings.EqualFold(col.name, job.PrimaryKey) return strings.EqualFold(col.name, job.PrimaryKey)
}) })
if indexPrimaryKey == -1 { if indexPrimaryKey == -1 {
exError := JobError{ exError := ExtractorError{
ShouldCancelJob: true, Batch: Batch{
Msg: "Primary key not found in provided columns", RetryCounter: maxRetryAttempts,
},
HasLastId: false,
Msg: "Primary key not found in columns provided",
} }
chJobErrorsOut <- exError chErrorsOut <- exError
return return
} }
@@ -89,7 +91,6 @@ func extractFromMssql(
} }
lastRow := rowsChunk[len(rowsChunk)-1] lastRow := rowsChunk[len(rowsChunk)-1]
chChunksOut <- rowsChunk
chErrorsOut <- ExtractorErrorFromLastRowMssql(lastRow, indexPrimaryKey, &batch, err) chErrorsOut <- ExtractorErrorFromLastRowMssql(lastRow, indexPrimaryKey, &batch, err)
return return
} }

View File

@@ -1,33 +0,0 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
type JobError struct {
ShouldCancelJob bool
Msg string
Prev error
}
func (e *JobError) Error() string {
if e.Prev != nil {
return fmt.Sprintf("%s: %v", e.Msg, e.Prev)
}
return e.Msg
}
func jobErrorHandler(chErrorsIn <-chan JobError) error {
for err := range chErrorsIn {
if err.ShouldCancelJob {
return &err
}
log.Error(err)
}
return nil
}

View File

@@ -24,31 +24,18 @@ func processMigrationJob(sourceDb *sql.DB, targetDb *pgxpool.Pool, job Migration
logColumnTypes(sourceColTypes, "Source col types") logColumnTypes(sourceColTypes, "Source col types")
logColumnTypes(targetColTypes, "Target col types") logColumnTypes(targetColTypes, "Target col types")
ctx, cancel := context.WithCancel(context.Background()) mssqlCtx := context.Background()
defer cancel() batches, err := batchGeneratorMssql(mssqlCtx, sourceDb, job)
batches, err := batchGeneratorMssql(ctx, sourceDb, job)
if err != nil { if err != nil {
log.Error("Unexpected error calculating batch ranges: ", err) log.Error("Unexpected error calculating batch ranges: ", err)
} }
chJobErrors := make(chan JobError) chGlobalErrors := make(chan error)
defer close(chJobErrors) defer close(chGlobalErrors)
go func() {
if err := jobErrorHandler(chJobErrors); err != nil {
cancel()
}
}()
chBatches := make(chan Batch, len(batches)) chBatches := make(chan Batch, len(batches))
chExtractorErrors := make(chan ExtractorError, len(batches))
go func() {
extractorErrorHandler(chExtractorErrors, chBatches, chJobErrors)
}()
chChunks := make(chan []UnknownRowValues, QueueSize) chChunks := make(chan []UnknownRowValues, QueueSize)
chExtractorErrors := make(chan ExtractorError, len(batches))
maxExtractors := min(NumExtractors, len(batches)) maxExtractors := min(NumExtractors, len(batches))
var wgMssqlExtractors sync.WaitGroup var wgMssqlExtractors sync.WaitGroup
@@ -56,7 +43,7 @@ func processMigrationJob(sourceDb *sql.DB, targetDb *pgxpool.Pool, job Migration
extractStartTime := time.Now() extractStartTime := time.Now()
for range maxExtractors { for range maxExtractors {
wgMssqlExtractors.Go(func() { wgMssqlExtractors.Go(func() {
extractFromMssql(ctx, sourceDb, job, sourceColTypes, ChunkSize, chBatches, chChunks, chExtractorErrors, chJobErrors) extractFromMssql(mssqlCtx, sourceDb, job, sourceColTypes, ChunkSize, chBatches, chChunks, chExtractorErrors)
}) })
} }
@@ -68,6 +55,10 @@ func processMigrationJob(sourceDb *sql.DB, targetDb *pgxpool.Pool, job Migration
close(chExtractorErrors) close(chExtractorErrors)
}() }()
go func() {
extractorErrorHandler(chExtractorErrors, chBatches, chGlobalErrors)
}()
go func() { go func() {
wgMssqlExtractors.Wait() wgMssqlExtractors.Wait()
close(chChunks) close(chChunks)
@@ -92,15 +83,17 @@ func processMigrationJob(sourceDb *sql.DB, targetDb *pgxpool.Pool, job Migration
}() }()
var wgPostgresLoaders sync.WaitGroup var wgPostgresLoaders sync.WaitGroup
postgresLoaderCtx := context.Background()
log.Infof("Starting %d PostgreSQL loader(s)...", NumLoaders) log.Infof("Starting %d PostgreSQL loader(s)...", NumLoaders)
loaderStartTime := time.Now() loaderStartTime := time.Now()
for range NumLoaders { for range NumLoaders {
wgPostgresLoaders.Go(func() { wgPostgresLoaders.Go(func() {
if err := loadRowsPostgres(ctx, job, targetColTypes, targetDb, chRowsTransform); err != nil { if err := loadRowsPostgres(postgresLoaderCtx, job, targetColTypes, targetDb, chRowsTransform); err != nil {
log.Error("Unexpected error loading data into postgres: ", err) log.Error("Unexpected error loading data into postgres: ", err)
} }
// fakeLoader(job, sourceColTypes, chRowsTransform)
}) })
} }