Durability: acknowledged data can be lost after power loss, and a damaged manifest can make recovery delete every segment

Aperta
#789 2 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@zhourrr ci sta già lavorando.

Dal 29/9/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

Summary

We use zvec as an embedded store and audited its persistence path (collection, segment, WAL, version manager, RocksDB contexts) at main (98aa126). We found that there is currently no way for a caller to know that a write will survive power loss. We also found cases where a crash or a full disk can lead recovery to delete data that was already safely on disk.

Process crashes (kill -9) are mostly handled well, and #768 fixes most of the remaining WAL-recovery windows. The remaining problems are about the order of operations around checkpoints and manifests, and about fsync.

The failing tests are in #788, a PR into #760's branch that reuses #760's harness. We'd be glad to help implement the fix below.

Findings

  1. A checkpoint deletes the WAL before the replacement data is on disk. SegmentImpl::flush (explicit flush(), block rotation, segment rollover, close(), iterators, DDL) fsyncs the WAL, then writes forward blocks, vector index files, the delete snapshot and a new manifest without fsync, then removes the WAL and the old manifest. After a power loss, data acknowledged long before can be missing, or the collection may not open.

  2. The manifest write is not atomic and has no checksum, and a damaged manifest makes recovery delete segments. The manifest is written in place to its final name through ofstream, and the implicit close/flush of the stream is never checked. A manifest cut at a field boundary (including 0 bytes) still decodes as valid. On the next writable open, orphan cleanup removes every segment the damaged manifest doesn't list, and then crashes with SIGSEGV.

    • manifest_codec.cc#L949-L993; collection.cc#L2107, collection.cc#L2172-L2253

      https://github.com/alibaba/zvec/blob/98aa126b742dfb0b78c43bf534bd7e8d7429acf1/src/db/collection.cc#L2086-L2108

    • Reproduced on 8321c131; master and #768 have the same code on this path. The collection has 2600 docs in segments 0, 1 and 2.

    • 0-byte newest manifest (power loss before the data reached disk): a writable open logs "Recovery removed orphan segment directory" for 0, 1 and 2, then crashes. Only idmap.0/, del.0, LOCK and the empty manifest remain, and every later open crashes. A read-only open crashes but deletes nothing.

    • Other truncation lengths: of all 439 possible lengths, the five cuts that fall on a field boundary all crash. Three of them delete every segment and two delete some. Cut 437 opens "complete" with next_segment_id = 0.

    • Disk full during flush() (HFS+ 30 MiB image, manifest about 10 KiB). The bytes beyond the last 4 KiB boundary are written only when the stream is destroyed, and that write is unchecked.

      • With 48 KiB free, flush() returns "Failed to serialize manifest to file" but leaves an empty manifest.4 next to the valid manifest.3. The next writable open deletes segments 0 and 1 and crashes. If the process closes afterwards, close() returns OK and leaves more empty manifests behind.
      • With 52 KiB free, flush() returns OK, but the old manifest is unlinked and the new one is cut at 8192 bytes. The collection can no longer be opened.

    This turns a disk-full or power-loss event into data loss. The orphan cleanup was added in #674 (for #673), and it trusts whatever the newest manifest decodes to.

  3. Optimize and DDL retire the old segments and index files before the replacement is durable.

  4. No write is ever fsynced before it is acknowledged.

  5. Platform barriers.

    • On macOS, fsync does not flush the drive cache; F_FULLFSYNC does.
    • mmap-backed index files are flushed with msync(MS_ASYNC), and the result is ignored.
    • Directories are never fsynced after create, rename or unlink.
  6. Swallowed errors in the persistence path. Examples:

    • zvec_collection_close always returns ZVEC_OK;
    • IDMap::remove ignores its status;
    • segment close discards its flush and close errors;
    • forward-store close hides writer-close failures;
    • index seal errors are overwritten by later results.
  7. From code reading; not yet covered by #788 or confirmed by tests.

    • The RocksDB-backed idmap and indexes run with their WAL disabled (rocksdb_context.cc#L93, rocksdb_context.cc#L199), but they can be flushed ahead of the zvec WAL: an auto memtable flush, the idmap destructor after a failed checkpoint, or a crash between the idmap flush and the manifest save (segment.cc#L2216-L2252). Replay then works from a map that is ahead of the WAL. An upsert can delete the version it is rebuilding, and because doc ids are reused, a lookup by key can return a different document. #768's predecessor-aware replay addresses part of this. Id fencing (never reusing a doc id that a durable index may still reference) would close it.
    • Rows deleted before a failed checkpoint can reappear after the checkpoint is retried.
    • A segment rollover can split its state across two manifests.
    • delete_by_filter can leave the key mapped in the idmap, so a later insert of the same key fails.

    We'll add tests for these if they are useful to you.

Proposed fix: one small durability API; everything else is internal

A. Crash-safe checkpoints for everyone, with no API change

  • Each writer fsyncs the files it owns: forward block, vector index and delete snapshot. RocksDB is flushed through its owner. The new directories are then fsynced.
  • The manifest is published atomically: write manifest.N.tmp, check close, fsync, rename, fsync the directory. It carries a checksum.
  • Only after publication are the WAL, the old manifest and the old files retired. Optimize and DDL follow the same retire-after-publish order.
  • Recovery picks the newest manifest that validates. Orphan cleanup runs only after that, and never deletes a directory that still holds a WAL. If no complete manifest exists, open fails closed and deletes nothing.
  • Add F_FULLFSYNC on Apple, a directory-fsync helper, and checked msync(MS_SYNC) and close. Errors are propagated rather than logged.

Result: flush() returning OK really means that every earlier write survives power loss, which matches the existing documentation.

B. A group-committed WAL barrier: Collection::sync_wal() (C++, C API, Python)

  • sync_wal() returns OK only when every write that returned before the call is on disk. It fsyncs the WAL, and the WAL's directory entry when the WAL is new, and returns any fsync error to the caller.
  • It runs outside the write lock, and one fsync covers every caller waiting on it (group commit). Applications that batch writes (several upsert calls per logical transaction) pay one fsync per transaction, not one per call, and can release their own locks before waiting.
  • After an fsync failure, the collection refuses further writes until it is reopened. A later barrier must never report success over data the failed fsync dropped.
  • Optional convenience: a sync_writes collection option that calls the barrier after every write call, for applications that want per-call durability without managing barriers.
  • The default write path stays exactly as fast as today.

Suggested README wording:

By default, writes survive a process crash, and data flushed with flush() survives power loss. Call sync_wal() when writes made so far must survive power loss; it is cheap and shared across concurrent callers.

Expected cost:

  • The default write path: no change.
  • Each checkpoint: a few extra fsyncs and one rename.
  • sync_wal(): one fsync per call, shared by concurrent callers, and zero when nothing new has been written since the last barrier.

Tests

The tests in #788 extend #760's harness. They fail on main today and should pass with the fix:

  • a truncated or empty manifest on reopen, which must not delete data;
  • ENOSPC while writing the manifest;
  • kill -9 after a write that triggers block rotation, which #768 fixes;
  • a power-loss simulation. A test-only syscall shim records exactly what each fsync made durable, including RocksDB's and Arrow's own syncs, and builds the post-crash image with unsynced renames and unlinks both applied and not applied. It covers "after flush() returns" and later rotation, rollover and optimize.
  • #760's dm-log-writes block replay extended with rotation, index create/drop and optimize workloads, cut at the durable point;
  • fsync failure injection.

Suggested order

  1. #768, rebased onto main after #759.
  2. The tests, into #760.
  3. Durability primitives and error propagation.
  4. Atomic manifest and safe recovery.
  5. Crash-safe checkpoint, optimize and DDL.
  6. sync_wal() with group commit.

We can open PRs for steps 3-6 in this order if that works for you, and we'd welcome guidance on how you'd like them split.

Lingua principale
C++
Stelle
16.3k
Fork
1k
Merge medio
2g 10h
PR unite (30g)
32

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di alibaba/zvec

Tutte le issue di alibaba/zvec

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.