Skip to content

MOD-20882: test RDB encoding v5 with active defrag and the AOF preamble in the plain lane - #1706

Merged
LiranAbir merged 2 commits into
MOD-20882-rdb-dictionary-clusterfrom
MOD-20882-rdb-dictionary-defrag
Nov 18, 2026
Merged

LiranAbir merged 2 commits into
MOD-20882-rdb-dictionary-clusterfrom
MOD-20882-rdb-dictionary-defrag

Conversation

@LiranAbir

@LiranAbir LiranAbir commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Stacks on #1699 (base MOD-20882-rdb-dictionary-cluster). Merge #1699 first, or merge this into it.

Why

§3.10 of the MOD-20882 OSS test plan covers active defrag and the memory checkers:

  • Defrag: defrag_start keeps the string cache while a stream load holds strings (load_session_is_live()), and the save encoder knows a string by its address. No test ran active defrag around a v5 stream.
  • Memory checkers: the sanitizer job runs only the plain lane (QUICK=1), where test_shared_dictionary_in_aof_rdb_preamble skips, so the AOF preamble stream never ran under the sanitizer.

What

A new tests/pytest/test_rdb_dictionary_defrag.py, test-only. All 3 tests run in the plain lane only; they skip in cluster, --use-slaves, --use-aof and existing-env.

  • test_an_aof_preamble_streams_and_loads_in_the_plain_lane: turns AOF on at runtime, which writes a base file whose RDB preamble is a stream (mode=stream), adds a tail that reuses the shared strings, and loads it with DEBUG LOADAOF (phase=loading_ended, no refusal). Every document comes back. It needs no defrag, so it also runs under the sanitizer and valgrind.
  • test_active_defrag_during_an_aof_stream_load_keeps_the_strings_shared: Redis defragments during a load only while it loads an AOF. The preamble fills a fresh heap, where defrag finds nothing to do, so the tail fragments the heap and goes on writing before and after its shared-string documents. A second connection turns active defrag on once the load starts, and off once a cycle runs, while the load is still going. The test checks that the cycle both started and stopped during the load. The save after it must define each of the 4 shared strings exactly once (defs=4).
  • test_defrag_between_two_saves_keeps_the_strings_shared: after a stream SAVE, freed filler documents fragment the heap and 2 full defrag cycles move every document. A BGSAVE then still streams (poisoned=false, RDB more than 4 times smaller than with sharing off), and DEBUG RELOAD NOSAVE loads that file with every document intact.

Active defrag needs Redis built with its jemalloc. The macOS, sanitizer and valgrind builds use libc malloc, so the two defrag tests skip there (CONFIG SET activedefrag yes fails). They run on the Linux build jobs.

Two details:

  • AOF directory: the servers of a run share one directory, and an AOF turned on at runtime keeps its default name. So the AOF goes in a directory of its own, as test_auto_create_paths.py does, and the log watch follows the server's current directory. Without that, two tests running in parallel overwrote each other's AOF.
  • Imports: the file imports only common.TimeLimit and never sets Defaults.* at module level.

Testing

Run locally on macOS arm64 against Redis unstable 456b4b9a, with RedisJSON built from this branch. The defrag tests need jemalloc, so I also built Redis with make MALLOC=jemalloc (malloc=jemalloc-5.3.0). Every run loads test_short_read too, as CI does.

Run Result
jemalloc Redis, plain lane, 15 runs 3/3 passed every time
libc Redis (the default macOS build) AOF test passed; 2 defrag tests skipped
--use-aof, --use-slaves 3/3 skipped
--module-args 'json-rdb-dictionary-max-memory-percent 0' all 3 fail on the mode=stream checks, as expected
Mutation: defrag_start resets the cache even while a load runs the during-load test fails (defs=8, expected 4), 3/3 runs
Mutation: the per-key defrag callback resets the cache, so defrag drops sharing both defrag tests fail (RDB as big as with sharing off)

Not covered: replication under the sanitizer. Valgrind skips the --use-slaves lane in tests.sh, and the sanitizer job runs only the plain lane. Covering it means running more lanes in the sanitizer workflow, which changes CI time for the whole suite, so it's left out of this PR.

🤖 Generated with Claude Code


Note

Low Risk
Test-only additions with no production code, persistence, or auth changes; defrag cases skip on non-jemalloc builds like existing defrag tests.

