Skip to content

Conversation

@nischitpra
Copy link
Collaborator

@nischitpra nischitpra commented Aug 8, 2025

Summary by CodeRabbit

  • New Features

    • Automatic cleanup of outdated staging data before starting regular operations, ensuring more efficient storage management.
    • Added capability to delete older block data entries from storage for improved data management.
  • Bug Fixes

    • Improved handling and logging of errors during staging data cleanup processes.
  • Tests

    • Enhanced test coverage with new mock methods to support validation of staging data cleanup functionality.

@coderabbitai
Copy link

coderabbitai bot commented Aug 8, 2025

Walkthrough

A new cleanup mechanism for staging data was introduced. The Committer now calls a method to remove stale staging data before starting its main loop. This is supported by new DeleteOlderThan methods in the storage connectors and interface, with corresponding updates to mocks for testing. Tests were updated to expect this cleanup call.

Changes

Cohort / File(s) Change Summary
Committer Staging Cleanup
internal/orchestrator/committer.go
Added a private cleanupStagingData method to Committer that deletes stale staging data before regular operation. The Start method now invokes this cleanup at startup. Tests in committer_test.go were updated to expect the cleanup call on staging storage.
Storage Interface & Implementations
internal/storage/connector.go, internal/storage/clickhouse.go, internal/storage/postgres.go
Introduced a DeleteOlderThan method in the IStagingStorage interface. Implemented this method in both ClickHouseConnector and PostgresConnector to delete or mark as deleted block data older than or equal to a specified block number.
Mocks for Testing
test/mocks/MockIStagingStorage.go
Added a mock implementation for the DeleteOlderThan method, including typed expectation helpers and call struct to support testing of the new cleanup behavior.

Sequence Diagram(s)

sequenceDiagram
    participant Committer
    participant MainStorage
    participant StagingStorage

    Committer->>MainStorage: Get latest committed block number
    alt Blocks committed exist
        Committer->>StagingStorage: DeleteOlderThan(chainId, blockNumber)
        StagingStorage-->>Committer: Success/Error
    end
    Committer->>Committer: Enter main loop
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch np/cleanup_simple

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
  • 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 explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this 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.

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

🧹 Nitpick comments (1)
internal/orchestrator/committer.go (1)

119-139: Clarify inclusive semantics in comments/logs and prefer structured error logging.

  • The info log says “older than or equal to” (inclusive). Ensure both backends implement the same (Postgres should use <=).
  • Prefer structured logging with Err(err) for consistency.

Apply:

- latestCommittedBlockNumber, err := c.storage.MainStorage.GetMaxBlockNumber(c.rpc.GetChainID())
+ chainID := c.rpc.GetChainID()
+ latestCommittedBlockNumber, err := c.storage.MainStorage.GetMaxBlockNumber(chainID)
  if err != nil {
-   log.Error().Msgf("Error getting latest committed block number: %v", err)
+   log.Error().Err(err).Msg("Error getting latest committed block number")
    return
  }

  if latestCommittedBlockNumber.Sign() == 0 {
    log.Debug().Msg("No blocks committed yet, skipping staging data cleanup")
    return
  }

- // Delete all staging data older than the latest committed block number
- if err := c.storage.StagingStorage.DeleteOlderThan(c.rpc.GetChainID(), latestCommittedBlockNumber); err != nil {
-   log.Error().Msgf("Error deleting staging data older than %v: %v", latestCommittedBlockNumber, err)
+ // Delete all staging data older than or equal to the latest committed block number
+ if err := c.storage.StagingStorage.DeleteOlderThan(chainID, latestCommittedBlockNumber); err != nil {
+   log.Error().Err(err).Msgf("Error deleting staging data older than or equal to %v", latestCommittedBlockNumber)
    return
  }

- log.Info().Msgf("Deleted staging data older than or equal to %v", latestCommittedBlockNumber)
+ log.Info().Msgf("Deleted staging data older than or equal to %v", latestCommittedBlockNumber)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0381b98 and 17757d3.

📒 Files selected for processing (5)
  • internal/orchestrator/committer.go (2 hunks)
  • internal/storage/clickhouse.go (1 hunks)
  • internal/storage/connector.go (1 hunks)
  • internal/storage/postgres.go (1 hunks)
  • test/mocks/MockIStagingStorage.go (1 hunks)
🔇 Additional comments (2)
internal/orchestrator/committer.go (1)

82-85: Good placement: cleanup before main loop.

Running cleanup once on start is a safe, low-overhead way to clear stale staging data.

test/mocks/MockIStagingStorage.go (1)

239-284: Mocks look correct and consistent.

Signature matches the interface and helpers provide typed Run/Return ergonomics.

@nischitpra nischitpra merged commit 7bfa6de into main Aug 8, 2025
4 of 5 checks passed
@nischitpra nischitpra deleted the np/cleanup_simple branch August 8, 2025 19:41
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 (2)
internal/orchestrator/committer_test.go (2)

407-409: Strengthen expectation: assert the call and match big.Int by value

Good to stub the cleanup call. To actually verify it happens and avoid pointer-equality pitfalls on *big.Int, match by value and assert the invocation.

Apply within the changed lines:

-	// Add expectation for DeleteOlderThan call during cleanup
-	mockStagingStorage.On("DeleteOlderThan", chainID, big.NewInt(100)).Return(nil)
+	// Expect cleanup to delete anything older than the latest committed block (by value)
+	mockStagingStorage.
+		On("DeleteOlderThan", chainID, mock.MatchedBy(func(b *big.Int) bool { return b.Cmp(big.NewInt(100)) == 0 })).
+		Return(nil).
+		Once()

Additionally, assert after the short wait to ensure it was called:

// After time.Sleep(200 * time.Millisecond)
mockStagingStorage.AssertCalled(t, "DeleteOlderThan", chainID, mock.MatchedBy(func(b *big.Int) bool {
	return b.Cmp(big.NewInt(100)) == 0
}))

444-446: Mirror the same robust expectation and assertion here

Match big.Int by value and verify the call occurs once when the committer starts, then stops on cancel.

Within the changed lines:

-	// Add expectation for DeleteOlderThan call during cleanup
-	mockStagingStorage.On("DeleteOlderThan", chainID, big.NewInt(100)).Return(nil)
+	// Expect cleanup to delete anything older than the latest committed block (by value)
+	mockStagingStorage.
+		On("DeleteOlderThan", chainID, mock.MatchedBy(func(b *big.Int) bool { return b.Cmp(big.NewInt(100)) == 0 })).
+		Return(nil).
+		Once()

And after the goroutine finishes (after <-done):

mockStagingStorage.AssertCalled(t, "DeleteOlderThan", chainID, mock.MatchedBy(func(b *big.Int) bool {
	return b.Cmp(big.NewInt(100)) == 0
}))
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1109c08 and 60f7788.

📒 Files selected for processing (2)
  • internal/orchestrator/committer_test.go (2 hunks)
  • internal/storage/clickhouse.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/storage/clickhouse.go

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.

3 participants