Skip to content

connpool: Don't use go internal sema functionality#18719

Merged
arthurschreiber merged 2 commits intomainfrom
arthur/pool-dont-use-sema
Oct 8, 2025
Merged

connpool: Don't use go internal sema functionality#18719
arthurschreiber merged 2 commits intomainfrom
arthur/pool-dont-use-sema

Conversation

@arthurschreiber
Copy link
Member

@arthurschreiber arthurschreiber commented Oct 7, 2025

Description

This replaces the semaphore implementation that uses go internals with a channel based implementation that effectively performs the same, if not even a tiny bit better.

The "trick" here is the use of runtime.Gosched() right after writing to the channel, which allows the goroutine that's waiting on the channel to start running immediately. This behaves like the direct handoff functionality of the previous semaphore implementation, but without reaching into go internals.

This allows removing one of the go:linkname dependencies, without any negative side effects. It also means we don't have to ship a custom semaphore implementation for builds that have the race detector enabled.

I'm on the fence on whether this should be backported to v21 and v22, as this doesn't directly fix any issues.

Benchmark Results

$ git checkout main
go test -memprofile mem.out -cpuprofile cpu.out -benchmem -run '^$' -bench '^BenchmarkGetPut$' vitess.io/vitess/go/pools/smartconnpool/benchmarking -v -count 10 | tee pr.out

$ git checkout arthur/pool-dont-use-sema
go test -memprofile mem.out -cpuprofile cpu.out -benchmem -run '^$' -bench '^BenchmarkGetPut$' vitess.io/vitess/go/pools/smartconnpool/benchmarking -v -count 10 | tee main.out

$ benchstat main.out pr.out
goos: linux
goarch: arm64
pkg: vitess.io/vitess/go/pools/smartconnpool/benchmarking
                            │  main.out   │                pr.out                │
                            │   sec/op    │    sec/op     vs base                │
GetPut/Smart/x1-cap64-14      84.05n ± 2%   84.42n ±  3%        ~ (p=0.363 n=10)
GetPut/Smart/x8-cap64-14      75.09n ± 6%   58.35n ± 20%  -22.29% (p=0.000 n=10)
GetPut/Smart/x32-cap64-14     39.66n ± 3%   36.12n ±  2%   -8.94% (p=0.000 n=10)
GetPut/Smart/x128-cap64-14    43.66n ± 2%   42.01n ± 11%        ~ (p=0.143 n=10)
GetPut/Smart/x1-cap128-14     83.17n ± 2%   85.16n ±  3%   +2.39% (p=0.034 n=10)
GetPut/Smart/x8-cap128-14     82.16n ± 2%   84.02n ±  3%   +2.26% (p=0.019 n=10)
GetPut/Smart/x32-cap128-14    41.04n ± 4%   37.93n ±  3%   -7.58% (p=0.000 n=10)
GetPut/Smart/x128-cap128-14   40.54n ± 1%   37.45n ±  5%   -7.61% (p=0.000 n=10)
GetPut/Smart/x1-cap512-14     84.22n ± 2%   83.94n ±  3%        ~ (p=0.529 n=10)
GetPut/Smart/x8-cap512-14     82.83n ± 2%   83.66n ±  2%        ~ (p=0.148 n=10)
GetPut/Smart/x32-cap512-14    82.12n ± 2%   81.83n ±  3%        ~ (p=0.644 n=10)
GetPut/Smart/x128-cap512-14   41.46n ± 3%   39.04n ±  3%   -5.86% (p=0.000 n=10)
geomean                       61.54n        58.79n         -4.47%

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

AI Disclosure

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Oct 7, 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 Oct 7, 2025
@github-actions github-actions bot added this to the v23.0.0 milestone Oct 7, 2025
@arthurschreiber arthurschreiber changed the title Arthur/pool dont use sema connpool: Don't use go internal sema functionality Oct 7, 2025
@codecov
Copy link

