-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[queue] add Lag() command to measure consumer group lag (#159)
This adds a Lag() command to measure the lag of a consumer group over a queue. Lag is the number of messages in the queue that have not yet been picked up by a Read() command.
- Loading branch information
1 parent
3490956
commit 1682dcc
Showing
4 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-- Lag commands take the form | ||
-- | ||
-- EVALSHA sha 1 key group | ||
-- | ||
-- Note: strictly, it is illegal for a script to manipulate keys that are not | ||
-- explicitly passed to EVAL{,SHA}, but in practice this is fine as long as all | ||
-- keys are on the same server (e.g. in cluster scenarios). In our case a single | ||
-- queue, which may be composed of multiple streams and metadata keys, is always | ||
-- on the same server. | ||
|
||
local base = KEYS[1] | ||
local group = ARGV[1] | ||
|
||
local key_meta = base .. ':meta' | ||
|
||
local streams = tonumber(redis.call('HGET', key_meta, 'streams') or 1) | ||
local result = 0 | ||
|
||
for idx = 0, streams-1 do | ||
local stream = base .. ':s' .. idx | ||
|
||
-- the group must exist for us to measure its lag. we create it here; if it | ||
-- already exists this returns a BUSYGROUP error which we ignore | ||
redis.pcall('XGROUP', 'CREATE', stream, group, '0', 'MKSTREAM') | ||
|
||
local info = redis.pcall('XINFO', 'GROUPS', stream) | ||
if info['err'] == 'ERR no such key' then | ||
-- if the stream doesn't exist, treat it as zero lag | ||
elseif info['err'] then | ||
return redis.error_reply(info['err']..' accessing '..stream) | ||
else | ||
for i,v in ipairs(info) do | ||
if v[2] == group then | ||
if not v[12] then | ||
-- lag can be nil; we propagate this to the caller | ||
return redis.error_reply('ERR unknown lag for group '..group..' on stream '..stream) | ||
end | ||
|
||
result = result + v[12] | ||
break | ||
end | ||
end | ||
end | ||
end | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters