Repository navigation
MOD-16873 Fix TS.INCRBY/TS.DECRBY replicating a bogus TIMESTAMP label with LABELS - #2161
Conversation
When TIMESTAMP is omitted, replication rewrites the command by appending "TIMESTAMP <ts>" at the very end. If the create-on-write command also carries a trailing LABELS clause, appending TIMESTAMP after it gets swallowed as a bogus label/value pair on replicas and AOF-replay, since LABELS greedily consumes every token that follows it. Insert TIMESTAMP before the LABELS clause instead of after it.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b4082e8. Configure here.
… with LABELS (#2160) * Fix TS.INCRBY/TS.DECRBY replicating a bogus TIMESTAMP label with LABELS When TIMESTAMP is omitted, replication rewrites the command by appending "TIMESTAMP <ts>" at the very end. If the create-on-write command also carries a trailing LABELS clause, appending TIMESTAMP after it gets swallowed as a bogus label/value pair on replicas and AOF-replay, since LABELS greedily consumes every token that follows it. Insert TIMESTAMP before the LABELS clause instead of after it. * style: clang-format * test: also verify the replicated sample value/timestamp, not just labels * Address review: fixed-position TIMESTAMP insert, guard TIMESTAMP-in-LABELS-tail, bounds check, guard replication on write result - Insert TIMESTAMP right after <key> <value> (index 3) instead of scanning for LABELS: avoids matching a key literally named "LABELS" and removes the redundant argv scan. - Detect a "TIMESTAMP" match that falls inside a LABELS tail and treat it as a label, not a directive, so primary and replica agree on labels even when a label key is literally named "TIMESTAMP". - Bounds-check TIMESTAMP's value before dereferencing it (was a heap over-read when TIMESTAMP was the last token). - Only replicate when internalAdd() actually succeeded, matching TSDB_add. Tests: parametrize incrby/decrby, verify AOF-replay (not just live replication), and add regressions for the key-named-LABELS and label-named-TIMESTAMP cases plus the missing-value arity guard. * Address review: fixed-position insert was unsafe (breaks key named IGNORE), fix timestampLoc key-name collision, fix vacuous replica tests, fix restart-check overwrite bug - Revert the unconditional fixed-position insert: it shifts every other clause's operands when a keyword collides with the key name and takes multiple operands (e.g. a key named IGNORE), breaking replica/AOF parsing entirely. Back to inserting before LABELS only when a real LABELS clause exists (labelsLoc > 2); append at the end otherwise. - Apply the same "> 2" floor to timestampLoc itself: a key named "timestamp" was matching RMUtil_ArgIndex at index 1 and getting treated as an explicit TIMESTAMP directive. Tests: - test_incrby_create_with_key_named_labels_replicates_correctly and test_incrby_labels_key_named_timestamp_replicates_correctly were vacuous: with no warmup-key handshake, the key reaches the replica via the initial full-sync RDB before repl-diskless-sync-delay elapses, so the rewritten command is never actually parsed by the replica. Added the same handshake test_incrby_create_with_labels_replicates_correctly already used. - The post-restart block in test_incrby_create_with_labels_replicates_correctly reused master_sample, overwriting the pre-restart value, so nothing was ever compared across the restart. Now captured separately and compared. - Added test_incrby_create_with_key_named_ignore_replicates_correctly, a direct regression test for the IGNORE-key repro above. Not addressed here (flagged by review as out of scope / follow-up, not blocking this PR): the pre-existing parseLabelsFromArgs key-name collision for a key literally named LABELS, and TS.INCRBY accepting a negative explicit TIMESTAMP. * Address review: scan for TIMESTAMP/LABELS only past <key> <value>, not the whole argv Zeroing a late match (previous commit) still broke a key literally named "TIMESTAMP": RMUtil_ArgIndex returns the FIRST match, which for that key is the key name at argv[1], not the real directive appended by the rewrite. Zeroing it discarded the real one too, so master and replica each resolved their own timestamp - reproduced live with a stalled replica and a standalone AOF reload. Fix: scan starting at index 3, where a real TIMESTAMP or LABELS keyword is the earliest either can legally appear, so the key/value can never be mistaken for one. Add test_incrby_key_named_timestamp_replicates_correctly (key named "TIMESTAMP", stalled replica, mirrors test_incrby_no_timestamp_replicates_diverging_timestamp) - without the stall this would pass by luck. * Address review: dedupe replica-wait polling into shared helpers The "for _ in range(100): ... exists ... time.sleep(0.1) / else: raise" replica-wait loop was duplicated 10 times across the file, tripping SonarCloud's new-code duplication gate (6.1%, threshold 3%). Extracted _wait_for_replica_key() and _wait_for_replica_online() (the latter also performs the warmup-key handshake), and replaced all 10 call sites. No behavior change. * Address review: strengthen key-named-TIMESTAMP test to check actual behavior, not just consistency test_incrby_key_named_timestamp_replicates_correctly only asserted master_ts == slave_sample[0], which the pre-PR code also satisfied (wrong-but-consistent: both sides stored ts=5, the misread value slot, via ReplicateVerbatim). Add assertGreaterEqual(master_ts, before_ms) so the test actually catches a regression back to that behavior, not just a master/replica divergence. Also capture the stall thread's exception instead of swallowing it, so a broken DEBUG SLEEP can't silently turn this into a no-op that reports PASS.
|
gabsow
left a comment
There was a problem hiding this comment.
Approving.
Verified against master's merged fix (#2160, 4f26b4cb): a mechanical 3-way cherry-pick of both MOD-16873 commits onto 8.10 applies with zero conflicts and produces a tree byte-identical to this PR's head — git diff between them is empty. Nothing was hand-resolved, so there's nothing to second-guess.
src/module.c/TSDB_incrbyis character-for-character master's final version: theargv + 3scans, thelabelsLoc > 2 && timestampLoc > labelsLocreset, thetimestampLoc + 1 >= argcarity guard, and therv == REDISMODULE_OKreplication guard are all present.tests/flow/test_ts_incrby.pyis byte-identical to master's, and importing it against this base collects all 13 tests cleanly (Env,pytest,redis,_get_ts_info,_wait_for_replica_key/_wait_for_replica_onlineall resolve) — no harness skew.- The only red checks (
coverage, both macOS legs) are the pre-existinginstall: cannot stat 'src/redis-server'redis-core install break, equally red on master; not caused by this change.
One cosmetic nit, non-blocking: the branch carries 4 commits (the first 3 raw commits off #2160 plus the squash-merge on top), so the first three are redundant with the fourth — noisy history, but the final tree is correct.




Cherry-pick of d08f7d1 (fix for #2160) onto 8.10.
8.10 already carries the equivalent of ca3375a (MOD-16873, same buggy replication rewrite) via its own history, so only the fix commit was needed here.
Summary
TIMESTAMPis omitted,TSDB_incrby/TSDB_decrbyrewrite the replicated command by appendingTIMESTAMP <ts>at the end.LABELSclause, appendingTIMESTAMPafter it gets swallowed as a bogus label/value pair on replicas and AOF-replay, sinceLABELSgreedily consumes every token that follows it.TIMESTAMP <ts>before theLABELSclause instead of after it.Note
Medium Risk
Changes replication command shape and argv parsing for widely used write commands; mistakes could cause replica/AOF divergence, though scope is limited to INCRBY/DECRBY implicit-timestamp paths.
Overview
Fixes replication/AOF rewriting for
TS.INCRBY/TS.DECRBYwhen the primary resolves an implicit*timestamp, especially alongside create-on-writeLABELSand keys or label names that match reserved tokens.Argument parsing now locates
TIMESTAMPandLABELSonly after<key> <value>so a series key namedTIMESTAMPorLABELSis not treated as a directive. ATIMESTAMPtoken inside a trailingLABELSblock is ignored as a directive, and a bare trailingTIMESTAMPreturns wrong arity instead of reading pastargv.Replication still injects
TIMESTAMP <resolved_ms>when the command used local time, but inserts it before a realLABELSclause (sinceLABELSconsumes the rest of the argv); otherwise it keeps appending at the end so keys likeIGNOREare not broken by a fixed insert index. Replication runs only after a successful add; explicit timestamps still use verbatim replication.Flow tests add replica warmup helpers and coverage for labels, reserved key/label names, restart/AOF reload, stalled-replica timestamp parity, and the missing
TIMESTAMPvalue error.Reviewed by Cursor Bugbot for commit d609ba8. Bugbot is set up for automated code reviews on this repo. Configure here.