From 67f770d650a3c8c2033eb23fadc703e2c9639f0f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 16:53:21 -0800 Subject: [PATCH] storage: floor each pre-copy payload interval, not their sum The payload time sums several intervals (staging, the copy, the upload); three that each ended below the clock's tick read zero although together they took up to three ticks, so one floor on the sum overstated the rate. Each interval now adds at least one tick to an unexported floored sum that the rate divides by; the reported payload time is unchanged. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/chunk_layer.go | 6 ++-- seaweed-block/core/storage/chunk_precopy.go | 17 ++++++++--- .../storage/chunk_precopy_rate_floor_test.go | 29 +++++++++++++++++++ seaweed-block/core/storage/chunk_seam.go | 5 ++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 580ffddaec..a67bc42dbd 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1150,7 +1150,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch if !guestFrozen { staged, err = l.stage(ctx, &st, spill.bytes) } - st.PayloadNs += time.Since(stageStart).Nanoseconds() + st.addPayload(time.Since(stageStart).Nanoseconds()) if err != nil { return chunkCutResult{}, err } @@ -1334,7 +1334,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch st.CopyNs = time.Since(copyStart).Nanoseconds() if st.UploadedBytes > bytesBeforeCopy { - st.PayloadNs += st.CopyNs + st.addPayload(st.CopyNs) } else { st.NonPayloadCopyNs += st.CopyNs } @@ -1417,7 +1417,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch return chunkCutResult{}, fmt.Errorf("storage: capture upload: %w", errors.Join(errChunkRemoteUnavailable, errCutUpload, err)) } if len(uploads) > 0 { - st.PayloadNs += time.Since(payloadStart).Nanoseconds() + st.addPayload(time.Since(payloadStart).Nanoseconds()) } prepareStart = time.Now() for _, u := range uploads { diff --git a/seaweed-block/core/storage/chunk_precopy.go b/seaweed-block/core/storage/chunk_precopy.go index 5d9a1d037c..c3f03d9a1d 100644 --- a/seaweed-block/core/storage/chunk_precopy.go +++ b/seaweed-block/core/storage/chunk_precopy.go @@ -453,6 +453,13 @@ func precopyFixedPartsFromSample(s precopySample) PrecopyFixedParts { // every 0.5 ms). It lowers the rate of an upload measured faster than that. const precopyPayloadClockFloor = time.Millisecond +// addPayload adds one payload interval (d nanoseconds as measured) to the +// cut's payload time, and to its tick-floored sum. +func (st *chunkCutStats) addPayload(d int64) { + st.PayloadNs += d + st.payloadFloorNs += max(d, int64(precopyPayloadClockFloor)) +} + func (l *chunkLayerImpl) samplePrecopy(st chunkCutStats, fixedNs int64) { // A no-op root read is not a measurement of final CAS + confirmation. if st.CASNs <= 0 { @@ -466,10 +473,12 @@ func (l *chunkLayerImpl) samplePrecopy(st chunkCutStats, fixedNs int64) { s := precopySample{at: time.Now(), fixed: time.Duration(max(int64(1), fixedNs))} s.parts = precopyFixedParts(st, s.fixed) if st.UploadedBytes > 0 { - // An upload can also end below the clock's resolution (its payload - // time reads zero): its rate is taken at the resolution, a lower - // bound, so a fast round is never a missing or an infinite sample. - s.rate = float64(st.UploadedBytes) / max(time.Duration(st.PayloadNs), precopyPayloadClockFloor).Seconds() + // Each payload interval can end below the clock's resolution (and + // read zero): the rate divides by the intervals each counted at least + // one tick, a lower bound, so a fast round is never a missing or an + // overstated sample. + div := max(time.Duration(st.payloadFloorNs), time.Duration(st.PayloadNs), precopyPayloadClockFloor) + s.rate = float64(st.UploadedBytes) / div.Seconds() } l.precopySamples = append(l.precopySamples, s) if len(l.precopySamples) > 32 { diff --git a/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go b/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go index 0367b129a7..cd73bb31bc 100644 --- a/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go +++ b/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go @@ -27,3 +27,32 @@ func TestPrecopyRateSampleAtTheClocksResolution(t *testing.T) { t.Fatalf("the clock floor %s is outside any clock's resolution", precopyPayloadClockFloor) } } + +// The payload time sums several intervals (staging, the copy, the upload). +// Three intervals that each ended below the clock's resolution read zero +// although together they took up to three ticks: the rate divides by three +// ticks, never one, so it stays a lower bound. An interval longer than a +// tick counts as measured. +func TestPrecopyRateFloorsEachPayloadInterval(t *testing.T) { + const bytes = 1 << 20 + var st chunkCutStats + st.CASNs, st.CASResult, st.UploadedBytes = 5, "ok", bytes + for i := 0; i < 3; i++ { + st.addPayload(0) // a sub-tick interval, read as zero + } + l := &chunkLayerImpl{} + l.samplePrecopy(st, 1000) + if most := bytes / (3 * precopyPayloadClockFloor).Seconds(); l.precopySamples[0].rate > most+1 { + t.Fatalf("three sub-tick intervals gave rate %.0f B/s, want at most %.0f (bytes over three ticks)", l.precopySamples[0].rate, most) + } + var measured chunkCutStats + measured.CASNs, measured.CASResult, measured.UploadedBytes = 5, "ok", bytes + measured.addPayload(int64(5 * time.Millisecond)) + l.samplePrecopy(measured, 1000) + if want := bytes / (5 * time.Millisecond).Seconds(); l.precopySamples[1].rate < want-1 || l.precopySamples[1].rate > want+1 { + t.Fatalf("a measured 5 ms interval gave rate %.0f B/s, want %.0f", l.precopySamples[1].rate, want) + } + if measured.PayloadNs != int64(5*time.Millisecond) || st.PayloadNs != 0 { + t.Fatalf("the reported payload time changed: %d and %d", measured.PayloadNs, st.PayloadNs) + } +} diff --git a/seaweed-block/core/storage/chunk_seam.go b/seaweed-block/core/storage/chunk_seam.go index dc02640da8..ba12327c37 100644 --- a/seaweed-block/core/storage/chunk_seam.go +++ b/seaweed-block/core/storage/chunk_seam.go @@ -422,6 +422,11 @@ type chunkCutStats struct { NonPayloadCopyNs int64 PayloadNs int64 // disjoint streaming/copy/upload wall intervals + // payloadFloorNs is PayloadNs with each interval counted at least one + // clock tick (precopyPayloadClockFloor): an interval that ended below + // the clock's resolution reads zero, so the rate a sample takes from + // PayloadNs would be too high. Only the pre-copy rate reads it. + payloadFloorNs int64 // Local validated WAL attempts and local copy duration, including savers. COWWALReadAttempts uint64