From 3c97bc1b27a57b598acb56fe5de16d6a1b7518c3 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 21:21:38 -0800 Subject: [PATCH 1/2] test(storage): a cut attempt that runs out of time leaves uploads the next attempt keeps At 262,144 changed chunks, a cut whose context ends after a third of its uploads leaves HEAD unchanged; the next attempt uploads only the chunks not yet uploaded and sends none again. Fails when uploads are not remembered across attempts (the next attempt uploads all 262,144). Co-Authored-By: Claude Opus 5.5 --- .../chunk_cut_timeout_progress_test.go | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 seaweed-block/core/storage/chunk_cut_timeout_progress_test.go diff --git a/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go b/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go new file mode 100644 index 0000000000..68ceba9746 --- /dev/null +++ b/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go @@ -0,0 +1,136 @@ +package storage + +import ( + "context" + "errors" + "path/filepath" + "sync" + "testing" + "time" +) + +// oneTiBChunks is the chunk count of a 1 TiB volume with 4 MiB chunks. +const oneTiBChunks = 262144 + +// expiringRemote counts every PUT and runs out the attempt's time once a +// given number of PUTs completed: it cancels the attempt's context, as the +// control operation's limit does when it expires. +type expiringRemote struct { + *fakeChunkRemote + mu sync.Mutex + expire context.CancelFunc // nil: never + after int + completed int + attempts int + dups int + done map[string]bool +} + +func (r *expiringRemote) Put(ctx context.Context, key string, b []byte) error { + r.mu.Lock() + r.attempts++ + if r.done[key] { + r.dups++ + } + if r.expire != nil && r.completed >= r.after { + r.expire() + } + r.mu.Unlock() + if err := ctx.Err(); err != nil { + return err + } + if err := r.fakeChunkRemote.Put(ctx, key, b); err != nil { + return err + } + r.mu.Lock() + r.done[key] = true + r.completed++ + r.mu.Unlock() + return nil +} + +func (r *expiringRemote) counts() (attempts, completed, dups int) { + r.mu.Lock() + defer r.mu.Unlock() + return r.attempts, r.completed, r.dups +} + +// A cut of a 1 TiB volume (262,144 changed chunks) whose attempt runs out +// of its time limit part way through the uploads leaves the chunks it +// uploaded: the next attempt uploads only the rest and sends no completed +// chunk again. The time limit around the operation is judged by progress: +// work that grows with the volume may take more than one attempt, and no +// attempt redoes what an earlier one finished. +func TestACutAttemptThatRunsOutOfTimeLeavesUploadsTheNextAttemptKeeps(t *testing.T) { + if testing.Short() { + t.Skip("writes 262,144 chunks") + } + path := filepath.Join(t.TempDir(), "vol.bin") + s, err := CreateWALStoreWithOptions(path, oneTiBChunks, ctBlockSize, WALStoreOptions{ChunkBlocks: 1}) + if err != nil { + t.Fatal(err) + } + s.DisableAutoFlushForRecoveryTest() + t.Cleanup(func() { closeBounded(t, s) }) + f := newFakeChunkRemote() + r := &expiringRemote{fakeChunkRemote: f, done: map[string]bool{}} + cfg := ctConfig(1) + cfg.ChunkBlocks = 1 + cfg.FreshFromHead = true + l := mustAttach(t, s, r, cfg).(*chunkLayerImpl) + if _, err := l.Cut(ctTimeout(t)); err != nil { // the zero baseline + t.Fatal(err) + } + setup := time.Now() + for c := uint32(0); c < oneTiBChunks; c++ { + if _, err := s.Write(c, makeBlock(ctBlockSize, byte(c%251+1))); err != nil { + t.Fatal(err) + } + if c%8192 == 8191 { + mustSyncFlush(t, s) + } + } + mustSyncFlush(t, s) + t.Logf("wrote %d chunks in %v", oneTiBChunks, time.Since(setup).Round(time.Millisecond)) + base := f.stats().head.Gen + + const ranOut = oneTiBChunks / 3 + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + r.mu.Lock() + r.expire, r.after = cancel, ranOut + r.mu.Unlock() + start := time.Now() + if _, err := l.Cut(ctx); !errors.Is(err, context.Canceled) { + t.Fatalf("the attempt that ran out of time: %v; want its context's end", err) + } + if f.stats().head.Gen != base { + t.Fatal("the attempt that ran out of time moved HEAD") + } + _, kept, _ := r.counts() + t.Logf("first attempt: %d chunks uploaded before its time ran out (%v)", kept, time.Since(start).Round(time.Millisecond)) + if kept < ranOut { + t.Fatalf("setup: %d uploads completed before the time ran out, want at least %d", kept, ranOut) + } + + r.mu.Lock() + r.expire, r.attempts, r.completed, r.dups = nil, 0, 0, 0 + r.mu.Unlock() + start = time.Now() + res, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatalf("the next attempt: %v", err) + } + attempts, completed, dups := r.counts() + t.Logf("next attempt: %d uploads (%d sent again) in %v", completed, dups, time.Since(start).Round(time.Millisecond)) + if res.Head.Gen != base+1 { + t.Fatalf("the next attempt published generation %d, want %d", res.Head.Gen, base+1) + } + if dups != 0 { // a resend under the same key; a resend under a new key is caught by the count below + t.Fatalf("the next attempt sent %d chunks the attempt that ran out of time had uploaded", dups) + } + if want := oneTiBChunks - kept; completed != want || attempts != want { + t.Fatalf("the next attempt uploaded %d chunks (%d attempts); want only the %d not yet uploaded", completed, attempts, want) + } + assertNoSessionOpen(t, f) +} From 3ad4bf20798a98f93a26a26ae651b9d6a524dbac Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 21:33:21 -0800 Subject: [PATCH 2/2] test(storage): the timed-out cut attempt ends by deadline and by cancel; the published image is checked The first attempt is ended both by a real context deadline and by a cancel, and must return within a bound. The next attempt's HEAD is read back: every one of the 262,144 chunks named with its written bytes, the frontier, and every chunk the first attempt uploaded; a control drops one of those and is refused. Co-Authored-By: Claude Opus 5.5 --- .../chunk_cut_timeout_progress_test.go | 169 +++++++++++++++--- 1 file changed, 147 insertions(+), 22 deletions(-) diff --git a/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go b/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go index 68ceba9746..f57f0313ef 100644 --- a/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go +++ b/seaweed-block/core/storage/chunk_cut_timeout_progress_test.go @@ -1,8 +1,10 @@ package storage import ( + "bytes" "context" "errors" + "fmt" "path/filepath" "sync" "testing" @@ -12,13 +14,16 @@ import ( // oneTiBChunks is the chunk count of a 1 TiB volume with 4 MiB chunks. const oneTiBChunks = 262144 -// expiringRemote counts every PUT and runs out the attempt's time once a -// given number of PUTs completed: it cancels the attempt's context, as the -// control operation's limit does when it expires. +// expiringRemote counts every PUT. Once a given number of PUTs completed it +// ends the attempt: with expire set it cancels the attempt's context; in any +// case every later PUT waits for the attempt's context to end (its deadline +// or that cancel) and returns its error, as an upload cut off by the +// operation's time limit. type expiringRemote struct { *fakeChunkRemote mu sync.Mutex - expire context.CancelFunc // nil: never + gated bool + expire context.CancelFunc // nil: wait for the context's own deadline after int completed int attempts int @@ -32,10 +37,15 @@ func (r *expiringRemote) Put(ctx context.Context, key string, b []byte) error { if r.done[key] { r.dups++ } - if r.expire != nil && r.completed >= r.after { + cutOff := r.gated && r.completed >= r.after + if cutOff && r.expire != nil { r.expire() } r.mu.Unlock() + if cutOff { + <-ctx.Done() + return ctx.Err() + } if err := ctx.Err(); err != nil { return err } @@ -49,22 +59,91 @@ func (r *expiringRemote) Put(ctx context.Context, key string, b []byte) error { return nil } +func (r *expiringRemote) reset() { + r.mu.Lock() + defer r.mu.Unlock() + r.gated, r.expire, r.attempts, r.completed, r.dups = false, nil, 0, 0, 0 +} + func (r *expiringRemote) counts() (attempts, completed, dups int) { r.mu.Lock() defer r.mu.Unlock() return r.attempts, r.completed, r.dups } +func (r *expiringRemote) completedKeys() map[string]bool { + r.mu.Lock() + defer r.mu.Unlock() + out := make(map[string]bool, len(r.done)) + for k := range r.done { + out[k] = true + } + return out +} + +// timeoutVolumeBlock is chunk c's content: nonzero, so no chunk is elided. +func timeoutVolumeBlock(c uint32) []byte { return makeBlock(ctBlockSize, byte(c%251+1)) } + +// checkPublishedVolume reports how h differs from the volume every chunk of +// which was written once with timeoutVolumeBlock up to LSN frontier: every +// index named, each named object stored with that chunk's bytes, the +// frontier, and every key in kept named by h. +func checkPublishedVolume(f *fakeChunkRemote, h chunkHead, frontier uint64, kept map[string]bool) error { + if h.RemoteLSN != frontier { + return fmt.Errorf("HEAD frontier %d, want %d", h.RemoteLSN, frontier) + } + if len(h.Chunks) != oneTiBChunks { + return fmt.Errorf("HEAD names %d chunks, want %d", len(h.Chunks), oneTiBChunks) + } + named := make(map[string]bool, len(h.Chunks)) + f.mu.Lock() + defer f.mu.Unlock() + for c := uint32(0); c < oneTiBChunks; c++ { + ref, ok := h.Chunks[uint64(c)] + if !ok { + return fmt.Errorf("HEAD does not name chunk %d", c) + } + b, ok := f.objs[ref.Key] + if !ok { + return fmt.Errorf("HEAD names chunk %d key %q that is not stored", c, ref.Key) + } + if !bytes.Equal(b, timeoutVolumeBlock(c)) { + return fmt.Errorf("chunk %d (key %q) does not hold its written bytes", c, ref.Key) + } + named[ref.Key] = true + } + for k := range kept { + if !named[k] { + return fmt.Errorf("a chunk uploaded by the attempt that ran out of time (key %q) is not named by HEAD", k) + } + } + return nil +} + // A cut of a 1 TiB volume (262,144 changed chunks) whose attempt runs out -// of its time limit part way through the uploads leaves the chunks it -// uploaded: the next attempt uploads only the rest and sends no completed -// chunk again. The time limit around the operation is judged by progress: -// work that grows with the volume may take more than one attempt, and no -// attempt redoes what an earlier one finished. +// of time part way through its uploads leaves the chunks it uploaded: the +// next attempt uploads only the rest, sends no completed chunk again, and +// publishes the whole volume, the first attempt's uploads included. The +// time limit around the operation is judged by progress: work that grows +// with the volume may take more than one attempt, and no attempt redoes what +// an earlier one finished. +// +// The attempt is ended both by its context's deadline passing and by a +// cancel; the upload and capture path treats the two alike. What the first +// attempt keeps is kept in this process's memory: a restart between the +// attempts is another case, not shown here (the cut then uploads again what +// it cannot prove was uploaded, which costs time and nothing else, since +// names are by content). func TestACutAttemptThatRunsOutOfTimeLeavesUploadsTheNextAttemptKeeps(t *testing.T) { if testing.Short() { t.Skip("writes 262,144 chunks") } + for _, end := range []string{"deadline", "cancel"} { + t.Run(end, func(t *testing.T) { cutAttemptRunsOutOfTime(t, end) }) + } +} + +func cutAttemptRunsOutOfTime(t *testing.T, end string) { path := filepath.Join(t.TempDir(), "vol.bin") s, err := CreateWALStoreWithOptions(path, oneTiBChunks, ctBlockSize, WALStoreOptions{ChunkBlocks: 1}) if err != nil { @@ -82,8 +161,9 @@ func TestACutAttemptThatRunsOutOfTimeLeavesUploadsTheNextAttemptKeeps(t *testing t.Fatal(err) } setup := time.Now() + var frontier uint64 for c := uint32(0); c < oneTiBChunks; c++ { - if _, err := s.Write(c, makeBlock(ctBlockSize, byte(c%251+1))); err != nil { + if frontier, err = s.Write(c, timeoutVolumeBlock(c)); err != nil { t.Fatal(err) } if c%8192 == 8191 { @@ -94,28 +174,57 @@ func TestACutAttemptThatRunsOutOfTimeLeavesUploadsTheNextAttemptKeeps(t *testing t.Logf("wrote %d chunks in %v", oneTiBChunks, time.Since(setup).Round(time.Millisecond)) base := f.stats().head.Gen - const ranOut = oneTiBChunks / 3 - ctx, cancel := context.WithCancel(context.Background()) + // The first attempt: after a part of the uploads every further upload + // waits until the attempt's context ends. Its deadline is short and + // real; the cancel case ends it at once. Either way the attempt must + // return within a generous bound, or the test fails here. + const cutOffAfter = oneTiBChunks / 8 + var ctx context.Context + var cancel context.CancelFunc + want := context.Canceled + if end == "deadline" { + ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second) + want = context.DeadlineExceeded + } else { + ctx, cancel = context.WithCancel(context.Background()) + } defer cancel() r.mu.Lock() - r.expire, r.after = cancel, ranOut + r.gated, r.after = true, cutOffAfter + if end == "cancel" { + r.expire = cancel + } r.mu.Unlock() start := time.Now() - if _, err := l.Cut(ctx); !errors.Is(err, context.Canceled) { - t.Fatalf("the attempt that ran out of time: %v; want its context's end", err) + errc := make(chan error, 1) + go func() { + _, err := l.Cut(ctx) + errc <- err + }() + select { + case err := <-errc: + if !errors.Is(err, want) { + t.Fatalf("the attempt that ran out of time: %v; want %v", err, want) + } + case <-time.After(3 * time.Minute): + cancel() + select { + case <-errc: + case <-time.After(30 * time.Second): + } + t.Fatal("the attempt did not return within 3 minutes of its start") } if f.stats().head.Gen != base { t.Fatal("the attempt that ran out of time moved HEAD") } _, kept, _ := r.counts() - t.Logf("first attempt: %d chunks uploaded before its time ran out (%v)", kept, time.Since(start).Round(time.Millisecond)) - if kept < ranOut { - t.Fatalf("setup: %d uploads completed before the time ran out, want at least %d", kept, ranOut) + keptKeys := r.completedKeys() + t.Logf("first attempt (%s): %d chunks uploaded before its time ran out (%v)", end, kept, time.Since(start).Round(time.Millisecond)) + if kept == 0 || kept >= oneTiBChunks { + t.Fatalf("setup: %d uploads completed before the time ran out, want some but not all", kept) } - r.mu.Lock() - r.expire, r.attempts, r.completed, r.dups = nil, 0, 0, 0 - r.mu.Unlock() + r.reset() start = time.Now() res, err := l.Cut(ctTimeout(t)) if err != nil { @@ -132,5 +241,21 @@ func TestACutAttemptThatRunsOutOfTimeLeavesUploadsTheNextAttemptKeeps(t *testing if want := oneTiBChunks - kept; completed != want || attempts != want { t.Fatalf("the next attempt uploaded %d chunks (%d attempts); want only the %d not yet uploaded", completed, attempts, want) } + head := f.stats().head + if err := checkPublishedVolume(f, head, frontier, keptKeys); err != nil { + t.Fatalf("published HEAD: %v", err) + } + // Control: the check refuses a HEAD that drops one chunk the first + // attempt uploaded, so it measures the kept uploads reaching HEAD. + dropped := copyChunkHead(head) + for idx, ref := range dropped.Chunks { + if keptKeys[ref.Key] { + delete(dropped.Chunks, idx) + break + } + } + if err := checkPublishedVolume(f, dropped, frontier, keptKeys); err == nil { + t.Fatal("control: the check passed a HEAD without one of the first attempt's uploads") + } assertNoSessionOpen(t, f) }