diff --git a/seaweed-block/core/storage/walstore_bounded_flush_v3_test.go b/seaweed-block/core/storage/walstore_bounded_flush_v3_test.go index 0f989241f2..a2f20da1c4 100644 --- a/seaweed-block/core/storage/walstore_bounded_flush_v3_test.go +++ b/seaweed-block/core/storage/walstore_bounded_flush_v3_test.go @@ -6,6 +6,7 @@ import ( "math" "math/rand" "path/filepath" + "sort" "testing" "time" ) @@ -181,11 +182,11 @@ func TestWALStoreV3DrainHeldIsTheFlusherPhase(t *testing.T) { } // A dense image written as single 4 KiB writes in random order, three -// WALs' worth, on a slow disk (every extent write takes 2 ms), with a 500 ms admission -// budget. A whole-map cycle (about 480 entries, about 1 s) frees nothing -// until it ends, so a write times out waiting for space; bounded cycles free -// space every sixteenth of the WAL, and no write times out while reclaim -// moves. The guest sees a slow disk, not errors. +// WALs' worth, on a slow disk (every extent write takes 5 ms), with an +// admission budget of half a whole-map cycle. A whole-map cycle (about 480 +// entries) frees nothing until it ends, so a write times out waiting for +// space; bounded cycles free space every sixteenth of the WAL, and no write +// times out while reclaim moves. The guest sees a slow disk, not errors. func TestWALStoreV3SlowExtentKeepsReclaimMoving(t *testing.T) { const walSize = 2 << 20 const count = 3 * walSize / 4096 @@ -195,8 +196,17 @@ func TestWALStoreV3SlowExtentKeepsReclaimMoving(t *testing.T) { t.Fatal(err) } t.Cleanup(func() { _ = s.Close() }) - s.admissionTimeout = 500 * time.Millisecond - s.flusher.beforeExtentWriteForTest = func() { time.Sleep(2 * time.Millisecond) } + // The slow disk is a 5 ms sleep per extent write. The admission budget is + // half a whole-map cycle (about 480 records), from the sleep as measured + // on this host: a whole-map cycle still outlasts it (the red), while a + // bounded cycle (a sixteenth of this WAL, about 32 records) leaves room + // for a slow fsync on a busy host (the budget is about 1.2 s, the bounded + // cycle's writes about 160 ms). + const delay = 5 * time.Millisecond + sleep := measuredSleep(delay) + s.admissionTimeout = 240 * sleep + t.Logf("measured sleep %v: admission budget %v", sleep, s.admissionTimeout) + s.flusher.beforeExtentWriteForTest = func() { time.Sleep(delay) } lbas := rand.New(rand.NewSource(1)).Perm(count) start := time.Now() busy := 0 @@ -528,3 +538,15 @@ func TestWALStoreV3HeldDrainEndsInTheProductionDrain(t *testing.T) { } } } + +// measuredSleep is the median time a sleep of d actually takes on this host. +func measuredSleep(d time.Duration) time.Duration { + var got [9]time.Duration + for i := range got { + start := time.Now() + time.Sleep(d) + got[i] = time.Since(start) + } + sort.Slice(got[:], func(i, j int) bool { return got[i] < got[j] }) + return got[len(got)/2] +}