Skip to content

Commit 8468ded

Browse files
authored
Fix incorrect lag due to trimming stream via XTRIM or XADD command (redis#13958)
This PR fix the lag calculation by ensuring that when consumer group's last_id is behind the first entry, the consumer group's entries read is considered invalid and recalculated from the start of the stream Supplement to PR redis#13473 Close redis#13957 Signed-off-by: Ernesto Alejandro Santana Hidalgo <[email protected]>
1 parent a519182 commit 8468ded

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/t_stream.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,10 @@ size_t streamReplyWithRange(client *c, stream *s, streamID *start, streamID *end
16951695
while(streamIteratorGetID(&si,&id,&numfields)) {
16961696
/* Update the group last_id if needed. */
16971697
if (group && streamCompareID(&id,&group->last_id) > 0) {
1698-
if (group->entries_read != SCG_INVALID_ENTRIES_READ && !streamRangeHasTombstones(s,&group->last_id,NULL)) {
1698+
if (group->entries_read != SCG_INVALID_ENTRIES_READ &&
1699+
streamCompareID(&group->last_id, &s->first_id) >= 0 &&
1700+
!streamRangeHasTombstones(s,&group->last_id,NULL))
1701+
{
16991702
/* A valid counter and no tombstones between the group's last-delivered-id
17001703
* and the stream's last-generated-id mean we can increment the read counter
17011704
* to keep tracking the group's progress. */

tests/unit/type/stream-cgroups.tcl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,19 @@ start_server {
13001300
assert_equal [dict get $group entries-read] 1
13011301
assert_equal [dict get $group lag] 1
13021302

1303+
# When all the entries are read, the lag is always 0.
1304+
r XREADGROUP GROUP mygroup alice STREAMS x >
1305+
set reply [r XINFO STREAM x FULL]
1306+
set group [lindex [dict get $reply groups] 0]
1307+
assert_equal [dict get $group entries-read] 5
1308+
assert_equal [dict get $group lag] 0
1309+
1310+
r XADD x 6-0 data f
1311+
set reply [r XINFO STREAM x FULL]
1312+
set group [lindex [dict get $reply groups] 0]
1313+
assert_equal [dict get $group entries-read] 5
1314+
assert_equal [dict get $group lag] 1
1315+
13031316
# When all the entries were deleted, the lag is always 0.
13041317
r XTRIM x MAXLEN 0
13051318
set reply [r XINFO STREAM x FULL]

0 commit comments

Comments
 (0)