Skip to content

fix: get latest version in store after legacy pruning fails#1067

Merged
aljo242 merged 1 commit intocosmos:masterfrom
PaddyMc:fix/get-latest-version-in-store-after-legacy-pruning-fails
Mar 27, 2025
Merged

fix: get latest version in store after legacy pruning fails#1067
aljo242 merged 1 commit intocosmos:masterfrom
PaddyMc:fix/get-latest-version-in-store-after-legacy-pruning-fails

Conversation

@PaddyMc
Copy link
Contributor

@PaddyMc PaddyMc commented Mar 27, 2025

Description

Currently if legacy pruning fails it sets the firstVersion to latestLegacyVersion + 1

That works under the assumption that if legacy pruning fails that the non-legacy node is the first version is available in the store, this is not the case, and the code will then linearly iterate until it finds the next version, which could be a long wait.

Context

please read #1063 for context on this issue

What this function does

  • A binary search to find the latest non-legacy version in the store
  • This is a monkey patch and should only run only once when a node start pruning then never again, then the cached first version should be kept per node pruning session

What should actually happen

  • Pruning should succeed for legacy node
  • We should still search for the latest none-legacy node

Summary by CodeRabbit

  • New Features

    • Enhanced the process for cleaning up outdated versions, ensuring the system retains only valid data for improved reliability.
  • Tests

    • Added comprehensive tests that validate the improved version cleanup under various scenarios.

@PaddyMc PaddyMc requested a review from a team March 27, 2025 10:14
@coderabbitai
Copy link

coderabbitai bot commented Mar 27, 2025

Walkthrough

This change introduces a new method, getFirstNonLegacyVersion, in the node database module that performs a binary search to find the first non-legacy version. The deleteVersionsTo method is updated to use this new method for determining the correct starting point for deletion, replacing the prior logic that relied on legacy version offsets. In addition, a corresponding test function (TestGetFirstNonLegacyVersion) is added to validate the method under various database scenarios.

Changes

File Change Summary
nodedb.go Added getFirstNonLegacyVersion method which uses binary search with mutex locking to identify the first non-legacy version. Updated deleteVersionsTo to use this method with added error handling and comments.
nodedb_test.go Added TestGetFirstNonLegacyVersion, incorporating multiple test cases (empty database, legacy-only, and non-legacy version scenarios) using the require package for assertions.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant nodeDB
    Client->>nodeDB: deleteVersionsTo()
    nodeDB->>nodeDB: getFirstNonLegacyVersion()
    nodeDB->>nodeDB: getLatestVersion()
    nodeDB-->>Client: Return deletion result or error
Loading

Possibly related PRs

  • feat: allow saving version 0 #1002: Modifications in handling version retrieval (specifically getLatestVersion) in this PR are closely related to similar changes in the referenced PR.

Suggested labels

backport/v0.20.x, backport/v1.x

Suggested reviewers

  • aljo242
  • tac0turtle

Poem

Hopping 'round the code at night,
I found a search both swift and bright.
A binary leap to catch the right,
Non-legacy versions in my sight.
With tests that rhyme and locks in place,
This bunny cheers with boundless grace!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

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: 0

🧹 Nitpick comments (1)
nodedb_test.go (1)

449-488: Consider adopting sub-tests for clearer structure.

By splitting each test scenario into a separate t.Run block, you'll have well-defined test boundaries and clearer logs if one case fails.

Here is a quick example of using sub-tests:

-func TestGetFirstNonLegacyVersion(t *testing.T) {
-  // current test code ...
+func TestGetFirstNonLegacyVersion(t *testing.T) {
+  t.Run("Empty database", func(t *testing.T) {
+    firstVersion, err := ndb.getFirstNonLegacyVersion()
+    require.NoError(t, err)
+    require.Equal(t, int64(0), firstVersion)
+  })
+
+  t.Run("Only legacy versions", func(t *testing.T) {
+    // create legacy version, call getFirstNonLegacyVersion, etc.
+  })
+
+  t.Run("Mixed legacy and non-legacy versions", func(t *testing.T) {
+    // create version 2 as non-legacy, verify result is 2...
+  })
+
+  // additional scenarios...
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8a2e2fe and 9c0ba03.

📒 Files selected for processing (2)
  • nodedb.go (2 hunks)
  • nodedb_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Benchmarks
  • GitHub Check: Test
🔇 Additional comments (2)
nodedb_test.go (1)

449-488: Comprehensive test coverage for multiple scenarios.

This test method covers empty databases, only-legacy versions, mixed legacy and non-legacy, as well as multiple non-legacy versions. Each scenario is validated appropriately, ensuring getFirstNonLegacyVersion behaves consistently under different states. Good job!

nodedb.go (1)

777-804: Binary search logic is efficient and robust.

This implementation properly locks and unlocks before calling getLatestVersion, then uses a standard binary search to find the earliest existing version. The approach appears correct and improves performance over a linear lookup.

@aljo242 aljo242 merged commit 00941d2 into cosmos:master Mar 27, 2025
8 of 9 checks passed
@aljo242
Copy link
Contributor

aljo242 commented Mar 27, 2025

@mergify backport release/v1.2.x

@aljo242
Copy link
Contributor

aljo242 commented Mar 27, 2025

@Mergifyio backport release/v1.2.x

@mergify
Copy link
Contributor

mergify bot commented Mar 27, 2025

backport release/v1.2.x

✅ Backports have been created

Details

@aljo242
Copy link
Contributor

aljo242 commented Mar 27, 2025

@Mergifyio backport release/v1.3.x

@mergify
Copy link
Contributor

mergify bot commented Mar 27, 2025

backport release/v1.3.x

✅ Backports have been created

Details

mergify bot pushed a commit that referenced this pull request Mar 27, 2025
mergify bot pushed a commit that referenced this pull request Mar 27, 2025
aljo242 pushed a commit that referenced this pull request Mar 27, 2025
aljo242 pushed a commit that referenced this pull request Mar 27, 2025
…1067) (#1068)

Co-authored-by: PaddyMc <paddymchale@hotmail.com>
Co-authored-by: aljo242 <alex@interchainlabs.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants