Skip to content

Commit f84a322

Browse files
committed
perf(run-engine): dequeue gauges sample once, at return
The gauge slot is single-valued and the last write wins, so on the success path the post-admission resample made the entry sample pure waste. A return wrapper computes the gauge exactly once per call at exit, keeping every emitted value identical while dropping the discarded reads (about six per plain dequeue, ten per keyed dequeue at full sampling).
1 parent cac72ad commit f84a322

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

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

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ local __qm_g = false
171171
local function __qmret(r) if r == nil then r = false end return {r, __qm_g} end`;
172172

173173
// Fresh-read gauge for splice points with no reusable locals: enqueue slow-path (before
174-
// return 0) and the base dequeue top. Gated on the last ARGV so it is inert unless the
175-
// caller opts in. CK queues emit per-subqueue depth (queue_name aggregates via the MV).
174+
// return 0) and the base dequeue's sample-at-return wrapper. Gated on the last ARGV so it
175+
// is inert unless the caller opts in. CK queues emit per-subqueue depth (queue_name
176+
// aggregates via the MV).
176177
const QUEUE_METRICS_GAUGE_LUA = createMetricsGaugeComputeLua({
177178
enabledArg: "ARGV[#ARGV] == '1'",
178179
queued: "redis.call('ZCARD', queueKey)",
@@ -4694,7 +4695,16 @@ local gatesEnabled = ARGV[7] == '1'
46944695
local totalConcurrencyEnabled = ARGV[8] == '1'
46954696
${QUEUE_METRICS_GAUGE_PRELUDE}
46964697
${QUEUE_GATES_LUA_HELPERS}
4698+
-- Sample-at-return: the gauge is computed once, by the return wrapper, so every
4699+
-- exit emits the state as of that exit (post-admission on the success path) and
4700+
-- no path pays for a sample that a later one would overwrite.
4701+
local function __qmsample()
46974702
${QUEUE_METRICS_GAUGE_LUA}
4703+
end
4704+
do
4705+
local __qmret_inner = __qmret
4706+
__qmret = function(r) __qmsample() return __qmret_inner(r) end
4707+
end
46984708
46994709
-- Check current env concurrency against the limit
47004710
local envCurrentConcurrency = tonumber(redis.call('SCARD', envCurrentConcurrencyKey) or '0')
@@ -4806,10 +4816,6 @@ else
48064816
redis.call('ZADD', masterQueueKey, earliestMessage[2], queueName)
48074817
end
48084818
4809-
-- Re-sample the gauge so the emitted snapshot includes this batch's admissions;
4810-
-- the top-of-script sample only covers the early returns where nothing was admitted.
4811-
${QUEUE_METRICS_GAUGE_LUA}
4812-
48134819
-- Return results as a flat array: [messageId1, messageScore1, messagePayload1, messageId2, messageScore2, messagePayload2, ...]
48144820
return __qmret(results)
48154821
`,
@@ -5003,7 +5009,16 @@ local totalConcurrencyEnabled = ARGV[7] == '1'
50035009
local gatesEnabled = ARGV[8] == '1'
50045010
${QUEUE_METRICS_GAUGE_PRELUDE}
50055011
${QUEUE_GATES_LUA_HELPERS}
5012+
-- Sample-at-return: the gauge is computed once, by the return wrapper, so every
5013+
-- exit emits the state as of that exit (post-admission on the success path) and
5014+
-- no path pays for a sample that a later one would overwrite.
5015+
local function __qmsample()
50065016
${QUEUE_METRICS_CK_DEQUEUE_GAUGE_LUA}
5017+
end
5018+
do
5019+
local __qmret_inner = __qmret
5020+
__qmret = function(r) __qmsample() return __qmret_inner(r) end
5021+
end
50075022
50085023
local function decrLengthCounter()
50095024
if tonumber(redis.call('GET', lengthCounterKey) or '0') > 0 then
@@ -5192,10 +5207,6 @@ else
51925207
redis.call('ZADD', masterQueueKey, earliestIdx[2], ckWildcardName)
51935208
end
51945209
5195-
-- Re-sample the gauge so the emitted snapshot includes this batch's admissions;
5196-
-- the top-of-script sample only covers the early returns where nothing was admitted.
5197-
${QUEUE_METRICS_CK_DEQUEUE_GAUGE_LUA}
5198-
51995210
return __qmret(results)
52005211
`,
52015212
});

0 commit comments

Comments
 (0)