diff --git a/seaweed-block/core/storage/chunk_head_belt_test.go b/seaweed-block/core/storage/chunk_head_belt_test.go index fafa93406a..5a0b6a9845 100644 --- a/seaweed-block/core/storage/chunk_head_belt_test.go +++ b/seaweed-block/core/storage/chunk_head_belt_test.go @@ -49,6 +49,12 @@ func TestHeadBeltOldCASUnconfirmedKeepsIntentsAndFrontier(t *testing.T) { if got, err := s.Read(0); err != nil || !bytes.Equal(got, want) { t.Fatalf("local acknowledged bytes changed: %v", err) } + if err := layer.Close(); err != nil { + t.Fatal(err) + } + if len(liveIntents(t, f)) == 0 { + t.Fatal("Close removed unconfirmed publication roots") + } } func TestHeadBeltLowerEpochConflictFencesWithoutCleanup(t *testing.T) { @@ -80,4 +86,31 @@ func TestHeadBeltLowerEpochConflictFencesWithoutCleanup(t *testing.T) { if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, ErrChunkLayerFenced) || f.stats().cas != calls { t.Fatalf("fenced owner retried publication: %v", err) } + if err := layer.Close(); err != nil { + t.Fatal(err) + } + if len(liveIntents(t, f)) == 0 { + t.Fatal("Close removed fenced publication roots") + } +} + +func TestHeadBeltTakeoverWaitsBeforeConfirmation(t *testing.T) { + f := newFakeChunkRemote() + f.head = chunkHead{Gen: 1, Epoch: 1, Holder: "old", Chunks: map[uint64]chunkRef{}} + var committedAt time.Time + f.afterCAS = func(f *fakeChunkRemote) { + committedAt = time.Now() + } + ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) + defer cancel() + head, err := ClaimChunkHead(ctx, f, ChunkLayerConfig{Epoch: 2, Holder: "new"}) + if err != nil { + t.Fatal(err) + } + if committedAt.IsZero() || time.Since(committedAt) < 5*time.Second || f.stats().getHeads != 2 { + t.Fatal("takeover served before cooling and its confirmation read") + } + if head.Gen != 2 || head.Epoch != 2 || head.Holder != "new" { + t.Fatal("confirmed the wrong owner", head) + } } diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index f5cbde0d85..861f81f052 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1750,6 +1750,9 @@ func (l *chunkLayerImpl) dropIntents(ctx context.Context, ids []string) { // once, rather than rescanning every cached chunk for each delete. l.stMu.Lock() keep := make(map[string]bool) + for _, id := range l.pendingIntents { + keep[id] = true + } for idx, sc := range l.reusable { if l.closed || l.fenced || sc.ver != l.chunkVersion(idx) || sc.headGen != l.head.Gen { delete(l.reusable, idx)