Skip to content

Commit 9933847

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 35972f5 commit 9933847

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.
@@ -4102,6 +4103,13 @@ local baseQueueKey = KEYS[15]
41024103
-- Total-cap keys (KEYS 16-17)
41034104
local groupConcurrencyKey = KEYS[16]
41044105
local totalConcurrencyLimitKey = KEYS[17]
4106+
local __rawTotalLimit = nil
4107+
local function __totalLimitRaw()
4108+
if __rawTotalLimit == nil then
4109+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4110+
end
4111+
return __rawTotalLimit
4112+
end
41054113
41064114
local queueName = ARGV[1]
41074115
local messageId = ARGV[2]
@@ -4146,7 +4154,7 @@ if enableFastPath == '1' then
41464154
-- slow path (the message queues; the dequeue gate holds it).
41474155
local totalAllowsFastPath = true
41484156
if totalConcurrencyEnabled then
4149-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
4157+
local rawTotalLimit = __totalLimitRaw()
41504158
if rawTotalLimit then
41514159
local totalLimit = math.min(tonumber(rawTotalLimit), envLimit)
41524160
if tonumber(redis.call('SCARD', groupConcurrencyKey) or '0') >= totalLimit then
@@ -4273,6 +4281,13 @@ local baseQueueKey = KEYS[16]
42734281
-- Total-cap keys (KEYS 17-18)
42744282
local groupConcurrencyKey = KEYS[17]
42754283
local totalConcurrencyLimitKey = KEYS[18]
4284+
local __rawTotalLimit = nil
4285+
local function __totalLimitRaw()
4286+
if __rawTotalLimit == nil then
4287+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4288+
end
4289+
return __rawTotalLimit
4290+
end
42764291
42774292
local queueName = ARGV[1]
42784293
local messageId = ARGV[2]
@@ -4317,7 +4332,7 @@ if enableFastPath == '1' then
43174332
-- Total-cap gate: see enqueueMessageCkTracked.
43184333
local totalAllowsFastPath = true
43194334
if totalConcurrencyEnabled then
4320-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
4335+
local rawTotalLimit = __totalLimitRaw()
43214336
if rawTotalLimit then
43224337
local totalLimit = math.min(tonumber(rawTotalLimit), envLimit)
43234338
if tonumber(redis.call('SCARD', groupConcurrencyKey) or '0') >= totalLimit then
@@ -4956,6 +4971,13 @@ local lengthCounterKey = KEYS[10]
49564971
local runningCounterKey = KEYS[11]
49574972
local groupConcurrencyKey = KEYS[12]
49584973
local totalConcurrencyLimitKey = KEYS[13]
4974+
local __rawTotalLimit = nil
4975+
local function __totalLimitRaw()
4976+
if __rawTotalLimit == nil then
4977+
__rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey) or false
4978+
end
4979+
return __rawTotalLimit
4980+
end
49594981
49604982
local ckWildcardName = ARGV[1]
49614983
local currentTime = tonumber(ARGV[2])
@@ -4998,7 +5020,7 @@ local actualMaxCount = math.min(maxCount, envAvailableCapacity)
49985020
-- behind, and blocking on it would deadlock the run against itself).
49995021
local totalHeadroom = nil
50005022
if totalConcurrencyEnabled then
5001-
local rawTotalLimit = redis.call('GET', totalConcurrencyLimitKey)
5023+
local rawTotalLimit = __totalLimitRaw()
50025024
if rawTotalLimit then
50035025
local totalConcurrencyLimit = math.min(tonumber(rawTotalLimit), envConcurrencyLimit)
50045026
local groupCurrentConcurrency = tonumber(redis.call('SCARD', groupConcurrencyKey) or '0')

0 commit comments

Comments
 (0)