Skip to content

feat(replication): an old leader rejoins as a follower, and the High availability guide - #827

Open
MathiasVDA wants to merge 3 commits into
mainfrom
claude/project-thread-p99rit
Open

MathiasVDA wants to merge 3 commits into
mainfrom
claude/project-thread-p99rit

Conversation

@MathiasVDA

@MathiasVDA MathiasVDA commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Mathias · project thread

What and why

Closes #781, phase 4 of automatic failover (#14, decision record 0012, decision 6).

Before this change, an old leader that came back after a failover served its dataset fenced (writes 503) and never followed the new leader. Now, a --failover member that serves a dataset without the lease and sees another member holding it at a higher epoch rejoins as a follower:

  • Rejoining. replication::rejoin removes the (unheld) lease and writes a NAME.replica position from the member's own data: its timeline and the end of its log. A log just started by the shutdown checkpoint counts as the end of the log before it. The member then follows the cluster, holder first, reusing the engine it already serves.
  • No new base when the positions match. The follower's existing rules decide. When the holder's HISTORY has a promotion from exactly that position, the member reads on without a new base.
  • A new base otherwise. When the member holds writes past the promotion (never acknowledged), it copies a new base. Nothing of its timeline past the promotion is kept.
  • Restarts. It stays a follower when it starts again with the same command. Under --failover, serve follows a --dataset that has a non-promoted NAME.replica (rejoined_replicas) instead of serving it.
  • The election loop (failover::election::round) calls rejoin when the lease holder is another node at a higher epoch. Cluster gets follow_wait (--follow-wait) for that follower. A member that cannot follow from its own data (a dataset attached from outside --location) stays fenced and logs why once.

How it was tested

  • tests/failover.rs, all with the in-memory lease, and also against etcd when TRIPLETRAIL_TEST_ETCD is set:
    • an_old_leader_that_comes_back_follows_the_new_one: a leader is stopped, b or c is elected, and a restarts as it was started. a follows from its own data (no rail.r1), gets the new leader's writes, counts for --sync-replicas, refuses writes (405), and stays a follower. A second restart, as serve does it, follows again.
    • an_old_leader_drops_what_it_wrote_past_the_promotion: a holds a write no replica got (answered 503). b and c come back and elect one of them, and a restarts. It copies a new base and does not have that write.
  • cargo test -p tripletrail-server --test failover --test replication passes. The failover tests passed on all 6 local runs since the tests took their final form.
  • GUIDE_CHAPTER=14 cargo test -p tripletrail-server --test guide passes. I checked that a wrong expected output fails it.
  • cargo fmt, and cargo clippy --all-targets --all-features -D warnings on tripletrail-server.

Documentation

  • New guide chapter 14. High availability, linked from the index and from chapter 13. It covers:
    • a 3-member Docker Compose file with one etcd (test: file);
    • what happens when the leader goes away and comes back;
    • what a write answers when the cluster cannot keep it safe (tested, with a member whose etcd and peers are down);
    • stale reads and TripleTrail-Min-Version (tested).
  • docs/server.md "Automatic failover": a new bullet on how an old leader rejoins, and the "Not yet" note is gone.
  • CHANGELOG (Added).

Generated by Claude Code

Under --failover, a member that serves a dataset without the lease and
finds another member holding it at a higher epoch follows that member
(decision record 0012, decision 6). It goes on from its own data when
the new leader was promoted where its log ends, and copies a new base
otherwise, so nothing it wrote past the promotion is kept. It stays a
follower when it starts again with the same command line.

Refs #781
A cluster of three with Docker Compose and one etcd, what happens when
the leader goes away and comes back, what a write answers when the
cluster cannot keep it safe, and stale reads with
TripleTrail-Min-Version. docs/server.md describes how an old leader
rejoins.

Closes #781
@MathiasVDA MathiasVDA self-assigned this Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

The audit check fails because of a new advisory for calamine 0.38 (RUSTSEC-2026-0628), not because of this PR's changes. The same check is red on main (cab0f26), and #828 bumps calamine to 0.39 to fix it. Once #828 is merged I'll merge main into this branch.


Generated by Claude Code

@MathiasVDA MathiasVDA added the review:changes Reviewer asks for changes label Dec 3, 2026 — with Claude

Copy link
Copy Markdown
Contributor Author

Changes requested at a2e4fb1. The design follows decision record 0012 (decision 6), the two new tests pass here, and the guide chapter reads well. One must-fix, on the moment of the rejoin itself.

Must fix: the old leader can take a write while it rejoins, and then follows from its own data with that write in it.

replication::rejoin reads own = store.log_position(), then removes the unheld lease from state.leases, and only later the follower thread attaches the dataset as Mode::ReadOnly (start_following, after remove_other_copies and warm_up). check_lease lets a write through when the dataset has no lease, so between the removal and the read-only attach the dataset is writable with no fence. A write that lands there is past the position written to NAME.replica, so the follower rules see a matching promotion point, take no new base, and apply the new leader's log on top. The write is never acknowledged (it waits for --sync-replicas and answers 503), but it stays on that member and is served by it. The same holds for a write that started before the lease ran out on a leader that was cut off and commits after own is read.

Reproduced with a scratch test on top of an_old_leader_that_comes_back_follows_the_new_one: eight threads send INSERT DATA to a from before its restart until it reports role: follower, with --sync-timeout 5 s. In 1 run of 3, a ended with six triples the leader does not have (s1013 to s1019), rail.r1 did not exist, and /$/replication/rail said following:

left  (a):      s0 s1 s1013 s1014 s1015 s1016 s1018 s1019 s2 s3 s4 s5 s6
right (leader): s0 s1 s2 s3 s4 s5 s6

What I expect: no write can commit on the dataset after the position is read. For example, keep the fence (the unheld lease, or the read-only mode set first) until the dataset is attached read-only, and read the log position only once running writes are done (under the store's write exclusion). Please add a test that writes during the rejoin and compares the member with the leader.

Also needed before merge: merge main, audit is red on this head because it predates #828.

Small, not blocking: when the old leader checkpointed at shutdown and the replica had already read the new (empty) generation before it was promoted, the positions differ (g:0 against g-1:end) and the member copies a new base. That is safe, only slower than needed; a line in the docs or a follow-up is enough.


Generated by Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:changes Reviewer asks for changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(replication): an old leader rejoins as a follower, and the High availability guide

1 participant