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

fix(store/v2/pebble): handle version 0 in keys #22524

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kocubinski
Copy link
Member

@kocubinski kocubinski commented Nov 14, 2024

Description

Closes: #22523

Fixes handling of version 0 in keys. Version 0 is supported on the write side, but surprisingly not on read.

Also includes a fix for very large batches; I exceeded the 4gb limit in InitGenesis in load testing.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features
    • Introduced size tracking for key-value batches to enhance batch management.
  • Bug Fixes
    • Improved error handling for empty version slices in database operations.
    • Enhanced iteration logic to correctly handle tombstones and versions.
  • Refactor
    • Adjusted variable declarations and control flow in the iterator for robustness.

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

📝 Walkthrough

Walkthrough

The pull request introduces modifications to the pebbledb package, focusing on enhancing the Batch struct for size management, improving error handling for key versioning in the Database struct, and refining the iteration logic in the iterator struct. A new field size is added to track the cumulative size of key-value pairs, while new checks prevent errors from empty version slices. These changes collectively aim to improve the robustness and functionality of the PebbleDB implementation.

Changes

File Path Change Summary
store/v2/storage/pebbledb/batch.go Added size int to Batch struct; introduced keyValueSize function; modified set method for size management.
store/v2/storage/pebbledb/db.go Enhanced Prune and getMVCCSlice methods with checks for empty version byte slices to prevent decoding errors.
store/v2/storage/pebbledb/iterator.go Modified DebugRawIterate for improved handling of versioning and tombstone logic; adjusted error handling and iteration flow.

Assessment against linked issues

Objective Addressed Explanation
Prevent decoding errors for empty version slices (22523)
Ensure robustness in batch operations (22523)

Possibly related PRs

Suggested labels

C:Store

Suggested reviewers

  • tac0turtle
  • julienrbrt
  • cool-develope

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.

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.

const (
oneIf64Bit = ^uint(0) >> 63
maxUint32OrInt = (1<<31)<<oneIf64Bit - 1
maxVarintLen32 = 5
Copy link
Member Author

Choose a reason for hiding this comment

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

these consts are duplicated from the pebbledb library for size calculation.

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

🧹 Outside diff range and nitpick comments (5)
store/v2/storage/pebbledb/batch.go (3)

23-27: Consider adding documentation for platform-specific constants

The constants are well-designed for handling platform-specific size limits. Consider adding comments explaining the bit manipulation logic for better maintainability.

 const (
+	// oneIf64Bit is 1 on 64-bit platforms and 0 on 32-bit platforms
 	oneIf64Bit     = ^uint(0) >> 63
+	// maxUint32OrInt is math.MaxUint32 on 32-bit platforms and math.MaxInt on 64-bit platforms
 	maxUint32OrInt = (1<<31)<<oneIf64Bit - 1
+	// maxVarintLen32 is the maximum length of a varint-encoded 32-bit integer
 	maxVarintLen32 = 5
 )

29-31: Document size calculation components

The size calculation is correct but would benefit from documentation explaining the components of the calculation.

+// keyValueSize returns the total size needed to store a key-value pair,
+// including the overhead for varint encoding and metadata
 func keyValueSize(key, value []byte) int {
 	return len(key) + len(value) + 1 + 2*maxVarintLen32
 }

65-78: Improve error message for size limit exceeded case

The size management logic is well-implemented, but the error message could be more informative by including the actual sizes.

 		if err := b.batch.Commit(&pebble.WriteOptions{Sync: b.sync}); err != nil {
-			return fmt.Errorf("max batch size exceed: failed to write PebbleDB batch: %w", err)
+			return fmt.Errorf("batch size limit exceeded (current: %d, new: %d, max: %d): failed to write PebbleDB batch: %w",
+				b.size, size, maxUint32OrInt, err)
 		}
store/v2/storage/pebbledb/iterator.go (1)

Line range hint 238-244: Fix incorrect variable used in tombstone decoding.

There's a bug in the tombstone decoding logic where it uses vBz (version bytes) instead of tombBz (tombstone bytes).

Apply this fix:

  var tombstone uint64
  if len(tombBz) > 0 {
-   tombstone, err = decodeUint64Ascending(vBz)
+   tombstone, err = decodeUint64Ascending(tombBz)
    if err != nil {
      panic(fmt.Errorf("failed to decode value tombstone: %w", err))
    }
  }
store/v2/storage/pebbledb/db.go (1)

438-444: Consider explicit handling of version 0 comparison.

While the implementation correctly handles empty version bytes, consider adding explicit handling for the case where both the target version and key version are 0 to make the logic more robust.

Consider this modification:

 var keyVersion uint64
 // handle version 0 (no version prefix)
 if len(vBz) > 0 {
     keyVersion, err = decodeUint64Ascending(vBz)
     if err != nil {
         return nil, fmt.Errorf("failed to decode key version: %w", err)
     }
+} else if version == 0 {
+    // Special case: both target and key versions are 0
+    keyVersion = 0
 }
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e74799e and b17c4b3.

📒 Files selected for processing (3)
  • store/v2/storage/pebbledb/batch.go (3 hunks)
  • store/v2/storage/pebbledb/db.go (2 hunks)
  • store/v2/storage/pebbledb/iterator.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
store/v2/storage/pebbledb/batch.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/pebbledb/db.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/storage/pebbledb/iterator.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (5)
store/v2/storage/pebbledb/batch.go (3)

20-21: LGTM: Size tracking field addition

The addition of the size field to track cumulative batch size is well-designed and aligns with the PR's objective of handling large data sizes.


48-48: LGTM: Proper size initialization

The size initialization correctly accounts for the version key-value pair using the new helper function.


65-73: Verify handling of large genesis data

The auto-commit mechanism should handle the large genesis data case mentioned in the PR objectives. Let's verify this with a test case.

store/v2/storage/pebbledb/iterator.go (1)

226-232: LGTM: Version 0 handling looks correct.

The changes properly handle the case where a key has no version prefix (version 0) by checking the length of the version bytes before attempting to decode.

Let's verify the version 0 handling with the following script:

store/v2/storage/pebbledb/db.go (1)

241-247: LGTM! Robust handling of version 0 keys.

The implementation correctly handles both versioned and unversioned keys by checking the version byte slice length before decoding. This prevents the "insufficient bytes to decode uint64" error mentioned in issue #22523.

Let's verify the version handling implementation:

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.

[Bug]: simapp/v2 does not start with pebbledb
3 participants