Skip to content

Commit

Permalink
fix(chainindex): retry transaction if database connection is lost (#1…
Browse files Browse the repository at this point in the history
…2657)

* retry database lost connection

* log context cancellation

* address review
  • Loading branch information
aarshkshah1992 authored Oct 29, 2024
1 parent 4b6a582 commit 5489ae9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions chain/index/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion chain/index/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,14 @@ 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 {
return xerrors.Errorf("failed to get messages for tipset: %w", err)
}

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)
Expand All @@ -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)
Expand Down

0 comments on commit 5489ae9

Please sign in to comment.