Shared data

Three constructors are named after the builtins they mirror, and two functions after the os functions a file store needs. A name refers to the same data on every node: clusterio.dict("users") on host A and on host B is one dict.

NameBehaves likeHolds
clusterio.dict(name)dict (collections.abc.MutableMapping)str keys, JSON values
clusterio.list(name)list (collections.abc.MutableSequence)JSON values
clusterio.open(path, mode="r", encoding=None)openbytes; modes r, w, a, each with or without b
clusterio.listdir(path="")os.listdirnames under path
clusterio.remove(path)os.remove

dict#

python
import clusterio

users = clusterio.dict("users")
users["ada"] = {"lang": "python", "since": 1843}
users.setdefault("grace", {"lang": "cobol"})
print(users.get("ada"), len(users), sorted(users))
users.update(alan={"lang": "logic"})
users.pop("alan")
del users["grace"]

Every method of dict works. Each write method (pop, popitem, setdefault, update, |=, clear) runs as one step on every node. items(), keys(), values() and copy() read once and give a snapshot, a builtin dict or its view, so they never see a write half-way; | gives a builtin dict.

list#

python
import clusterio

jobs = clusterio.list("jobs")
jobs.extend([{"id": 1}, {"id": 2}])
jobs.append({"id": 3})
first = jobs.pop(0)
print(first, jobs[0], jobs[-1], len(jobs))
jobs.insert(0, {"id": 0})
jobs[1] = {"id": 2, "retry": True}

Every method of list works, and slices can be read but not assigned. Each write method (append, extend, +=, insert, pop, remove, reverse, sort, clear) runs as one step on every node, so two workers that pop the same list never get the same item, and two that remove different items never remove a third. sort takes no key, which could not run on every node: sort a copy, or in a transaction. index, count, copy(), reversed() and + read once; copy() and + give a builtin list.

Files#

python
import clusterio

with clusterio.open("reports/today.txt", "w") as f:
    f.write("hello\n")
with clusterio.open("reports/today.txt", "a") as f:
    f.write("world\n")
with clusterio.open("reports/today.txt") as f:
    print(f.read())

with clusterio.open("images/dot.bin", "wb") as f:
    f.write(b"\x89PNG")

print(clusterio.listdir("reports"))      # ['today.txt']
clusterio.remove("reports/today.txt")

A file is written when it is closed: everything written to it becomes one write to the cluster, so every reader sees it whole. If that write raises, the file is closed all the same and nothing is written. listdir lists the names directly under a directory, as os.listdir does; directories exist while they hold a file.

A path is names joined by /: relative, and none of them empty, . or .., so each file has one path. "/a", "a/", "a//b" and "a/../b" raise ValueError. As on a disk, a file cannot be under a file (NotADirectoryError) nor where a directory is (IsADirectoryError). listdir also takes "" for the top, and a trailing /.

Values#

A JSON value is None, bool, int, float, str, or a list or dict (with str keys) of JSON values. A tuple comes back as a list.

A write refuses what JSON or SQLite cannot hold, before anything is stored:

  • ValueError for NaN and infinities, for a string with a lone surrogate such as "\ud800" (it is not Unicode text), and for lists and dicts nested more than 100 deep (clusterio.store.MAX_DEPTH; how deep Python itself can go depends on the version and the thread, so a fixed limit keeps every node able to apply what one node accepted);
  • TypeError for bytes, sets, other objects, and dict keys that are not str.

In a transaction the same errors are raised from the write that made them, on every node alike, and undo the transaction.

A value read out is a copy, as with shelve:

python
import clusterio

users = clusterio.dict("users")
users["ada"] = {"lang": "python"}
users["ada"]["lang"] = "go"              # changes a copy
assert users["ada"]["lang"] == "python"

ada = users["ada"]
ada["lang"] = "go"
users["ada"] = ada                        # write the whole value back

A read followed by a write is two steps, and another node may write in between. When that matters, use a transaction.

Errors#

An operation that fails raises what Python raises: KeyError, IndexError, FileNotFoundError, TypeError for a value that is not JSON. A write that cannot reach a majority of the nodes raises clusterio.Unavailable; see Consistency.