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

remove errBadConnNoWrite and markBadConn #1583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

methane
Copy link
Member

@methane methane commented May 16, 2024

Description

Fix #1582.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

Copy link

coderabbitai bot commented May 16, 2024

Walkthrough

The changes streamline error handling in the MySQL connection implementation by removing redundant error marking and logging, and directly returning errors. This includes eliminating the markBadConn method and errBadConnNoWrite error, while adding IsValid and SessionResetter methods to improve connection validation and reset mechanisms.

Changes

File Change Summary
connection.go Removed markBadConn method, logging calls, adjusted error returns, added IsValid and SessionResetter methods.
connection_test.go Initialized closech channel in tests, changed error comparison to use errors.Is.
errors.go Removed errBadConnNoWrite error declaration.
packets.go Refactored writePacket method for better error handling and control flow.
statement.go Modified Exec and query methods to return errors directly without marking connection as bad.

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Improve ErrBadConn handling (#1582)
Remove errBadConnNoWrite (#1582)
Implement IsValid() and ResetSession() (#1582)

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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

@methane methane requested a review from a team May 16, 2024 08:51
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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between af8d793 and 0c65ab7.
Files selected for processing (5)
  • connection.go (11 hunks)
  • connection_test.go (2 hunks)
  • errors.go (1 hunks)
  • packets.go (8 hunks)
  • statement.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • errors.go
Additional comments not posted (20)
connection_test.go (2)

171-172: Good use of errors.Is for error comparison.


188-189: Good use of errors.Is for error comparison.

statement.go (2)

60-60: Simplified error handling by directly returning the error.


103-103: Simplified error handling by directly returning the error.

connection.go (6)

120-120: Simplified error handling by directly returning the error.


174-174: Simplified error handling by directly returning the error.


207-208: Simplified error handling by directly returning the error.


318-318: Simplified error handling by directly returning the error.


462-462: Simplified error handling by directly returning the error.


668-669: Good addition of the IsValid method to implement the driver.Validator interface.

packets.go (10)

125-125: Ensure proper cleanup on write error.

The call to mc.cleanup() on write error ensures that resources are properly released. This is a good practice to avoid resource leaks.


130-131: Return specific error on write failure.

Returning the specific error err on write failure helps in better error diagnosis and handling.


132-134: Handle short write scenario.

Returning io.ErrShortWrite when n is not equal to size + 4 ensures that partial writes are correctly identified and handled.


299-300: Ensure proper cleanup on buffer error.

The call to mc.cleanup() on buffer error ensures that resources are properly released. This is a good practice to avoid resource leaks.


388-389: Ensure proper cleanup on buffer error.

The call to mc.cleanup() on buffer error ensures that resources are properly released. This is a good practice to avoid resource leaks.


408-409: Ensure proper cleanup on buffer error.

The call to mc.cleanup() on buffer error ensures that resources are properly released. This is a good practice to avoid resource leaks.


427-428: Ensure proper cleanup on buffer error.

The call to mc.cleanup() on buffer error ensures that resources are properly released. This is a good practice to avoid resource leaks.


448-449: Ensure proper cleanup on buffer error.

The call to mc.cleanup() on buffer error ensures that resources are properly released. This is a good practice to avoid resource leaks.


990-991: Ensure proper cleanup on buffer error.

The call to mc.cleanup() on buffer error ensures that resources are properly released. This is a good practice to avoid resource leaks.


1189-1190: Ensure proper cleanup on buffer store error.

The call to mc.cleanup() on buffer store error ensures that resources are properly released. This is a good practice to avoid resource leaks.

@coveralls
Copy link

coveralls commented May 16, 2024

Coverage Status

coverage: 82.331% (-0.1%) from 82.462%
when pulling f58f76c on methane:bye-errBadConnNoWrite
into af8d793 on go-sql-driver:master.

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 0c65ab7 and f554b55.
Files selected for processing (2)
  • connection.go (11 hunks)
  • packets.go (9 hunks)
Files skipped from review as they are similar to previous changes (2)
  • connection.go
  • packets.go

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f554b55 and db0cc0e.
Files selected for processing (5)
  • connection.go (11 hunks)
  • connection_test.go (2 hunks)
  • errors.go (1 hunks)
  • packets.go (9 hunks)
  • statement.go (2 hunks)
Files skipped from review as they are similar to previous changes (5)
  • connection.go
  • connection_test.go
  • errors.go
  • packets.go
  • statement.go

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between db0cc0e and f58f76c.
Files selected for processing (2)
  • connection.go (11 hunks)
  • packets.go (9 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packets.go
Additional comments not posted (5)
connection.go (5)

120-120: Ensure proper error handling in the begin method.


323-323: Ensure that the error handling in Exec is consistent with the new error handling strategy.


331-331: Review the error handling in exec to ensure it aligns with the new error handling strategy.


471-471: Confirm that the error handling in Ping aligns with the new error handling strategy.


677-678: Confirm implementation of interfaces driver.SessionResetter and driver.Validator.

The implementation of these interfaces is crucial for the new connection validation logic. Good to see this aligned with the PR objectives.

Also applies to: 685-685

Comment on lines +209 to +213
mc.cleanup()
// interpolateParams would be called before sending any query.
// So its safe to retry.
mc.log(err)
return "", ErrInvalidConn
return "", driver.ErrBadConn
Copy link

@coderabbitai coderabbitai bot May 18, 2024

Choose a reason for hiding this comment

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

Consider simplifying the error handling in interpolateParams.

- mc.cleanup()
- mc.log(err)
+ return "", err

This change would make the error handling more straightforward and consistent with other parts of the code.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
mc.cleanup()
// interpolateParams would be called before sending any query.
// So its safe to retry.
mc.log(err)
return "", ErrInvalidConn
return "", driver.ErrBadConn
// interpolateParams would be called before sending any query.
// So its safe to retry.
return "", err

Copy link
Member Author

Choose a reason for hiding this comment

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

This is simply a bug fix. Since it did not close the connection and also did not return ErrBadConn, if the connection was in a state where takeCompleteBuffer() failed for any reason, ErrInvalidConn could be returned repeatedly, depending on how the application was used.
This fix may need to be backported. I will create a separate PR.

Copy link

Choose a reason for hiding this comment

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

@methane, thank you for your insights and for highlighting the importance of this bug fix. Ensuring that ErrBadConn is returned correctly is crucial for the robustness of error handling in connection management. If there's anything more you need regarding the backporting process or any other assistance, please feel free to let me know. Looking forward to seeing the improvements in the upcoming PR!

@methane methane requested review from shogo82148 and removed request for shogo82148 June 5, 2024 06:51
Copy link
Member Author

@methane methane left a comment

Choose a reason for hiding this comment

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

I will split this PR to smaller PRs.

return err
}
return driver.ErrBadConn
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This function and errBadConnNoWrite were completely meaningless. There is no need to use errBadConnNoWrite once and convert it to ErrBadConn, as you can just use ErrBadConn directly if you are not sending anything yet.

func (mc *mysqlConn) Begin() (driver.Tx, error) {
return mc.begin(false)
}

func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
if mc.closed.Load() {
mc.log(ErrInvalidConn)
Copy link
Member Author

Choose a reason for hiding this comment

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

This log only confuses users and does not help. When a connection is closed, the reason should be logged if necessary.

Comment on lines +209 to +213
mc.cleanup()
// interpolateParams would be called before sending any query.
// So its safe to retry.
mc.log(err)
return "", ErrInvalidConn
return "", driver.ErrBadConn
Copy link
Member Author

Choose a reason for hiding this comment

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

This is simply a bug fix. Since it did not close the connection and also did not return ErrBadConn, if the connection was in a state where takeCompleteBuffer() failed for any reason, ErrInvalidConn could be returned repeatedly, depending on how the application was used.
This fix may need to be backported. I will create a separate PR.

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.

Improve ErrBadConn
2 participants