diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 4cefc76529..08a2714ae9 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1717,11 +1717,13 @@ func (l *chunkLayerImpl) SettleDetach(ctx context.Context) (settled detachSettle l.stMu.Lock() fenced, pending, isDetach, base := l.fenced, l.pending, l.pendingDetach, l.pendingBase changes, marked := l.pendingChanges, l.pendingMarked - baseHead := l.wholeHeadLocked() + baseHead := l.cutBaseLocked(nil) // the cancel's base: identity only on the v1.1 format, whole before l.stMu.Unlock() if fenced { return detachFenced, chunkCutResult{}, nil } + // cancelled: this settlement's cancel CAS landed; cur is that cancel. + cancelled := false if pending == nil || !isDetach { // Nothing of this detach can land: it failed before its CAS or was // definitely refused (an unresolved ordinary cut settles as usual). @@ -1738,13 +1740,21 @@ func (l *chunkLayerImpl) SettleDetach(ctx context.Context) (settled detachSettle cancel := copyHead(baseHead) cancel.Epoch, cancel.Holder = l.cfg.Epoch, l.cfg.Holder cancel.State, cancel.DetachedBy = chunkHeadActive, nil - if err := l.checkCommitFirst(ctx, base, cancel); err != nil { + // On the v1.1 format the cancel is a commit by change with no + // change: the base's chunks stay as HEAD has them; only the identity + // and the state move (nil changes: the whole base image, before). + var none map[uint64]*chunkRef + if changes != nil { + none = map[uint64]*chunkRef{} + } + if err := l.checkCommitFirstBy(ctx, base, proposalOf(cancel, none), none); err != nil { return 0, chunkCutResult{}, fmt.Errorf("storage: detach outcome unresolved (%w); cancel check failed: %w", errChunkCASOutcomeUnknown, err) } - gen, casErr := casAndConfirmChunkHead(ctx, l.remote, base, cancel) + gen, casErr := casAndConfirmChunkHeadBy(ctx, l.remote, base, proposalOf(cancel, none), none, nil) if casErr == nil { cancel.Gen = gen cur = cancel + cancelled = none != nil } else if errors.Is(casErr, errChunkLayerFenced) { l.fencePublication(chunkHead{}) return detachFenced, chunkCutResult{}, nil @@ -1782,6 +1792,12 @@ func (l *chunkLayerImpl) SettleDetach(ctx context.Context) (settled detachSettle l.fenced = true l.setHeadLocked(copyHead(cur)) return detachFenced, chunkCutResult{}, nil + case cancelled: + // The cancel by change landed: HEAD is the base's chunks at this + // holder's identity, active. Installed in place (identity only); its + // chunks are the table's already. + l.applyCommitLocked(cur, nil) + return detachNotPublished, chunkCutResult{}, nil default: if l.adoptHeadLocked(cur) != nil { return detachFenced, chunkCutResult{}, nil diff --git a/seaweed-block/core/storage/chunk_v11_outcomes_test.go b/seaweed-block/core/storage/chunk_v11_outcomes_test.go index c4e660e6f9..b87ebf5251 100644 --- a/seaweed-block/core/storage/chunk_v11_outcomes_test.go +++ b/seaweed-block/core/storage/chunk_v11_outcomes_test.go @@ -201,3 +201,78 @@ func TestV11NoOpCutFencesAForeignRootAtItsOwnIdentity(t *testing.T) { } }) } + +// unresolvedV11Detach is a v1.1 layer with chunk 0 published and a detach +// (writing chunk 2) whose CAS by change was lost before it applied: HEAD +// is still the detach's base, and the outcome is unresolved. +func unresolvedV11Detach(t *testing.T) (*chunkLayerImpl, *deltaSpyRemote, chunkHead) { + t.Helper() + withV11Format(t) + s, l, spy := deltaSpyLayer(t) + w := newWriteLog() + w.write(t, s, 0, 0x10) + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + base, err := spy.GetHead(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + w.write(t, s, 2*ctChunkBlocks, 0x22) + spy.mu.Lock() + spy.lose = true + spy.mu.Unlock() + if _, err := l.Detach(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) { + t.Fatalf("detach with a lost CAS: %v, want an unresolved outcome", err) + } + return l, spy, base +} + +// On the v1.1 format a detach settlement whose detach did not land cancels +// it with a commit by change carrying no change, at the detach's base: HEAD +// keeps the base's chunks and returns to this holder's active identity. A +// cancel that lands is installed in place (the layer stays unfenced and its +// table is the remote's HEAD); a cancel whose reply is lost after it applied +// is read back and adopted, as before. +func TestV11DetachCancelGoesByChangeAndInstallsInPlace(t *testing.T) { + settleAndCheck := func(t *testing.T, l *chunkLayerImpl, spy *deltaSpyRemote, base chunkHead) { + t.Helper() + settled, _, err := l.SettleDetach(ctTimeout(t)) + if err != nil || settled != detachNotPublished { + t.Fatalf("SettleDetach: %v %v, want the detach cancelled (not published)", settled, err) + } + spy.mu.Lock() + last, full := spy.deltas[len(spy.deltas)-1], spy.fullCAS + spy.mu.Unlock() + if len(last) != 0 || full != 0 { + t.Fatalf("the cancel carried %d changes and %d whole-image CASes; want one commit by change with none", len(last), full) + } + head, err := spy.GetHead(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + if head.Gen != base.Gen+1 || chunkHeadIsDetached(head) || head.Holder != base.Holder || !reflect.DeepEqual(head.Chunks, base.Chunks) { + t.Fatalf("HEAD after the cancel: gen %d detached %v holder %q, %d chunks; want gen %d active with the base's %d chunks", head.Gen, chunkHeadIsDetached(head), head.Holder, len(head.Chunks), base.Gen+1, len(base.Chunks)) + } + l.stMu.Lock() + fenced, whole := l.fenced, l.wholeHeadLocked() + l.stMu.Unlock() + if fenced { + t.Fatal("a cancelled detach fenced the layer") + } + if whole.Gen != head.Gen || !reflect.DeepEqual(whole.Chunks, head.Chunks) { + t.Fatalf("the layer's HEAD after the cancel is gen %d with %d chunks; the remote's gen %d with %d", whole.Gen, len(whole.Chunks), head.Gen, len(head.Chunks)) + } + } + t.Run("cancel lands", func(t *testing.T) { + l, spy, base := unresolvedV11Detach(t) + settleAndCheck(t, l, spy, base) + }) + t.Run("cancel reply lost after it applied", func(t *testing.T) { + l, spy, base := unresolvedV11Detach(t) + spy.fakeChunkRemote.mu.Lock() + spy.fakeChunkRemote.casModes = []fakeCASMode{casAppliedLost} + spy.fakeChunkRemote.mu.Unlock() + settleAndCheck(t, l, spy, base) + }) +}