From 0ef32a75c7d28a5104a319a4f24d9f097c8aa0a0 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 23:15:00 -0800 Subject: [PATCH 1/4] sw-block: ops chunk commands that write chunk metadata run the single-filer start check (O-ENG-4) Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/sw-block/chunk.go | 41 +++++++++++-- seaweed-block/cmd/sw-block/chunk_gc.go | 5 ++ seaweed-block/cmd/sw-block/chunk_retain.go | 4 +- .../cmd/sw-block/chunk_start_check_test.go | 60 +++++++++++++++++++ .../cmd/sw-block/chunk_volume_delete.go | 2 +- 5 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 seaweed-block/cmd/sw-block/chunk_start_check_test.go diff --git a/seaweed-block/cmd/sw-block/chunk.go b/seaweed-block/cmd/sw-block/chunk.go index 80cc5f67ad..46411e71e4 100644 --- a/seaweed-block/cmd/sw-block/chunk.go +++ b/seaweed-block/cmd/sw-block/chunk.go @@ -42,6 +42,29 @@ var dialChunkFiler = func(addr string) (chunkstore.Filer, func() error, error) { return c, c.Close, nil } +// checkSingleFiler is the start check of a command that writes chunk +// metadata (O-ENG-4): block metadata goes through exactly one filer, the +// same rule blockvolume enforces at start. Tests replace it. +var checkSingleFiler = func(ctx context.Context, f chunkstore.Filer) error { + if s, ok := f.(*chunkstore.SWFS); ok { + return s.CheckSingleFiler(ctx) + } + return nil +} + +// refuseUnlessSingleFiler runs the start check; exit >= 0 means refused. +func refuseUnlessSingleFiler(name string, f chunkstore.Filer, stdout, stderr io.Writer) int { + err := checkSingleFiler(context.Background(), f) + switch { + case err == nil: + return -1 + case errors.Is(err, chunkstore.ErrNotSingleFiler): + return chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false) + default: + return chunkFail(name, stdout, stderr, chunkExitFailed, codeUnavailableCLI, "single-filer start check: "+err.Error(), true) + } +} + func runOpsChunk(args []string, stdout, stderr io.Writer) int { if len(args) == 0 { return chunkFail("", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "expected cut|snapshot|snapshots|snapshot-delete|fork|detach|show|gc|usage|retain|volume-delete", false) @@ -259,7 +282,9 @@ func chunkDetach(args []string, stdout, stderr io.Writer) int { // chunkCatalog opens the catalog under --chunk-path on --chunk-filer. // It returns exit -1 on success, else the exit code after reporting. -func chunkCatalog(name, filer, base string, stdout, stderr io.Writer) (*chunkstore.Catalog, func() error, int) { +// chunkCatalog dials the filer and opens the catalog; a command that writes +// (writes) first passes the single-filer start check. +func chunkCatalog(name, filer, base string, writes bool, stdout, stderr io.Writer) (*chunkstore.Catalog, func() error, int) { if filer == "" { return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--chunk-filer is required", false) } @@ -267,6 +292,12 @@ func chunkCatalog(name, filer, base string, stdout, stderr io.Writer) (*chunksto if err != nil { return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false) } + if writes { + if exit := refuseUnlessSingleFiler(name, f, stdout, stderr); exit >= 0 { + _ = closeFn() + return nil, nil, exit + } + } cat, err := chunkstore.NewCatalog(f, base) if err != nil { _ = closeFn() @@ -364,7 +395,7 @@ func chunkSnapshots(args []string, stdout, stderr io.Writer) int { if volumeID == "" { return chunkFail("snapshots", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false) } - cat, closeFn, exit := chunkCatalog("snapshots", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("snapshots", filer, base, false, stdout, stderr) if exit >= 0 { return exit } @@ -392,7 +423,7 @@ func chunkSnapshotDelete(args []string, stdout, stderr io.Writer) int { if volumeID == "" || name == "" { return chunkFail("snapshot-delete", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --name are required", false) } - cat, closeFn, exit := chunkCatalog("snapshot-delete", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("snapshot-delete", filer, base, true, stdout, stderr) if exit >= 0 { return exit } @@ -428,7 +459,7 @@ func chunkFork(args []string, stdout, stderr io.Writer) int { requestID = newRequestID() fmt.Fprintf(stderr, "sw-block ops chunk fork: request id %s (reuse it to retry)\n", requestID) } - cat, closeFn, exit := chunkCatalog("fork", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("fork", filer, base, true, stdout, stderr) if exit >= 0 { return exit } @@ -456,7 +487,7 @@ func chunkShow(args []string, stdout, stderr io.Writer) int { if volumeID == "" { return chunkFail("show", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false) } - cat, closeFn, exit := chunkCatalog("show", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("show", filer, base, false, stdout, stderr) if exit >= 0 { return exit } diff --git a/seaweed-block/cmd/sw-block/chunk_gc.go b/seaweed-block/cmd/sw-block/chunk_gc.go index a5a8cdfc61..b802d26e68 100644 --- a/seaweed-block/cmd/sw-block/chunk_gc.go +++ b/seaweed-block/cmd/sw-block/chunk_gc.go @@ -41,6 +41,11 @@ func chunkGC(args []string, stdout, stderr io.Writer) int { return chunkResult("gc", nil, e, stdout, stderr) } defer closeFn() + if apply { + if exit := refuseUnlessSingleFiler("gc", f, stdout, stderr); exit >= 0 { + return exit + } + } blobs, ok := f.(chunkstore.BlobDeleter) if !ok { return chunkFail("gc", stdout, stderr, chunkExitFailed, codeInternalCLI, "chunk store cannot delete immutable blobs", false) diff --git a/seaweed-block/cmd/sw-block/chunk_retain.go b/seaweed-block/cmd/sw-block/chunk_retain.go index 7b9702672c..34246710c3 100644 --- a/seaweed-block/cmd/sw-block/chunk_retain.go +++ b/seaweed-block/cmd/sw-block/chunk_retain.go @@ -31,7 +31,7 @@ func chunkUsage(args []string, stdout, stderr io.Writer) int { if volumeID == "" { return chunkFail("usage", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false) } - cat, closeFn, exit := chunkCatalog("usage", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("usage", filer, base, false, stdout, stderr) if exit >= 0 { return exit } @@ -74,7 +74,7 @@ func chunkRetain(args []string, stdout, stderr io.Writer) int { if setPolicy && dryRun { return chunkFail("retain", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--set-policy and --dry-run do not combine", false) } - cat, closeFn, exit := chunkCatalog("retain", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("retain", filer, base, !dryRun, stdout, stderr) if exit >= 0 { return exit } diff --git a/seaweed-block/cmd/sw-block/chunk_start_check_test.go b/seaweed-block/cmd/sw-block/chunk_start_check_test.go new file mode 100644 index 0000000000..605bde12a9 --- /dev/null +++ b/seaweed-block/cmd/sw-block/chunk_start_check_test.go @@ -0,0 +1,60 @@ +package main + +import ( + "bytes" + "context" + "strings" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" +) + +// O-ENG-4: every ops chunk command that writes chunk metadata runs the +// single-filer start check before it reads or writes anything, and refuses +// when the filer is not the only one of its group; read-only commands (and +// read-only modes) do not run it. +func TestOpsChunk_WritingCommandsRunTheSingleFilerCheck(t *testing.T) { + mem := chunkstore.NewMemFiler() + prevDial, prevCheck := dialChunkFiler, checkSingleFiler + t.Cleanup(func() { dialChunkFiler, checkSingleFiler = prevDial, prevCheck }) + dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return mem, func() error { return nil }, nil } + checks := 0 + checkSingleFiler = func(context.Context, chunkstore.Filer) error { + checks++ + return chunkstore.ErrNotSingleFiler + } + filer := []string{"--chunk-filer", "filer:18888"} + writing := map[string][]string{ + "snapshot-delete": append([]string{"ops", "chunk", "snapshot-delete", "--volume", "v1", "--name", "s1"}, filer...), + "fork": append([]string{"ops", "chunk", "fork", "--source", "v1", "--snapshot", "s1", "--target", "v2", "--request-id", "r"}, filer...), + "retain": append([]string{"ops", "chunk", "retain", "--volume", "v1"}, filer...), + "volume-delete": append([]string{"ops", "chunk", "volume-delete", "--volume", "v1", "--master", "m:1", "--request-id", "r"}, filer...), + "gc --apply": append([]string{"ops", "chunk", "gc", "--master", "m:1", "--apply"}, filer...), + } + for name, args := range writing { + before := checks + var out, errb bytes.Buffer + code := run(args, &out, &errb) + if checks != before+1 { + t.Errorf("%s: start check ran %d times; want once", name, checks-before) + } + if code == 0 || !strings.Contains(out.String()+errb.String(), "exactly one filer") { + t.Errorf("%s: exit %d, out %q err %q; want the single-filer refusal", name, code, out.String(), errb.String()) + } + } + readOnly := map[string][]string{ + "snapshots": append([]string{"ops", "chunk", "snapshots", "--volume", "v1"}, filer...), + "show": append([]string{"ops", "chunk", "show", "--volume", "v1"}, filer...), + "usage": append([]string{"ops", "chunk", "usage", "--volume", "v1"}, filer...), + "retain --dry-run": append([]string{"ops", "chunk", "retain", "--volume", "v1", "--dry-run"}, filer...), + "gc (read-only)": append([]string{"ops", "chunk", "gc", "--master", "m:1"}, filer...), + } + for name, args := range readOnly { + before := checks + var out, errb bytes.Buffer + run(args, &out, &errb) + if checks != before { + t.Errorf("%s: a read-only command ran the start check", name) + } + } +} diff --git a/seaweed-block/cmd/sw-block/chunk_volume_delete.go b/seaweed-block/cmd/sw-block/chunk_volume_delete.go index 52e05da0cc..9054369431 100644 --- a/seaweed-block/cmd/sw-block/chunk_volume_delete.go +++ b/seaweed-block/cmd/sw-block/chunk_volume_delete.go @@ -46,7 +46,7 @@ func chunkVolumeDelete(args []string, stdout, stderr io.Writer) int { if volumeID == "" || master == "" || requestID == "" || timeout <= 0 { return chunkFail("volume-delete", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "a volume, --master and --request-id are required; --timeout must be positive", false) } - cat, closeFn, exit := chunkCatalog("volume-delete", filer, base, stdout, stderr) + cat, closeFn, exit := chunkCatalog("volume-delete", filer, base, true, stdout, stderr) if exit >= 0 { return exit } From e3a06ab435f0100b6a92eb0454c1c8ce3dc7945e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 23:18:09 -0800 Subject: [PATCH 2/4] invariants: INV-WS-SINGLE-FILER-001 names the ops chunk enforcement and its test Co-Authored-By: Claude Opus 5.5 --- seaweed-block/docs/design/invariants.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index ba83dc3ba6..1f40a16a20 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -501,7 +501,7 @@ Notes: | `INV-WS-CAS-UNKNOWN-001` | An unknown HEAD CAS outcome is settled by re-reading HEAD, never by a blind retry; until it is settled the proposal stays pending and its intents stay. | M5 CAS → Pending | `core/storage/chunk_layer.go` `captureLocked`, `resolvePending`; `core/storage/chunk_restore.go` `ClaimChunkHead` | `TestChunkRemote_UncertainCASIsNotPrecondition` (`core/frontend/chunkstore/chunk_remote_test.go`); `TestChunkLayer_C6_UnknownNotAppliedNeverClaimsSuccess` (`core/storage/chunk_layer_contract_test.go`); `TestClaimChunkHeadSettlesUnknownWithoutReplay` (`core/storage/chunk_restore_test.go`); `TestChunkIntent_UncertainCASKeepsIntentUntilSettled` (`core/storage/chunk_layer_intent_test.go`) | — | ACTIVE | | `INV-WS-CLAIM-BEFORE-SERVE-001` | A new holder claims HEAD at its master-issued epoch before I/O opens; a HEAD with a higher epoch, or another holder at the same epoch, fences the claim. | M4 Claiming → Holder \| Fenced | `core/storage/chunk_restore.go` `ClaimChunkHead`; `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestClaimChunkHeadRefusesStaleAuthorityAndAdmissionDrift` (`core/storage/chunk_restore_test.go`); `TestRestoreSeedPrecedesTakeoverCAS` (`core/frontend/durable/provider_seed_order_test.go`); `TestWorkspaceMoveRestoresLazilyAndFencesOldHost` (`core/host/master/workspace_restore_test.go`) | `WorkspaceTakeover` `SeedMatchesTakeover` | ACTIVE | | `INV-WS-STALE-LOCAL-ROOT-001` | An existing local root whose HEAD another holder has published is not served or cut; the attach fails closed with `ErrChunkStaleLocalRoot`. | M4 Claiming → Refused | `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestWALStoreChunk_MoveBackOntoAnOldRootIsRefused`, `TestWALStoreChunk_MoveBackRefusedOnDeferredAttach` (`core/frontend/durable/provider_chunk_moveback_test.go`) | — | ACTIVE | -| `INV-WS-SINGLE-FILER-001` | A chunk volume starts only when `--chunk-filer` is the only filer of its group: a master listing another filer or several refuses at once, whichever master answers first; a filer no master lists within 60 s is refused. A startup census, not a fence: a filer added to the group later is not detected. | M4 (startup) → Refused | `core/frontend/chunkstore/single_filer.go` `CheckSingleFiler`; `cmd/blockvolume/chunk_remote.go` `newChunkWiring` | `TestCheckSingleFilerRefusesAnotherOrSeveralFilers`, `TestCheckSingleFilerFailsClosed`, `TestCheckSingleFilerFavourableThenStalledRefuses`, `TestIsTargetComparesScopedAddresses`, `TestCheckSingleFilerPasses` (`core/frontend/chunkstore/single_filer_test.go`), `TestChunkWiringRefusesAMultiFilerGroup` (`cmd/blockvolume/single_filer_test.go`) | — | ACTIVE | +| `INV-WS-SINGLE-FILER-001` | A chunk volume starts only when `--chunk-filer` is the only filer of its group: a master listing another filer or several refuses at once, whichever master answers first; a filer no master lists within 60 s is refused. A startup census, not a fence: a filer added to the group later is not detected. | M4 (startup) → Refused | `core/frontend/chunkstore/single_filer.go` `CheckSingleFiler`; `cmd/blockvolume/chunk_remote.go` `newChunkWiring`; `cmd/sw-block/chunk.go` `refuseUnlessSingleFiler` | `TestCheckSingleFilerRefusesAnotherOrSeveralFilers`, `TestCheckSingleFilerFailsClosed`, `TestCheckSingleFilerFavourableThenStalledRefuses`, `TestIsTargetComparesScopedAddresses`, `TestCheckSingleFilerPasses` (`core/frontend/chunkstore/single_filer_test.go`), `TestChunkWiringRefusesAMultiFilerGroup` (`cmd/blockvolume/single_filer_test.go`), `TestOpsChunk_WritingCommandsRunTheSingleFilerCheck` (`cmd/sw-block/chunk_start_check_test.go`) | — | ACTIVE | | `INV-WS-LEASE-001` | A host admits frontend I/O and cuts only under an unexpired lease for its exact `{volume, replica, epoch}`; a fenced guard never reopens, even on a late renewal reply. | M4 lease guard: Valid → Fenced | `core/host/volume/workspace_lease.go` `permits`, `Refresh` | `TestWorkspaceLeaseExpiryFencesWithoutTimerAndCannotReopen`, `TestWorkspaceDelayedRenewalCannotUndoExpiry` (`core/host/volume/workspace_lease_test.go`); `TestWorkspaceHostRequiresLeaseBeforeFrontendAndCut` (`core/host/volume/workspace_runtime_test.go`); `TestWorkspacePausedGranteeRefusedAtAttach` (`core/host/master/workspace_paused_grantee_test.go`) | `WorkspaceTakeover` `NoDualServing` | ACTIVE | | `INV-WS-REVOKE-ACK-001` | A host acknowledges a revocation only after its frontend and cuts have stopped; a cancelled or expired drain sends no acknowledgement. | M4 lease guard: Revoking → Acked | `core/host/volume/workspace_lease.go` `drain` | `TestWorkspaceRevocationAckOnlyAfterFrontendAndCutsStop` (`core/host/volume/workspace_lease_test.go`); `TestWorkspaceHostCancelOrExpiryDuringRevokeDrainNeverACKs` (`core/host/volume/workspace_runtime_test.go`) | `WorkspaceTakeover` `CleanNoLoss` | ACTIVE | | `INV-WS-STOP-PENDING-001` | Stop-pending changes only when the lease guard renews, never what it admits or acknowledges: from the start of a chunk detach until the guard fences, its sleep after a completed renewal is at most 250 ms; it returns to the normal renewal delay only when that same (latest) detach attempt provably did not happen and serving resumed. It bounds scheduling, not the time from a revocation to its acknowledgement. | M4 lease guard: Valid → StopPending → Valid \| Fenced | `core/frontend/durable/provider_chunk.go` `DetachVolume`, `resumeAfterDetach`; `core/host/volume/workspace_runtime.go` `WorkspaceStopPending`, `WorkspaceStopResumed`; `core/host/volume/workspace_lease.go` `StopPending`, `ClearStopPending`, `renewDelay` | `TestWorkspaceLeaseStopPendingSchedule`, `TestWorkspaceHostStopResumedClearsOnlyTheLatestAttempt`, `TestWorkspaceHostFailedDetachStopPendingByOutcome`, `TestWorkspaceHostDetachHookFencesWithinPoll` (`core/host/volume/workspace_lease_stop_pending_test.go`); `TestWALStoreChunk_DetachHookPrecedesDrain`, `TestWALStoreChunk_DetachResumeHookOnlyOnProvenAbort`, `TestWALStoreChunk_DetachCallsStopHook` (`core/frontend/durable/provider_chunk_detach_hook_test.go`) | — | ACTIVE | @@ -512,6 +512,7 @@ Notes: Notes: +- `INV-WS-SINGLE-FILER-001`: the same census runs in every `sw-block ops chunk` command that writes chunk metadata (snapshot-delete, fork, retain, volume-delete, gc --apply) before it reads or writes; read-only commands and modes do not run it. - `INV-WS-HEAD-CAS-001`: The integration test needs build tag `chunkstore_integration` and a pinned `weed` binary. - `INV-WS-CAS-UNKNOWN-001`: An unknown-outcome CAS is not in the TLA+ models (`docs/spec/README.md`, Limits). - `INV-WS-FORCED-MOVE-001`: TLA+: `W4_drift` found the gap before #158; the skew margin itself is not in the model. Extended for a new holder that dies between its grant and its HEAD claim: the move issue starts its lease, admission uses that grant's lease, HEAD is read after admission, an older-epoch HEAD is accepted (its generation and remote LSN may have moved by an earlier holder's late claim), and a returning holder is refused by two fences, its lease-gated attach and the higher HEAD epoch. `WorkspaceTakeover` models one move from A to B and does not cover a second move away from an unclaimed grant; that model check is open and not claimed here. From c5478a990f5ef13a77ca51be0ab04d2f21333430 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 23:27:09 -0800 Subject: [PATCH 3/4] sw-block: the single-filer start check runs within the command's --timeout Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/sw-block/chunk.go | 22 ++++++++----- seaweed-block/cmd/sw-block/chunk_gc.go | 2 +- seaweed-block/cmd/sw-block/chunk_retain.go | 4 +-- .../cmd/sw-block/chunk_start_check_test.go | 33 +++++++++++++++++++ .../cmd/sw-block/chunk_volume_delete.go | 2 +- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/seaweed-block/cmd/sw-block/chunk.go b/seaweed-block/cmd/sw-block/chunk.go index 46411e71e4..983a7ca611 100644 --- a/seaweed-block/cmd/sw-block/chunk.go +++ b/seaweed-block/cmd/sw-block/chunk.go @@ -52,9 +52,13 @@ var checkSingleFiler = func(ctx context.Context, f chunkstore.Filer) error { return nil } -// refuseUnlessSingleFiler runs the start check; exit >= 0 means refused. -func refuseUnlessSingleFiler(name string, f chunkstore.Filer, stdout, stderr io.Writer) int { - err := checkSingleFiler(context.Background(), f) +// refuseUnlessSingleFiler runs the start check within the command's own +// timeout (a stalled filer or master cannot hold the command longer); exit +// >= 0 means refused. +func refuseUnlessSingleFiler(name string, f chunkstore.Filer, timeout time.Duration, stdout, stderr io.Writer) int { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + err := checkSingleFiler(ctx, f) switch { case err == nil: return -1 @@ -284,7 +288,7 @@ func chunkDetach(args []string, stdout, stderr io.Writer) int { // It returns exit -1 on success, else the exit code after reporting. // chunkCatalog dials the filer and opens the catalog; a command that writes // (writes) first passes the single-filer start check. -func chunkCatalog(name, filer, base string, writes bool, stdout, stderr io.Writer) (*chunkstore.Catalog, func() error, int) { +func chunkCatalog(name, filer, base string, writes bool, timeout time.Duration, stdout, stderr io.Writer) (*chunkstore.Catalog, func() error, int) { if filer == "" { return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--chunk-filer is required", false) } @@ -293,7 +297,7 @@ func chunkCatalog(name, filer, base string, writes bool, stdout, stderr io.Write return nil, nil, chunkFail(name, stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, err.Error(), false) } if writes { - if exit := refuseUnlessSingleFiler(name, f, stdout, stderr); exit >= 0 { + if exit := refuseUnlessSingleFiler(name, f, timeout, stdout, stderr); exit >= 0 { _ = closeFn() return nil, nil, exit } @@ -395,7 +399,7 @@ func chunkSnapshots(args []string, stdout, stderr io.Writer) int { if volumeID == "" { return chunkFail("snapshots", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false) } - cat, closeFn, exit := chunkCatalog("snapshots", filer, base, false, stdout, stderr) + cat, closeFn, exit := chunkCatalog("snapshots", filer, base, false, timeout, stdout, stderr) if exit >= 0 { return exit } @@ -423,7 +427,7 @@ func chunkSnapshotDelete(args []string, stdout, stderr io.Writer) int { if volumeID == "" || name == "" { return chunkFail("snapshot-delete", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume and --name are required", false) } - cat, closeFn, exit := chunkCatalog("snapshot-delete", filer, base, true, stdout, stderr) + cat, closeFn, exit := chunkCatalog("snapshot-delete", filer, base, true, timeout, stdout, stderr) if exit >= 0 { return exit } @@ -459,7 +463,7 @@ func chunkFork(args []string, stdout, stderr io.Writer) int { requestID = newRequestID() fmt.Fprintf(stderr, "sw-block ops chunk fork: request id %s (reuse it to retry)\n", requestID) } - cat, closeFn, exit := chunkCatalog("fork", filer, base, true, stdout, stderr) + cat, closeFn, exit := chunkCatalog("fork", filer, base, true, timeout, stdout, stderr) if exit >= 0 { return exit } @@ -487,7 +491,7 @@ func chunkShow(args []string, stdout, stderr io.Writer) int { if volumeID == "" { return chunkFail("show", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false) } - cat, closeFn, exit := chunkCatalog("show", filer, base, false, stdout, stderr) + cat, closeFn, exit := chunkCatalog("show", filer, base, false, timeout, stdout, stderr) if exit >= 0 { return exit } diff --git a/seaweed-block/cmd/sw-block/chunk_gc.go b/seaweed-block/cmd/sw-block/chunk_gc.go index b802d26e68..41d7071f29 100644 --- a/seaweed-block/cmd/sw-block/chunk_gc.go +++ b/seaweed-block/cmd/sw-block/chunk_gc.go @@ -42,7 +42,7 @@ func chunkGC(args []string, stdout, stderr io.Writer) int { } defer closeFn() if apply { - if exit := refuseUnlessSingleFiler("gc", f, stdout, stderr); exit >= 0 { + if exit := refuseUnlessSingleFiler("gc", f, timeout, stdout, stderr); exit >= 0 { return exit } } diff --git a/seaweed-block/cmd/sw-block/chunk_retain.go b/seaweed-block/cmd/sw-block/chunk_retain.go index 34246710c3..dee26ad7a7 100644 --- a/seaweed-block/cmd/sw-block/chunk_retain.go +++ b/seaweed-block/cmd/sw-block/chunk_retain.go @@ -31,7 +31,7 @@ func chunkUsage(args []string, stdout, stderr io.Writer) int { if volumeID == "" { return chunkFail("usage", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--volume is required", false) } - cat, closeFn, exit := chunkCatalog("usage", filer, base, false, stdout, stderr) + cat, closeFn, exit := chunkCatalog("usage", filer, base, false, timeout, stdout, stderr) if exit >= 0 { return exit } @@ -74,7 +74,7 @@ func chunkRetain(args []string, stdout, stderr io.Writer) int { if setPolicy && dryRun { return chunkFail("retain", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "--set-policy and --dry-run do not combine", false) } - cat, closeFn, exit := chunkCatalog("retain", filer, base, !dryRun, stdout, stderr) + cat, closeFn, exit := chunkCatalog("retain", filer, base, !dryRun, timeout, stdout, stderr) if exit >= 0 { return exit } diff --git a/seaweed-block/cmd/sw-block/chunk_start_check_test.go b/seaweed-block/cmd/sw-block/chunk_start_check_test.go index 605bde12a9..f3c5e61ab1 100644 --- a/seaweed-block/cmd/sw-block/chunk_start_check_test.go +++ b/seaweed-block/cmd/sw-block/chunk_start_check_test.go @@ -5,6 +5,7 @@ import ( "context" "strings" "testing" + "time" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" ) @@ -58,3 +59,35 @@ func TestOpsChunk_WritingCommandsRunTheSingleFilerCheck(t *testing.T) { } } } + +// The start check runs within the command's own --timeout: a check stalled +// on its filer or master (here one that blocks until its context ends) ends +// the command at the timeout, as a retryable refusal. +func TestOpsChunk_StartCheckIsBoundedByTheTimeout(t *testing.T) { + mem := chunkstore.NewMemFiler() + prevDial, prevCheck := dialChunkFiler, checkSingleFiler + t.Cleanup(func() { dialChunkFiler, checkSingleFiler = prevDial, prevCheck }) + dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return mem, func() error { return nil }, nil } + checkSingleFiler = func(ctx context.Context, _ chunkstore.Filer) error { + <-ctx.Done() + return ctx.Err() + } + filer := []string{"--chunk-filer", "filer:18888", "--timeout", "200ms"} + for name, args := range map[string][]string{ + "snapshot-delete": append([]string{"ops", "chunk", "snapshot-delete", "--volume", "v1", "--name", "s1"}, filer...), + "gc --apply": append([]string{"ops", "chunk", "gc", "--master", "m:1", "--apply"}, filer...), + } { + done := make(chan struct{}) + var out, errb bytes.Buffer + var code int + go func() { code = run(args, &out, &errb); close(done) }() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatalf("%s: a stalled start check held the command past its 200ms --timeout", name) + } + if code == 0 || !strings.Contains(out.String(), "remote_unavailable") || !strings.Contains(out.String(), `"retryable":true`) { + t.Errorf("%s: exit %d, out %q; want a retryable remote_unavailable refusal", name, code, out.String()) + } + } +} diff --git a/seaweed-block/cmd/sw-block/chunk_volume_delete.go b/seaweed-block/cmd/sw-block/chunk_volume_delete.go index 9054369431..0413f1709f 100644 --- a/seaweed-block/cmd/sw-block/chunk_volume_delete.go +++ b/seaweed-block/cmd/sw-block/chunk_volume_delete.go @@ -46,7 +46,7 @@ func chunkVolumeDelete(args []string, stdout, stderr io.Writer) int { if volumeID == "" || master == "" || requestID == "" || timeout <= 0 { return chunkFail("volume-delete", stdout, stderr, ops.VolumeStatusExitInvalid, codeBadRequestCLI, "a volume, --master and --request-id are required; --timeout must be positive", false) } - cat, closeFn, exit := chunkCatalog("volume-delete", filer, base, true, stdout, stderr) + cat, closeFn, exit := chunkCatalog("volume-delete", filer, base, true, timeout, stdout, stderr) if exit >= 0 { return exit } From 1ef132686657db69476201e6205d8fdf99a145f6 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 23:58:32 -0800 Subject: [PATCH 4/4] sw-block: start-check comments describe the behaviour only Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/sw-block/chunk.go | 2 +- seaweed-block/cmd/sw-block/chunk_start_check_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/seaweed-block/cmd/sw-block/chunk.go b/seaweed-block/cmd/sw-block/chunk.go index 983a7ca611..7942b7a517 100644 --- a/seaweed-block/cmd/sw-block/chunk.go +++ b/seaweed-block/cmd/sw-block/chunk.go @@ -43,7 +43,7 @@ var dialChunkFiler = func(addr string) (chunkstore.Filer, func() error, error) { } // checkSingleFiler is the start check of a command that writes chunk -// metadata (O-ENG-4): block metadata goes through exactly one filer, the +// metadata: block metadata goes through exactly one filer, the // same rule blockvolume enforces at start. Tests replace it. var checkSingleFiler = func(ctx context.Context, f chunkstore.Filer) error { if s, ok := f.(*chunkstore.SWFS); ok { diff --git a/seaweed-block/cmd/sw-block/chunk_start_check_test.go b/seaweed-block/cmd/sw-block/chunk_start_check_test.go index f3c5e61ab1..05ff3c2399 100644 --- a/seaweed-block/cmd/sw-block/chunk_start_check_test.go +++ b/seaweed-block/cmd/sw-block/chunk_start_check_test.go @@ -10,7 +10,7 @@ import ( "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" ) -// O-ENG-4: every ops chunk command that writes chunk metadata runs the +// Every ops chunk command that writes chunk metadata runs the // single-filer start check before it reads or writes anything, and refuses // when the filer is not the only one of its group; read-only commands (and // read-only modes) do not run it.