Skip to content

Phase 2: multi-Raft by ranges (plan)

Phase 1 is one node with a warm standby and a witness (docs/ha.md); it stays the first product. Phase 2 grows past one node: the ordered path-key space is cut into ranges, each range is a Raft group with its own LSM tree, and a node holds replicas of many ranges. This page is the plan the work follows, step by step; each step is a branch on the accepted core with its own tests. Steps 2 to 4 are built (below); the rest is not yet.

Scope

  • One key space per store (the path keys of spec/path-keys.md), cut into ranges at database record boundaries (a range never splits the segments of one root).
  • Each range: a Raft group (three replicas by default, five for critical databases), one oliviadb-core database (LSM) applied from the group's log.
  • A meta range holds the range descriptors; a node routes a path key to its range and forwards to the leader.
  • Transactions within one range commit in one Raft round; a unit of work across ranges (the usual batch unit of work) commits with two-phase commit and hybrid logical clocks.
  • Open core (AGPL), as the engine is: Raft, path keys, LSM and MVCC are core.

Not in phase 2: geo-replication across regions (DR), placement by jurisdiction beyond zone labels, online schema change across ranges.

Where expected behaviour comes from

Raft's safety and liveness rules are taken from the published papers, never from what OliviaDB prints: Ongaro and Ousterhout, "In Search of an Understandable Consensus Algorithm" (USENIX ATC 2014), and Ongaro, "Consensus: Bridging Theory and Practice" (Stanford PhD thesis, 2014): chapter 3 (basic Raft), 4 (membership changes), 5 (log compaction), 6 (client interaction, linearizable reads), 9.6 (pre-vote). Each test names the section its rule comes from. Linearizability is checked by a history checker (Wing and Gong's algorithm, as in Porcupine), not by comparing two OliviaDB paths.

Crates

crate what IO
oliviadb-raft one group as a pure state machine: ticks and messages in; messages, entries to persist and entries to apply out none (no clock, no disk, no network)
oliviadb-raftlog the node's multiplexed log: one segment sequence per node, one fsync per batch, a per-group index Vfs (the simulator's in tests)
oliviadb-range a range: a Db applied from a group's log, snapshots, splits and merges Vfs
oliviadb-node many groups per node over the TLS transport, tick driver, batching, routing tokio
oliviadb-pd placement driver: replica placement, rebalancing, leader balancing, quotas through the meta range

Invariants (each checked by the simulator on every seed)

  • Election Safety: at most one leader per term.
  • Log Matching: two logs with an entry of the same index and term agree up to it.
  • Leader Completeness: a committed entry is in the log of every later leader.
  • State Machine Safety: no two replicas apply different entries at one index.
  • Linearizability of client histories over a key-value machine (reads included, with ReadIndex and leader leases).
  • Range replicas byte-identical (tables and manifests) after the same applied index.
  • A unit of work across ranges is all or nothing; a reader never sees half of one.

Steps

