Skip to content

Cherry-pick: MOD-16873 TS.INCRBY replicate master timestamp when timestamp is local server clock (#2109) - #2159

Merged
AvivDavid23 merged 9 commits into
1.8from
cherry-pick-ca3375a9-to-1.8
Sep 3, 2026
Merged

AvivDavid23 merged 9 commits into
1.8from
cherry-pick-ca3375a9-to-1.8

Conversation

@AvivDavid23

@AvivDavid23 AvivDavid23 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

MOD-16873 TS.INCRBY replicate master timestamp when timestamp is local server clock (#2109)

  • MOD-16873 TS.INCRBY replicate master timestamp when timestamp is local server clock

  • .

Cherry-pick of ca3375a onto 1.8

Note: had a minor conflict in tests/flow/test_ts_incrby.py (1.8 was missing several test functions this commit adds at the end of the file, including test_incrby_error_cases and test_ts_incrby_NaN) — resolved by taking the incoming tests as-is.


Note

Medium Risk
Changes replication and command parsing for widely used write paths; mistakes could break replicas, AOF replay, or edge-case argv layouts, though behavior for explicit timestamps is unchanged.

Overview
Fixes primary/replica timestamp drift when TS.INCRBY / TS.DECRBY use implicit “now” (TIMESTAMP omitted or TIMESTAMP *). The master no longer uses verbatim replication in that case; it rewrites the propagated command with the concrete millisecond timestamp it applied (aligned with TS.ADD / TS.MADD).

Argument parsing for TIMESTAMP and LABELS now scans only after <key> <value>, so keys named TIMESTAMP / LABELS are not mistaken for directives. Matches inside a trailing LABELS tail are ignored, and a bare trailing TIMESTAMP returns wrong arity instead of reading past argv.

Replication layout inserts TIMESTAMP <ts> before an actual LABELS clause (so replicas/AOF do not treat it as label pairs), appends it at the end when there is no LABELS, or substitutes the numeric value when TIMESTAMP * was used; explicit numeric timestamps still replicate verbatim.

Flow tests add master/replica coverage (including delayed replica apply, create-on-write with LABELS, restart/AOF reload, and awkward key/label names).

Reviewed by Cursor Bugbot for commit 2ada5e7. Bugbot is set up for automated code reviews on this repo. Configure here.

#2109)

* MOD-16873 `TS.INCRBY` replicate master timestamp when timestamp is local server clock

* .
Comment thread src/module.c Resolved
Comment thread tests/flow/test_ts_incrby.py Resolved
Comment thread tests/flow/test_ts_incrby.py Resolved
…crby_arg_validation_before_creation accidentally reintroduced by cherry-pick conflict resolution

None of these three tests were part of ca3375a and none belong on 1.8 at this point; only test_incrby_no_timestamp_replicates_diverging_timestamp is the actual new test from that commit.
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.
Comment thread src/module.c Resolved
AvivDavid23 and others added 3 commits September 1, 2026 13:07
… 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.
Comment thread tests/flow/test_ts_incrby.py Resolved
Comment thread tests/flow/test_ts_incrby.py Resolved

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes — this one is more severe than the sibling backports: the whole test file breaks, not just the collateral tests.

src/module.c is correct. A mechanical cherry-pick of both MOD-16873 commits onto 1.8 (public) auto-merges the module cleanly with zero conflicts, byte-identical to this PR's head. TSDB_incrby correctly preserves 1.8-specific behavior (key created before validation, no isnan(result) guard) while picking up master's fix verbatim for the parts that matter — the argv + 3 scans, the labelsLoc/timestampLoc reset, the arity guard, the rv == REDISMODULE_OK wrapper.

The final commit overwrote tests/flow/test_ts_incrby.py with master's copy wholesale, including master's commented-out import header (# from utils import Env). That's the wrong direction for this branch: master's includes.py defines Env and imports redis/pytest at module scope, so commenting out the direct import is fine there — but 1.8's includes.py only has from RLTest import Env as _rltestEnv (underscore-prefixed, excluded from import *) and no redis/pytest imports at all. With the live from utils import Env (1.8's own tests/flow/utils.py:18) commented out, Env, pytest, and redis are all undefined at every call site. The baseline at this PR's exact base SHA was 32 success / 1 failure (only an unrelated leg was ever red), so nothing here can be blamed on a flaky baseline. All 13 tests in the file fail with name 'Env' is not defined on every CI leg — this happens on top of a base that was otherwise clean, so 100% of the flow-suite damage here is this PR's.

Fix, two parts:

  1. Restore the live from utils import Env / import pytest / import redis at the top of tests/flow/test_ts_incrby.py instead of master's commented-out header.
  2. Remove test_ts_incrby_arg_validation_before_creation (needs MOD-8167, absent on 1.8 — 1.8 creates the key before validating the increment, so the follow-up TS.GET doesn't raise) and test_ts_incrby_NaN (needs isnan(result) guard, absent on 1.8's TSDB_incrby) — same collateral-test issue as every sibling branch in this stack.

An earlier commit on this branch (6d9c0f7c, "fixup: remove test_incrby_error_cases, test_ts_incrby_NaN, test_ts_incrby_arg_validation_before_creation") already had this right, keeping the live import and removing the collateral tests — the final commit reverted both parts of that fix.

Non-blocking nit for later: test_incrby_create_with_key_named_ignore_replicates_correctly's comment describes parseIgnoreArgs, which doesn't exist on 1.8 at all — the test still passes (the key name is just an ignored stray token) but for a different reason than stated.

… unsupported on this branch

This branch's includes.py never re-exports RLTest's Env (unlike newer
branches, whose includes.py defines its own Env() wrapper picked up by
`from includes import *`), so every test in this file was failing with
NameError: name 'Env' is not defined. Add the explicit import used by the
other flow test files on this branch.

Also remove test_incrby_error_cases, test_ts_incrby_NaN, and
test_ts_incrby_arg_validation_before_creation: this branch predates NaN
support and the MOD-8167 validate-before-create fix, and its arg-count/
value validation for TS.INCRBY behaves differently, so all three fail
here (same reason they were already removed on this branch before, and
got pulled back in by the wholesale test-file cherry-pick of the
LABELS/TIMESTAMP fix).

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ 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 1950024. Configure here.

Comment thread tests/flow/test_ts_incrby.py Resolved
Comment thread src/module.c
} else {
const size_t replArgc = argc - 1;
RedisModule_Replicate(
ctx, replCmd, "vcl", argv + 1, replArgc, "TIMESTAMP", currentUpdatedTime);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LABELS key replica metadata diverges

Low Severity

Create-on-write TS.INCRBY/TS.DECRBY on a key named LABELS now appends TIMESTAMP and the resolved time. parseLabelsFromArgs treats the key as a LABELS clause and consumes those extra tokens as a label pair, so the replica and AOF replay store a label the primary never had.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 1950024. Configure here.

Same issue as the Env import: this branch's includes.py never brings
pytest into scope via `from includes import *`, so pytest.raises() failed
with NameError: name 'pytest' is not defined in every test that uses it.
Same issue as pytest/Env: this branch's includes.py never brings the
redis module into scope via `from includes import *`, so redis.ResponseError
failed with NameError: name 'redis' is not defined.
@sonarqubecloud

sonarqubecloud Bot commented Sep 3, 2026

Copy link
Copy Markdown

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving - the fix landed and I verified it independently rather than taking the CI-green summary at face value.

Removed/fixed: the import fix and the two collateral tests.

Both halves of the fix verified: the live import pytest / import redis / from RLTest import Env are restored (confirmed by an actual Python import check - Env/pytest/redis all resolve and pytest collects 10 tests with zero collection errors), and test_ts_incrby_NaN / test_ts_incrby_arg_validation_before_creation are gone. Every completed CI leg (all Linux x64/arm64 distros, macOS 14/15/26) shows every test_ts_incrby test as [PASS] or the designed [SKIP], zero [ERROR]. The one build failure (arm64-mariner2) is an unrelated Docker base-image package-mirror timeout before any test runs; macOS reds are pre-existing OSS-cluster RedisClusterException failures in unrelated test files, confirmed outside this PR's diff.

@AvivDavid23
AvivDavid23 merged commit 6536bc4 into 1.8 Sep 3, 2026
53 of 60 checks passed
@AvivDavid23
AvivDavid23 deleted the cherry-pick-ca3375a9-to-1.8 branch September 3, 2026 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants