Skip to content

Commit

Permalink
Improve performance when populating message indax
Browse files Browse the repository at this point in the history
  • Loading branch information
fridrik01 committed Mar 24, 2023
1 parent 59640a8 commit 48c57d3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions chain/index/msgindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,20 @@ func PopulateAfterSnapshot(lctx context.Context, basePath string, cs ChainStore)
return xerrors.Errorf("error creating msgindex database: %w", err)
}

insertStmt, err := db.Prepare(dbqInsertMessage)
tx, err := db.Begin()
if err != nil {
return xerrors.Errorf("prepare insertMsgStmt: %w", err)
return xerrors.Errorf("error when starting transaction: %w", err)
}

rollback := func() {
if err := tx.Rollback(); err != nil {
log.Errorf("error in rollback: %s", err)
}
}

insertStmt, err := tx.Prepare(dbqInsertMessage)
if err != nil {
return xerrors.Errorf("error preparing insertMsgStmt: %w", err)
}
defer insertStmt.Close()

Expand All @@ -201,14 +212,13 @@ func PopulateAfterSnapshot(lctx context.Context, basePath string, cs ChainStore)
for curTs != nil {
tscid, err := curTs.Key().Cid()
if err != nil {
rollback()
return xerrors.Errorf("error computing tipset cid: %w", err)
}

tskey := tscid.String()
epoch := int64(curTs.Height())

//log.Infof("epoch %d-%d, populating msgindex with tipset %s", curTs.Height(), startHeight-curTs.Height(), tskey)

msgs, err := cs.MessagesForTipset(lctx, curTs)
if err != nil {
log.Infof("stopping import after %d tipsets", startHeight-curTs.Height())
Expand All @@ -218,16 +228,23 @@ func PopulateAfterSnapshot(lctx context.Context, basePath string, cs ChainStore)
for _, msg := range msgs {
key := msg.Cid().String()
if _, err := insertStmt.Exec(key, tskey, epoch); err != nil {
rollback()
return xerrors.Errorf("error inserting message: %w", err)
}
}

curTs, err = cs.GetTipSetFromKey(lctx, curTs.Parents())
if err != nil {
rollback()
return xerrors.Errorf("error walking chain: %w", err)
}
}

err = tx.Commit()
if err != nil {
return xerrors.Errorf("error commiting transaction: %w", err)
}

return nil
}

Expand Down

0 comments on commit 48c57d3

Please sign in to comment.