mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
docs: COW design, review fold: pre-publication save, owners, abort protocol
Design review of the copy-before-write proposal, folded in one revision: - the COW step runs before publication and outside store.mu (right after captureMu shared, before lifecycleMu/mu, the WAL append and the dirty-map insert), because the save uses the ordinary read path; a post-publication hook cannot do this; lock order stated; - one owner per chunk (copier: pending -> copying -> done; writer: pending -> saving -> saved), a per-chunk mutex never held across I/O, waiters on a per-chunk condition; - batches visit chunks in ascending index and finish each before the next, so they cannot deadlock with each other or the copier; - terminal states closed or aborted; abort on save error, cancel, fence, and before Recover/reset/Close in the same open, taken before the mutation proceeds; waiters woken; the cut fails retryable and keeps its dirty set; a guest write is never refused for a cut problem; - the side store is deleted only after close/abort with its refcount at zero; upload, manifest and CAS work from the buffers, so an unknown CAS keeps today's semantics; - BASE: capMu stays held for the whole cut and already excludes Begin/EndBaseInstall (the earlier open point is removed); - reads bind to the format-v3 read path, no flusher hook; - the Model paragraph states what the model does not cover (owner races, batch order, abort), and the test list grows to match. Text only; the TLA+ model is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4408dd8ee1
commit
451fa936a7
1 file changed
+17
-12
@@ -143,24 +143,29 @@ Per volume, under the chunk base directory in the filer (`core/frontend/chunksto
|
||||
**Problem.** Steps 3–4 hold `captureMu` exclusively while every non-staged candidate chunk is copied; `Write`, `WriteBatch`, `ApplyEntry` and `WriteExtentDirect` take `captureMu` shared, so every write waits for the whole copy. A cut-stall measurement on two lab hosts gave a maximum stall of 1.50 s with 3 GiB dirty and 0.67 s with 512 MiB (p99 15–20 ms). Pre-copy staging shortens it only when the staging rounds converge.
|
||||
|
||||
**Design.**
|
||||
1. **Short barrier.** Under `captureMu` exclusive, the cut's `Sync` fixes H and the candidate set D is fixed as today (the dirty bitmap plus the chunks of WAL entries above the checkpoint). A COW window `{H, D, state per chunk}` is installed in the chunk layer, then `captureMu` is released. The stall becomes `Sync` plus fixing D, independent of the bytes to copy.
|
||||
2. **Per-chunk state** for each chunk of D: `pending → copying → done` or `pending → saved → done`, changed under a small per-chunk mutex.
|
||||
3. **Write hook.** Every mutation that can change what a read returns (`Write`, `WriteBatch`, `ApplyEntry`) checks the chunk of each LBA before the entry becomes visible to the read path. `WriteExtentDirect` (a BASE install) must not run inside an open window: today `BeginBaseInstall` only makes later cuts refuse, so it must also wait for an open window to close (open point, to be specified with the code).
|
||||
- not in D, or `done` or `saved`: proceed;
|
||||
- `pending`: save the chunk's current content, which is still its content at H, read through the normal read path, into the side store; mark it `saved`; proceed;
|
||||
- `copying`: wait until the copier marks it `done`. A chunk copy reads 1,024 blocks; without this wait a write landing mid-copy makes the uploaded chunk a mix of old and new blocks.
|
||||
4. **Copier**, outside the barrier and parallel: for each chunk of D, take `pending → copying` (or find `saved`), read the live chunk (or the saved bytes), hash it, mark `done`. Upload, manifest and CAS are unchanged (steps 5–11).
|
||||
5. **Side store.** Local and per cut: memory up to a bound, then a file in the volume's local root; at most |D| × 4 MiB, in practice the chunks written during the window. It is dropped after the CAS and after any cut failure. Recovery never reads it: after a crash the cut is gone, HEAD is unchanged, and the dirty set is rebuilt from the bitmap and the WAL as today.
|
||||
6. **No flusher hook.** The flusher moves WAL content to the extent and does not change what a read returns; saving at the flusher instead of at the write is wrong (it misses writes still in the WAL).
|
||||
7. **After the CAS** the dirty set keeps every chunk written after H, as `finishCut` does today, so the next cut uploads them.
|
||||
1. **Short barrier; `capMu` held for the whole cut.** The chunk layer's `capMu` stays held from admission to the end of the cut, as today; it already serializes Capture with Evict and Begin/EndBaseInstall, so no BASE install can start inside a window. Under `captureMu` exclusive the cut's `Sync` fixes H and the candidate set D is fixed as today (the dirty bitmap plus the chunks of WAL entries above the checkpoint). A COW window `{H, D, state per chunk, refcount}` is installed in the chunk layer, then only `captureMu` is released. The stall becomes `Sync` plus fixing D, independent of the bytes to copy.
|
||||
2. **Per-chunk state and one owner.** Each chunk of D is `pending`, then exactly one owner moves it on, under a small per-chunk mutex that is never held across I/O:
|
||||
- the copier: `pending → copying → done`;
|
||||
- a writer: `pending → saving → saved` (the copier later reads the saved bytes: `saved → done`).
|
||||
Everyone else who finds `copying` or `saving` waits on that chunk's condition until it reaches `done` or `saved`, or until the window is aborted (step 5).
|
||||
3. **The save happens before publication, outside `store.mu`.** Every mutation that can change what a read returns (`Write`, `WriteBatch`, `ApplyEntry`) runs the COW step right after it takes `captureMu` shared and **before** it takes `lifecycleMu` / `mu`, appends to the WAL or inserts into the dirty map. The save reads the chunk through the ordinary read path, which needs `store.mu`, so it cannot run inside the mutation's critical section; a hook after publication (on the WAL append, the dirty-map insert or the flusher's extent write) would see the new bytes and cannot implement this. Lock order: `captureMu` (shared) → per-chunk COW mutex (leaf, released before any read) → the read path's own locks.
|
||||
- chunk not in D, `done` or `saved`: proceed;
|
||||
- `pending`: become the owner (`saving`), read the chunk (still its content at H), put it in the side store, mark `saved`, wake waiters, proceed;
|
||||
- `copying` or `saving`: wait (step 2), then re-check.
|
||||
- **Batches across chunks** (`WriteBatch`, a multi-block `ApplyEntry`): visit the batch's chunks in ascending chunk index and finish each chunk's step (wait, or save) before the next; no chunk is held while waiting on another, so batches cannot deadlock with each other or with the copier, which owns one chunk at a time. The batch publishes only after all its chunks are `saved`, `done` or outside D.
|
||||
4. **Copier**, outside the barrier and parallel: for each chunk of D, become the owner (`copying`) or find `saved`; read the live chunk (or the saved bytes) into the cut's immutable buffer, hash it, mark `done`, wake waiters. When every chunk of D is `done`, the window closes: no mutation runs the COW step any more. Upload, manifest and CAS are unchanged (steps 5–11) and work from the buffers, so an unknown CAS outcome keeps today's semantics (proposal pending, intents kept); it does not need the side store.
|
||||
5. **Terminal states: closed, or aborted.** The window ends exactly once, either closed (step 4) or aborted. Abort is taken on a save error (side store full or an I/O error), cut cancellation, fencing, and before `Recover`, a reset or `Close` in the same open. Abort happens **before** the mutation that hit the error proceeds: the cut is marked failed (retryable, `finishCut` does not run, so the dirty set keeps every candidate and the next cut takes them again), all waiters are woken and proceed without saving, and the copier stops. A guest write is never refused because of a cut problem. `Recover`, a reset and `Close` wait for the abort to finish (step 6) before they take their own locks.
|
||||
6. **Side store and drain.** Local and per cut: memory up to a bound, then a file in the volume's local root; at most |D| × 4 MiB, in practice the chunks written during the window. Every reader (the copier reading a saved chunk) holds the window's refcount; the side store is deleted only after the window has closed or aborted **and** the refcount is zero. Recovery never reads it: after a crash the cut is gone, HEAD is unchanged, and the dirty set is rebuilt from the bitmap and the WAL as today.
|
||||
7. **Reads go through the store's read path as the format-v3 work defines it** (the stable, validated reader), both for the save and for the copier; COW adds no flusher hook and no raw extent read. The flusher moves WAL content to the extent without changing what a read returns, so saving at the flusher would miss writes still in the WAL.
|
||||
8. **After the CAS** the dirty set keeps every chunk written after H, as `finishCut` does today, so the next cut uploads them.
|
||||
|
||||
**What it does not change.** The bytes a cut uploads (whole 4 MiB chunks whose content changed), the manifest work and the load on SeaweedFS stay the same; COW moves the copy out of the write stall, it does not reduce upload volume. Per-cut statistics (`barrierPauseNs`, `uploadedChunks`, `uploadedBytes` in the `/chunk/cut` body) measure both.
|
||||
|
||||
**Alternatives.** Holding the flusher for candidate chunks (reading the at-H content back from the extent and WAL entries up to H) needs no side store, but the WAL cannot be released for those chunks during the window, and a long upload fills it. Raising `BarrierBound` staging does not bound the stall when writes keep re-dirtying staged chunks. Redirect-on-write at the extent changes the on-disk layout and is out of scope for the current format work.
|
||||
|
||||
**Model.** [`docs/spec/CutCOW.tla`](../spec/CutCOW.tla) (S4c in the [spec README](../spec/README.md)) checks today's barrier cut and this design with one crash: every uploaded chunk equals the image at H (`HeadImage`, `BufFrozen`), reads return the newest write, and the side store never outlives its cut. Both pass exhaustively at MaxLSN 6 with 2 cuts. Each rule above is necessary: a saved copy taken after the write, no save, admitting writes before D is fixed, saving at the flusher, reusing the side store in the next cut, or skipping the per-chunk wait each uploads a chunk that is not the image at H.
|
||||
**Model.** [`docs/spec/CutCOW.tla`](../spec/CutCOW.tla) (S4c in the [spec README](../spec/README.md)) checks today's barrier cut and this design with one crash: every uploaded chunk equals the image at H (`HeadImage`, `BufFrozen`), reads return the newest write, and the side store never outlives its cut. Both pass exhaustively at MaxLSN 6 with 2 cuts. The model abstracts a write to one whole chunk, has one copier and no abort action (an abort behaves like a crash for HEAD: the cut is dropped and HEAD is unchanged), so owner races between writers, batch ordering and the abort protocol (steps 2, 3 and 5) are specified here, not model-checked. Each rule the model does cover is necessary: a saved copy taken after the write, no save, admitting writes before D is fixed, saving at the flusher, reusing the side store in the next cut, or skipping the per-chunk wait each uploads a chunk that is not the image at H.
|
||||
|
||||
**Tests to write with the code.** RED first: a write during the window to an uncopied candidate, with the hook absent, must upload new bytes. Then: a write racing the copier on the same chunk (the per-chunk wait); a crash during the window (reopen, no side store read, HEAD unchanged, next cut correct); the barrier pause no longer grows with dirty bytes; new statistics (`cowSavedChunks`, `cowSaveNs`, `cowWaitNs`) in `ChunkCutStats`, which the cut-body mirror test then forces into `/chunk/cut`.
|
||||
**Tests to write with the code.** RED first: a write during the window to an uncopied candidate, with the hook absent, must upload new bytes. Then: a write racing the copier on the same chunk (the per-chunk wait); two writers racing for one pending chunk (one owner, one wait); two batches over the same chunks in opposite LBA order (no deadlock); a save error (cut fails retryable, the write succeeds, the next cut uploads every candidate); `Close` and `Recover` during an open window (abort, drain, side store deleted once); an unknown CAS outcome after the window closed; a crash during the window (reopen, no side store read, HEAD unchanged, next cut correct); the barrier pause no longer grows with dirty bytes; new statistics (`cowSavedChunks`, `cowSaveNs`, `cowWaitNs`) in `ChunkCutStats`, which the cut-body mirror test then forces into `/chunk/cut`.
|
||||
|
||||
## 6. Snapshots, forks, retention, GC, volume delete
|
||||
|
||||
|
||||
Reference in new issue
Block a user