diff --git a/seaweed-block/core/storage/chunk_cut_presync_test.go b/seaweed-block/core/storage/chunk_cut_presync_test.go index 12c6fcf745..ae81163b7b 100644 --- a/seaweed-block/core/storage/chunk_cut_presync_test.go +++ b/seaweed-block/core/storage/chunk_cut_presync_test.go @@ -10,9 +10,8 @@ import ( // presyncCutAfterFill fills the volume with n writes, then cuts while a // writer keeps writing (a loaded cut), under a sync cost model: a store // Sync's file sync takes perWrite for every write since the previous one -// (write-back that grows with what is dirty). It returns the cut's in-pause -// sync time and its pre-barrier sync time. -func presyncCutAfterFill(t *testing.T, n int, perWrite time.Duration) (inPause, pre time.Duration) { +// (write-back that grows with what is dirty). It returns the cut's stats. +func presyncCutAfterFill(t *testing.T, n int, perWrite time.Duration) chunkCutStats { t.Helper() s, _ := newChunkTestStore(t) l := mustAttach(t, s, newFakeChunkRemote(), ctConfig(1)).(*chunkLayerImpl) @@ -61,20 +60,42 @@ func presyncCutAfterFill(t *testing.T, n int, perWrite time.Duration) (inPause, if err != nil { t.Fatal(err) } - return time.Duration(r.Stats.SyncNs), time.Duration(r.Stats.PreSyncNs) + return r.Stats } // The first loaded cut after a fill: its in-pause sync does not grow with the -// fill. The pre-barrier sync pays the fill's write-back and repeats until one -// round is short, so the pause flushes only what the guest wrote in about -// that window. (One pre-barrier sync left the pause the writes made during -// the fill's write-back, which grow with the fill.) +// fill. The pre-barrier sync pays the fill's write-back and repeats until the +// bytes dirtied during its last pass are at most the bound, so the pause +// flushes only that tail. (One pre-barrier sync left the pause the writes +// made during the fill's write-back, which grow with the fill.) func TestTheFirstLoadedCutAfterAFillHasAFlatInPauseSync(t *testing.T) { const perWrite = 50 * time.Microsecond - smallPause, smallPre := presyncCutAfterFill(t, 256, perWrite) - largePause, largePre := presyncCutAfterFill(t, 4096, perWrite) - t.Logf("fill 256: pre %v in-pause %v; fill 4096: pre %v in-pause %v", smallPre, smallPause, largePre, largePause) + small := presyncCutAfterFill(t, 256, perWrite) + large := presyncCutAfterFill(t, 4096, perWrite) + smallPause, largePause := time.Duration(small.SyncNs), time.Duration(large.SyncNs) + t.Logf("fill 256: pre %v (%d passes, tail %d B) in-pause %v; fill 4096: pre %v (%d passes, tail %d B) in-pause %v", + time.Duration(small.PreSyncNs), small.PreSyncRounds, small.TailBytes, smallPause, + time.Duration(large.PreSyncNs), large.PreSyncRounds, large.TailBytes, largePause) if largePause > 2*smallPause+5*time.Millisecond { t.Fatalf("the in-pause sync grew with the fill: %v after 256 writes, %v after 4096", smallPause, largePause) } + for _, st := range []chunkCutStats{small, large} { + if st.TailBytes > preSyncTailBound || st.TailOverBound { + t.Fatalf("a cut left a pre-barrier tail of %d bytes (bound %d, over=%v)", st.TailBytes, preSyncTailBound, st.TailOverBound) + } + } +} + +// When the tail never falls to the bound (sustained writes faster than the +// device; here the bound is forced below any tail), the pre-barrier sync +// stops after its last allowed pass, reports the tail above the bound as a +// finding, and the cut proceeds: a cut is never starved. +func TestAPreBarrierSyncThatCannotReachTheBoundStillCuts(t *testing.T) { + saved := preSyncTailBound + preSyncTailBound = 0 // every pass leaves some guest writes: never reached + t.Cleanup(func() { preSyncTailBound = saved }) + st := presyncCutAfterFill(t, 64, 20*time.Microsecond) + if st.PreSyncRounds != preSyncMaxRounds || !st.TailOverBound || st.TailBytes == 0 { + t.Fatalf("an unreachable bound: %d passes (want %d), over=%v, tail %d B", st.PreSyncRounds, preSyncMaxRounds, st.TailOverBound, st.TailBytes) + } } diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 230ee6c1aa..793d29b86a 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1522,14 +1522,16 @@ func (l *chunkLayerImpl) recordCapturedEqualMarks(captured []capturedChunk, vers } } -// The pre-barrier sync repeats while a round takes longer than -// preSyncShortRound (above one device flush, about 5-8 ms), at most -// preSyncMaxRounds times: after a short round the pause's own sync has only -// about that round's writes to flush. -const ( - preSyncShortRound = 10 * time.Millisecond - preSyncMaxRounds = 6 -) +// The pre-barrier sync repeats until the store-file bytes dirtied during its +// last pass (WAL appends and extent writes) are at most preSyncTailBound, so +// the pause's own sync flushes at most about that much; at most +// preSyncMaxRounds passes, then the cut proceeds anyway (sustained writes +// faster than the device) and reports the tail above the bound. A cut is +// never starved. preSyncTailBound is a variable only so a test can force the +// bounded path. +var preSyncTailBound uint64 = 1 << 20 + +const preSyncMaxRounds = 6 func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res chunkCutResult, err error) { defer func() { err = l.latchIdentityMoved(err) }() @@ -1700,23 +1702,25 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch // Before the pause: the once-per-layer UNKNOWN seed, and the pre-barrier // sync, so the pause's own sync flushes only what came after it. The first // cut after a fill pays the fill's write-back here, and what the guest - // writes meanwhile would be the pause's work, so the sync repeats until a - // round is short (preSyncShortRound, at most preSyncMaxRounds): the pause - // then flushes only about one short round of writes, whatever the fill. - // The in-pause sync still fixes H. + // writes meanwhile would be the pause's work, so the sync repeats until the + // bytes dirtied during its last pass are at most preSyncTailBound (at most + // preSyncMaxRounds passes): the pause then flushes at most about that tail, + // whatever the fill. The in-pause sync still fixes H. l.prepareTouched() preSync := time.Now() for { - round := time.Now() st.PreSyncRounds++ + dirtied := l.s.storeFileDirtiedBytes() if _, err := l.s.syncWithPhases(nil); err != nil { st.PreSyncNs = time.Since(preSync).Nanoseconds() return chunkCutResult{}, fmt.Errorf("storage: capture sync (before the barrier): %w", err) } - if time.Since(round) <= preSyncShortRound || st.PreSyncRounds >= preSyncMaxRounds || ctx.Err() != nil { + st.TailBytes = l.s.storeFileDirtiedBytes() - dirtied + if st.TailBytes <= preSyncTailBound || st.PreSyncRounds >= preSyncMaxRounds || ctx.Err() != nil { break } } + st.TailOverBound = st.TailBytes > preSyncTailBound // a finding: the cut proceeds anyway st.PreSyncNs = time.Since(preSync).Nanoseconds() // Quiesced cut. The guest pause starts at the Lock REQUEST: a pending diff --git a/seaweed-block/core/storage/chunk_seam.go b/seaweed-block/core/storage/chunk_seam.go index 4863464e0d..91e93fc66d 100644 --- a/seaweed-block/core/storage/chunk_seam.go +++ b/seaweed-block/core/storage/chunk_seam.go @@ -434,7 +434,13 @@ type chunkCutStats struct { // pause (never part of BarrierPauseNs): the pause's own sync then has // only what came after it. PreSyncNs int64 - PreSyncRounds int // pre-barrier sync rounds (repeated until one is short) + PreSyncRounds int // pre-barrier sync passes (repeated until the tail is at most the bound) + // TailBytes is the store-file bytes (WAL appends and extent writes) + // dirtied during the last pre-barrier sync pass: about what the pause's own + // sync has to flush. TailOverBound is set when it stayed above the bound + // after the last pass (a finding; the cut proceeds). + TailBytes uint64 + TailOverBound bool // Disjoint non-payload intervals within barrier-to-settlement fixed cost. CandidateNs int64 WindowSetupNs int64 diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index 1c37658ecd..639073c02e 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -1249,6 +1249,16 @@ var _ LogicalStorage = (*WALStore)(nil) // Sync calls or the separate checkpoint/extent metadata sync operations. func (s *WALStore) WALSyncCount() uint64 { return s.walSyncCalls.Load() } +// storeFileDirtiedBytes counts the bytes written into the store file in this +// open: WAL appends and the flusher's extent writes (cumulative atomics). +func (s *WALStore) storeFileDirtiedBytes() uint64 { + n := s.instr.walAppendBytes.Load() + if s.flusher != nil { + n += s.flusher.instr.extentWriteBytes.Load() + } + return n +} + // walSyncCovered is used only after the caller has captured its publication // frontier. A later concurrent write may force extra I/O, never a false ACK. func (s *WALStore) walSyncCovered() bool { diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index b202e99154..154638ea61 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -313,7 +313,9 @@ Durable store state of the served volume. Go type `volume.durableStatusBody`. | `Volumes[].Chunk.Stats.LastCut.CandidateVisits` | int64 | CandidateVisits counts the chunk indices the barrier's candidate step visited (scanned entries and set members), the work a pause pays for. | | `Volumes[].Chunk.Stats.LastCut.PauseCandidateOps` | int64 | PauseCandidateOps counts per-candidate operations done while the guest is paused (from the barrier lock to its release): zero when the pause does O(1) work. | | `Volumes[].Chunk.Stats.LastCut.PreSyncNs` | int64 | PreSyncNs is the sync right before the barrier, outside the guest pause (never part of BarrierPauseNs): the pause's own sync then has only what came after it. | -| `Volumes[].Chunk.Stats.LastCut.PreSyncRounds` | int | pre-barrier sync rounds (repeated until one is short) | +| `Volumes[].Chunk.Stats.LastCut.PreSyncRounds` | int | pre-barrier sync passes (repeated until the tail is at most the bound) | +| `Volumes[].Chunk.Stats.LastCut.TailBytes` | uint64 | TailBytes is the store-file bytes (WAL appends and extent writes) dirtied during the last pre-barrier sync pass: about what the pause's own sync has to flush. TailOverBound is set when it stayed above the bound after the last pass (a finding; the cut proceeds). | +| `Volumes[].Chunk.Stats.LastCut.TailOverBound` | bool | (group of `TailBytes`) TailBytes is the store-file bytes (WAL appends and extent writes) dirtied during the last pre-barrier sync pass: about what the pause's own sync has to flush. TailOverBound is set when it stayed above the bound after the last pass (a finding; the cut proceeds). | | `Volumes[].Chunk.Stats.LastCut.CandidateNs` | int64 | Disjoint non-payload intervals within barrier-to-settlement fixed cost. | | `Volumes[].Chunk.Stats.LastCut.WindowSetupNs` | int64 | (group of `CandidateNs`) Disjoint non-payload intervals within barrier-to-settlement fixed cost. | | `Volumes[].Chunk.Stats.LastCut.PublishPrepareNs` | int64 | (group of `CandidateNs`) Disjoint non-payload intervals within barrier-to-settlement fixed cost. |