From b100c78df95e4443ba1ef8eeaeea21ae49ef9e12 Mon Sep 17 00:00:00 2001 From: huangzhw Date: Fri, 4 Jun 2021 21:48:51 +0800 Subject: [PATCH 1/4] Fix XTRIM or XADD with LIMIT may delete more entries than limit. --- src/t_stream.c | 7 +++---- tests/unit/type/stream.tcl | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/t_stream.c b/src/t_stream.c index 933ab2ff0262..b5da94fc159f 100644 --- a/src/t_stream.c +++ b/src/t_stream.c @@ -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; @@ -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; lpFree(lp); raxRemove(s->rax,ri.key,ri.key_len,NULL); raxSeek(&ri,">=",ri.key,ri.key_len); diff --git a/tests/unit/type/stream.tcl b/tests/unit/type/stream.tcl index a89a6529973f..0b19ab5e4d01 100644 --- a/tests/unit/type/stream.tcl +++ b/tests/unit/type/stream.tcl @@ -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} + } + test {XRANGE COUNT works as expected} { assert {[llength [r xrange mystream - + COUNT 10]] == 10} } @@ -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}} { From f340b2d2d8de9e564cd04d5e59662421a0e3014d Mon Sep 17 00:00:00 2001 From: huangzhw Date: Fri, 4 Jun 2021 22:26:40 +0800 Subject: [PATCH 2/4] change order --- src/t_stream.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/t_stream.c b/src/t_stream.c index b5da94fc159f..6bd769ca29c0 100644 --- a/src/t_stream.c +++ b/src/t_stream.c @@ -708,6 +708,10 @@ int64_t streamTrim(stream *s, streamAddTrimArgs *args) { unsigned char *lp = ri.data, *p = lpFirst(lp); int64_t entries = lpGetInteger(p); + /* Check if we exceeded the amount of work we could do */ + if (limit && (deleted + entries) > limit) + break; + /* Check if we can remove the whole node. */ int remove_node; streamID master_id = {0}; /* For MINID */ @@ -726,9 +730,6 @@ 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; lpFree(lp); raxRemove(s->rax,ri.key,ri.key_len,NULL); raxSeek(&ri,">=",ri.key,ri.key_len); From 3a49bf455f3a3a13337dd74b1e89ff6a764bd9e2 Mon Sep 17 00:00:00 2001 From: huangzhw Date: Fri, 4 Jun 2021 23:17:03 +0800 Subject: [PATCH 3/4] update test --- tests/unit/type/stream.tcl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/type/stream.tcl b/tests/unit/type/stream.tcl index 0b19ab5e4d01..63a87fe300f2 100644 --- a/tests/unit/type/stream.tcl +++ b/tests/unit/type/stream.tcl @@ -205,7 +205,7 @@ start_server { r XADD yourstream * xitem v } r XADD yourstream MAXLEN ~ 0 limit 1 * xitem v - assert {[r XLEN yourstream] >= 3} + assert {[r XLEN yourstream] == 4} } test {XRANGE COUNT works as expected} { @@ -541,7 +541,7 @@ start_server { for {set j 0} {$j < 3} {incr j} { r XADD mystream * xitem v } - assert {[r XTRIM mystream MAXLEN ~ 0 LIMIT 1] <= 1} + assert {[r XTRIM mystream MAXLEN ~ 0 LIMIT 1] == 0} } } From 95ef9fa475ae14dc11f921011edd3f4431e96915 Mon Sep 17 00:00:00 2001 From: huangzhw Date: Sat, 5 Jun 2021 06:47:50 +0800 Subject: [PATCH 4/4] add more test --- tests/unit/type/stream.tcl | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/type/stream.tcl b/tests/unit/type/stream.tcl index 63a87fe300f2..f1ee56a8a2c9 100644 --- a/tests/unit/type/stream.tcl +++ b/tests/unit/type/stream.tcl @@ -542,6 +542,7 @@ start_server { r XADD mystream * xitem v } assert {[r XTRIM mystream MAXLEN ~ 0 LIMIT 1] == 0} + assert {[r XTRIM mystream MAXLEN ~ 0 LIMIT 2] == 2} } }