You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
smartconnpool: add per-pool waiter cap to limit queries waiting for a connection (#827)
This is backport of vitessio#19811
Adds a configurable limit on the number of queries that can wait for a connection from each pool. When the cap is reached, new requests are rejected immediately with RESOURCE_EXHAUSTED instead of queueing unboundedly.
New flags (Default 0 = unlimited):
--queryserver-config-query-pool-waiter-cap
--queryserver-config-stream-pool-waiter-cap
--queryserver-config-txpool-waiter-cap
How it works
Cap enforcement lives inside waitlist.waitForConn using a double-checked locking pattern: a lockless atomic pre-check rejects most over-cap requests early, and a strict check under the mutex guarantees correctness.
WaitCount is now incremented via a callback (onWait) when the wait actually begins (before mutex acquisition), not after completion.
A new WaiterCapRejected counter (exposed as {name}WaiterCapRejected) tracks how many requests were rejected due to the cap.
New metrics
Metric Type Description
{pool}WaiterCapRejected Counter Requests rejected because the waiter cap was reached
---------
Signed-off-by: Eduardo Ortega <5791035+ejortegau@users.noreply.github.com>
Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
Copy file name to clipboardExpand all lines: go/flags/endtoend/vtcombo.txt
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -298,18 +298,21 @@ Flags:
298
298
--queryserver-config-pool-size int query server read pool size, connection pool is used by regular queries (non streaming, not in a transaction) (default 16)
299
299
--queryserver-config-query-cache-memory int query server query cache size in bytes, maximum amount of memory to be used for caching. vttablet analyzes every incoming query and generate a query plan, these plans are being cached in a lru cache. This config controls the capacity of the lru cache. (default 33554432)
300
300
--queryserver-config-query-pool-timeout duration query server query pool timeout, it is how long vttablet waits for a connection from the query pool. If set to 0 (default) then the overall query timeout is used instead.
301
+
--queryserver-config-query-pool-waiter-cap uint query server query pool waiter cap is the maximum number of queries allowed to wait for a connection from the pool. If set to 0 (default) then there is no limit.
301
302
--queryserver-config-query-timeout duration query server query timeout, this is the query timeout in vttablet side. If a query takes more than this timeout, it will be killed. (default 30s)
302
303
--queryserver-config-schema-change-signal query server schema signal, will signal connected vtgates that schema has changed whenever this is detected. VTGates will need to have -schema_change_signal enabled for this to work (default true)
303
304
--queryserver-config-schema-reload-time duration query server schema reload time, how often vttablet reloads schemas from underlying MySQL instance. vttablet keeps table schemas in its own memory and periodically refreshes it from MySQL. This config controls the reload time. (default 30m0s)
304
305
--queryserver-config-stream-buffer-size int query server stream buffer size, the maximum number of bytes sent from vttablet for each stream call. It's recommended to keep this value in sync with vtgate's stream_buffer_size. (default 32768)
305
306
--queryserver-config-stream-pool-size int query server stream connection pool size, stream pool is used by stream queries: queries that return results to client in a streaming fashion (default 200)
306
307
--queryserver-config-stream-pool-timeout duration query server stream pool timeout, it is how long vttablet waits for a connection from the stream pool. If set to 0 (default) then there is no timeout.
308
+
--queryserver-config-stream-pool-waiter-cap uint query server stream pool waiter cap is the maximum number of streaming queries allowed to wait for a connection from the pool. If set to 0 (default) then there is no limit.
307
309
--queryserver-config-strict-table-acl only allow queries that pass table acl checks
308
310
--queryserver-config-terse-errors prevent bind vars from escaping in client error messages
309
311
--queryserver-config-transaction-cap int query server transaction cap is the maximum number of transactions allowed to happen at any given point of a time for a single vttablet. E.g. by setting transaction cap to 100, there are at most 100 transactions will be processed by a vttablet and the 101th transaction will be blocked (and fail if it cannot get connection within specified timeout) (default 20)
310
312
--queryserver-config-transaction-timeout duration query server transaction timeout, a transaction will be killed if it takes longer than this value (default 30s)
311
313
--queryserver-config-truncate-error-len int truncate errors sent to client if they are longer than this value (0 means do not truncate)
312
314
--queryserver-config-txpool-timeout duration query server transaction pool timeout, it is how long vttablet waits if tx pool is full (default 1s)
315
+
--queryserver-config-txpool-waiter-cap uint query server transaction pool waiter cap is the maximum number of transactions allowed to wait for a connection from the pool. If set to 0 (default) then there is no limit.
313
316
--queryserver-config-warn-result-size int query server result size warning threshold, warn if number of rows returned from vttablet for non-streaming queries exceeds this
314
317
--queryserver-enable-settings-pool Enable pooling of connections with modified system settings (default true)
315
318
--queryserver-enable-views Enable views support in vttablet.
Copy file name to clipboardExpand all lines: go/flags/endtoend/vttablet.txt
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -288,18 +288,21 @@ Flags:
288
288
--queryserver-config-pool-size int query server read pool size, connection pool is used by regular queries (non streaming, not in a transaction) (default 16)
289
289
--queryserver-config-query-cache-memory int query server query cache size in bytes, maximum amount of memory to be used for caching. vttablet analyzes every incoming query and generate a query plan, these plans are being cached in a lru cache. This config controls the capacity of the lru cache. (default 33554432)
290
290
--queryserver-config-query-pool-timeout duration query server query pool timeout, it is how long vttablet waits for a connection from the query pool. If set to 0 (default) then the overall query timeout is used instead.
291
+
--queryserver-config-query-pool-waiter-cap uint query server query pool waiter cap is the maximum number of queries allowed to wait for a connection from the pool. If set to 0 (default) then there is no limit.
291
292
--queryserver-config-query-timeout duration query server query timeout, this is the query timeout in vttablet side. If a query takes more than this timeout, it will be killed. (default 30s)
292
293
--queryserver-config-schema-change-signal query server schema signal, will signal connected vtgates that schema has changed whenever this is detected. VTGates will need to have -schema_change_signal enabled for this to work (default true)
293
294
--queryserver-config-schema-reload-time duration query server schema reload time, how often vttablet reloads schemas from underlying MySQL instance. vttablet keeps table schemas in its own memory and periodically refreshes it from MySQL. This config controls the reload time. (default 30m0s)
294
295
--queryserver-config-stream-buffer-size int query server stream buffer size, the maximum number of bytes sent from vttablet for each stream call. It's recommended to keep this value in sync with vtgate's stream_buffer_size. (default 32768)
295
296
--queryserver-config-stream-pool-size int query server stream connection pool size, stream pool is used by stream queries: queries that return results to client in a streaming fashion (default 200)
296
297
--queryserver-config-stream-pool-timeout duration query server stream pool timeout, it is how long vttablet waits for a connection from the stream pool. If set to 0 (default) then there is no timeout.
298
+
--queryserver-config-stream-pool-waiter-cap uint query server stream pool waiter cap is the maximum number of streaming queries allowed to wait for a connection from the pool. If set to 0 (default) then there is no limit.
297
299
--queryserver-config-strict-table-acl only allow queries that pass table acl checks
298
300
--queryserver-config-terse-errors prevent bind vars from escaping in client error messages
299
301
--queryserver-config-transaction-cap int query server transaction cap is the maximum number of transactions allowed to happen at any given point of a time for a single vttablet. E.g. by setting transaction cap to 100, there are at most 100 transactions will be processed by a vttablet and the 101th transaction will be blocked (and fail if it cannot get connection within specified timeout) (default 20)
300
302
--queryserver-config-transaction-timeout duration query server transaction timeout, a transaction will be killed if it takes longer than this value (default 30s)
301
303
--queryserver-config-truncate-error-len int truncate errors sent to client if they are longer than this value (0 means do not truncate)
302
304
--queryserver-config-txpool-timeout duration query server transaction pool timeout, it is how long vttablet waits if tx pool is full (default 1s)
305
+
--queryserver-config-txpool-waiter-cap uint query server transaction pool waiter cap is the maximum number of transactions allowed to wait for a connection from the pool. If set to 0 (default) then there is no limit.
303
306
--queryserver-config-warn-result-size int query server result size warning threshold, warn if number of rows returned from vttablet for non-streaming queries exceeds this
304
307
--queryserver-enable-settings-pool Enable pooling of connections with modified system settings (default true)
305
308
--queryserver-enable-views Enable views support in vttablet.
Copy file name to clipboardExpand all lines: go/pools/smartconnpool/waitlist.go
+45-2Lines changed: 45 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -39,21 +39,60 @@ type waitlist[C Connection] struct {
39
39
nodes sync.Pool
40
40
mu sync.Mutex
41
41
list list.List[waiter[C]]
42
+
// onWait is called when a client gets to the point in which it is waiting for a connection - or the mutex that it needs to grab to wait for a connection.
43
+
onWaitfunc()
44
+
// onWaiterCapReached is called when the waitlist has reached its maximum capacity.
45
+
onWaiterCapReachedfunc()
42
46
}
43
47
44
48
// waitForConn blocks until a connection with the given Setting is returned by another client,
45
49
// or until the given context expires.
50
+
// If maxWaiters is > 0 and the waitlist already has that many waiters, it returns
51
+
// ErrPoolWaiterCapReached immediately without blocking.
46
52
// The returned connection may _not_ have the requested Setting. This function can
47
53
// also return a `nil` connection even if our context has expired, if the pool has
48
54
// forced an expiration of all waiters in the waitlist.
0 commit comments