codecov bot commented Oct 7, 2025

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.50%. Comparing base (484183c) to head (00fe4fe).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
go/pools/smartconnpool/waitlist.go 84.61% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #18719   +/-   ##
=======================================
  Coverage   67.50%   67.50%           
=======================================
  Files        1606     1605    -1     
  Lines      263773   263756   -17     
=======================================
- Hits       178048   178038   -10     
+ Misses      85725    85718    -7     

☔ 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.

Base automatically changed from arthur/pool-race-conditions to main October 7, 2025 09:20
@arthurschreiber arthurschreiber added Component: VTTablet Type: Enhancement Logical improvement (somewhere between a bug and feature) 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 Oct 7, 2025
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
@arthurschreiber arthurschreiber force-pushed the arthur/pool-dont-use-sema branch from b484295 to be9818e Compare October 7, 2025 09:33
@arthurschreiber arthurschreiber marked this pull request as ready for review October 7, 2025 09:34
…/pool-dont-use-sema

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
@arthurschreiber arthurschreiber merged commit 6880e4a into main Oct 8, 2025
103 of 104 checks passed
@arthurschreiber arthurschreiber deleted the arthur/pool-dont-use-sema branch October 8, 2025 09:33
tanjinx pushed a commit to slackhq/vitess that referenced this pull request Oct 9, 2025
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
tanjinx added a commit to slackhq/vitess that referenced this pull request Oct 16, 2025
…ng for connection (#712)

* CONNPOOL: Fix race condition when waiting for connection (vitessio#18712)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Tanjin Xu <tanjin.xu@slack-corp.com>

* connpool: Bump the hang detection timeout to 1 second. (vitessio#18722)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>

* connpool: Don't use go internal `sema` functionality (vitessio#18719)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>

---------

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Signed-off-by: Tanjin Xu <tanjin.xu@slack-corp.com>
Co-authored-by: Arthur Schreiber <arthurschreiber@github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
tanjinx pushed a commit to slackhq/vitess that referenced this pull request Oct 24, 2025
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
tanjinx added a commit to slackhq/vitess that referenced this pull request Nov 3, 2025
…ting for connection (#727)

* [release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>

* [release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>

* Revert "[release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)"

This reverts commit 4dac149.

* Revert "[release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)"

This reverts commit 9ab76bf.

* [release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>

* connpool: Don't use go internal `sema` functionality (vitessio#18719)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>

* [release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>

---------

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: Arthur Schreiber <arthurschreiber@github.com>
tanjinx added a commit to slackhq/vitess that referenced this pull request Nov 10, 2025
…ting for connection (#727)

* [release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>

* [release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>

* Revert "[release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)"

This reverts commit 4dac149.

* Revert "[release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)"

This reverts commit 9ab76bf.

* [release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>

* connpool: Don't use go internal `sema` functionality (vitessio#18719)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>

* [release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>

---------

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: Arthur Schreiber <arthurschreiber@github.com>
sbaker617 pushed a commit to slackhq/vitess that referenced this pull request Feb 5, 2026
…ting for connection (#727)

* [release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>

* [release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>

* Revert "[release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)"

This reverts commit 4dac149.

* Revert "[release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)"

This reverts commit 9ab76bf.

* [release-22.0] CONNPOOL: Fix race condition when waiting for connection (vitessio#18713) (vitessio#18721)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>

* connpool: Don't use go internal `sema` functionality (vitessio#18719)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>

* [release-22.0] connpool: Bump the hang detection timeout to fix flakiness (vitessio#18722) (vitessio#18724)

Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>

---------

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Matt Lord <mattalord@gmail.com>
Co-authored-by: Arthur Schreiber <arthur@planetscale.com>
Co-authored-by: Arthur Schreiber <arthurschreiber@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: VTTablet Type: Enhancement Logical improvement (somewhere between a bug and feature)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants