Skip to content
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

Batch index updates #332

Merged
merged 43 commits into from
Jul 9, 2024
Merged

Batch index updates #332

merged 43 commits into from
Jul 9, 2024

Conversation

devbugging
Copy link
Contributor

@devbugging devbugging commented Jul 2, 2024

Closes: #116

Description

Make sure we make the index updates atomic across different storage implementations.


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • Refactor

    • Updated method signatures to include batch parameter for improved batch processing.
    • Renamed variables for clarity and consistency across the codebase.
  • New Features

    • Added support for batch operations in storage modules.
  • Bug Fixes

    • Improved error handling in several storage methods.
  • Tests

    • Added new test for batch operations to ensure data consistency.
    • Updated existing tests to reflect changes in method signatures.

@devbugging devbugging self-assigned this Jul 2, 2024
@devbugging devbugging marked this pull request as draft July 2, 2024 14:03
Copy link
Contributor

coderabbitai bot commented Jul 2, 2024

Warning

Rate limit exceeded

@sideninja has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 15 minutes and 17 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 36d43ec and c09bf25.

Walkthrough

The changes revolve around batch processing operations in the pebble database to ensure atomicity, meaning that if an error occurs during the batch process, no partial data is persisted. This includes modifying methods to accept batch parameters, updating signatures, and altering error handling across various files including api, bootstrap, services, storage, and models.

Changes

Files Change Summary
api/api.go Updated GetNonce call to pass address directly
bootstrap/bootstrap.go Renamed pebbleDB to store; added batch parameter
models/mocks/Engine.go Updated mockery version and refactored Engine methods
services/ingestion/engine.go Added store field and batch operations to Engine struct
services/traces/engine.go Added nil parameter to StoreTransaction method
storage/index.go Added batch parameter to various indexer methods
storage/mocks/TransactionIndexer.go Updated Store method to include batch parameter
storage/mocks/mocks.go Reordered import statements
storage/pebble/accounts.go Added batch parameter and updated method signatures
storage/pebble/blocks.go Added batch parameter to Store and SetLatestCadenceHeight
storage/pebble/storage.go Modified batch handling methods, renamed newBatch to NewBatch
storage/pebble/storage_test.go Updated tests to include batch operations

Assessment against linked issues

