fix: preserve snapshot outcomes after uncertain provider failures

This commit is contained in:
pingqiu committed 2026-12-01 18:48:40 -08:00
1 parent 5b25575a55
commit 6add466034
3 files changed
+161 -19

No files matched your search

+23 -19
View File
@@ -883,35 +883,39 @@ func (c *chunkControl) snapshotOperationAttempt(ctx context.Context, volumeID st
// Request cancellation stops work, not the durable outcome record. This
// joined, bounded cleanup writes only its own receipt, never HEAD.
cleanup, stop := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
status, code, retryable := chunkErrorCode(err)
// A provider failure is not proof that its HEAD publication did not
// apply. Only the pre-execution checks above may record refusal.
// First honor a terminal outcome another observer already settled.
if current, found, readErr := store.ReadSnapshotOperation(cleanup, op.ID, op.Verb); readErr == nil && found {
if result, terminalErr, terminal := snapshotOperationTerminalAnswer(current); terminal {
stop()
return result, terminalErr
}
}
_, code, _ := chunkErrorCode(err)
if code == codeFenced || code == codeEpochStale || code == codeNotPrimary {
// A displaced worker may observe a receipt, but cannot mutate it.
stop()
return chunkstore.SnapshotInfo{}, err
}
state := "stalled"
var detail []chunkstore.SnapshotOperationFailure
if !retryable && status >= 400 && status < 500 && ctx.Err() == nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
state = "refused"
msg := err.Error()
var ce *chunkstore.CatalogError
if errors.As(err, &ce) {
msg = ce.Detail()
_, writeErr := store.AdvanceSnapshotOperation(cleanup, op, "stalled", nil, "")
if errors.Is(writeErr, chunkstore.ErrSnapshotOperationOwner) || errors.Is(writeErr, chunkstore.ErrSnapshotOperationTerminal) {
current, found, readErr := store.ReadSnapshotOperation(cleanup, op.ID, op.Verb)
stop()
if readErr != nil {
return chunkstore.SnapshotInfo{}, errors.Join(err, readErr)
}
if len(msg) > 512 {
msg = "snapshot operation refused (detail exceeds receipt bound)"
if found {
if result, terminalErr, terminal := snapshotOperationTerminalAnswer(current); terminal {
return result, terminalErr
}
}
detail = []chunkstore.SnapshotOperationFailure{{Status: status, Message: msg}}
} else {
code = ""
return chunkstore.SnapshotInfo{}, chunkstore.ErrSnapshotOperationOwner
}
_, writeErr := store.AdvanceSnapshotOperation(cleanup, op, state, nil, code, detail...)
stop()
if writeErr != nil && !errors.Is(writeErr, chunkstore.ErrSnapshotOperationOwner) && !errors.Is(writeErr, chunkstore.ErrSnapshotOperationTerminal) {
if writeErr != nil {
return chunkstore.SnapshotInfo{}, errors.Join(err, writeErr)
}
if state == "refused" && writeErr == nil {
return chunkstore.SnapshotInfo{}, &snapshotOperationRefusal{status: status, code: code, message: detail[0].Message, cause: err}
}
}
return chunkstore.SnapshotInfo{}, err
}
@@ -2,6 +2,7 @@ package volume
import (
"context"
"encoding/json"
"errors"
"maps"
"net/http"
@@ -14,6 +15,18 @@ import (
const snapshotOperationQueueLimit = 4096
func snapshotOperationTerminalAnswer(op chunkstore.SnapshotOperation) (chunkstore.SnapshotInfo, error, bool) {
if op.State == "refused" {
return chunkstore.SnapshotInfo{}, &snapshotOperationRefusal{status: op.ErrorStatus, code: op.ErrorCode, message: op.ErrorMessage}, true
}
if op.State != "succeeded" {
return chunkstore.SnapshotInfo{}, nil, false
}
var result chunkstore.SnapshotInfo
err := json.Unmarshal(op.Result, &result)
return result, err, true
}
type snapshotOperationJob struct {
volume string
request ChunkSnapshotRequest
@@ -13,8 +13,133 @@ import (
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
"github.com/seaweedfs/seaweed-block/core/frontend/durable/testutil"
"github.com/seaweedfs/seaweed-block/core/storage"
)
type snapshotOperationAnswerFault struct {
*durable.DurableProvider
after func(context.Context, layout.Head) error
before error
}
func (p *snapshotOperationAnswerFault) SnapshotVolume(ctx context.Context, volume, name string, meta layout.SnapshotMeta) (layout.Head, error) {
if p.before != nil {
return layout.Head{}, p.before
}
h, err := p.DurableProvider.SnapshotVolume(ctx, volume, name, meta)
if err == nil && p.after != nil {
return h, p.after(ctx, h)
}
return h, err
}
func TestSnapshotOperationAppliedAnswerFailureNeverBecomesRefused(t *testing.T) {
for _, settled := range []bool{false, true} {
t.Run(map[bool]string{false: "unsettled", true: "settled-by-removal"}[settled], func(t *testing.T) {
ctx := context.Background()
dp, f, s, head := limitVolume(t)
if _, err := s.Write(0, bytes.Repeat([]byte{'A'}, 4096)); err != nil {
t.Fatal(err)
}
st, err := f.Catalog.Store(f.Ref)
if err != nil {
t.Fatal(err)
}
p := &snapshotOperationAnswerFault{DurableProvider: dp}
p.after = func(ctx context.Context, h layout.Head) error {
if settled {
if _, err := st.SettleSnapshotOperationFromHead(ctx, h, "daily"); err != nil {
t.Fatal(err)
}
}
return errors.Join(storage.ErrChunkCASOutcomeUnknown, storage.ErrChunkLayerDetached)
}
req := ChunkSnapshotRequest{Name: "daily", RequestID: "applied-confirmation-lost"}
ctl := NewChunkControl(p, f.Catalog, f.Ref)
got, err := ctl.Snapshot(ctx, "v1", req)
op, found, readErr := st.ReadSnapshotOperation(ctx, req.RequestID, "create")
if readErr != nil || !found || op.State == "refused" {
t.Fatalf("applied write became final refusal: %+v found=%v err=%v", op, found, readErr)
}
if settled && (err != nil || got.Name != "daily") {
t.Fatalf("lost receipt race ignored stored success: %+v %v", got, err)
}
before := head()
again, err := NewChunkControl(dp, f.Catalog, f.Ref).Snapshot(ctx, "v1", req)
if err != nil || again.Name != "daily" || !reflect.DeepEqual(before, head()) {
t.Fatalf("retry failed original result or republished: %+v %v", again, err)
}
if _, err := f.Catalog.DeleteSnapshot(ctx, f.Ref, "daily"); err != nil {
t.Fatal(err)
}
})
}
}
func TestSnapshotOperationFailureBeforePublicationRemainsNonterminal(t *testing.T) {
ctx := context.Background()
dp, f, _, head := limitVolume(t)
p := &snapshotOperationAnswerFault{DurableProvider: dp, before: storage.ErrChunkLayerDetached}
req := ChunkSnapshotRequest{Name: "daily", RequestID: "pre-publication-failure"}
_, err := NewChunkControl(p, f.Catalog, f.Ref).Snapshot(ctx, "v1", req)
if err == nil {
t.Fatal("fault returned success")
}
st, _ := f.Catalog.Store(f.Ref)
op, found, err := st.ReadSnapshotOperation(ctx, req.RequestID, "create")
if err != nil || !found || op.State != "stalled" {
t.Fatalf("uncertain work became terminal: %+v found=%v err=%v", op, found, err)
}
if _, err = NewChunkControl(dp, f.Catalog, f.Ref).Snapshot(ctx, "v1", req); err != nil {
t.Fatal(err)
}
before := head()
if _, err = NewChunkControl(dp, f.Catalog, f.Ref).Snapshot(ctx, "v1", req); err != nil || !reflect.DeepEqual(before, head()) {
t.Fatalf("replay republished: %v", err)
}
}
func TestSnapshotOperationFencedFailurePreservesPublishedBinding(t *testing.T) {
for _, door := range []string{"control", "catalog", "retention"} {
t.Run(door, func(t *testing.T) {
ctx := context.Background()
dp, f, s, head := limitVolume(t)
if _, err := s.Write(0, bytes.Repeat([]byte{'A'}, 4096)); err != nil {
t.Fatal(err)
}
p := &snapshotOperationAnswerFault{DurableProvider: dp, after: func(context.Context, layout.Head) error {
return errors.Join(storage.ErrChunkCASOutcomeUnknown, storage.ErrChunkLayerFenced)
}}
req := ChunkSnapshotRequest{Name: "daily", RequestID: "fenced-confirmation"}
if _, err := NewChunkControl(p, f.Catalog, f.Ref).Snapshot(ctx, "v1", req); !errors.Is(err, storage.ErrChunkLayerFenced) {
t.Fatalf("fencing evidence lost: %v", err)
}
st, _ := f.Catalog.Store(f.Ref)
op, found, err := st.ReadSnapshotOperation(ctx, req.RequestID, "create")
if err != nil || !found || op.State != "running" {
t.Fatalf("fenced worker mutated its receipt: %+v %v", op, err)
}
original := chunkstore.SnapshotInfoFromHead("v1", "daily", head())
switch door {
case "control":
_, err = NewChunkControl(dp, f.Catalog, f.Ref).DeleteSnapshot(ctx, "v1", "daily")
case "catalog":
_, err = f.Catalog.DeleteSnapshot(ctx, f.Ref, "daily")
case "retention":
_, err = layout.RunRetireSnapshot(ctx, st, "daily", original.CreatedNs+1)
}
if err != nil {
t.Fatal(err)
}
before := head()
got, err := NewChunkControl(dp, f.Catalog, f.Ref).Snapshot(ctx, "v1", req)
if err != nil || !sameSnapshotOutcome(original, got) || !reflect.DeepEqual(before, head()) {
t.Fatalf("removed fenced publication lost original outcome: %+v %v", got, err)
}
})
}
}
func sameSnapshotOutcome(a, b chunkstore.SnapshotInfo) bool {
// A replay may decorate the answer, but cannot change the original facts.
a.Replayed, b.Replayed = false, false