From b6fc15dd8192eef13302bff78b10e369fe65f1c2 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 21 Jun 2023 21:32:07 +0800 Subject: [PATCH] Port: Fix deferred rollback on error --- indexer/database/sql/lazy_tx.go | 4 ++-- metrics_helpers.go | 11 ++++++----- service.go | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/indexer/database/sql/lazy_tx.go b/indexer/database/sql/lazy_tx.go index 6b67921..7543944 100644 --- a/indexer/database/sql/lazy_tx.go +++ b/indexer/database/sql/lazy_tx.go @@ -84,13 +84,13 @@ func (tx *DelayedTx) Commit(ctx context.Context) error { for _, item := range tx.cache { switch item := item.(type) { case *copyFrom: - _, err := base.CopyFrom(ctx, item.tableName, item.columnNames, item.rows) + _, err = base.CopyFrom(ctx, item.tableName, item.columnNames, item.rows) if err != nil { log.Error("COPY error", "table", item.tableName, "error", err) return err } case cachedStmt: - _, err := base.Exec(ctx, item.sql, item.args...) + _, err = base.Exec(ctx, item.sql, item.args...) if err != nil { return err } diff --git a/metrics_helpers.go b/metrics_helpers.go index 54e5440..d59abc7 100644 --- a/metrics_helpers.go +++ b/metrics_helpers.go @@ -38,19 +38,20 @@ func countStateDiffBegin(block *types.Block) (time.Time, log.Logger) { return start, logger } -func countStateDiffEnd(start time.Time, logger log.Logger, err error) time.Duration { +func countStateDiffEnd(start time.Time, logger log.Logger, err *error) time.Duration { duration := time.Since(start) defaultStatediffMetrics.underway.Dec(1) - if nil == err { - defaultStatediffMetrics.succeeded.Inc(1) - } else { + failed := nil != err && nil != *err + if failed { defaultStatediffMetrics.failed.Inc(1) + } else { + defaultStatediffMetrics.succeeded.Inc(1) } defaultStatediffMetrics.totalProcessingTime.Inc(duration.Milliseconds()) logger.Debug("writeStateDiff END", "duration", duration, - "error", err != nil, + "error", failed, "underway", defaultStatediffMetrics.underway.Count(), "succeeded", defaultStatediffMetrics.succeeded.Count(), "failed", defaultStatediffMetrics.failed.Count(), diff --git a/service.go b/service.go index be45a6e..02d598b 100644 --- a/service.go +++ b/service.go @@ -681,7 +681,7 @@ func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, p var err error var tx interfaces.Batch start, logger := countStateDiffBegin(block) - defer countStateDiffEnd(start, logger, err) + defer countStateDiffEnd(start, logger, &err) if sds.indexer == nil { return fmt.Errorf("indexer is not set; cannot write indexed diffs") } @@ -716,7 +716,7 @@ func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, p }, params, output, ipldOutput) // TODO this anti-pattern needs to be sorted out eventually - if err := tx.Submit(err); err != nil { + if err = tx.Submit(err); err != nil { return fmt.Errorf("batch transaction submission failed: %w", err) }