Objective Addressed Explanation
Batch database operations (#116)

Poem

In a batch, data flows in streams,
Error-free, protected by our dreams,
Every change, a mindful act,
Code refined, no turning back.
With pebble's grace, in seamless quest,
Our system's stronger, at its best. 🌸


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@devbugging devbugging marked this pull request as ready for review July 2, 2024 16:25
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a3ad40a and 666e0c6.

Files selected for processing (24)
  • api/api.go (1 hunks)
  • bootstrap/bootstrap.go (5 hunks)
  • models/mocks/Engine.go (5 hunks)
  • services/ingestion/engine.go (6 hunks)
  • services/ingestion/engine_test.go (22 hunks)
  • services/ingestion/mocks/EventSubscriber.go (3 hunks)
  • services/traces/engine.go (1 hunks)
  • services/traces/engine_test.go (3 hunks)
  • services/traces/mocks/Downloader.go (3 hunks)
  • storage/index.go (6 hunks)
  • storage/index_testsuite.go (20 hunks)
  • storage/mocks/AccountIndexer.go (5 hunks)
  • storage/mocks/BlockIndexer.go (10 hunks)
  • storage/mocks/ReceiptIndexer.go (6 hunks)
  • storage/mocks/TraceIndexer.go (4 hunks)
  • storage/mocks/TransactionIndexer.go (4 hunks)
  • storage/mocks/mocks.go (1 hunks)
  • storage/pebble/accounts.go (4 hunks)
  • storage/pebble/blocks.go (5 hunks)
  • storage/pebble/receipts.go (3 hunks)
  • storage/pebble/storage.go (4 hunks)
  • storage/pebble/storage_test.go (6 hunks)
  • storage/pebble/traces.go (2 hunks)
  • storage/pebble/transactions.go (3 hunks)
Files skipped from review due to trivial changes (5)
  • services/ingestion/mocks/EventSubscriber.go
  • services/traces/mocks/Downloader.go
  • storage/index_testsuite.go
  • storage/mocks/TransactionIndexer.go
  • storage/mocks/mocks.go
Additional comments not posted (57)
storage/pebble/transactions.go (2)

Line range hint 27-38: LGTM!

The Store function is well-implemented with proper error handling, mutex locking for thread safety, and batch operations.


Line range hint 40-52: LGTM!

The Get function is well-implemented with proper error handling and read-locking for thread safety.

storage/pebble/traces.go (2)

28-35: LGTM!

The StoreTransaction function is well-implemented with proper error handling, mutex locking for thread safety, and batch operations.


Line range hint 37-49: LGTM!

The GetTransaction function is well-implemented with proper error handling and read-locking for thread safety.

models/mocks/Engine.go (1)

Line range hint 72-78: LGTM!

The NewEngine function is well-implemented and includes a cleanup function to assert mock expectations.

storage/mocks/TraceIndexer.go (2)

46-55: LGTM!

The StoreTransaction function is well-implemented and handles batch operations correctly.


Line range hint 65-71: LGTM!

The NewTraceIndexer function is well-implemented and includes a cleanup function to assert mock expectations.

storage/mocks/AccountIndexer.go (3)

22-38: LGTM!

The function GetBalance has been updated to accept a common.Address instead of a pointer. This change is straightforward and does not introduce any issues.


48-62: LGTM!

The function GetNonce has been updated to accept a common.Address instead of a pointer. This change is straightforward and does not introduce any issues.


72-81: LGTM!

The function Update has been updated to include a batch parameter. This change is straightforward and does not introduce any issues.

storage/pebble/accounts.go (3)

Line range hint 35-64: LGTM!

The function Update has been updated to include a batch parameter. This change ensures atomicity and does not introduce any issues.


87-95: LGTM!

The function GetNonce has been updated to accept a common.Address instead of a pointer. This change is straightforward and does not introduce any issues.


98-100: LGTM!

The function GetBalance has been updated to accept a common.Address instead of a pointer. This change is straightforward and does not introduce any issues.

services/traces/engine.go (1)

115-115: LGTM!

The function indexBlockTraces has been updated to include a nil parameter in the StoreTransaction function call. This change is straightforward and does not introduce any issues.

storage/mocks/ReceiptIndexer.go (1)

111-119: LGTM!

The function Store has been updated to include a batch parameter. This change is straightforward and does not introduce any issues.

storage/pebble/storage.go (6)

89-90: Ensure atomicity and caching logic in the set method.

The set method now supports batch operations, but the caching logic is skipped when using a batch. This is correct as the batch may not be committed yet.


92-93: Cache the value after setting it in the database.

The value is added to the cache after it is successfully set in the database. This ensures that subsequent reads can benefit from the cache.


104-104: Check the cache before accessing the database.

The get method checks the cache before accessing the database. This improves performance by reducing the number of database reads.


109-109: Handle cache misses by loading from the database.

If the value is not found in the cache, it is loaded from the database. This ensures that the cache is used effectively while maintaining data consistency.


118-120: Add the value to the cache after loading from the database.

The value is added to the cache after it is successfully loaded from the database. This ensures that subsequent reads can benefit from the cache.


131-131: Create a new batch for atomic operations.

The NewBatch method creates a new batch for atomic operations. This allows multiple operations to be grouped and committed together.

storage/index.go (8)

18-21: Add batch parameter to the Store method.

The Store method now includes a batch parameter for atomic operations. This ensures that multiple operations can be grouped and committed together.


49-50: Add batch parameter to the SetLatestCadenceHeight method.

The SetLatestCadenceHeight method now includes a batch parameter for atomic operations. This ensures that multiple operations can be grouped and committed together.


68-71: Add batch parameter to the Store method.

The Store method now includes a batch parameter for atomic operations. This ensures that multiple operations can be grouped and committed together.


93-96: Add batch parameter to the Store method.

The Store method now includes a batch parameter for atomic operations. This ensures that multiple operations can be grouped and committed together.


106-107: Add batch parameter to the Update method.

The Update method now includes a batch parameter for atomic operations. This ensures that multiple operations can be grouped and committed together.


111-111: Update the GetNonce method signature.

The GetNonce method now accepts a common.Address instead of a pointer. This change simplifies the method signature and usage.


114-114: Update the GetBalance method signature.

The GetBalance method now accepts a common.Address instead of a pointer. This change simplifies the method signature and usage.


119-121: Add batch parameter to the StoreTransaction method.

The StoreTransaction method now includes a batch parameter for atomic operations. This ensures that multiple operations can be grouped and committed together.

storage/mocks/BlockIndexer.go (2)

195-201: Add batch parameter to the SetLatestCadenceHeight method.

The mock implementation of the SetLatestCadenceHeight method now includes a batch parameter. This change aligns with the updated interface and ensures that batch operations are correctly mocked.


209-215: Add batch parameter to the Store method.

The mock implementation of the Store method now includes a batch parameter. This change aligns with the updated interface and ensures that batch operations are correctly mocked.

storage/pebble/storage_test.go (5)

37-39: Include nil batch parameter in the Store method calls.

The Store method calls now include a nil batch parameter. This change aligns with the updated method signature and ensures that the tests are correctly updated.


72-72: Include nil batch parameter in the Store method call.

The Store method call now includes a nil batch parameter. This change aligns with the updated method signature and ensures that the test is correctly updated.


86-86: Include nil batch parameter in the Store method call.

The Store method call now includes a nil batch parameter. This change aligns with the updated method signature and ensures that the test is correctly updated.


113-113: Include nil batch parameter in the Store method call.

The Store method call now includes a nil batch parameter. This change aligns with the updated method signature and ensures that the test is correctly updated.


137-165: Test batch operations for atomicity.

The TestBatch test case verifies that batch operations are correctly implemented and committed. This ensures that multiple operations can be grouped and committed together, maintaining data consistency and atomicity.

storage/pebble/blocks.go (2)

35-40: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to Store match the new signature.


179-183: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to SetLatestCadenceHeight match the new signature.

storage/pebble/receipts.go (1)

41-41: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to Store match the new signature.

services/ingestion/engine.go (5)

234-259: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to indexTransaction match the new signature.

Verification successful

The function call to indexTransaction matches the new signature.

The code changes are correct and consistent with the new function signature.

  • services/ingestion/engine.go: if err := e.indexTransaction(tx, receipts[i], batch); err != nil {
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `indexTransaction` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'indexTransaction'

Length of output: 193


Line range hint 194-227: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to indexBlock match the new signature.

Verification successful

Verified function usage in the codebase.

The changes to the indexBlock function signature are correctly applied and isolated within the services/ingestion/engine.go file.

  • indexBlock is called with the new signature in services/ingestion/engine.go.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `indexBlock` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'indexBlock'

Length of output: 439


Line range hint 94-138: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to Run match the new signature.

Verification successful

All function calls to Run match the new signature.

The code changes are verified and approved.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `Run` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'Run'

Length of output: 9007



Script:

#!/bin/bash
# Extract and verify the context of `Run` calls in `services/ingestion/engine_test.go`.

# Print the lines around the `Run` function calls to provide context.
rg --type go --context 5 'Run\(context\.Background\(\)\)' services/ingestion/engine_test.go

Length of output: 1175


Line range hint 37-51: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to NewEventIngestionEngine match the new signature.

Verification successful

Function calls match the new signature.

All function calls to NewEventIngestionEngine correctly include the store parameter as per the new signature.

  • services/ingestion/engine_test.go
  • bootstrap/bootstrap.go
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `NewEventIngestionEngine` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'NewEventIngestionEngine'

Length of output: 526



Script:

#!/bin/bash
# Description: Verify that all usages of `NewEventIngestionEngine` match the new signature.

# Check the lines where `NewEventIngestionEngine` is called in the test file
rg --type go --context 5 'NewEventIngestionEngine' services/ingestion/engine_test.go

# Check the lines where `NewEventIngestionEngine` is called in the bootstrap file
rg --type go --context 5 'NewEventIngestionEngine' bootstrap/bootstrap.go

Length of output: 1601


Line range hint 140-190: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to processEvents match the new signature.

Verification successful

All function calls to processEvents match the new signature. Verification complete.

  • services/ingestion/engine.go: The function processEvents is only called and defined within this file.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `processEvents` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'processEvents'

Length of output: 304



Script:

#!/bin/bash
# Description: Use ast-grep to search for all function calls to `processEvents` in the entire codebase.

ast-grep --lang go --pattern $'$_ := $_.processEvents($_)'

Length of output: 59



Script:

#!/bin/bash
# Description: Use rg to search for all occurrences of `processEvents` in the entire codebase to ensure comprehensive coverage.

rg 'processEvents'

Length of output: 294

services/traces/engine_test.go (3)

176-177: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to StoreTransaction match the new signature.

Verification successful

All function calls to StoreTransaction match the new signature.

The updated signature of StoreTransaction with the batch parameter is consistently used across the codebase.

  • storage/index.go
  • storage/index_testsuite.go
  • storage/pebble/traces.go
  • storage/pebble/storage_test.go
  • storage/mocks/TraceIndexer.go
  • services/traces/engine_test.go
  • services/traces/engine.go
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `StoreTransaction` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'StoreTransaction'

Length of output: 1297


77-78: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to StoreTransaction match the new signature.

Verification successful

Function signature updated consistently

The StoreTransaction function has been updated to the new signature across the codebase. The following locations confirm the consistent usage:

  • storage/index.go
  • storage/index_testsuite.go
  • storage/pebble/traces.go
  • storage/pebble/storage_test.go
  • services/traces/engine_test.go
  • services/traces/engine.go

No instances of the old signature were found.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `StoreTransaction` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'StoreTransaction'

Length of output: 1297


176-177: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to StoreTransaction match the new signature.

bootstrap/bootstrap.go (4)

33-33: Initialize pebble storage correctly.

The initialization of the store variable looks good. Ensure that cfg.DatabaseDir and logger are correctly configured.


38-42: Update function calls to use store.

The function calls are correctly updated to use the new store variable.


51-51: Include nil argument in SetLatestCadenceHeight.

The additional nil argument is correctly added to the SetLatestCadenceHeight function call.


122-122: Pass store parameter to startIngestion.

The store parameter is correctly passed to the startIngestion function.

Also applies to: 144-144

services/ingestion/engine_test.go (5)

9-10: Import pebbleDB package.

The import of the pebbleDB package is necessary for the batch operations in the tests.


12-12: Import pebble package.

The import of the pebble package is necessary for the storage operations in the tests.


38-40: Initialize store variable in tests.

The store variable is correctly initialized in the test function.


65-65: Pass store parameter in test functions.

The store parameter is correctly passed to the NewEventIngestionEngine function in various test functions.

Also applies to: 145-145, 218-219, 255-255, 354-354, 449-449


91-92: Include batch parameter in mock functions.

The batch parameter is correctly included in each mock function.

Also applies to: 171-172, 229-230, 237-238, 274-275, 284-285, 292-293, 336-337, 374-375, 382-383, 390-391, 479-480, 500-501, 510-511

api/api.go (1)

604-604: Update GetNonce method to accept address parameter.

The GetNonce method is correctly updated to accept an address parameter instead of a pointer.

Copy link
Collaborator

@m-Peter m-Peter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just a comment about the generated mocks.

@devbugging devbugging requested a review from m-Peter July 9, 2024 14:04
Copy link
Collaborator

@m-Peter m-Peter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 🚀

Co-authored-by: Ardit Marku <markoupetr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Batch database operations
2 participants