Overview
Adds test_rdb_dictionary_defrag.py (test-only) to close gaps in the MOD-20882 test plan: RDB encoding v5 stream persistence under active defrag and under sanitizer/valgrind’s plain lane.

The new file defines helpers to seed shared-string JSON documents, watch JSON RDB dictionary server logs (mode=stream, defs/refs, poisoned=false), run AOF preamble round-trips in an isolated dir (avoids parallel AOF clashes), and skip cluster/slaves/pre-enabled AOF/flex—or jemalloc-less builds for defrag.

Three tests run only in the plain lane:

  • test_an_aof_preamble_streams_and_loads_in_the_plain_lane: runtime AOF + RDB preamble stream save/load via DEBUG LOADAOF (no defrag; suitable for sanitizer).
  • test_active_defrag_during_an_aof_stream_load_keeps_the_strings_shared: a thread enables defrag during AOF reload (fragmented tail + _DefragWhileLoading); asserts the cycle runs only while loading and a follow-up SAVE still has defs=4 shared strings.
  • test_defrag_between_two_saves_keeps_the_strings_shared: defrag between saves/BGSAVE; expects compact stream RDB and successful DEBUG RELOAD NOSAVE with documents intact.

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

…ane AOF path

Redis defragments while it loads only during an AOF load, where a stream
load holds the strings it interned. A defrag cycle that starts while the
AOF tail replays must leave the string cache to the load: the save after
it must define each shared string once. A defrag cycle between two saves
moves every string, and the second save must still share them.

The AOF preamble test runs in the plain lane, the only lane that the
sanitizer job runs. test_rdb_dictionary.py checks the preamble only in
the --use-aof lane.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.74%. Comparing base (ccd924c) to head (e07990b).

Additional details and impacted files
@@                        Coverage Diff                        @@
##           MOD-20882-rdb-dictionary-cluster    #1706   +/-   ##
=================================================================
  Coverage                             87.74%   87.74%           
=================================================================
  Files                                    18       18           
  Lines                                  6723     6723           
=================================================================
  Hits                                   5899     5899           
  Misses                                  824      824           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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 c7f5ff0. Configure here.

…abase

