Skip to content

Commit e78d62b

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 933a062 commit e78d62b

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
@@ -227,11 +227,12 @@ const QUEUE_METRICS_CK_GAUGE_EXTRAS = {
227227
};
228228

229229
// Total-concurrency tail (gauge[10]/gauge[11]): live group cardinality + raw stored cap.
230-
// Requires groupConcurrencyKey/totalConcurrencyLimitKey locals; the CK scripts that actually
231-
// run (the Tracked variants and the CK dequeue) all declare them for the total-cap gate.
230+
// Requires the groupConcurrencyKey local and the __totalLimitRaw memo (one GET shared with
231+
// the total-cap gate); the CK scripts that run this (the Tracked variants and the CK
232+
// dequeue) declare both. The group SCARD stays a fresh read: it must be post-admission.
232233
const QUEUE_METRICS_TOTAL_GAUGE_EXTRAS = {
233234
totalRunning: "redis.call('SCARD', groupConcurrencyKey)",
234-
totalLimit: "redis.call('GET', totalConcurrencyLimitKey) or '0'",
235+
totalLimit: "__totalLimitRaw() or '0'",
235236
};
236237

237238
// CK enqueue variants of the two gauges above, extended with the CK-health tail.
@@ -4204,6 +4205,13 @@ local baseQueueKey = KEYS[15]
42044205
-- Total-cap keys (KEYS 16-17)
42054206
local groupConcurrencyKey = KEYS[16]
42064207
local totalConcurrencyLimitKey = KEYS[17]
4208+
local __rawTotalLimit = nil
4209+
local function __totalLimitRaw()
4210+
if __rawTotalLimit == nil then
4211+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4212+
end
4213+
return __rawTotalLimit
4214+
end
42074215
42084216
local queueName = ARGV[1]
42094217
local messageId = ARGV[2]
@@ -4248,7 +4256,7 @@ if enableFastPath == '1' then
42484256
-- slow path (the message queues; the dequeue gate holds it).
42494257
local totalAllowsFastPath = true
42504258
if totalConcurrencyEnabled then
4251-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
4259+
local rawTotalLimit = __totalLimitRaw()
42524260
if rawTotalLimit then
42534261
local totalLimit = math.min(tonumber(rawTotalLimit), envLimit)
42544262
if tonumber(redis.call('SCARD', groupConcurrencyKey) or '0') >= totalLimit then
@@ -4375,6 +4383,13 @@ local baseQueueKey = KEYS[16]
43754383
-- Total-cap keys (KEYS 17-18)
43764384
local groupConcurrencyKey = KEYS[17]
43774385
local totalConcurrencyLimitKey = KEYS[18]
4386+
local __rawTotalLimit = nil
4387+
local function __totalLimitRaw()
4388+
if __rawTotalLimit == nil then
4389+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4390+
end
4391+
return __rawTotalLimit
4392+
end
43784393
43794394
local queueName = ARGV[1]
43804395
local messageId = ARGV[2]
@@ -4419,7 +4434,7 @@ if enableFastPath == '1' then
44194434
-- Total-cap gate: see enqueueMessageCkTracked.
44204435
local totalAllowsFastPath = true
44214436
if totalConcurrencyEnabled then
4422-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
4437+
local rawTotalLimit = __totalLimitRaw()
44234438
if rawTotalLimit then
44244439
local totalLimit = math.min(tonumber(rawTotalLimit), envLimit)
44254440
if tonumber(redis.call('SCARD', groupConcurrencyKey) or '0') >= totalLimit then
@@ -5087,6 +5102,13 @@ local lengthCounterKey = KEYS[10]
50875102
local runningCounterKey = KEYS[11]
50885103
local groupConcurrencyKey = KEYS[12]
50895104
local totalConcurrencyLimitKey = KEYS[13]
5105+
local __rawTotalLimit = nil
5106+
local function __totalLimitRaw()
5107+
if __rawTotalLimit == nil then
5108+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
5109+
end
5110+
return __rawTotalLimit
5111+
end
50905112
50915113
local ckWildcardName = ARGV[1]
50925114
local currentTime = tonumber(ARGV[2])
@@ -5129,7 +5151,7 @@ local actualMaxCount = math.min(maxCount, envAvailableCapacity)
51295151
-- behind, and blocking on it would deadlock the run against itself).
51305152
local totalHeadroom = nil
51315153
if totalConcurrencyEnabled then
5132-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
5154+
local rawTotalLimit = __totalLimitRaw()
51335155
if rawTotalLimit then
51345156
local totalConcurrencyLimit = math.min(tonumber(rawTotalLimit), envConcurrencyLimit)
51355157
local groupCurrentConcurrency = tonumber(redis.call('SCARD', groupConcurrencyKey) or '0')

0 commit comments

Comments
 (0)