-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Improved Connection Pooling #14034
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
Merged
Merged
Improved Connection Pooling #14034
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2a02fa2
smartconnpool: add implementation
vmg 195a2f9
hack: expose FastRand
vmg 4d9a1b4
conn: add ConnCheck
vmg f280ceb
vttablet: wire up new connpool
vmg 810a561
connpool: cleanup naming
vmg 24e969b
smartconnpool: docs [wip]
vmg 737f9ac
smartconnpool: add documentation
vmg 2b6b01d
go/list: move a generic List into its own package
vmg fbc53f7
mysql: document ConnCheck
vmg b5b5333
connpool: do not re-open idle connections
vmg 909fecb
resource_pool: remove setting-related statistics
vmg 19ec1cd
review feedback
vmg 6b3dd4c
connpool: fix popping
vmg 083eab7
smartconnpool: fix ABA issue in stacks
vmg 4068b48
smartconnpool: add stress benchmarks
vmg 9b1de67
smartconnpool: cleanups
vmg 777085a
connpool: fix deadlock when shutting down the pool
vmg 45f6696
atomic128: add a spinlock fallback
vmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| //go:build amd64 || arm64 | ||
|
|
||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package atomic2 | ||
|
|
||
| import ( | ||
| "unsafe" | ||
| ) | ||
|
|
||
| //go:linkname writeBarrier runtime.writeBarrier | ||
| var writeBarrier struct { | ||
| enabled bool // compiler emits a check of this before calling write barrier | ||
| pad [3]byte // compiler uses 32-bit load for "enabled" field | ||
| needed bool // identical to enabled, for now (TODO: dedup) | ||
| alignme uint64 // guarantee alignment so that compiler can use a 32 or 64-bit load | ||
| } | ||
|
|
||
| //go:linkname atomicwb runtime.atomicwb | ||
| //go:nosplit | ||
| func atomicwb(ptr *unsafe.Pointer, new unsafe.Pointer) | ||
|
|
||
| type PointerAndUint64[T any] struct { | ||
| p unsafe.Pointer | ||
| u uint64 | ||
| } | ||
|
|
||
| //go:nosplit | ||
| func loadUint128_(addr *unsafe.Pointer) (pp unsafe.Pointer, uu uint64) | ||
|
|
||
| func (x *PointerAndUint64[T]) Load() (*T, uint64) { | ||
| p, u := loadUint128_(&x.p) | ||
| return (*T)(p), u | ||
| } | ||
|
|
||
| //go:nosplit | ||
| func compareAndSwapUint128_(addr *unsafe.Pointer, oldp unsafe.Pointer, oldu uint64, newp unsafe.Pointer, newu uint64) (swapped bool) | ||
|
|
||
| //go:nosplit | ||
| func compareAndSwapUint128(addr *unsafe.Pointer, oldp unsafe.Pointer, oldu uint64, newp unsafe.Pointer, newu uint64) bool { | ||
| if writeBarrier.enabled { | ||
| atomicwb(addr, newp) | ||
| } | ||
| return compareAndSwapUint128_(addr, oldp, oldu, newp, newu) | ||
| } | ||
|
|
||
| func (x *PointerAndUint64[T]) CompareAndSwap(oldp *T, oldu uint64, newp *T, newu uint64) bool { | ||
| return compareAndSwapUint128(&x.p, unsafe.Pointer(oldp), oldu, unsafe.Pointer(newp), newu) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2023 The Vitess Authors. | ||
| // Copyright (c) 2021, Carlo Alberto Ferraris | ||
| // Copyright (c) 2017, Tom Thorogood | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Use of this source code is governed by a | ||
| // Modified BSD License that can be found in | ||
| // the LICENSE file. | ||
|
|
||
| //+build !noasm,!appengine | ||
|
|
||
| #include "textflag.h" | ||
|
|
||
| TEXT ·compareAndSwapUint128_(SB), NOSPLIT, $0-41 | ||
| MOVQ addr+0(FP), R8 | ||
| MOVQ oldp+8(FP), AX | ||
| MOVQ oldu+16(FP), DX | ||
| MOVQ newp+24(FP), BX | ||
| MOVQ newu+32(FP), CX | ||
| LOCK | ||
| CMPXCHG16B (R8) | ||
| SETEQ swapped+40(FP) | ||
| RET | ||
|
|
||
| TEXT ·loadUint128_(SB), NOSPLIT, $0-24 | ||
| MOVQ addr+0(FP), R8 | ||
| XORQ AX, AX | ||
| XORQ DX, DX | ||
| XORQ BX, BX | ||
| XORQ CX, CX | ||
| LOCK | ||
| CMPXCHG16B (R8) | ||
| MOVQ AX, pp+8(FP) | ||
| MOVQ DX, uu+16(FP) | ||
| RET | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright 2023 The Vitess Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //+build !noasm,!appengine | ||
|
|
||
| #include "textflag.h" | ||
|
|
||
| TEXT ·compareAndSwapUint128_(SB), NOSPLIT, $0-41 | ||
| MOVD addr+0(FP), R5 | ||
| MOVD oldp+8(FP), R0 | ||
| MOVD oldu+16(FP), R1 | ||
| MOVD newp+24(FP), R2 | ||
| MOVD newu+32(FP), R3 | ||
| MOVD R0, R6 | ||
| MOVD R1, R7 | ||
| CASPD (R0, R1), (R5), (R2, R3) | ||
| CMP R0, R6 | ||
| CCMP EQ, R1, R7, $0 | ||
| CSET EQ, R0 | ||
| MOVB R0, ret+40(FP) | ||
| RET | ||
|
|
||
| TEXT ·loadUint128_(SB), NOSPLIT, $0-24 | ||
| MOVD addr+0(FP), R3 | ||
| LDAXP (R3), (R0, R1) | ||
| MOVD R0, val+8(FP) | ||
| MOVD R1, val+16(FP) | ||
| RET |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //go:build !amd64 && !arm64 | ||
|
|
||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package atomic2 | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| type PointerAndUint64[T any] struct { | ||
| spin atomic.Uint64 | ||
| p *T | ||
| u uint64 | ||
| } | ||
|
|
||
| func (x *PointerAndUint64[T]) Store(p *T, u uint64) { | ||
| for !x.spin.CompareAndSwap(0, 1) { | ||
| runtime.Gosched() | ||
| } | ||
| defer x.spin.Store(0) | ||
| x.p = p | ||
| x.u = u | ||
| } | ||
|
|
||
| func (x *PointerAndUint64[T]) Load() (*T, uint64) { | ||
| for !x.spin.CompareAndSwap(0, 1) { | ||
| runtime.Gosched() | ||
| } | ||
| defer x.spin.Store(0) | ||
| return x.p, x.u | ||
| } | ||
|
|
||
| func (x *PointerAndUint64[T]) CompareAndSwap(oldp *T, oldu uint64, newp *T, newu uint64) bool { | ||
| for !x.spin.CompareAndSwap(0, 1) { | ||
| runtime.Gosched() | ||
| } | ||
| defer x.spin.Store(0) | ||
|
|
||
| if x.p == oldp && x.u == oldu { | ||
| x.p = newp | ||
| x.u = newu | ||
| return true | ||
| } | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package atomic2 | ||
|
|
||
| import ( | ||
| "testing" | ||
| "unsafe" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCompareAndSwap(t *testing.T) { | ||
| i1 := new(int) | ||
| i2 := new(int) | ||
| n := &PointerAndUint64[int]{p: unsafe.Pointer(i1), u: 12345} | ||
|
|
||
| ok := n.CompareAndSwap(i1, 12345, i2, 67890) | ||
| require.Truef(t, ok, "unexpected CAS failure") | ||
|
|
||
| pp, uu := n.Load() | ||
| require.Equal(t, i2, pp) | ||
| require.Equal(t, uint64(67890), uu) | ||
|
|
||
| ok = n.CompareAndSwap(i1, 12345, nil, 0) | ||
| require.Falsef(t, ok, "unexpected CAS success") | ||
|
|
||
| pp, uu = n.Load() | ||
| require.Equal(t, pp, i2) | ||
| require.Equal(t, uu, uint64(67890)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vmg Do we need to do anything for this to guarantee proper alignment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we do -- we need to align it ourselves or the instruction will trap. 😅
Unfortunately there's no way to force compiler alignment at this point, but any alignment issues will be quickly caught in CI as they're thoroughly tested. The good news is that I did not pull this API off my ass: 128 bit atomic have been approved and will be implemented using this exact same API in the next Go release, so we'll be able to switch seamlessly and get automatic compiler alignment for free very soon.