Expand description
Client database of a single shard.
§Architecture
The database is stored on a ClientDatabaseStorageBackend, a file preallocated to its full
size or a raw block device, so no write fails due to lack of disk space. It is read and written
in whole pages of OwnedPages::ALIGN bytes, which is a multiple of the sector size of modern
SSDs, and has to be formatted with ClientDatabase::format() before it can be opened.
Pages are grouped into page groups of ClientDatabaseFormatOptions::page_group_size pages,
which are the unit of allocation. A page group is either permanent or temporary. Permanent page
groups are never freed, so data only goes there in amounts that fill whole pages, like segment
headers once there are enough of them, or once per database, like its chain parameters.
Temporary page groups store blocks and everything else until it becomes outdated or moves to
permanent page groups, and are freed whole once nothing in them is needed anymore. The database
starts with one permanent and one temporary page group.
Page groups of each kind form an append-only log, starting with the first page group of that kind. A page group holds storage items, each occupying whole pages: a header first, then storage items in the order they were written. The last page is left for a “next page group” storage item, which names the page group the log continues in. Every storage item holds its sequence number and a link to the previous storage item of the log, so the log can only be read in the order it was written.
Right after each permanent storage item, a commit entry that links to it is written to the temporary log, which effectively determines the end of the permanent log. This allows replaying both in the original write order without extra complexity.
The first permanent storage item after the header holds the chain parameters of the database:
the root of the genesis block of the chain it stores and the block confirmation depth, which
must stay the same (see ClientDatabaseOptions). ClientDatabase::open() writes them when
the database doesn’t have them, and otherwise refuses to open the database with other values
before anything else is replayed.
Segment headers and super segment headers are all kept in memory. Each page they fill is written to permanent page groups once, and the headers after the last page go to a temporary storage item.
ClientDatabase::open() reads both logs, one page group of each at a time, and reconstructs
the state in memory by replaying storage items. Page groups on neither log are free. Writing
continues after the last storage item of the temporary log and the last committed permanent
storage item, overwriting whatever is left there. The block lifecycle in
ClientDatabaseOptions describes which blocks are kept in memory and when they are written to
disk.
§Worker
ClientDatabase::open() returns a ClientDatabaseWorker together with the database, which
the caller runs. The worker is the only writer, it writes tasks one at a time, in the order they
were sent.
A method that writes updates the state in memory and sends a task to the worker without awaiting anything in between, so a dropped future either changed nothing or its task is written completely. A block stays in memory until its write finishes.
The worker returns once all database handles are dropped and everything sent to it is written,
or once a write failed. After that, methods that write return Unavailable errors.
§Design constraints
Disk usage is fixed by the size of the storage backend, so the space the database needs has an upper bound that follows from protocol parameters, and storage is reused one page group at a time.
RAM usage has an upper bound too, which matters as much as disk usage, since a node participates in several shards, each with its own database instance on the same storage. Only recent blocks are kept in memory with their details, and a new fork is only supposed to start being imported if it is going to become the new best block, which limits how many forks are in memory.
The blockchain is replicated by every node, after a crash the node re-syncs whatever didn’t make
it to disk. So writes don’t need hard durability: nothing waits for data to reach the disk
(there is no fsync), and nothing should add latency for it. Writes that are in flight complete
in any order, so after a crash any of them may be missing while later ones are present. The
first storage item that is broken (incomplete, corrupted, out of sequence or not linked to the
previous one) is where the log ends, which is not an error. Storage items left over from before,
like the ones after the end of the log or in a reused page group, never continue the log, so
nothing has to be erased to keep them out. That holds even when the storage item in front of
them is written again the same way, like a block imported again, since every storage item has a
random value chosen when the database was opened or formatted. A storage item that continues the
log, but can’t be decoded or is not valid where it is, is an error though, since checksums leave
nothing but a bug as the cause, like a change of the format without a change of the database
version. Pruning and confirmation are not recorded either, ClientDatabase::open() prunes and
confirms blocks again as it replays them, and the best block after opening doesn’t have to be
the best block before shutdown. Headers after the last page and the highest local segment
indices of shards are written again with later storage items, which ClientDatabase::open()
handles by replaying them once. A known block is never written again, so a block that continues
the log a second time is an error.
Storage backend read and write errors are fatal by design: if the storage backend fails, the node is broken and can’t make progress anyway, so it is expected to stop. Nothing tries to recover from such errors (for example, the worker returns after a write failed), and there is no need to add recovery logic.
Modules§
- storage_
backend - worker
- Worker of
ClientDatabase, see the crate documentation
Structs§
- Client
Database - Client database
- Client
Database Format Options - Options for
ClientDatabase::format() - Client
Database Options - Options for
ClientDatabase. - Database
Id - Unique identifier for a database
- Genesis
Block Builder Result
Enums§
- Client
Database Error - Client
Database Format Error - Error for
ClientDatabase::format() - Invalid
Storage Item Error - Why a storage item is not valid, see
ClientDatabaseError::InvalidStorageItem - Page
Group Size Error - Invalid page group size, see
ClientDatabaseFormatOptions::page_group_size