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): using defer to ensure close db handle #20871

Merged
merged 3 commits into from
Jul 17, 2024

Conversation

yukionfire
Copy link
Contributor

@yukionfire yukionfire commented Jul 4, 2024

batch will miss to close if oprations of batch encount error, this PR uses defer to avoid this

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for the commitment metadata storage process to ensure more reliable operations.

@yukionfire yukionfire requested a review from a team as a code owner July 4, 2024 07:01
Copy link
Contributor

coderabbitai bot commented Jul 4, 2024

Walkthrough

Walkthrough

The flushCommitInfo method within the MetadataStore in metadata.go has been updated to explicitly return an error. This method now ensures proper closure of the batch operation and improves error handling for better clarity and efficiency.

Changes

Files Change Summary
store/v2/commitment/metadata.go Updated flushCommitInfo method to return an error explicitly and improved error handling to ensure proper batch closure.

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 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
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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 51d33ad and eb525ed3730fa762ec0bacdafa9952ce190fba2e.

Files selected for processing (1)
  • store/v2/commitment/metadata.go (2 hunks)
Additional context used
Path-based instructions (1)
store/v2/commitment/metadata.go (1)

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

Additional comments not posted (2)
store/v2/commitment/metadata.go (2)

69-69: Good use of defer for resource management.

The addition of defer batch.Close() ensures that the batch is closed properly, even if an error occurs during the operations. This is a good practice for resource management and enhances the robustness of the code.


91-91: Return nil upon success.

Returning nil upon a successful write operation is a good practice. It clearly indicates that the operation completed without errors.

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

lgtm, error is lost but this is more go semantically correct

Copy link
Collaborator

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

This change as is is now incorrect because the caller won't ever know if batch.Close() succeeded and that's critical. I have prescribed the proper fix down below /cc @tac0turtle @julienrbrt

@@ -87,7 +88,7 @@ func (m *MetadataStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo)
if err := batch.WriteSync(); err != nil {
return err
}
return batch.Close()
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This change is no longer correct though. The proper fix when you introduce defer would be to use a named return variable for the error and then in the defer check if the return error was already nil or not then set it so

func (m *MetadataStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo) (err error) {
      defer func() {
        cErr := batch.Close()
        if err == nil {
           err = cErr
        }
     }()

     ...
     return nil
}

Copy link
Contributor Author

@yukionfire yukionfire Jul 9, 2024

Choose a reason for hiding this comment

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

Yep, we should change to this if we care about the error of batch.Close.

BTW, if we care about the cErr, I think it's better to wrap it with err(if err is not nil)?

Copy link
Member

@kocubinski kocubinski left a comment

Choose a reason for hiding this comment

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

nice catch.

we should retain the close error. @odeke-em's approach is correct. happy to approve once implemented.

@yukionfire yukionfire force-pushed the fix/ensure-close-db branch from 5d44a4f to e00eb86 Compare July 17, 2024 10:31
@yukionfire
Copy link
Contributor Author

@kocubinski Hi sir, suggestion changes done

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, codebase verification and nitpick comments (1)
store/v2/commitment/metadata.go (1)

62-96: Review of flushCommitInfo Method

1. Error Handling and Resource Management:
The use of a named return variable (err) and the defer block for closing the batch operation is appropriate. This ensures that the batch is always closed, even if an error occurs during the method execution, which aligns with the PR's objective.

2. Code Logic:

  • The method checks if cInfo is null and returns early if it is, which is sensible for an empty or initializing store.
  • The batch operation is created and deferred closure is set up to handle errors from the batch.Close() method. This is crucial for proper resource management.
  • The method handles setting values in the batch and writes them synchronously, ensuring data integrity.

3. Error Handling in defer:
The defer block correctly checks if err is nil before setting it to cErr. This prevents overwriting of more significant errors by the error from batch.Close(), which is a good practice. However, there might be a need to consider how errors from batch.Close() are handled if err is not nil.

4. Uber Golang Style Guide Compliance:

  • Proper error handling is used.
  • Naming conventions are followed.
  • The code is concise and well-organized.

5. Potential Improvements:

  • Consider logging or wrapping the error from batch.Close() if err is not nil, to ensure that it does not go unnoticed.

Overall, the changes are well-implemented and improve the robustness and clarity of the function. The method now properly handles resource cleanup and error propagation, which are critical for database operations.

Consider enhancing error visibility when batch.Close() fails but another error is already present. This could be achieved by logging or wrapping the error.

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eb525ed3730fa762ec0bacdafa9952ce190fba2e and e00eb862d020524fc9a6b56f7a544d71a0b04d5e.

Files selected for processing (1)
  • store/v2/commitment/metadata.go (1 hunks)
Additional context used
Path-based instructions (1)
store/v2/commitment/metadata.go (1)

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

Copy link
Collaborator

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

Hey @yukionfire, thanks for the update but you didn't have to change err := to err = and then skipping the returns, no need for that edit at all since named returns handle that already, please simply just ensure your diff contains the named return (err error) and then just the defer

@yukionfire
Copy link
Contributor Author

@odeke-em Hi sir, suggestion changes addressed

@yukionfire yukionfire force-pushed the fix/ensure-close-db branch from 8872c3c to f27eaaf Compare July 17, 2024 14:02
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.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e00eb862d020524fc9a6b56f7a544d71a0b04d5e and f27eaaf.

Files selected for processing (1)
  • store/v2/commitment/metadata.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • store/v2/commitment/metadata.go

Copy link
Collaborator

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @yukionfire!

@kocubinski kocubinski enabled auto-merge July 17, 2024 15:02
@kocubinski kocubinski added this pull request to the merge queue Jul 17, 2024
Merged via the queue into cosmos:main with commit 7d2a6b9 Jul 17, 2024
68 of 71 checks passed
@yukionfire yukionfire deleted the fix/ensure-close-db branch July 20, 2024 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants