diff --git a/seaweed-block/core/storage/chunk_precopy.go b/seaweed-block/core/storage/chunk_precopy.go index b18407cf38..61a746c4cd 100644 --- a/seaweed-block/core/storage/chunk_precopy.go +++ b/seaweed-block/core/storage/chunk_precopy.go @@ -371,16 +371,24 @@ func (l *chunkLayerImpl) precopyEstimate(tail uint64, attempts int) PrecopyRepor if len(rates) > 3 { rates = rates[:3] } + if tail == 0 && fixedCount == 0 { + // No bytes remain beyond the published HEAD. There is no upload rate + // to infer; declare the full ordinary allowance and report an overrun + // if the final detach actually exceeds it. + r.Estimate = precopyAllowance + r.Reason = "fit" + return r + } if fixedCount == 0 || r.Fixed <= 0 { return r } r.Estimate = r.Fixed + precopyMargin if tail != 0 { - if len(rates) < 3 { + if len(rates) == 0 { return r } - minRate := rates[len(rates)-3] - for _, rate := range rates[len(rates)-2:] { + minRate := rates[0] + for _, rate := range rates[1:] { if rate < minRate { minRate = rate } @@ -443,7 +451,12 @@ func precopyFixedPartsFromSample(s precopySample) PrecopyFixedParts { 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 { - return + if st.CASNs != 0 || (st.CASResult != "ok" && st.CASResult != "resolved-by-reread") { + return + } + // A successful CAS can complete below the clock's resolution. Its + // measured zero interval is still an observation, unlike a no-op. + st.CASNs = 1 } s := precopySample{at: time.Now(), fixed: time.Duration(max(int64(1), fixedNs))} s.parts = precopyFixedParts(st, s.fixed) diff --git a/seaweed-block/core/storage/chunk_precopy_amendment4_test.go b/seaweed-block/core/storage/chunk_precopy_amendment4_test.go index 7de3cda90d..de34a1582a 100644 --- a/seaweed-block/core/storage/chunk_precopy_amendment4_test.go +++ b/seaweed-block/core/storage/chunk_precopy_amendment4_test.go @@ -40,8 +40,8 @@ func TestPrecopyFixedWindowDecays(t *testing.T) { for i := range l.precopySamples { l.precopySamples[i].at = now.Add(-16 * time.Minute) } - if r := l.precopyEstimate(0, 0); r.Reason != "samples_unknown" { - t.Fatalf("expired samples admitted: %+v", r) + if r := l.precopyEstimate(0, 0); r.Reason != "fit" || r.RateSamples != 0 || r.Estimate != precopyAllowance { + t.Fatalf("empty tail retained expired samples: %+v", r) } l.precopySamples = []precopySample{{at: now, fixed: 100 * time.Millisecond}} if r := l.precopyEstimate(0, 0); r.Fixed != 100*time.Millisecond || r.Reason != "fit" { @@ -107,8 +107,8 @@ func TestPrecopyFixedWindowUsesAvailableFreshSamples(t *testing.T) { } r := l.precopyEstimate(0, 0) if n == 0 { - if r.Reason != "samples_unknown" { - t.Fatalf("zero fresh admitted: %+v", r) + if r.Reason != "fit" || r.RateSamples != 0 || r.Estimate != precopyAllowance { + t.Fatalf("empty tail used an unknown rate: %+v", r) } return } 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 3e42a6e3f7..6dfa04fcf0 100644 --- a/seaweed-block/core/storage/chunk_precopy_cold_estimator_test.go +++ b/seaweed-block/core/storage/chunk_precopy_cold_estimator_test.go @@ -41,6 +41,7 @@ func TestPrecopyColdNonemptyTailUsesFirstMeasuredRound(t *testing.T) { if _, err := s.Write(1, makeBlock(ctBlockSize, 0x72)); err != nil { t.Fatal(err) } + layer.precopySamples = nil rounds := 0 layer.testBeforeBarrierLockHook = func() { rounds++ @@ -65,3 +66,17 @@ func TestPrecopyColdNonemptyTailUsesFirstMeasuredRound(t *testing.T) { t.Fatalf("first measured round did not admit remaining tail: rounds=%d stopped=%d report=%+v err=%v", rounds, stopped, r.Precopy, err) } } + +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)) + 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) + 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_detach_test.go b/seaweed-block/core/storage/chunk_precopy_detach_test.go index 7389efaaaf..5bf588c2b0 100644 --- a/seaweed-block/core/storage/chunk_precopy_detach_test.go +++ b/seaweed-block/core/storage/chunk_precopy_detach_test.go @@ -422,7 +422,7 @@ func precopyAppliedDetachRetryControl(t *testing.T, slow bool) { } } -func TestPrecopyDetachColdNoopRequiresMeasuredCASCost(t *testing.T) { +func TestPrecopyDetachColdNoopUsesEmptyTailFact(t *testing.T) { s, _ := newChunkTestStore(t) f := newFakeChunkRemote() f.seedHead(t, make([]byte, ctNumBlocks*ctBlockSize), 0, 1) @@ -431,22 +431,12 @@ func TestPrecopyDetachColdNoopRequiresMeasuredCASCost(t *testing.T) { cfg.FreshFromHead = true l := mustAttach(t, s, f, cfg).(*chunkLayerImpl) stopped := 0 - _, err := l.PrecopyDetach(ctTimeout(t), func() bool { stopped++; return true }) - if !errors.Is(err, ErrPrecopyUnknown) || stopped != 0 { - t.Fatalf("no-op read treated as measured CAS cost: stopped=%d err=%v", stopped, err) - } - if f.stats().head.Gen != 1 { - t.Fatal("unknown cost published a final detached HEAD") - } - if _, err := s.Write(1, makeBlock(ctBlockSize, 0x61)); err != nil { - t.Fatal(err) - } - if _, err := l.Cut(ctTimeout(t)); err != nil { - t.Fatal(err) - } r, err := l.PrecopyDetach(ctTimeout(t), func() bool { stopped++; return true }) - if err != nil || !r.Precopy.Frozen || stopped != 1 { - t.Fatalf("measured CAS did not enable empty-tail freeze: report=%+v stopped=%d err=%v", r.Precopy, stopped, err) + if err != nil || !r.Precopy.Frozen || stopped != 1 || r.Precopy.TailBytes != 0 || r.Precopy.RateSamples != 0 { + t.Fatalf("empty tail needed a rate sample: report=%+v stopped=%d err=%v", r.Precopy, stopped, err) + } + if f.stats().head.State != ChunkHeadDetached { + t.Fatal("empty-tail detach did not publish") } }