-
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] create PendingCount (to replace Lag) (#160)
This adds a new PendingCount command which returns the aggregate number of pending messages in a queue. ----- I tried working with Lag but it has the problem that it is fundamentally heuristic, based on keeping a track of two counters: - how many messages have ever been added to the stream - how many messages have ever been read by the given consumer group "Lag" is then the difference between these counters - the number of messages that have been added to the stream but not read by the consumer group. Unfortunately, these counters can desync horribly if you ever XDEL a message before it gets read by a consumer group. This is because deleting a message does not decrement the "added to stream" counter, nor does it increment the "read by consumer group" counter. Currently we rely on being able to XDEL messages. Fixing that would be nontrivial. But we can measure the size of the PEL with XPENDING, and we can measure the length of the stream with XLEN, so we can calculate lag as PendingCount() - Len().
- Loading branch information
1 parent
1682dcc
commit 6c095f7
Showing
5 changed files
with
47 additions
and
69 deletions.
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 was deleted.
Oops, something went wrong.
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,34 @@ | ||
-- pendingcount 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 | ||
|
||
local info = redis.pcall('XPENDING', stream, group) | ||
if info['err'] then | ||
if string.match(info['err'], '^NOGROUP ') then | ||
-- if either the stream or group don't exist, there are zero pending entries | ||
else | ||
return redis.error_reply(info['err']..' accessing '..stream) | ||
end | ||
else | ||
result = result + info[1] | ||
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