Each step is a branch, check.sh green, the simulator seeds in the test suite, and an entry in docs/status.md.

  1. This page.
  2. oliviadb-raft: election, replication, commit, pre-vote, check-quorum, ReadIndex and leader leases; the simulator (partitions, drops, reordering, duplication, crashes, disk faults) with the four Raft invariants and the linearizability checker.
  3. Membership by single-server changes (thesis 4.1: one change at a time, the leader commits an entry of its term before the next change), learners, snapshots and InstallSnapshot, log truncation; simulator seeds for each.
  4. oliviadb-raftlog: one log per node for all its groups, one fsync per batch, segment truncation once every group has flushed past it; crash tests with torn writes and a lost unsynced tail.
  5. oliviadb-range: a Db applied from the log; flushes decided by applied index and volume only (deterministic: memory pressure becomes back-pressure, not a flush), so replicas stay byte-identical; snapshot as a table transfer; repair of a bad block from a peer.
  6. oliviadb-node: many groups per node over the existing TLS stack, a tick driver, message batching; three-node local cluster tests with process kills and partitions (through a proxy), waiting on observable state only.
  7. Addressing: the meta range of range descriptors; routing; the problems "not leader" and "range moved" with a leader hint (an oliviadb-spec change first, then the clients' retry contract).
  8. Split (by size or load, a command in the log, tables shared by reference count on a node) and merge (after colocation); simulator and cluster tests.
  9. oliviadb-pd: placement by zone, rebalancing, leader balancing (pinning a batch's leaders to its node for the window), memtable quota reservations.
  10. Transactions across ranges: hybrid logical clocks with an uncertainty window and self-fencing on clock skew, write intents, two-phase commit with parallel commits; DL/I units of work and native transactions across ranges; histories checked for serializability.
  11. Migration from phase 1 (a warm standby pair to a three-replica group without a stop), backup and restore per range, change data capture across ranges, the documentation, benchmarks against phase 1.

Step 2: what was built

Crate open/crates/oliviadb-raft (no dependencies, no I/O):

  • Raft: election with randomized timeouts, replication with the consistency check and conflict truncation, commit of entries of the leader's term only, the new leader's no-op, pre-vote, check-quorum, the rule that a follower hearing a leader ignores RequestVote, ReadIndex reads and leader-lease reads. Inputs are tick, step, propose, read (and campaign); the output is a Ready: hard state and entries to persist, messages to send after that, committed entries to apply, reads to answer.
  • sim: the seeded simulator. Per seed: message delay with reordering, loss, duplication; partitions of four shapes (one node cut off, two sides, one link cut both ways so connectivity is not transitive, one link cut one way); crashes that keep only what was persisted; disk faults (a crash part way through persisting a Ready); fault rates, client count, write share and client patience drawn per seed from calm to storm. Checked on every step: Election Safety, Log Matching (on the persisted logs, through prefix hashes per index and term), Leader Completeness (when a node becomes leader, against every entry a leader applied), State Machine Safety; after the faults: liveness within 600 ticks; at the end: linearizability of the clients' history.
  • lin: the checker (Wing and Gong's search with Lowe's memoisation, per key by Herlihy and Wing's locality), with puts of unknown outcome bounded by what was read.
  • oliviadb-raft-sim --seed N replays a seed; --seeds, --nodes, --lease, --no-pre-vote, --no-check-quorum, --mutation NAME.

Tests: about 2,000 seeds over 3, 5 nodes, ReadIndex and lease, with and without pre-vote and check-quorum; and a mutation test per property, each a deliberately broken variant the simulator catches: votes twice in a term (Election Safety), accepts entries without the prev check (Log Matching), votes without the election restriction (Leader Completeness), commits beyond the matched entries (State Machine Safety), reads without a quorum round, a lease three election timeouts long (Linearizability). Two rules are shown by directed scenarios instead, because random runs reach them too rarely: Figure 8 of the paper (a leader counting replicas of an older term's entry applies an entry that is later overwritten) and the vote guard that leases rest on (without it a node cut off from the leader wins through a follower that still hears it, and the old leader's lease serves a stale read). Pre-vote, check-quorum, ReadIndex and the election restriction have scenarios too.

Open after step 2: the simulator's clocks do not drift, so leases are checked with synchronized ticks only (a drift model belongs with step 6's tick driver); disk faults are modelled as a lost tail of one Ready, not as corruption of stored entries (step 4's log checks its own records); membership, snapshots and log truncation are step 3.

Step 3: what was built

In oliviadb-raft, on the same tests and simulator:

  • Membership by single-server changes (propose_conf_change: add a voter, add a learner, promote, remove): a server uses the latest configuration in its log, committed or not, and a truncated configuration entry gives way to the one before it (thesis 4.1); a change is refused while the previous one is uncommitted, and until an entry of the leader's own term has committed (Ongaro, raft-dev, July 2015). Votes and quorums count the voters of the configuration in force; a server outside it, a learner or a spare not yet added never campaigns (4.2.1); a leader that removes itself keeps replicating without counting itself and steps down when the change commits (4.2.2). The leader, too, ignores RequestVote while it leads (4.2.3), so a removed server cannot depose it.
  • Snapshots and log truncation: compact(index, data) drops the log up to an applied index under the state machine's snapshot (with the configuration as of that index); a leader sends a follower whose next entry is compacted the whole snapshot in one InstallSnapshot, and the follower keeps its entries after the snapshot only if its entry at the snapshot's index has the snapshot's term (paper section 7, Figure 13). A Ready carries a received snapshot; a restarted node starts from its stored snapshot.
  • Simulator: two spare servers; membership changes through the leader during the faults (with at least three voters kept), each node compacting every 5, 20 or 100 applied entries (per seed), restarts from the stored snapshot. New checks: each applied index and each snapshot carries a hash chain of everything applied before it, so a snapshot that differs from the applied history is a State Machine Safety failure; Log Matching follows the persisted logs across snapshots; Leader Completeness covers the leader's snapshot; liveness asks only the members of the leader's committed configuration to catch up.

Mutation tests added: learners counted in the commit quorum (caught as Leader Completeness) and a snapshot install that keeps a conflicting log (caught as Log Matching), by the simulator; a second change while one is pending, and a change before an entry of the leader's term committed, by directed scenarios (random runs do not reach them; each scenario shows two different entries applied at one index, and the correct code refusing the change). Scenarios also cover learners, a leader removing itself, and a follower catching up from a snapshot. Sweeps of 3 000 seeds per configuration (3, 5 and 1 initial nodes, ReadIndex and lease, with and without pre-vote and check-quorum) passed with about five membership changes, a hundred compactions and fifteen snapshot installs per seed.

Open after step 3: snapshots are sent whole (chunking with the transport, step 6); the leader does not wait for a new server to catch up before adding it as a voter (adding it as a learner first does that by hand; an automatic rule belongs with the placement driver, step 9); leadership transfer (thesis 3.10) is only campaign, without the leader's handover.

Step 4: what was built

Crate open/crates/oliviadb-raftlog (on oliviadb-core's Vfs and oliviadb-raft's types):

  • RaftLog: one sequence of segment files per node for all its groups. A Batch gathers what many groups must persist (Batch::ready takes a group's Ready: hard state, received snapshot, entries; Op::Compact records a compaction); write appends it as one record and syncs once, and only then may the groups send their messages ([R] Figure 2: persistent state is updated on stable storage before responding to RPCs). A batch is atomic: one record with one checksum.
  • Per group in memory: hard state, snapshot metadata, last index, and for each entry its segment, offset, length and term. Each stored entry carries its own checksum and its group, so a read checks the bytes and that they belong to the group and index asked for.
  • Rules checked before anything is written: entries join the log (no gap, after the snapshot), are in sequence with terms that do not go back; a compaction lies inside the log and names the term of its entry; a received snapshot lies after the last one; a term never goes back. New entries replace the stored ones from their index on ([R] Figure 2, AppendEntries rule 3); a snapshot discards the log ([R] Figure 13, rule 7); a compaction discards it up to its index ([R] 7).
  • Each segment begins with a checkpoint of every group (hard state, snapshot metadata, last index), so any run of newest segments replays on its own. After a write, segments older than the oldest one that holds a live entry of any group are removed, oldest first. Recovery replays the longest run of consecutive segments ending with the newest one (a segment whose removal did not reach the disk is either part of it or removed again), checks later checkpoints against the replay, cuts a torn last record of the last segment, refuses a damaged record anywhere else, requires every group to hold every entry from its first to its last index, and syncs the last segment and the directory before it returns (a batch whose sync failed may be in the file but not on the disk).
  • SimVfs::power_off_after(n) (in oliviadb-core): the power fails at the n-th changing operation, so a crash can be placed in a write, while a segment is begun or removed, and during recovery.

Tests: a crash sweep (300 seeds in the suite, 3,000 run by hand, about a million writes): up to eight groups, random batches with hard states, appends, appends that replace a suffix, compactions, received snapshots, groups removed and added, segments of 600 to 8,000 bytes; failed appends and syncs, power losses at any operation, torn tails with garbage. After every recovery each group's state and entries read back from the files must equal a model the test keeps from acknowledged batches; a failed batch may be there only whole. Mutation tests: a write without its sync (acknowledged entries lost), replaced entries kept (entries back after they were replaced), removal past a lagging group's segments (entries lost), a torn tail left in place (later writes lost), recovery without checksums (a torn record with garbage taken, a directed scenario). Directed tests: one sync for a batch of 200 groups; refused batches write nothing; a crash at every operation of a write that removes segments; removed segments coming back, adjacent or behind a gap; damaged sealed segments and damaged records before valid ones stop recovery; three nodes with three groups each persisting through their logs, one node losing power, restarting from its log and catching up.

Open after step 4: a bit flipped by the medium in the last synced record looks like a torn write and that batch is dropped (as with the engine's WAL); a write that begins a segment syncs the directory too; the snapshot's data are the state machine's to keep (step 5); the log has one writer, the node's tick driver (step 6) gathers the batches.

What each step proves

step proven by
2, 3 simulator seeds: the four invariants and linearizable histories under every fault
4 crash tests on the simulated file system: no acknowledged entry lost, no group sees another's entries
5 random seeds: replicas byte-identical; a snapshot restores the same tables
6, 8 cluster tests: kills and partitions, waits on observable state
7 client tests against the spec's problem codes
9 placement scenarios with zone labels; no range below its replica count after a node loss
10 serializability checker on histories with clock skew injected
11 a running phase-1 pair migrated under load with no lost acknowledged commit