Skip to content

Conversation

arthurschreiber
Copy link
Contributor

Description

This fixes the two race conditions described in #18202 (comment)

Related Issue(s)

#18202

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 Jul 14, 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 Jul 14, 2025
@github-actions github-actions bot added this to the v23.0.0 milestone Jul 14, 2025
@arthurschreiber
Copy link
Contributor Author

Unfortunately, this does have performance impact on the pool performance (but whether that's noticeable in real world scenarios is unclear to me).

BenchmarkGetPut on main

goos: linux
goarch: arm64
pkg: vitess.io/vitess/go/pools/smartconnpool/benchmarking
BenchmarkGetPut
BenchmarkGetPut/Legacy/x1-cap64
BenchmarkGetPut/Legacy/x1-cap64-10         	 4718317	       244.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x1-cap64
BenchmarkGetPut/Smart/x1-cap64-10          	 8040607	       147.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x8-cap64
BenchmarkGetPut/Legacy/x8-cap64-10         	 5174402	       253.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x8-cap64
BenchmarkGetPut/Smart/x8-cap64-10          	 7262599	       159.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x32-cap64
BenchmarkGetPut/Legacy/x32-cap64-10        	 3322944	      1099 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x32-cap64
BenchmarkGetPut/Smart/x32-cap64-10         	 2600947	       651.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x128-cap64
BenchmarkGetPut/Legacy/x128-cap64-10       	 5083953	      1493 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x128-cap64
BenchmarkGetPut/Smart/x128-cap64-10        	 1000000	      1129 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x1-cap128
BenchmarkGetPut/Legacy/x1-cap128-10        	 4936233	       240.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x1-cap128
BenchmarkGetPut/Smart/x1-cap128-10         	 8478964	       144.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x8-cap128
BenchmarkGetPut/Legacy/x8-cap128-10        	 4848466	       250.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x8-cap128
BenchmarkGetPut/Smart/x8-cap128-10         	 6993130	       150.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x32-cap128
BenchmarkGetPut/Legacy/x32-cap128-10       	 5002472	       472.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x32-cap128
BenchmarkGetPut/Smart/x32-cap128-10        	 3141266	       383.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x128-cap128
BenchmarkGetPut/Legacy/x128-cap128-10      	 5176632	      1190 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x128-cap128
BenchmarkGetPut/Smart/x128-cap128-10       	 1203177	      1167 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x1-cap512
BenchmarkGetPut/Legacy/x1-cap512-10        	 5144636	       240.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x1-cap512
BenchmarkGetPut/Smart/x1-cap512-10         	 8336198	       140.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x8-cap512
BenchmarkGetPut/Legacy/x8-cap512-10        	 4724248	       235.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x8-cap512
BenchmarkGetPut/Smart/x8-cap512-10         	 8215454	       147.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x32-cap512
BenchmarkGetPut/Legacy/x32-cap512-10       	 5252892	       238.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x32-cap512
BenchmarkGetPut/Smart/x32-cap512-10        	 8174509	       131.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x128-cap512
BenchmarkGetPut/Legacy/x128-cap512-10      	 4794348	       260.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x128-cap512
BenchmarkGetPut/Smart/x128-cap512-10       	 3052800	       562.1 ns/op	       0 B/op	       0 allocs/op
PASS

BenchmarkGetPut on this PR

goos: linux
goarch: arm64
pkg: vitess.io/vitess/go/pools/smartconnpool/benchmarking
BenchmarkGetPut
BenchmarkGetPut/Legacy/x1-cap64
BenchmarkGetPut/Legacy/x1-cap64-10         	 4590030	       248.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x1-cap64
BenchmarkGetPut/Smart/x1-cap64-10          	 4411610	       262.9 ns/op	      16 B/op	       1 allocs/op
BenchmarkGetPut/Legacy/x8-cap64
BenchmarkGetPut/Legacy/x8-cap64-10         	 4916294	       242.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x8-cap64
BenchmarkGetPut/Smart/x8-cap64-10          	 4339180	       292.6 ns/op	      15 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x32-cap64
BenchmarkGetPut/Legacy/x32-cap64-10        	 4627542	      1319 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x32-cap64
BenchmarkGetPut/Smart/x32-cap64-10         	 1388763	       851.7 ns/op	       5 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x128-cap64
BenchmarkGetPut/Legacy/x128-cap64-10       	 4709551	      1437 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x128-cap64
BenchmarkGetPut/Smart/x128-cap64-10        	 1000000	      1248 ns/op	       2 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x1-cap128
BenchmarkGetPut/Legacy/x1-cap128-10        	 4775947	       233.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x1-cap128
BenchmarkGetPut/Smart/x1-cap128-10         	 4516155	       264.2 ns/op	      16 B/op	       1 allocs/op
BenchmarkGetPut/Legacy/x8-cap128
BenchmarkGetPut/Legacy/x8-cap128-10        	 5315210	       255.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x8-cap128
BenchmarkGetPut/Smart/x8-cap128-10         	 4522465	       264.7 ns/op	      16 B/op	       1 allocs/op
BenchmarkGetPut/Legacy/x32-cap128
BenchmarkGetPut/Legacy/x32-cap128-10       	 6970027	       238.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x32-cap128
BenchmarkGetPut/Smart/x32-cap128-10        	 2172734	       596.6 ns/op	      10 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x128-cap128
BenchmarkGetPut/Legacy/x128-cap128-10      	 4773750	      1072 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x128-cap128
BenchmarkGetPut/Smart/x128-cap128-10       	 1000000	      1130 ns/op	       3 B/op	       0 allocs/op
BenchmarkGetPut/Legacy/x1-cap512
BenchmarkGetPut/Legacy/x1-cap512-10        	 6387140	       254.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x1-cap512
BenchmarkGetPut/Smart/x1-cap512-10         	 4622458	       264.1 ns/op	      16 B/op	       1 allocs/op
BenchmarkGetPut/Legacy/x8-cap512
BenchmarkGetPut/Legacy/x8-cap512-10        	 4919572	       240.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x8-cap512
BenchmarkGetPut/Smart/x8-cap512-10         	 4418607	       265.4 ns/op	      16 B/op	       1 allocs/op
BenchmarkGetPut/Legacy/x32-cap512
BenchmarkGetPut/Legacy/x32-cap512-10       	 4791676	       252.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x32-cap512
BenchmarkGetPut/Smart/x32-cap512-10        	 4681575	       236.5 ns/op	      16 B/op	       1 allocs/op
BenchmarkGetPut/Legacy/x128-cap512
BenchmarkGetPut/Legacy/x128-cap512-10      	 4881553	       228.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkGetPut/Smart/x128-cap512
BenchmarkGetPut/Smart/x128-cap512-10       	 1668379	       755.6 ns/op	       9 B/op	       0 allocs/op
PASS

@arthurschreiber arthurschreiber added 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 Backport to: release-22.0 Needs to be backport to release-22.0 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 Jul 14, 2025
Copy link

codecov bot commented Jul 14, 2025

Codecov Report

Attention: Patch coverage is 77.50000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 67.48%. Comparing base (efae039) to head (a4ab0b0).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
go/pools/smartconnpool/waitlist.go 28.57% 5 Missing ⚠️
go/pools/smartconnpool/pool.go 87.87% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18447      +/-   ##
==========================================
- Coverage   67.51%   67.48%   -0.03%     
==========================================
  Files        1607     1607              
  Lines      262687   262854     +167     
==========================================
+ Hits       177340   177374      +34     
- Misses      85347    85480     +133     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@arthurschreiber
Copy link
Contributor Author

I have a test over at https://gist.github.com/arthurschreiber/fffe5c2ecf550e75fdee6e97cc08f9df that can reproduce the problem. Unfortunately, the test is flaky so I've not added it to this PR.

} else {
stack := connSetting.bucket & stackMask
pool.settings[stack].Push(conn)
pool.freshSettingsStack.Store(int64(stack))
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder, would an alternative approach work to avoid having to lock? What if we still optimistically put it into the stack, but then check after we've completed that if we have since been closed and then remove it again?

So you only pay the price during closing mostly and then having to do any locking or other synchronization?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm definitely open to alternative approaches. For your suggestion, don't we have to take a lock on capacityMu to correctly decide whether the pool is closed or not?

Copy link
Member

Choose a reason for hiding this comment

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

We can think about optimistic concurrency approach with epoch validation.
ConnPool can have epoch atomic.Uint64. We invalidated with increment whenever there is a change like Close or Capacity.
In tryReturnConn we read the epoch value and then read the capacity. Before returning the connection back to the stack we check the epoch again If it is changed we continue otherwise return to the stack.
After returning we check again and if there is a change we try to get the connection back, if we receive the connection continue again.

We do not expect capacity to change that frequently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am working on an additional change that uses a pool generation number to allow Close() to return instantly and basically break the link between checked out connections and the pool.

I think that could be used for the optimistic concurrency check. I'll try this out and see if I can make this work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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 Backport to: release-22.0 Needs to be backport to release-22.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants