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

[storage] Add index to Archive #176

Merged
merged 42 commits into from
Nov 11, 2024
Merged

Conversation

patrick-ogrady
Copy link
Contributor

@patrick-ogrady patrick-ogrady commented Nov 8, 2024

Related: #175 (Finalized Height <> Block index)

  • Track which files changed since last sync and call sync on those rather than in put (also update Journal)
  • Add index to put and infer section
  • Ensure we handle case in Archive where last record is too short (even if key successfully read)
  • How to prune IntervalTree (use rangemap)?
  • Update comments

Summary by CodeRabbit

  • New Features

    • Introduced a new dependency for enhanced storage capabilities.
    • Implemented improved handling of keys and indices in the Archive module, including a new Identifier type.
    • Enhanced buffer management in the Journal module for better data handling.
  • Bug Fixes

    • Improved error handling for duplicate indices and blob boundary checks in the Journal's replay method.
  • Documentation

    • Updated comments and documentation for clarity on new functionalities and changes.

Copy link

coderabbitai bot commented Nov 8, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces several key changes to the storage module, including the addition of a new dependency (rangemap) in the Cargo.toml file. Significant modifications are made to the Archive and Journal modules, focusing on refining data handling with the introduction of an Identifier type and adjustments to method signatures. The error handling mechanisms are enhanced, particularly concerning duplicate indices and blob integrity checks. Additionally, updates to tests ensure alignment with the new logic and terminology throughout the codebase.

Changes

File Change Summary
storage/Cargo.toml Added dependency: rangemap = "1.5.1"
storage/src/archive/mod.rs - Added section_mask field to Config struct.
- Updated put, get, and has methods to use Identifier.
- Changed error message for duplicates from "duplicate key found during replay" to "duplicate index."
- Updated tests for consistency with new logic.
storage/src/archive/storage.rs - Introduced Identifier enum and Record struct.
- Updated put, get, and has methods to accept Identifier.
- Enhanced error handling for duplicates and pruned indices.
- Updated initialization logic in init method.
storage/src/journal/mod.rs - Updated import statement for Bytes to include BufMut.
- Modified item writing logic in journal_read_item_missing function.
- Expanded tests for reading items with missing data.
storage/src/journal/storage.rs - Updated replay method to include checks for end of blob and enhanced error handling for checksum mismatches and trailing bytes.

Possibly related PRs


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@patrick-ogrady
Copy link
Contributor Author

@coderabbitai review

Copy link

coderabbitai bot commented Nov 11, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@patrick-ogrady patrick-ogrady changed the title [storage] Introduce Index [storage] Add index to Archive Nov 11, 2024
Copy link

@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: 10

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between dc37e64 and eda38a9.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • storage/Cargo.toml (1 hunks)
  • storage/src/archive/mod.rs (27 hunks)
  • storage/src/archive/storage.rs (11 hunks)
  • storage/src/journal/mod.rs (2 hunks)
  • storage/src/journal/storage.rs (1 hunks)
🧰 Additional context used
📓 Learnings (1)
storage/src/journal/storage.rs (1)
Learnt from: patrick-ogrady
PR: commonwarexyz/monorepo#161
File: storage/src/journal/storage.rs:307-310
Timestamp: 2024-11-04T17:31:58.490Z
Learning: The test `test_journal_read_checksum_missing` in `storage/src/journal/mod.rs` covers the code handling blob truncation when trailing bytes are detected.
🪛 GitHub Check: codecov/patch
storage/src/archive/mod.rs

[warning] 269-269: storage/src/archive/mod.rs#L269
Added line #L269 was not covered by tests


[warning] 274-274: storage/src/archive/mod.rs#L274
Added line #L274 was not covered by tests


[warning] 287-287: storage/src/archive/mod.rs#L287
Added line #L287 was not covered by tests


[warning] 292-292: storage/src/archive/mod.rs#L292
Added line #L292 was not covered by tests


[warning] 299-299: storage/src/archive/mod.rs#L299
Added line #L299 was not covered by tests


[warning] 411-411: storage/src/archive/mod.rs#L411
Added line #L411 was not covered by tests


[warning] 620-620: storage/src/archive/mod.rs#L620
Added line #L620 was not covered by tests


[warning] 677-677: storage/src/archive/mod.rs#L677
Added line #L677 was not covered by tests


[warning] 685-685: storage/src/archive/mod.rs#L685
Added line #L685 was not covered by tests


[warning] 998-998: storage/src/archive/mod.rs#L998
Added line #L998 was not covered by tests


[warning] 1048-1048: storage/src/archive/mod.rs#L1048
Added line #L1048 was not covered by tests


[warning] 1083-1083: storage/src/archive/mod.rs#L1083
Added line #L1083 was not covered by tests

storage/src/archive/storage.rs

[warning] 207-207: storage/src/archive/storage.rs#L207
Added line #L207 was not covered by tests


[warning] 235-236: storage/src/archive/storage.rs#L235-L236
Added lines #L235 - L236 were not covered by tests


[warning] 247-247: storage/src/archive/storage.rs#L247
Added line #L247 was not covered by tests


[warning] 378-378: storage/src/archive/storage.rs#L378
Added line #L378 was not covered by tests


[warning] 489-489: storage/src/archive/storage.rs#L489
Added line #L489 was not covered by tests


[warning] 511-511: storage/src/archive/storage.rs#L511
Added line #L511 was not covered by tests


[warning] 553-553: storage/src/archive/storage.rs#L553
Added line #L553 was not covered by tests


