From 5489ae936ceadf878faf8fdb715691427c0837c0 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Tue, 29 Oct 2024 18:53:47 +0400 Subject: [PATCH] fix(chainindex): retry transaction if database connection is lost (#12657) * retry database lost connection * log context cancellation * address review --- chain/index/api.go | 3 +++ chain/index/indexer.go | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/chain/index/api.go b/chain/index/api.go index 5262a200326..1bb19de7a67 100644 --- a/chain/index/api.go +++ b/chain/index/api.go @@ -331,6 +331,9 @@ func (si *SqliteIndexer) backfillMissingTipset(ctx context.Context, ts *types.Ti if ipld.IsNotFound(err) { return nil, xerrors.Errorf("failed to backfill tipset at epoch %d: chain store does not contain data: %w", ts.Height(), err) } + if ctx.Err() != nil { + log.Errorf("failed to backfill tipset at epoch %d due to context cancellation: %s", ts.Height(), err) + } return nil, xerrors.Errorf("failed to backfill tipset at epoch %d; err: %w", ts.Height(), err) } diff --git a/chain/index/indexer.go b/chain/index/indexer.go index 10ea4fc9c8b..7cee575a6df 100644 --- a/chain/index/indexer.go +++ b/chain/index/indexer.go @@ -278,7 +278,6 @@ func (si *SqliteIndexer) indexTipset(ctx context.Context, tx *sql.Tx, ts *types. } height := ts.Height() - insertTipsetMsgStmt := tx.Stmt(si.stmts.insertTipsetMessageStmt) msgs, err := si.cs.MessagesForTipset(ctx, ts) if err != nil { @@ -286,6 +285,7 @@ func (si *SqliteIndexer) indexTipset(ctx context.Context, tx *sql.Tx, ts *types. } if len(msgs) == 0 { + insertTipsetMsgStmt := tx.Stmt(si.stmts.insertTipsetMessageStmt) // If there are no messages, just insert the tipset and return if _, err := insertTipsetMsgStmt.ExecContext(ctx, tsKeyCidBytes, height, 0, nil, -1); err != nil { return xerrors.Errorf("failed to insert empty tipset: %w", err) @@ -294,6 +294,7 @@ func (si *SqliteIndexer) indexTipset(ctx context.Context, tx *sql.Tx, ts *types. } for i, msg := range msgs { + insertTipsetMsgStmt := tx.Stmt(si.stmts.insertTipsetMessageStmt) msg := msg if _, err := insertTipsetMsgStmt.ExecContext(ctx, tsKeyCidBytes, height, 0, msg.Cid().Bytes(), i); err != nil { return xerrors.Errorf("failed to insert tipset message: %w", err)