diff --git a/seaweed-block/core/storage/chunk_precopy.go b/seaweed-block/core/storage/chunk_precopy.go index 61a746c4cd..5d9a1d037c 100644 --- a/seaweed-block/core/storage/chunk_precopy.go +++ b/seaweed-block/core/storage/chunk_precopy.go @@ -448,6 +448,11 @@ func precopyFixedPartsFromSample(s precopySample) PrecopyFixedParts { return p } +// 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 + 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 { @@ -460,8 +465,11 @@ 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 { + // 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() } 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 new file mode 100644 index 0000000000..0367b129a7 --- /dev/null +++ b/seaweed-block/core/storage/chunk_precopy_rate_floor_test.go @@ -0,0 +1,29 @@ +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) + 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) + 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) + } +}