-
Notifications
You must be signed in to change notification settings - Fork 14
(Requires Audit) PLEX-2473 LogPoller switch to batch inserts (Part 2) #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhaidashenko
wants to merge
5
commits into
feature/PLEX-2473-fix-reorg-handing-on-replay
Choose a base branch
from
feature/PLEX-2473-lp-batch-insert-blocks
base: feature/PLEX-2473-fix-reorg-handing-on-replay
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -956,7 +956,7 @@ func (lp *logPoller) backfill(ctx context.Context, start, end int64) error { | |||||||
| } | ||||||||
|
|
||||||||
| lp.lggr.Debugw("Inserting backfilled logs with batch endblock", "from", from, "to", to, "logs", len(gethLogs), "blocks", blocks) | ||||||||
| err = lp.orm.InsertLogsWithBlock(ctx, convertLogs(gethLogs, blocks, lp.lggr, lp.ec.ConfiguredChainID()), endblock) | ||||||||
| err = lp.orm.InsertLogsWithBlocks(ctx, convertLogs(gethLogs, blocks, lp.lggr, lp.ec.ConfiguredChainID()), []Block{endblock}) | ||||||||
| if err != nil { | ||||||||
| lp.lggr.Warnw("Unable to insert logs, retrying", "err", err, "from", from, "to", to) | ||||||||
| return err | ||||||||
|
|
@@ -1178,52 +1178,115 @@ func (lp *logPoller) pollAndSaveLogs(ctx context.Context, currentBlockNumber int | |||||||
| return fmt.Errorf("failed to backfill finalized logs: %w", err) | ||||||||
| } | ||||||||
| currentBlockNumber = lastSafeBackfillBlock + 1 | ||||||||
| currentBlock, err = lp.getCurrentBlockMaybeHandleReorg(ctx, currentBlockNumber, nil, isReplay) | ||||||||
| if err != nil { | ||||||||
| // If there's an error handling the reorg, we can't be sure what state the db was left in. | ||||||||
| // Resume from the latest block saved. | ||||||||
| return fmt.Errorf("failed to get current block: %w", err) | ||||||||
| } | ||||||||
| currentBlockNumber = currentBlock.Number | ||||||||
| } | ||||||||
|
|
||||||||
| for { | ||||||||
| if currentBlockNumber > currentBlock.Number { | ||||||||
| currentBlock, err = lp.getCurrentBlockMaybeHandleReorg(ctx, currentBlockNumber, nil, isReplay) | ||||||||
| if err != nil { | ||||||||
| // If there's an error handling the reorg, we can't be sure what state the db was left in. | ||||||||
| // Resume from the latest block saved. | ||||||||
| return fmt.Errorf("failed to get current block: %w", err) | ||||||||
|
|
||||||||
| blocks, logs, err := lp.getUnfinalizedLogs(ctx, currentBlock, latestBlockNumber, safeBlockNumber, latestFinalizedBlockNumber, isReplay) | ||||||||
| // even if we have an error, we may have partial logs and blocks that can be saved, so we save what we have and then retry. | ||||||||
| if len(logs) > 0 || len(blocks) > 0 { | ||||||||
| lp.lggr.Debugw("Saving logs", "logs", len(logs), "blocks", len(blocks), "currentBlockNumber", currentBlockNumber) | ||||||||
| insertErr := lp.orm.InsertLogsWithBlocks(ctx, logs, blocks) | ||||||||
| if insertErr != nil { | ||||||||
| lp.lggr.Warnw("Unable to save logs, retrying later", "insertErr", insertErr, "block", currentBlockNumber, "err", err) | ||||||||
| return nil | ||||||||
| } | ||||||||
| currentBlockNumber = currentBlock.Number | ||||||||
| } | ||||||||
|
|
||||||||
| if err == nil { | ||||||||
| lp.lggr.Debugw("Finished processing unfinalized blocks", "from", currentBlockNumber, "to", latestBlockNumber) | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| var reorgErr *reorgError | ||||||||
| if !errors.As(err, &reorgErr) { | ||||||||
|
Comment on lines
+1208
to
+1209
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use
Suggested change
|
||||||||
| return fmt.Errorf("failed to get unfinalized logs: %w", err) | ||||||||
| } | ||||||||
|
|
||||||||
| lp.lggr.Warnw("Reorg detected during unfinalized log processing, handling reorg", "err", err, "currentBlockNumber", currentBlockNumber, "lastKnownMatchingHead", reorgErr.ReorgedAt.Number) | ||||||||
| currentBlock, err = lp.handleReorg(ctx, reorgErr.ReorgedAt) | ||||||||
| if err != nil { | ||||||||
| return fmt.Errorf("failed to handle reorg: %w", err) | ||||||||
| } | ||||||||
| lp.lggr.Infow("Finished handling reorg, resuming log processing from new block after LCA", "currentBlockNumber", currentBlock.Number) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| type reorgError struct { | ||||||||
| ReorgedAt *evmtypes.Head | ||||||||
| } | ||||||||
|
|
||||||||
| func newReorgError(reorgedAt *evmtypes.Head) error { | ||||||||
| return &reorgError{ReorgedAt: reorgedAt} | ||||||||
| } | ||||||||
|
|
||||||||
| func (e *reorgError) Error() string { | ||||||||
| return fmt.Sprintf("reorg detected at block %d", e.ReorgedAt.Number) | ||||||||
| } | ||||||||
|
|
||||||||
| func (lp *logPoller) getUnfinalizedLogs(ctx context.Context, currentBlock *evmtypes.Head, latest, safe, finalized int64, isReplay bool) ([]Block, []Log, error) { | ||||||||
| const maxUnfinalizedBlocks = 2000 | ||||||||
| var logs []Log | ||||||||
| var blocks []Block | ||||||||
| for { | ||||||||
| h := currentBlock.Hash | ||||||||
| var logs []types.Log | ||||||||
| logs, err = lp.latencyMonitor.FilterLogs(ctx, lp.Filter(nil, nil, &h)) | ||||||||
| rpcLogs, err := lp.latencyMonitor.FilterLogs(ctx, lp.Filter(nil, nil, &h)) | ||||||||
| if err != nil { | ||||||||
| lp.lggr.Warnw("Unable to query for logs, retrying", "err", err, "block", currentBlockNumber) | ||||||||
| return nil | ||||||||
| lp.lggr.Warnw("Unable to query for logs, retrying on next poll", "err", err, "block", currentBlock.Number) | ||||||||
| return blocks, logs, nil | ||||||||
| } | ||||||||
| lp.lggr.Debugw("Unfinalized log query", "logs", len(logs), "currentBlockNumber", currentBlockNumber, "blockHash", currentBlock.Hash, "timestamp", currentBlock.Timestamp) | ||||||||
| lp.lggr.Debugw("Unfinalized log query", "logs", len(logs), "currentBlockNumber", currentBlock.Number, "blockHash", currentBlock.Hash, "timestamp", currentBlock.Timestamp) | ||||||||
| block := Block{ | ||||||||
| BlockHash: h, | ||||||||
| BlockNumber: currentBlockNumber, | ||||||||
| BlockNumber: currentBlock.Number, | ||||||||
| BlockTimestamp: currentBlock.Timestamp, | ||||||||
| FinalizedBlockNumber: latestFinalizedBlockNumber, | ||||||||
| SafeBlockNumber: safeBlockNumber, | ||||||||
| } | ||||||||
| err = lp.orm.InsertLogsWithBlock( | ||||||||
| ctx, | ||||||||
| convertLogs(logs, []Block{block}, lp.lggr, lp.ec.ConfiguredChainID()), | ||||||||
| block, | ||||||||
| ) | ||||||||
| FinalizedBlockNumber: finalized, | ||||||||
| SafeBlockNumber: safe, | ||||||||
| } | ||||||||
| logs = append(logs, convertLogs(rpcLogs, []Block{block}, lp.lggr, lp.ec.ConfiguredChainID())...) | ||||||||
| blocks = append(blocks, block) | ||||||||
|
|
||||||||
| if currentBlock.Number >= latest { | ||||||||
| return blocks, logs, nil | ||||||||
| } | ||||||||
|
|
||||||||
| if len(blocks) >= maxUnfinalizedBlocks { | ||||||||
| lp.lggr.Warnw("Too many unfinalized blocks, stopping log retrieval to avoid OOM", "currentBlockNumber", currentBlock.Number, "latestBlockNumber", latest) | ||||||||
| return blocks, logs, nil | ||||||||
| } | ||||||||
|
|
||||||||
| nextBlock, err := lp.headerByNumber(ctx, currentBlock.Number+1) | ||||||||
| if err != nil { | ||||||||
| lp.lggr.Warnw("Unable to save logs resuming from last saved block + 1", "err", err, "block", currentBlockNumber) | ||||||||
| return nil | ||||||||
| lp.lggr.Warnw("Unable to get next block header, retrying on next poll", "err", err, "block", currentBlock.Number) | ||||||||
| return blocks, logs, nil | ||||||||
| } | ||||||||
| // Update current block. | ||||||||
| // Same reorg detection on unfinalized blocks. | ||||||||
| currentBlockNumber++ | ||||||||
| if currentBlockNumber > latestBlockNumber { | ||||||||
| break | ||||||||
|
|
||||||||
| if nextBlock.ParentHash != currentBlock.Hash { | ||||||||
| return blocks, logs, newReorgError(nextBlock) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return nil | ||||||||
| if isReplay { | ||||||||
| // During replay, we also check if the next block matches what we have in the DB to avoid false positives on reorgs due to finality violation. | ||||||||
| nextBlockDB, err := lp.orm.SelectBlockByNumber(ctx, nextBlock.Number) | ||||||||
| if err != nil && !pkgerrors.Is(err, sql.ErrNoRows) { | ||||||||
| lp.lggr.Warnw("Unable to get next block from DB during replay, retrying on next poll", "err", err, "block", nextBlock.Number) | ||||||||
| return blocks, logs, nil | ||||||||
| } | ||||||||
|
|
||||||||
| if nextBlockDB != nil && nextBlock.Hash != nextBlockDB.BlockHash { | ||||||||
| return blocks, logs, newReorgError(nextBlock) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| currentBlock = nextBlock | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Returns information about latestBlock, latestFinalizedBlockNumber provided by HeadTracker | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.