API reference
Everything here is importable from clusterio. The module functions use the default node, configured by the environment; a Cluster has the same functions as methods.
Data#
clusterio.dict(name)#
The dict called name, shared by the cluster: a Dict, which is a collections.abc.MutableMapping with str keys and JSON values. Each write method is atomic; items(), keys(), values() and copy() are snapshots. See Shared data.
clusterio.list(name)#
The list called name: a List, which is a collections.abc.MutableSequence of JSON values. Slices can be read, not assigned. Each write method is atomic; sort takes no key. See Shared data.
clusterio.open(path, mode="r", encoding=None)#
Open the shared file at path. Modes are r, w and a, each with or without b; text modes use encoding, UTF-8 by default. A file opened for writing is written to the cluster as one write when it is closed. Raises FileNotFoundError for a missing file in mode r.
clusterio.listdir(path="")#
The names directly under the directory path, sorted, as os.listdir.
clusterio.remove(path)#
Remove the file at path, as os.remove. Raises FileNotFoundError if there is none.
Coordination#
@clusterio.transaction#
Make a module-level function run as one entry of the log, atomically, on every node, in log order. Returns a Transaction, called as the function is. Its arguments and result are JSON values. Raises TypeError for a function that is not defined at a module's top level. See Transactions.
clusterio.lock(name, ttl=10.0)#
The lock called name: a Lock, as threading.Lock, held by one holder in the cluster at a time. A lease of ttl seconds, renewed every ttl / 3 while held. See Coordination.
clusterio.semaphore(name, value=1, ttl=10.0)#
The semaphore called name: a Semaphore of value permits, as threading.BoundedSemaphore, each a lease as a lock is.
clusterio.event(name)#
The event called name: an Event, as threading.Event.
clusterio.barrier(name, parties, timeout=None)#
The barrier called name: a Barrier for parties parties, as threading.Barrier. Raises clusterio.BrokenBarrierError.
clusterio.queue(name, maxsize=0)#
The queue called name: a Queue of JSON values, as queue.Queue, without task_done and join. Raises queue.Empty and queue.Full.
clusterio.set(name)#
The set called name: a Set, a collections.abc.MutableSet of JSON values.
clusterio.aio#
The coordination types with the interfaces of asyncio, and transaction, wait_for and sync as coroutines. Cluster.aio has the same functions for a given node. See Coordination.
clusterio.sync()#
Wait until this node has applied every write that completed before the call, anywhere in the cluster, so the reads that follow are linearizable. One write's round trip; raises Unavailable without a majority. See Consistency.
clusterio.wait_for(predicate, timeout=None)#
Block until predicate() is true of this node's copy of the data, calling it again each time this node applies entries; False if timeout passes first. Not in a transaction.
clusterio.now()#
The time in seconds since the epoch. Inside a transaction, the time its entry was made, the same on every node; outside, time.time().
Nodes#
clusterio.Cluster(node=None, peers=None, data=None, timeout=5.0)#
This process's node of a cluster. It has the functions above as methods, and leader, close() and the context-manager protocol. After close(), as after a file's, every call raises RuntimeError; so do calls waiting on the node when it closes, a write waiting for a majority among them. See Embedding.
clusterio.status()#
What this node knows of itself and the cluster, as a dict: node, peers, role (leader, candidate or follower), term, leader, commit_index, applied, log_length, heard (on a leader, the seconds since each peer last answered), halted (True once the node has stopped applying the log, as when it lacks a transaction's code; it still stores and votes), failed (True once it has stopped altogether) and closed. For health checks:
import clusterio
status = clusterio.status()
print(status["role"], status["leader"], status["applied"])clusterio.Unavailable#
Raised by a write that cannot reach a majority within the cluster's timeout. A subclass of TimeoutError. The write may still be applied.
Environment#
| Variable | Meaning | Default |
|---|---|---|
CLUSTERIO_NODE | This node's name | local |
CLUSTERIO_PEERS | name=host:port for every node, comma-separated | this node alone |
CLUSTERIO_DATA | Directory for the node's database | .clusterio/<node> |
Command line#
clusterio run [APP] [--node NAME] [--peers PEERS] [--data DIR] [-- ARGS...]Start a node, call APP (module:function or file.py:function; the function defaults to main), and stop the node when it returns. See Configuration and CLI.