Architecture
user code ── Dict / List / File ── Cluster ──┬── reads ──► SQLite (WAL)
└── writes ─► Raft node ── TCP ── peers
│
apply committed entries
▼
SQLite (WAL)Each host runs a node. The nodes keep one replicated log with Raft and apply it, in order, to a SQLite database on each node. Reads query the local database; writes append to the log and wait until it is applied.
Modules#
| Module | Role |
|---|---|
clusterio.raft | The Raft node, with its Temporal Typing specification on it; RaftSpec, a model of a cluster; and Raft's decisions as pure functions |
clusterio.replica | A node with its disk and its clock: stores what the node decides, then sends what it says |
clusterio.store | SQLite: the term, vote and log, and the tables the log is applied to |
clusterio.data | Dict, List, files, Transaction and Lock: what user code sees |
clusterio.net | Length-prefixed JSON frames over TCP |
clusterio.cluster | Cluster: runs the node and the network in a background thread |
clusterio.cli | clusterio run |
A write#
users["ada"] = 1becomes a log entry: an operation, its arguments, a request id and the time.- If this node leads, it appends the entry to its log; if not, it forwards the entry to the leader. The caller waits on a future.
- The leader sends the entry to the followers in
AppendEntries, batched with any others waiting. Once a majority stores it, it is committed, and the leader tells the followers at once, not at the next heartbeat. - Each node applies committed entries in order, in one SQLite transaction per entry together with the index it was applied at.
- When the calling node applies the entry, the future resolves with its result.
A write is retried after a lost message; the request id makes the retry apply once.
A transaction#
A transaction's entry holds its name, module:qualname, and its arguments. Each node looks the function up and runs it inside the SQLite transaction that applies the entry, with the entry's time as clusterio.now(). A savepoint undoes its writes if it raises. A context variable tells Dict, List and files whether they run inside an entry (read and write the database directly) or outside (read the local copy, write through the log).
Threads#
The node and the network run on an asyncio event loop in a background thread, so the API is synchronous and works from any thread. A read opens a SQLite connection per calling thread; WAL mode lets reads run while the node applies entries.
Raft#
The node implements Raft as the paper describes it: elections with randomized timeouts, log replication with the consistency check, and commitment of entries from the leader's current term only. On top of the paper:
- a new leader appends a no-op entry, so it commits its predecessors' entries promptly;
- a leader that has not heard from a majority for an election timeout steps down;
- a leader that closes tells its followers its commit index first;
- followers forward writes to the leader.
raft.Node is the node and its specification at once: a dataclass whose fields are the server's state and whose methods are its actions, with Temporal Typing clauses on both. It does no I/O and keeps no time, which is what lets Tanny check its bodies. replica.Replica wraps it: it loads the node from SQLite, stores the term, the vote and any change to the log before the node's messages leave, delivers each message to the action for its type, and keeps the election and heartbeat timers.
How this is checked is in Correctness.