Skip to content

Commit f12bf10

Browse files
committed
perf(run-engine): share the combined-limit read between the admit gate and gauges
The CK enqueue and dequeue scripts read the combined concurrency limit key once for admission and again for the metrics gauge tail. A per-call memo makes whichever runs first do the single GET; limits cannot change mid-script, so the value stays exact. Group cardinality remains a fresh read because gauges must reflect post-admission state.
1 parent 0068887 commit f12bf10

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

  • internal-packages/run-engine/src/run-queue

‎internal-packages/run-engine/src/run-queue/index.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,12 @@ const QUEUE_METRICS_CK_GAUGE_EXTRAS = {
209209
};
210210

211211
// Total-concurrency tail (gauge[10]/gauge[11]): live group cardinality + raw stored cap.
212-
// Requires groupConcurrencyKey/totalConcurrencyLimitKey locals; the CK scripts that actually
213-
// run (the Tracked variants and the CK dequeue) all declare them for the total-cap gate.
212+
// Requires the groupConcurrencyKey local and the __totalLimitRaw memo (one GET shared with
213+
// the total-cap gate); the CK scripts that run this (the Tracked variants and the CK
214+
// dequeue) declare both. The group SCARD stays a fresh read: it must be post-admission.
214215
const QUEUE_METRICS_TOTAL_GAUGE_EXTRAS = {
215216
totalRunning: "redis.call('SCARD', groupConcurrencyKey)",
216-
totalLimit: "redis.call('GET', totalConcurrencyLimitKey) or '0'",
217+
totalLimit: "__totalLimitRaw() or '0'",
217218
};
218219

219220
// CK enqueue variants of the two gauges above, extended with the CK-health tail.
@@ -4118,6 +4119,13 @@ local baseQueueKey = KEYS[15]
41184119
-- Total-cap keys (KEYS 16-17)
41194120
local groupConcurrencyKey = KEYS[16]
41204121
local totalConcurrencyLimitKey = KEYS[17]
4122+
local __rawTotalLimit = nil
4123+
local function __totalLimitRaw()
4124+
if __rawTotalLimit == nil then
4125+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4126+
end
4127+
return __rawTotalLimit
4128+
end
41214129
41224130
local queueName = ARGV[1]
41234131
local messageId = ARGV[2]
@@ -4162,7 +4170,7 @@ if enableFastPath == '1' then
41624170
-- slow path (the message queues; the dequeue gate holds it).
41634171
local totalAllowsFastPath = true
41644172
if totalConcurrencyEnabled then
4165-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
4173+
local rawTotalLimit = __totalLimitRaw()
41664174
if rawTotalLimit then
41674175
local totalLimit = math.min(tonumber(rawTotalLimit), envLimit)
41684176
if tonumber(redis.call('SCARD', groupConcurrencyKey) or '0') >= totalLimit then
@@ -4289,6 +4297,13 @@ local baseQueueKey = KEYS[16]
42894297
-- Total-cap keys (KEYS 17-18)
42904298
local groupConcurrencyKey = KEYS[17]
42914299
local totalConcurrencyLimitKey = KEYS[18]
4300+
local __rawTotalLimit = nil
4301+
local function __totalLimitRaw()
4302+
if __rawTotalLimit == nil then
4303+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4304+
end
4305+
return __rawTotalLimit
4306+
end
42924307
42934308
local queueName = ARGV[1]
42944309
local messageId = ARGV[2]
@@ -4333,7 +4348,7 @@ if enableFastPath == '1' then
43334348
-- Total-cap gate: see enqueueMessageCkTracked.
43344349
local totalAllowsFastPath = true
43354350
if totalConcurrencyEnabled then
4336-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
4351+
local rawTotalLimit = __totalLimitRaw()
43374352
if rawTotalLimit then
43384353
local totalLimit = math.min(tonumber(rawTotalLimit), envLimit)
43394354
if tonumber(redis.call('SCARD', groupConcurrencyKey) or '0') >= totalLimit then
@@ -4972,6 +4987,13 @@ local lengthCounterKey = KEYS[10]
49724987
local runningCounterKey = KEYS[11]
49734988
local groupConcurrencyKey = KEYS[12]
49744989
local totalConcurrencyLimitKey = KEYS[13]
4990+
local __rawTotalLimit = nil
4991+
local function __totalLimitRaw()
4992+
if __rawTotalLimit == nil then
4993+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4994+
end
4995+
return __rawTotalLimit
4996+
end
49754997
49764998
local ckWildcardName = ARGV[1]
49774999
local currentTime = tonumber(ARGV[2])
@@ -5014,7 +5036,7 @@ local actualMaxCount = math.min(maxCount, envAvailableCapacity)
50145036
-- behind, and blocking on it would deadlock the run against itself).
50155037
local totalHeadroom = nil
50165038
if totalConcurrencyEnabled then
5017-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
5039+
local rawTotalLimit = __totalLimitRaw()
50185040
if rawTotalLimit then
50195041
local totalConcurrencyLimit = math.min(tonumber(rawTotalLimit), envConcurrencyLimit)
50205042
local groupCurrentConcurrency = tonumber(redis.call('SCARD', groupConcurrencyKey) or '0')

0 commit comments

Comments
 (0)