Flex saves each value on its own (mode=local reason=flex), so it writes no
stream for these tests to check. test_rdb_dictionary.py skips Flex the same way.
The checks count every key the server holds, so the test env helper now runs
FLUSHALL first, in case a run reuses a server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LiranAbir
LiranAbir merged commit 0797661 into MOD-20882-rdb-dictionary-cluster Nov 18, 2026
67 checks passed
@LiranAbir
LiranAbir deleted the MOD-20882-rdb-dictionary-defrag branch November 18, 2026 12:22
LiranAbir added a commit that referenced this pull request Nov 19, 2026
…ation, damaged input, data shapes and defrag (#1699)

* MOD-20882: test RDB encoding v5 on every shard of a 3-shard cluster

test_rdb_dictionary.py checks one server and skips cluster mode, and the CI
cluster lane runs one shard. The new file starts its own cluster of 3 shards,
each with a replica, and checks that every shard writes and loads its own
stream:

- a SAVE on each master logs mode=stream and refers to its keys' shared
  strings, and the RDB is more than 4 times smaller than with sharing off;
- a restart and DEBUG RELOAD of each shard load that stream and keep the data;
- each replica full-syncs a stream from its master and holds the same data;
- a promoted replica writes and reloads its own stream.

The tests skip outside the oss-cluster lane, including existing-env.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: keep server logs on outside the short-read test

test_short_read.py set Defaults.no_log = True when it was imported. RLTest
imports every test file before a run, so every server in a full run logged
to /dev/null, and every test that reads the server log skipped in CI: the
three new cluster tests, and three tests of test_rdb_dictionary.py.

testShortReadJson now builds its own Env with no_log set, and restores the
default right after, so only its servers log nowhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: test RDB encoding v5 across a resharding with MIGRATE (#1700)

`redis-cli --cluster reshard` moves a slot with MIGRATE, and the target
RESTOREs every key without a load session, so each payload must be
self-contained. A new test file starts its own 2-shard cluster and moves a
slot of documents that share long strings with the target's own documents:
one key alone, then COPY and REPLACE over KEYS, and again while the source
runs a BGSAVE. The documents must arrive intact, the target must refuse no
record, and both shards must still save and load a stream afterwards.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: test RDB encoding v5 on more full-sync paths (#1701)

test_rdb_dictionary.py syncs a replica from a file and over the RDB channel
into an empty replica. The new tests add the socket sync on the replication
link itself, a replica asked for swapdb, a replica of a replica, and a replica
that restarts on its own RDB and resumes with a partial sync. Each one checks
that the sending side saved a stream, the receiving side loaded it with no
refused record, and every document arrived.

swapdb never runs with RedisJSON loaded: the module doesn't declare that it
handles async loading, so the replica loads from a file. The test pins that.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: test RDB encoding v5 around atomic slot migration (#1702)

A new test_rdb_dictionary_asm.py starts its own 2-shard cluster and holds
a slot export open after its snapshot fork, so saves can run during it:

- A BGSAVE on the exporting node logs mode=local reason=asm, while one on
  the importing node keeps streaming. Writes to shared-string keys during
  the snapshot reach the destination intact, nothing is refused, and the
  source trims the keys it gave away. Afterwards both nodes save and
  reload a stream again.
- After a cancelled export and after one that fails at its handoff, the
  source streams again.

Stacks on #1699.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: test RDB encoding v5 with unusual data shapes (#1703)

* MOD-20882: test RDB encoding v5 with unusual data shapes

A new tests/pytest/test_rdb_dictionary_shapes.py, test-only, plain lane
only. Each test saves a stream, checks the counters the module logged for
it, and reloads every document exactly:

- strings under 8 UTF-8 bytes (and empty ones) are never shared, while
  8-byte, emoji and CJK strings are;
- a document larger than a 1% table fills it mid-document: the rest stays
  inline, and the next document starts with a reset;
- a 2 MB string is written once for three documents, next to FP32 typed
  arrays that keep their type through the reload;
- COPY, JSON.MERGE and a value equal to an object key add owners;
- STRAPPEND, JSON.DEL, JSON.SET and DEL between saves still round-trip;
- a shared string at JSON.SET's nesting limit round-trips.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: skip the stream checks on Flex

Flex saves each value on its own (mode=local reason=flex), so it writes no
stream for these tests to check. test_rdb_dictionary.py skips Flex the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: test truncated and partial RDB encoding v5 input (#1704)

* MOD-20882: test truncated and partial RDB encoding v5 input

Adds tests/pytest/test_rdb_dictionary_truncated.py (test plan section 3.7):

- A stream RDB cut at record boundaries and inside records, including
  inside a definition and inside a record that refers to it: the server
  stops with a short read, the module refuses nothing and nothing crashes.
- A stream RDB missing a later definition record, or a reset record: the
  load is refused with base_id_mismatch at the record after the gap.
- A replica whose full sync from a mock master is cut inside a record,
  then between two records, retries and loads the whole stream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: skip the stream checks on Flex

Flex saves each value on its own (mode=local reason=flex), so it writes no
stream for these tests to check. test_rdb_dictionary.py skips Flex the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: test RDB encoding v5 with active defrag and the AOF preamble in the plain lane (#1706)

* MOD-20882: test RDB encoding v5 with active defrag and in the plain-lane AOF path

Redis defragments while it loads only during an AOF load, where a stream
load holds the strings it interned. A defrag cycle that starts while the
AOF tail replays must leave the string cache to the load: the save after
it must define each shared string once. A defrag cycle between two saves
moves every string, and the second save must still share them.

The AOF preamble test runs in the plain lane, the only lane that the
sanitizer job runs. test_rdb_dictionary.py checks the preamble only in
the --use-aof lane.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: skip the stream checks on Flex and start from an empty database

Flex saves each value on its own (mode=local reason=flex), so it writes no
stream for these tests to check. test_rdb_dictionary.py skips Flex the same way.
The checks count every key the server holds, so the test env helper now runs
FLUSHALL first, in case a run reuses a server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* MOD-20882: check the exact key set after reloads and MIGRATE

The cluster and MIGRATE tests checked only the keys that SCAN found
after a restart, a DEBUG RELOAD, a promotion or a slot move, so a load
or a MIGRATE that lost keys still passed while more than one key was
left (Cursor Bugbot). Each check now compares the keys a server holds
with the keys it should hold:

- test_rdb_dictionary_cluster.py: the keys each shard held once the
  documents were written. The full-sync check also catches extra keys
  on the replica now.
- test_rdb_dictionary_migrate.py: after the reshard, the target holds
  its own keys plus every moved key, and the source holds none of the
  moved keys. A DEBUG RELOAD keeps the keys the server held before it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant