Skip to content

HA: a materialized view makes the Raft leader ship page versions followers never received - WALVersionGapException, non-converging snapshot-resync loop, lost committed writes #5492

Description

@robfrank

Summary

On a 3-node Raft HA cluster, the presence of a materialized view on a database causes the leader to ship a TX_ENTRY whose page version is ahead of what followers hold. Followers throw WALVersionGapException, trigger a full snapshot resync, and the resync does not repair it - the next entry gaps again on a different file. The cluster enters a resync loop, every request routed to a resyncing follower is deflected with a 503, and the run ends with committed writes permanently missing from one node.

This is lost data, not split-brain: the affected node's id set is a strict subset of the others.

Scope note (2026-07-29). This issue originally bundled two defects. The second one - materialized view refresh requests being dropped rather than queued when a refresh was already running - was confirmed, fixed and merged in #5502 (6642e203e). That half is done and has been removed from this description. What remains, and what this issue now tracks exclusively, is the replication defect below.

Evidence: controlled A/B

Same load, same cluster, same client. The only difference is whether the schema contains a materialized view.

Arm Result
With CREATE MATERIALIZED VIEW UserStats ... REFRESH INCREMENTAL 2041/3000 user writes fail, 12 snapshot-resync cycles in ~9 s, nodes freeze at 1120/1122/1122 users and never converge
Without the view 3000/3000 users, 30000/30000 photos, zero errors, all three nodes converge immediately

Reproduced twice locally against arcadedata/arcadedb:latest. The view is the trigger, not the load and not the client.

Causal chain

  1. The leader ships a TX_ENTRY whose page version is 2 ahead of what followers hold. The first occurrence is identical and simultaneous on both followers: PageId(/142/0) version in WAL (1648) does not match with existent version (1646). Identical on both followers points at a leader-side replication-completeness defect, not a follower race. File 142 is the materialized view's own backing bucket (UserStats_1.142.65536.v0.bucket, confirmed by creating the same schema standalone and reading the file names).
  2. The follower throws WALVersionGapException -> ReplicationException: WAL version gap detected - snapshot resync required and starts a full snapshot resync.
  3. The resync does not fix it. The very next entry gaps on a different file (User_0, fileId 1, WAL 304 vs local 302). This loops 12 times.
  4. While resyncing, AbstractServerHttpHandler.handleRequest deflects every request with 503 Server is installing a snapshot, please retry. A client writing exclusively through that follower loses those writes.
  5. Final state: one node's User id set is a strict subset of the other two, missing specific ids plus their dependent records.

Leading candidate: recordFileChanges drops buffered WAL entries

RaftReplicatedDatabase.recordFileChanges buffers WAL produced by commits that run inside the DDL callback, then decides whether to ship it:

// ha-raft/.../RaftReplicatedDatabase.java
final List<byte[]> walEntries = new ArrayList<>(schemaWalBuffer.get());
final List<Map<Integer, Integer>> bucketDeltas = new ArrayList<>(schemaBucketDeltaBuffer.get());
schemaWalBuffer.get().clear();
schemaBucketDeltaBuffer.get().clear();

if (!addFiles.isEmpty() || !removeFiles.isEmpty() || schemaChanged) {   // <-- walEntries NOT in the condition
  ...replicateSchema(..., walEntries, bucketDeltas);
}

walEntries is omitted from the guard, and the finally block clears the buffers unconditionally. So when a callback commits pages but creates/removes no file and leaves the schema version unchanged, the leader applies those pages locally and never ships them. Follower page versions then trail the leader's by exactly the number of dropped page writes, which is the shape reported above (leader 1648 vs follower 1646).

The sibling method runWithCompactionReplication guards the same buffers correctly:

if (addFiles.isEmpty() && removeFiles.isEmpty() && walEntries.isEmpty() && sealedBlobs.isEmpty())
  return result;

That asymmetry is a genuine leader-side "applied locally, never replicated" hole regardless of materialized views. It has not been shown to be the path the MV refresh takes, so fixing it may not by itself close the A/B gap - but it is a real correctness defect on the Raft leader path and should be closed on its own merits.

What is NOT the cause

Please do not re-propose these; they were checked and ruled out:

Not reproduced in-process

A 3-node in-process Raft harness built on BaseRaftHATest (whose consistency check routes through DatabaseComparator and fails on any cross-node page-version mismatch) produced zero WALVersionGapException and zero resync loops across every configuration tried: writes on the leader, writes through a follower, with and without a UNIQUE index on the source type, at 180 and 450 records. The container-based A/B remains the only known reproduction.

Still unproven: the exact leader-side path by which the refresh's page mutation escapes replication. The refresh runs TRUNCATE TYPE ... UNSAFE (which commits the enclosing transaction mid-scan, TruncateTypeStatement.java:112-114) plus a full re-run of the defining query inside a dedicated transaction (joinCurrentTx=false), started from an after-commit callback of the source transaction on concurrent writer threads.

Reproduction

load-tests/src/test/java/com/arcadedb/test/load/ThreeNodesLoadTestIT.java is the closest existing harness (3-node Raft HA, 3 threads x 1000 users x 10 photos, Awaitility convergence check). To reproduce, add a REFRESH INCREMENTAL materialized view over the User type to the schema setup and run both arms. Diffing the per-node id sets from /api/v1/server?mode=cluster distinguishes the two failure modes: a subset means lag or loss, a mutual difference means divergence.

Note that the numbers in the A/B table above were measured before #5502 and #5503 were fixed; a re-run on current main is worthwhile to confirm the symptom is unchanged.

Impact

Silent loss of committed writes on any HA cluster whose database carries a materialized view, plus a sustained 503 storm while the resync loop runs. Worth treating as a correctness bug rather than a performance one.

Activity

  1. added
    concurrency
    high_priority
    on Jul 28, 2026
  2. added this to the 26.8.1 milestone on Jul 28, 2026
  3. robfrank commented on Jul 28, 2026

    @robfrank
    CollaboratorAuthor

    Picked this up. Taking you up on the offer to split: PR #5502 fixes the second defect, and deliberately does not close this issue - the replication half is still open.

    Second defect: fixed (#5502)

    Confirmed and fixed the dropped-refresh half, though the mechanism is slightly different from the framing in the report. The harm is not just that a concurrent refresh is skipped - it is that the refresh is triggered from a post-commit callback, so the request that gets dropped belongs to a transaction that has already committed, and the in-flight refresh it defers to started before that commit. Nothing reschedules, so the view stays missing those records indefinitely.

    Reproduced with no HA involved at all, as you suspected: a 3-node run showed the leader's own view holding 449 of 450 committed records.

    The fix coalesces requests onto the running refresh rather than dropping them. Releasing ownership and testing for a pending request had to become one atomic step - split in two, a request registered in between is seen by neither the outgoing owner nor the requester, which is the same lost-wakeup in a narrower window.

    I did not implement true incremental maintenance. MaterializedViewChangeListener still schedules a full refresh regardless of REFRESH INCREMENTAL. Per-record maintenance is a feature with real correctness risk (wrong view contents rather than merely slow ones), and coalescing already removes the redundant rebuilds that made the with-view arm collapse.

    Replication half: not reproduced, staying open

    Built a 3-node in-process Raft harness (BaseRaftHATest, whose consistency check routes through DatabaseComparator and fails on any cross-node page-version mismatch). Got zero WALVersionGapException and zero resync loops across every configuration: writes on the leader, writes through a follower, with and without a UNIQUE index on the source type, at 180 and 450 records. Since the root cause is unproven, I did not want to ship a speculative change to the Raft commit path.

    Two notes on the analysis in your report:

    • Your "what is NOT the cause" list is correct on the LocalSchema point. LocalSchema is constructed with wrappedDatabaseInstance (LocalDatabase.java:2423), so the refresh does go through RaftReplicatedDatabase. Worth keeping that ruled out.
    • One genuine leader-side asymmetry turned up that I could not show is reachable from the view path, so I left it alone: RaftReplicatedDatabase.recordFileChanges drains buffered WAL into walEntries, but its replication guard omits walEntries from the condition and the finally then clears the buffer. Its sibling runWithCompactionReplication guards correctly against exactly that. If the leader really is applying pages it never ships, that shape is worth a look.

    Unrelated defect found on the way: #5503

    Concurrent single-record transactions through a follower's handle corrupt records and lose 60-70% of committed writes, logging Invalid record size ... deleting record. Your A/B methodology caught it: the control arm with no view fails identically, so it is not view-related. Single-threaded follower writes are clean, and concurrent writes against the leader are clean. Filed as #5503 with the repro.

  4. added 4 commits that reference this issue on Jul 28, 2026
  5. robfrank commented on Jul 29, 2026

    @robfrank
    CollaboratorAuthor

    Scope trimmed. The second defect (dropped refresh requests) is fixed and merged in #5502 (6642e203e) and has been removed from the description - this issue now tracks the replication half only.

    Promoted the one provable leader-side defect out of the "not yet proven" section into its own heading: RaftReplicatedDatabase.recordFileChanges omits walEntries from its replication guard while the finally clears the buffer unconditionally, so a DDL callback that commits pages without creating a file or bumping the schema version has those pages applied on the leader and never shipped. runWithCompactionReplication guards the identical buffers correctly. That produces exactly the reported shape - follower page versions trailing the leader's - and is a correctness defect on the Raft leader path whether or not it turns out to be the materialized view's route.

    Also ruled out #5503 explicitly (different failure mode, and the no-view control arm was clean) so it does not get re-proposed.

    Picking up the recordFileChanges fix now.

  6. added a commit that references this issue on Jul 29, 2026
  7. added 3 commits that reference this issue on Aug 14, 2026
  8. added a commit that references this issue on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconcurrencyhigh_priority

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions