Skip to the content
hinny
Back to news

Engineering

Every file can be checked

An embedded database keeps its data in files beside the application, where disks fail and processes are killed in the middle of a write. This is what Hinny does so that damage is found, not believed.

A search index is derived from other data, and it is tempting to treat it as disposable. But rebuilding one means reading and embedding everything again, and an index that quietly returns wrong results is worse than one that is gone. Two rules run through Hinny's storage: a damaged file must be noticed, and a file that cannot be understood must be refused, never guessed at.

Every file says what it is

Every file starts by naming its kind and the version of its format: the manifest, each write-ahead log, each deletion file, each segment, each file of stored text, and the schema. A release reads the formats of the release before it and writes its own; older segments are rewritten when a merge reaches them. A file of a newer format is refused with an error that names both versions, never misread.

The repository keeps a database written by each release, and a test checks that the current code reads it, writes on top of it and merges it.

A checksum on every file

FileWhat it holdsChecksumChecked
ManifestWhich segments make up the indexCRC32 of the whole fileWhen the index is opened
Write-ahead logWrites that are not in a segment yetCRC32 of every record; a batch is one recordWhen the log is replayed
Deletion fileWhich of a segment's documents are deletedA header and a CRC32When the index is opened
SegmentPosting lists, vector codes, attribute columnsCRC32 of the header, and one of every mebibyte after itThe header when the segment is opened; the rest by verify
Stored textThe documents' text, compressed in blockszstd's checksum in every blockWhen a block is read
The files of a namespace and what guards each. Segment checksums are in segment format 8 and later; segments from before it are rewritten, with checksums, when a merge reaches them.

A batch of writes is one log record under one checksum, so after a crash a batch is there whole or not at all, and a record torn off at the end of the log is ignored on replay. A manifest or a deletion file that fails its checksum is refused: before format 4, damage that still parsed was believed.

A segment file from format 8 on. The header's checksum is checked whenever the segment is opened; the checksums of the body, one for each mebibyte, are at the end of the file.

Verify reads everything

Segments are memory-mapped, not read through a call that could check them, so a query does not verify checksums as it goes: that would mean reading whole files to answer from a small part of them. Instead, verify on a namespace (ns.verify() in Python, hinny_verify in C) reads every file against its checksums and names the first damaged one. Reading everything is what it costs. Segments from before format 8 have no checksums, and verify counts them as unchecked.

Refusing instead of guessing

The dangerous moment for a damaged index is opening it. An audit of the recovery code found that an unreadable manifest opened as an empty index, whose cleanup then deleted the segments it no longer named. Now opening rejects an unreadable or malformed manifest or deletion file before anything is cleaned up. A missing manifest beside existing segment files requires explicit recovery, and the files are kept. Every log's header is checked before a writer is created.

Failures while writing are handled in the same spirit. A failed append to the log stops the writer until the index is reopened, because appending behind a torn record could lose later records on replay. A deletion bitmap whose save failed stays marked as unsaved. A flush or merge that fails to publish keeps the previous state and its files, to be retried.

Tested by breaking it

TestWhat it breaksWhat must hold
integritySingle bits in a segment's postings, its header, its checksums and in stored textVerify finds every one
faultsEach of the engine's writes and syncs, failed in turn at every point a workload reaches it: 344 cases, including log appends torn in halfThe index reopens to exactly the state after a prefix of the batches, with every batch acknowledged before the last successful sync
killsA writing process, killed with SIGKILL at a random moment while flushes and merges run, 40 timesEvery acknowledged batch survives, whole and in order
formatsNothing: it opens a database written by each earlier releaseThe current code reads it, writes on top of it and merges it
The tests are in the repository's crates/hinny/tests. The fault sweep was 296 cases before torn log appends were added to it.

The tests are checked against the bugs they exist for. Putting back a deletion bug that had just been fixed makes the fault sweep fail, and removing the flush of the log on each write makes the kill test lose acknowledged batches at once.

What it does not promise

  • A write is acknowledged once its WAL record reaches the operating system. That survives a process crash; an OS crash or power loss can lose writes made since the last successful sync() or flush().
  • Synchronization relies on the filesystem and storage device honoring sync requests. On non-Unix platforms, directory entries are not synchronized.
  • The crash tests kill processes and simulate torn log tails; they are not a hardware power-loss certification.
  • From format 7 onward, deletion files are numbered and named in the manifest, so removing one externally causes open to fail. For legacy pre-format-7 segments, a missing deletion file still means no deletions.
  • Checksums are verified on file open, when reading stored text blocks, and during verify(). They are not checked on every query.

How writes, segments and merges fit together is in the docs, and the dated history of this work, commit by commit, is in the dev log.