Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XTRIM or XADD with LIMIT may delete more entries than Count. #9048

Merged
merged 4 commits into from Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/t_stream.c
Expand Up @@ -702,10 +702,6 @@ int64_t streamTrim(stream *s, streamAddTrimArgs *args) {

int64_t deleted = 0;
while (raxNext(&ri)) {
/* Check if we exceeded the amount of work we could do */
if (limit && deleted >= limit)
break;

if (trim_strategy == TRIM_STRATEGY_MAXLEN && s->length <= maxlen)
break;

Expand All @@ -730,6 +726,9 @@ int64_t streamTrim(stream *s, streamAddTrimArgs *args) {
}

if (remove_node) {
/* Check if we exceeded the amount of work we could do */
if (limit && (deleted + entries) > limit)
break;
oranagra marked this conversation as resolved.
Show resolved Hide resolved
lpFree(lp);
raxRemove(s->rax,ri.key,ri.key_len,NULL);
raxSeek(&ri,">=",ri.key,ri.key_len);
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/type/stream.tcl
Expand Up @@ -199,6 +199,15 @@ start_server {
assert {[r EXISTS otherstream] == 0}
}

test {XADD with LIMIT delete entries no more than limit} {
r del yourstream
for {set j 0} {$j < 3} {incr j} {
r XADD yourstream * xitem v
}
r XADD yourstream MAXLEN ~ 0 limit 1 * xitem v
assert {[r XLEN yourstream] >= 3}
oranagra marked this conversation as resolved.
Show resolved Hide resolved
}

test {XRANGE COUNT works as expected} {
assert {[llength [r xrange mystream - + COUNT 10]] == 10}
}
Expand Down Expand Up @@ -525,6 +534,15 @@ start_server {
}
assert_error ERR* {r XTRIM mystream MAXLEN 1 LIMIT 30}
}

test {XTRIM with LIMIT delete entries no more than limit} {
r del mystream
r config set stream-node-max-entries 2
for {set j 0} {$j < 3} {incr j} {
r XADD mystream * xitem v
}
assert {[r XTRIM mystream MAXLEN ~ 0 LIMIT 1] <= 1}
}
}

start_server {tags {"stream"} overrides {appendonly yes}} {
Expand Down