[warning] 560-560: storage/src/archive/storage.rs#L560
Added line #L560 was not covered by tests

🔇 Additional comments (5)
storage/Cargo.toml (1)

25-25: Verify rangemap dependency

✅ Verification successful

Dependency verified and good to go

  • Version 1.5.1 is the latest available
  • License (MIT/Apache-2.0) is compatible with the project
  • Properly used via RangeInclusiveSet in storage implementation
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check latest version and license of rangemap
curl -s https://crates.io/api/v1/crates/rangemap | jq -r '.crate | "Latest version: " + .max_version + "\nLicense: " + .license'

# Verify actual usage of rangemap in the codebase
rg -l "use.*rangemap" 
rg -l "::rangemap"

Length of output: 236


Script:

#!/bin/bash
# Check rangemap's license from its repository
curl -s https://raw.githubusercontent.com/jeffparsons/rangemap/master/Cargo.toml | rg -A 1 'license'

# Check actual usage context
rg -A 5 -B 5 'rangemap' storage/src/archive/storage.rs

# Check if rangemap is actually used in the implementation
ast-grep --pattern 'use rangemap::$_'

Length of output: 710

storage/src/journal/mod.rs (1)

145-145: LGTM!

The addition of BufMut import is appropriate for the buffer manipulation needs.

storage/src/journal/storage.rs (1)

271-288: 🧹 Nitpick (assertive)

🛠️ Refactor suggestion

Add tests for new blob boundary conditions

Ensure the new checks for end-of-blob and over-read conditions in the replay method are covered by unit tests to prevent regressions.

Do you want assistance in creating these tests or opening a GitHub issue to track this?

⛔ Skipped due to learnings
Learnt from: patrick-ogrady
PR: commonwarexyz/monorepo#161
File: storage/src/journal/storage.rs:307-310
Timestamp: 2024-11-04T17:31:58.490Z
Learning: The test `test_journal_read_checksum_missing` in `storage/src/journal/mod.rs` covers the code handling blob truncation when trailing bytes are detected.
storage/src/archive/storage.rs (2)

378-379: Unnecessary Use of .ok_or After a Non-Fallible Call

The call to .get on the journal should always return Some if indices contains the key. The use of .ok_or(Error::RecordCorrupted)? suggests that None is possible, which may not be the case. Verify whether this check is necessary or if it can be removed to simplify the code.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 378-378: storage/src/archive/storage.rs#L378
Added line #L378 was not covered by tests


445-452: 🧹 Nitpick (assertive)

Simplify the has Method Logic

The has method can be streamlined by directly returning the result of the checks without matching on Identifier unnecessarily.

Apply this diff for a cleaner implementation:

pub async fn has(&self, identifier: Identifier<'_>) -> Result<bool, Error> {
    self.has.inc();
    match identifier {
-       Identifier::Index(index) => Ok(self.has_index(index)),
-       Identifier::Key(key) => self.has_key(key).await,
+       Identifier::Index(index) => Ok(self.indices.contains_key(&index)),
+       Identifier::Key(key) => {
+           let result = self.get_key(key).await?.is_some();
+           Ok(result)
+       }
    }
}

Likely invalid or redundant comment.

storage/src/journal/mod.rs Show resolved Hide resolved
storage/src/archive/storage.rs Show resolved Hide resolved
storage/src/archive/storage.rs Show resolved Hide resolved
storage/src/archive/storage.rs Show resolved Hide resolved
storage/src/archive/storage.rs Show resolved Hide resolved
storage/src/archive/storage.rs Show resolved Hide resolved
storage/src/archive/storage.rs Outdated Show resolved Hide resolved
storage/src/archive/storage.rs Show resolved Hide resolved
storage/src/archive/mod.rs Show resolved Hide resolved
storage/src/archive/mod.rs Outdated Show resolved Hide resolved
Some(prefix) => Self::read_prefix(blob, offset, prefix).await,
None => Self::read(blob, offset).await,
};

// Ensure a full read wouldn't put us past the end of the blob
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a bug.

coderabbitai[bot]
coderabbitai bot previously approved these changes Nov 11, 2024
@patrick-ogrady patrick-ogrady marked this pull request as ready for review November 11, 2024 17:52
@patrick-ogrady patrick-ogrady merged commit 793d38e into main Nov 11, 2024
6 checks passed
@patrick-ogrady patrick-ogrady deleted the index+interior-mutability branch November 11, 2024 18:07
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

Attention: Patch coverage is 95.10022% with 22 lines in your changes missing coverage. Please review.

Project coverage is 93.09%. Comparing base (dc37e64) to head (618c178).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
storage/src/archive/mod.rs 94.58% 13 Missing ⚠️
storage/src/archive/storage.rs 95.43% 9 Missing ⚠️
@@            Coverage Diff             @@
##             main     #176      +/-   ##
==========================================
+ Coverage   92.96%   93.09%   +0.13%     
==========================================
  Files          56       56              
  Lines       11320    11505     +185     
==========================================
+ Hits        10524    10711     +187     
+ Misses        796      794       -2     
Files with missing lines Coverage Δ
storage/src/journal/mod.rs 85.84% <100.00%> (+0.10%) ⬆️
storage/src/journal/storage.rs 93.28% <100.00%> (+0.13%) ⬆️
storage/src/archive/storage.rs 94.77% <95.43%> (+1.55%) ⬆️
storage/src/archive/mod.rs 91.81% <94.58%> (-0.10%) ⬇️

... and 3 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc37e64...618c178. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant