diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index c4b47e1191..cb1cf4ed23 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1133,6 +1133,13 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch start := time.Now() recoveredPublication := l.pending != nil var st chunkCutStats + // payloadFloor is the cut's payload intervals each floored at a clock + // tick, for the pre-copy rate only (st.PayloadNs stays as measured). + var payloadFloor int64 + addPayload := func(d int64) { + st.PayloadNs += d + payloadFloor += flooredPayload(d) + } work := &cutAttemptObservation{} ctx = context.WithValue(ctx, cutObservationKey{}, work) persistCapturedMap := func(versions map[uint64]uint64) { @@ -1179,7 +1186,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch res.Stats = st if !pauseStart.IsZero() { fixed := time.Since(pauseStart).Nanoseconds() - st.PayloadNs + payloadBeforeBarrier - l.samplePrecopy(st, fixed) + l.samplePrecopy(st, fixed, payloadFloor) } res.CandidateTail = l.estimateCandidateTail(tailBytes) } @@ -1266,7 +1273,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() + addPayload(time.Since(stageStart).Nanoseconds()) if err != nil { return chunkCutResult{}, err } @@ -1450,7 +1457,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 + addPayload(st.CopyNs) } else { st.NonPayloadCopyNs += st.CopyNs } @@ -1533,7 +1540,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() + 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 61a746c4cd..a55ffda7f9 100644 --- a/seaweed-block/core/storage/chunk_precopy.go +++ b/seaweed-block/core/storage/chunk_precopy.go @@ -448,7 +448,20 @@ func precopyFixedPartsFromSample(s precopySample) PrecopyFixedParts { return p } -func (l *chunkLayerImpl) samplePrecopy(st chunkCutStats, fixedNs int64) { +// precopyPayloadClockFloor is the least payload time a rate sample divides +// by: the coarsest clock resolution a host may have (Windows ticks about +// every 0.5 ms). It lowers the rate of an upload measured faster than that. +const precopyPayloadClockFloor = time.Millisecond + +// flooredPayload is one payload interval (d nanoseconds as measured) counted +// at least one clock tick: an interval that ended below the clock's +// resolution reads zero, so a rate taken from the raw sum would be too high. +func flooredPayload(d int64) int64 { return max(d, int64(precopyPayloadClockFloor)) } + +// samplePrecopy records one cut's pre-copy sample. payloadFloorNs is the +// cut's payload intervals each floored (flooredPayload), which the rate +// divides by; st.PayloadNs stays the measured sum the cut reports. +func (l *chunkLayerImpl) samplePrecopy(st chunkCutStats, fixedNs, payloadFloorNs int64) { // A no-op root read is not a measurement of final CAS + confirmation. if st.CASNs <= 0 { if st.CASNs != 0 || (st.CASResult != "ok" && st.CASResult != "resolved-by-reread") { @@ -460,8 +473,13 @@ 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 && st.PayloadNs > 0 { - s.rate = float64(st.UploadedBytes) / time.Duration(st.PayloadNs).Seconds() + if st.UploadedBytes > 0 { + // 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(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_cold_estimator_test.go b/seaweed-block/core/storage/chunk_precopy_cold_estimator_test.go index 6dfa04fcf0..894bab1150 100644 --- a/seaweed-block/core/storage/chunk_precopy_cold_estimator_test.go +++ b/seaweed-block/core/storage/chunk_precopy_cold_estimator_test.go @@ -70,12 +70,12 @@ func TestPrecopyColdNonemptyTailUsesFirstMeasuredRound(t *testing.T) { func TestPrecopySuccessfulCASBelowClockResolutionIsMeasured(t *testing.T) { layer := &chunkLayerImpl{} st := chunkCutStats{CASResult: "ok", CASNs: 0, UploadedBytes: ctChunkBytes, PayloadNs: int64(10 * time.Millisecond)} - layer.samplePrecopy(st, int64(20*time.Millisecond)) + layer.samplePrecopy(st, int64(20*time.Millisecond), 0) if len(layer.precopySamples) != 1 || layer.precopySamples[0].rate <= 0 { t.Fatalf("successful zero-ns CAS lost its measured upload: %+v", layer.precopySamples) } - layer.samplePrecopy(chunkCutStats{CASNs: 0, UploadedBytes: ctChunkBytes, PayloadNs: st.PayloadNs}, st.PayloadNs) - layer.samplePrecopy(chunkCutStats{CASResult: "precondition", CASNs: 0, UploadedBytes: ctChunkBytes, PayloadNs: st.PayloadNs}, st.PayloadNs) + layer.samplePrecopy(chunkCutStats{CASNs: 0, UploadedBytes: ctChunkBytes, PayloadNs: st.PayloadNs}, st.PayloadNs, 0) + layer.samplePrecopy(chunkCutStats{CASResult: "precondition", CASNs: 0, UploadedBytes: ctChunkBytes, PayloadNs: st.PayloadNs}, st.PayloadNs, 0) if len(layer.precopySamples) != 1 { t.Fatalf("no-op or refused CAS became a sample: %+v", layer.precopySamples) } diff --git a/seaweed-block/core/storage/chunk_precopy_fixed_commit_test.go b/seaweed-block/core/storage/chunk_precopy_fixed_commit_test.go index 53d4b89ac8..2b692cf1e2 100644 --- a/seaweed-block/core/storage/chunk_precopy_fixed_commit_test.go +++ b/seaweed-block/core/storage/chunk_precopy_fixed_commit_test.go @@ -112,11 +112,11 @@ func TestPrecopyFixedPartsStayWithLargestObservation(t *testing.T) { _, _, l, _ := precopyLayer(t, 1024, 0) l.precopySamples = nil for i := 0; i < 3; i++ { - l.samplePrecopy(chunkCutStats{CASNs: 1}, int64(time.Millisecond)) + l.samplePrecopy(chunkCutStats{CASNs: 1}, int64(time.Millisecond), 0) } st := chunkCutStats{CASNs: 1, DrainNs: int64(10 * time.Millisecond), LockWaitNs: int64(30 * time.Millisecond), SyncNs: int64(40 * time.Millisecond), CASWriteNs: int64(50 * time.Millisecond), HeadReadbackNs: int64(60 * time.Millisecond), MapPersistNs: int64(10 * time.Millisecond)} - l.samplePrecopy(st, int64(200*time.Millisecond)) - l.samplePrecopy(chunkCutStats{CASNs: 1, SyncNs: int64(100 * time.Millisecond)}, int64(150*time.Millisecond)) + l.samplePrecopy(st, int64(200*time.Millisecond), 0) + l.samplePrecopy(chunkCutStats{CASNs: 1, SyncNs: int64(100 * time.Millisecond)}, int64(150*time.Millisecond), 0) r := l.precopyEstimate(0, 0) p := r.FixedParts if r.Fixed != 200*time.Millisecond || p.Drain != 10*time.Millisecond || p.StoreLock != 20*time.Millisecond || p.DataSync != 40*time.Millisecond || p.HeadCAS != 50*time.Millisecond || p.HeadReadback != 60*time.Millisecond || p.MapPersist != 10*time.Millisecond || p.Other != 10*time.Millisecond { diff --git a/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go b/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go new file mode 100644 index 0000000000..09b386b4d3 --- /dev/null +++ b/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go @@ -0,0 +1,59 @@ +package storage + +import ( + "testing" + "time" +) + +// A round that uploaded bytes in less time than the clock resolves (its +// payload time reads zero) is still a rate sample: its rate is taken at the +// clock's resolution, a lower bound, never missing and never infinite. A +// round that uploaded nothing gives no rate, as before. +func TestPrecopyRateSampleAtTheClocksResolution(t *testing.T) { + l := &chunkLayerImpl{} + l.samplePrecopy(chunkCutStats{CASNs: 5, CASResult: "ok", UploadedBytes: 16384, PayloadNs: 0}, 1000, 0) + if len(l.precopySamples) != 1 { + t.Fatalf("%d samples, want 1", len(l.precopySamples)) + } + rate := l.precopySamples[0].rate + if most := 16384 / precopyPayloadClockFloor.Seconds(); rate <= 0 || rate > most { + t.Fatalf("a sub-resolution upload's rate is %.0f B/s, want above 0 and at most %.0f (its bytes over the clock's resolution)", rate, most) + } + l.samplePrecopy(chunkCutStats{CASNs: 5, CASResult: "ok", UploadedBytes: 0, PayloadNs: 0}, 1000, 0) + if r := l.precopySamples[1].rate; r != 0 { + t.Fatalf("a round that uploaded nothing gave rate %.0f, want none", r) + } + if precopyPayloadClockFloor < time.Microsecond || precopyPayloadClockFloor > 10*time.Millisecond { + 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 + var floor int64 + for i := 0; i < 3; i++ { + floor += flooredPayload(0) // a sub-tick interval, read as zero + } + l := &chunkLayerImpl{} + l.samplePrecopy(st, 1000, floor) + 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.PayloadNs = int64(5 * time.Millisecond) + l.samplePrecopy(measured, 1000, flooredPayload(measured.PayloadNs)) + 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_precopy_sample_bound_test.go b/seaweed-block/core/storage/chunk_precopy_sample_bound_test.go index 5be9c0093f..bd87e33ae4 100644 --- a/seaweed-block/core/storage/chunk_precopy_sample_bound_test.go +++ b/seaweed-block/core/storage/chunk_precopy_sample_bound_test.go @@ -12,7 +12,7 @@ func TestPrecopySampleHistoryKeepsNewest32(t *testing.T) { const taken = 40 for i := 1; i <= taken; i++ { // The fixed cost carries the sample's order. - l.samplePrecopy(chunkCutStats{CASNs: 1}, int64(i)) + l.samplePrecopy(chunkCutStats{CASNs: 1}, int64(i), 0) } if len(l.precopySamples) != 32 { t.Fatalf("sample history holds %d entries, want 32", len(l.precopySamples))