fix: deduplicate synchronous retries of active snapshot operations

This commit is contained in:
pingqiu committed 2026-12-01 18:48:40 -08:00
1 parent e53cb39cbe
commit 77e14a9428
4 files changed
+111

No files matched your search

@@ -767,6 +767,15 @@ func (c *chunkControl) Snapshot(ctx context.Context, volumeID string, req ChunkS
c.opMu.Unlock()
return chunkstore.SnapshotInfo{}, storage.ErrChunkLayerClosed
}
_, duplicate := c.opJobs[req.RequestID]
duplicate = duplicate && req.RequestID != ""
if !duplicate && req.RequestID != "" {
if len(c.opJobs)+c.opAdmissions >= snapshotOperationQueueLimit {
c.opMu.Unlock()
return chunkstore.SnapshotInfo{}, &chunkstore.CatalogError{Code: "operation_capacity", Message: "snapshot operation queue is full; retry the same request ID", Retryable: true}
}
c.opJobs[req.RequestID] = struct{}{}
}
c.opAdmissions++
c.opMu.Unlock()
ctx, cancel := context.WithCancel(ctx)
@@ -775,10 +784,16 @@ func (c *chunkControl) Snapshot(ctx context.Context, volumeID string, req ChunkS
stopRoot()
cancel()
c.opMu.Lock()
if !duplicate && req.RequestID != "" {
delete(c.opJobs, req.RequestID)
}
c.opAdmissions--
c.maybeJoinSnapshotOperationsLocked()
c.opMu.Unlock()
}()
if duplicate {
return c.snapshotOperationInFlight(ctx, volumeID, req)
}
return c.snapshotOperationAttempt(ctx, volumeID, req, nil)
}
func (c *chunkControl) snapshotOperationAttempt(ctx context.Context, volumeID string, req ChunkSnapshotRequest, expected *chunkstore.SnapshotOperation) (chunkstore.SnapshotInfo, error) {
@@ -27,6 +27,37 @@ func snapshotOperationTerminalAnswer(op chunkstore.SnapshotOperation) (chunkstor
return result, err, true
}
// Legacy synchronous observation of an in-flight ID never starts another cut.
// Changed intent still refuses deterministically; a settled result can replay
// before the original worker removes its process-local queue entry.
func (c *chunkControl) snapshotOperationInFlight(ctx context.Context, volume string, req ChunkSnapshotRequest) (chunkstore.SnapshotInfo, error) {
if _, err := c.catalogVolume(volume); err != nil {
return chunkstore.SnapshotInfo{}, err
}
st, err := c.cat.Store(c.volume)
if err != nil {
return chunkstore.SnapshotInfo{}, err
}
op, found, err := st.ReadSnapshotOperation(ctx, req.RequestID, "create")
if err != nil {
return chunkstore.SnapshotInfo{}, err
}
if !found {
return chunkstore.SnapshotInfo{}, chunkstore.ErrSnapshotOperationOwner
}
fingerprint, err := snapshotOperationFingerprint(req)
if err != nil {
return chunkstore.SnapshotInfo{}, err
}
if op.Name != req.Name || op.Fingerprint != fingerprint {
return chunkstore.SnapshotInfo{}, chunkstore.ErrSnapshotOperationIntent
}
if result, terminalErr, terminal := snapshotOperationTerminalAnswer(op); terminal {
return result, terminalErr
}
return chunkstore.SnapshotInfo{}, chunkstore.ErrSnapshotOperationOwner
}
type snapshotOperationJob struct {
volume string
request ChunkSnapshotRequest
@@ -8,6 +8,7 @@ import (
"fmt"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
"github.com/seaweedfs/seaweed-block/core/storage"
"net/http"
"net/http/httptest"
@@ -17,6 +18,69 @@ import (
"time"
)
type snapshotOperationSingleWorkerProvider struct {
*durable.DurableProvider
calls atomic.Int32
}
func (p *snapshotOperationSingleWorkerProvider) SnapshotVolume(ctx context.Context, volume, name string, meta layout.SnapshotMeta) (layout.Head, error) {
if p.calls.Add(1) != 1 {
return layout.Head{}, errors.New("second worker entered snapshot provider")
}
return p.DurableProvider.SnapshotVolume(ctx, volume, name, meta)
}
func TestSnapshotOperationSynchronousRetryDoesNotDuplicateAsyncWorker(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)
}
entered := make(chan struct{}, 1)
f.Blobs.PutHook = func(ctx context.Context) error { entered <- struct{}{}; <-ctx.Done(); return ctx.Err() }
p := &snapshotOperationSingleWorkerProvider{DurableProvider: dp}
ctl := NewChunkControl(p, f.Catalog, f.Ref).(*chunkControl)
t.Cleanup(func() {
closing, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := ctl.closeSnapshotOperations(closing); err != nil {
t.Error(err)
}
})
req := ChunkSnapshotRequest{Name: "daily", RequestID: "single-worker"}
if _, err := ctl.startSnapshotOperation(ctx, "v1", req); err != nil {
t.Fatal(err)
}
select {
case <-entered:
case <-time.After(time.Second):
t.Fatal("async worker did not enter upload")
}
st, _ := f.Catalog.Store(f.Ref)
before, found, err := st.ReadSnapshotOperation(ctx, req.RequestID, "create")
if err != nil || !found {
t.Fatal(err)
}
h := head()
_, err = ctl.Snapshot(ctx, "v1", req)
if !errors.Is(err, chunkstore.ErrSnapshotOperationOwner) {
t.Fatalf("in-flight synchronous retry did not refuse typed/retryably: %v", err)
}
_, _, retryable := chunkErrorCode(err)
if !retryable {
t.Fatal("in-flight refusal is terminal")
}
changed := req
changed.Name = "other"
if _, err = ctl.Snapshot(ctx, "v1", changed); !errors.Is(err, chunkstore.ErrSnapshotOperationIntent) {
t.Fatalf("in-flight changed intent lost conflict: %v", err)
}
after, _, err := st.ReadSnapshotOperation(ctx, req.RequestID, "create")
if err != nil || !reflect.DeepEqual(before, after) || !reflect.DeepEqual(h, head()) || p.calls.Load() != 1 {
t.Fatalf("retry changed receipt/HEAD or ran duplicate worker: calls=%d err=%v", p.calls.Load(), err)
}
}
func TestSnapshotOperationChangedIntentHTTPConflictBeforeWrite(t *testing.T) {
ctx := context.Background()
dp, f, _, head := limitVolume(t)
+1
View File
@@ -1290,6 +1290,7 @@ Notes:
| `INV-SNAPK-SNAP-DELETE-001` | A named snapshot leaves HEAD by one conditional update, refused while it has dependents; the automatic delete (retention) also refuses a snapshot pinned at its time. | M9 idle → idle | `core/frontend/chunkstore/layout` `snapshots.go` `Head.DeleteSnapshot`, `Head.RetireSnapshot` | `TestDeleteSnapshot_Closure`, `TestRetireSnapshot_Closure`, `TestRunRetireSnapshot_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAP-OP-RECEIPT-001` | A snapshot operation receipt survives reopening its volume store; changed intent is refused, terminal outcomes are immutable, and a stale owner or revision cannot advance the receipt. | M9 operation receipt | `LayoutStore.CreateSnapshotOperation`, `AdvanceSnapshotOperation` (create coupling and process-owned executor wired; retirement pending) | `TestSnapshotOperationReceiptSurvivesReopenAndRejectsChangedIntent`, `TestSnapshotOperationStallReadmissionFencesOldOwner`, `TestSnapshotOperationSplitPublishReadmissionSettlesOneBinding` | SnapOps run3 rows 211–219; split-publication model result and retirement owed | STUB |
| `INV-SNAP-OP-TERMINAL-WRITERS-001` | Snapshot operation success settles a HEAD binding. A terminal refusal is a HEAD conflict checked before provider execution; a provider error can only stall or leave the receipt unchanged. | M9 operation terminal outcome | `LayoutStore.SettleSnapshotOperationFromHead`, `chunkControl.snapshotOperationAttempt` | `TestSnapshotOperationTerminalWriterEnumeration`, `TestSnapshotOperationAppliedAnswerFailureNeverBecomesRefused`, `TestSnapshotOperationFencedFailurePreservesPublishedBinding` | SnapOps run3 row 218; split-publication refinement pending | STUB |
| `INV-SNAP-OP-WORKER-001` | A synchronous retry of an in-flight operation reads its receipt and replays a terminal outcome or refuses retryably, without starting another provider attempt. Changed intent still refuses before work. | M9 operation executor | `chunkControl.Snapshot`, `snapshotOperationInFlight`, process-local `opJobs` | `TestSnapshotOperationSynchronousRetryDoesNotDuplicateAsyncWorker` | SnapOps.Start; complete executor qualification pending | STUB |
| `INV-SNAP-OP-ORIGINAL-001` | Within the retained operation window, retrying a snapshot create returns its original outcome after control, catalog or retention removal, and after name reuse; it performs no new cut or HEAD write. | M9 operation replay (planned) | `chunkControl.Snapshot`, shared HEAD/receipt guard and process-owned executor; retirement pending | `TestSnapshotOperationRetryAfterRemovalKeepsOriginalOutcome`, `TestSnapshotOperationOldIDDoesNotAliasReusedName` (`core/host/volume/snapshot_operation_red_test.go`) | SnapOps.Send/Publish/Delete; rows 211–219 (first red tranche only) | STUB |
| `INV-SNAP-OP-FINGERPRINT-001` | A retained operation ID reused with changed snapshot intent refuses before any cut or HEAD write. | M9 operation admission (planned) | `CreateSnapshotOperation` canonical fingerprint; create executor wired; delete executor pending | `TestSnapshotOperationSameIDChangedIntentRefusesBeforeWrite` (`core/host/volume/snapshot_operation_red_test.go`) | SnapOps.Send; rows 211–219 | STUB |
| `INV-SNAP-OP-READMIT-001` | A create whose HEAD publication applied before its answer was lost reconciles that original outcome on re-admission; later removal cannot cause another execution under its retained operation ID. | M9 operation re-admission (planned) | `SnapshotOperationBinding`, `LayoutStore.GuardSnapshotOperations`; create executor wired; retirement pending | `TestSnapshotOperationLostPublishAnswerReadmitDeleteCannotRepublish`, `TestSnapshotOperationRemovalBeforeReadmitKeepsPublishedOutcome` (`core/host/volume/snapshot_operation_red_test.go`) | SnapOps.Send/Publish/Settle/Delete; rows 211–219 (persisted-stall removal controls included; concurrent-owner controls pending) | STUB |