refactor: remove extractor error channel and simplify retry logic in mssql and postgres extractors

This commit is contained in:
2026-04-17 00:07:51 -05:00
parent 1c3db39b21
commit 73b65e2a3f
3 changed files with 80 additions and 38 deletions

View File

@@ -200,12 +200,10 @@ func (mssqlEx *MssqlExtractor) ExtractWithRetries(
chBatchesOut chan<- models.Batch,
) (int64, error) {
var totalRowsRead int64
var fatalErr error
delay := time.Duration(time.Second * 1)
currentParitition := partition
for fatalErr != nil || currentParitition.RetryCounter < 3 {
currentParitition.RetryCounter++
for {
rowsRead, err := mssqlEx.Extract(
ctx,
tableInfo,
@@ -215,33 +213,36 @@ func (mssqlEx *MssqlExtractor) ExtractWithRetries(
indexPrimaryKey,
chBatchesOut,
)
totalRowsRead += rowsRead
if rowsRead > 0 {
totalRowsRead += int64(rowsRead)
if err == nil {
return totalRowsRead, nil
}
if err != nil {
var exError *custom_errors.ExtractorError
if errors.As(err, &exError) {
if exError.HasLastId {
currentParitition.ParentId = exError.Partition.Id
currentParitition.Id = uuid.New()
currentParitition.Range.Min = exError.LastId
currentParitition.Range.IsMinInclusive = false
}
var exError *custom_errors.ExtractorError
if errors.As(err, &exError) {
currentParitition.RetryCounter++
time.Sleep(delay)
} else {
fatalErr = err
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
}
break
return totalRowsRead, err
}
return totalRowsRead, fatalErr
}
func (mssqlEx *MssqlExtractor) Consume(
@@ -294,6 +295,7 @@ func (mssqlEx *MssqlExtractor) Consume(
indexPrimaryKey,
chBatchesOut,
)
wgActivePartitions.Done()
if rowsReadResult > 0 {
atomic.AddInt64(rowsRead, int64(rowsReadResult))
@@ -317,8 +319,6 @@ func (mssqlEx *MssqlExtractor) Consume(
continue
}
wgActivePartitions.Done()
}
}
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"sync"
"time"
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/custom_errors"
@@ -59,14 +60,14 @@ func (postgresEx *PostgresExtractor) Extract(
partition models.Partition,
indexPrimaryKey int,
chBatchesOut chan<- models.Batch,
) (int, error) {
) (int64, error) {
query := buildExtractQueryPostgres(tableInfo, columns)
if partition.HasRange {
return 0, errors.New("Batch config not yet supported")
}
rowsRead := 0
var rowsRead int64 = 0
rows, err := postgresEx.db.Query(ctx, query)
if err != nil {
return rowsRead, &custom_errors.ExtractorError{Partition: partition, HasLastId: false, Msg: err.Error()}
@@ -110,6 +111,61 @@ func (postgresEx *PostgresExtractor) Extract(
return rowsRead, nil
}
func (postgresEx *PostgresExtractor) ExtractWithRetries(
ctx context.Context,
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 := postgresEx.Extract(
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
}
}
func (postgresEx *PostgresExtractor) Consume(
ctx context.Context,
tableInfo config.SourceTableInfo,
@@ -117,7 +173,6 @@ func (postgresEx *PostgresExtractor) Consume(
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,