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

smartconnpool: do not allow connections to starve #17675

Merged
merged 3 commits into from
Feb 3, 2025

Conversation

vmg
Copy link
Collaborator

@vmg vmg commented Jan 31, 2025

Description

This is a potential fix for the issue described in #17662. The bug report has a very comprehensive explanation of the bug (thank you!!), so I'll stick to explaining the fix here.

I've considered introducing stronger synchronization to the pool to prevent the race but decided against it because the race does not affect correctness, as the connections in the pool would only starve in situations where the service just stops receiving new requests over time. As long as the pool periodically receives connection requests, no connection can starve because the clients who raced with other clients and are stuck in the waitlist will wake up right away when the new connections come in. I don't think persistently slowing down the pool to handle for this corner case is worth it.

Hence, my proposed solution reuses the expiry loop (the periodic loop that checks that the connections in the wailist haven't expired naturally by the client) to detect connections that could potentially be starving. This will not be a common occurrence, but when it happens, we can simply force a cycle of the pool (pull from the stacks and hand over to the waiting connections) and that will always awake any starving connections.

The pros of this approach is that it adds zero overhead to the normal get/put path for the pool.
The cons of this approach is that a starving connection can starve for up to 100ms (which is the frequency I've set for the starving loop).

Open to alternative takes for this fix, ideally ones without much overhead to the get/put path.

cc @arthurschreiber @mhamza15 @harshit-gangal @deepthi

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Copy link
Contributor

vitess-bot bot commented Jan 31, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jan 31, 2025
@github-actions github-actions bot added this to the v22.0.0 milestone Jan 31, 2025
@vmg vmg force-pushed the vmg/smartconnpool-starve branch from eddc5d7 to e00df5f Compare January 31, 2025 16:50
@vmg vmg added Type: Bug Component: Query Serving and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 31, 2025
Copy link

codecov bot commented Jan 31, 2025

Codecov Report

Attention: Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.

Project coverage is 67.77%. Comparing base (cb7d61a) to head (e0538b7).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
go/pools/smartconnpool/pool.go 97.05% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17675      +/-   ##
==========================================
+ Coverage   67.75%   67.77%   +0.01%     
==========================================
  Files        1587     1587              
  Lines      255780   255806      +26     
==========================================
+ Hits       173310   173369      +59     
+ Misses      82470    82437      -33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@arthurschreiber arthurschreiber added Backport to: release-19.0 Needs to be back ported to release-19.0 Backport to: release-20.0 Needs to be backport to release-20.0 Backport to: release-21.0 Needs to be backport to release-21.0 labels Feb 1, 2025
@mhamza15
Copy link
Contributor

mhamza15 commented Feb 3, 2025

Can confirm we're not seeing it in our CI either 👍. Thanks @vmg!

Signed-off-by: Dirkjan Bussink <[email protected]>
@@ -161,7 +161,6 @@ type ConnPool[C Connection] struct {
// The pool must be ConnPool.Open before it can start giving out connections
func NewPool[C Connection](config *Config[C]) *ConnPool[C] {
pool := &ConnPool[C]{}
pool.freshSettingsStack.Store(-1)
Copy link
Member

Choose a reason for hiding this comment

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

Was -1 being used as some kind of sentinel value? Or was this completely unnecessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was a sentinel value to mark when no Setting connection had been returned to the pool yet, but it was not an interesting optimization, so I removed it.

@dbussink dbussink merged commit 30c09f5 into main Feb 3, 2025
201 checks passed
@dbussink dbussink deleted the vmg/smartconnpool-starve branch February 3, 2025 16:34
dbussink pushed a commit that referenced this pull request Feb 3, 2025
) (#17685)

Signed-off-by: Dirkjan Bussink <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
dbussink pushed a commit that referenced this pull request Feb 3, 2025
) (#17684)

Signed-off-by: Dirkjan Bussink <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
dbussink pushed a commit that referenced this pull request Feb 3, 2025
) (#17683)

Signed-off-by: Dirkjan Bussink <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backport to: release-19.0 Needs to be back ported to release-19.0 Backport to: release-20.0 Needs to be backport to release-20.0 Backport to: release-21.0 Needs to be backport to release-21.0 Component: Query Serving Type: Bug
Projects
None yet
5 participants