From 68823d5b908edba659443edc1809a28c92db2dde Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 13:01:53 -0800 Subject: [PATCH] fix(chunkstore): the single-filer census passes only when every master answered Replaces the bound-classification fix with a rule without a clock. A round passes only when every master answered, none refused, and at least one listed exactly the configured filer. A master that does not answer, for any reason and at any time (it stalls, answers DeadlineExceeded before its deadline, is down, or its address does not parse), leaves the round unconfirmed, and the masters are asked again. The bound only ends the asking. Then the start is refused, naming the masters that did not answer, with "rerun when they answer". The two time tests are replaced by one rule test with no timing assertion, in both master orders. Price, stated on the standalone page: a chunk volume does not start while any master of the filer's group is unreachable. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/single_filer.go | 63 ++++++------- .../frontend/chunkstore/single_filer_test.go | 89 ++++++++++--------- seaweed-block/docs/design/invariants.md | 2 +- seaweed-block/docs/standalone-deployment.md | 15 ++-- 4 files changed, 85 insertions(+), 84 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/single_filer.go b/seaweed-block/core/frontend/chunkstore/single_filer.go index cb1c45a8f2..218d20acee 100644 --- a/seaweed-block/core/frontend/chunkstore/single_filer.go +++ b/seaweed-block/core/frontend/chunkstore/single_filer.go @@ -11,8 +11,6 @@ import ( "time" "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/masterpb" @@ -41,11 +39,15 @@ var ( // CheckSingleFiler asks the filer for its masters and filer group // (filer_pb.SeaweedFiler/GetFilerConfiguration), then asks every master for // the filers registered in that group (master_pb.Seaweed/ListClusterNodes, -// client_type "filer"). It passes when a master lists exactly one filer and -// that filer is the configured one. Any master listing another filer, or -// several, refuses at once, whichever master answers first. No master -// listing the target yet, or none answering, is retried until the bound and -// then refused (fail closed). +// client_type "filer"). It passes only in a round where EVERY master +// answered, none refused, and at least one listed exactly the configured +// filer. Any master listing another filer, or several, refuses at once, +// whichever master answers first. A master that did not answer, for any +// reason and at any time, leaves the round unconfirmed, and the masters are +// asked again; so is a round where no master lists the target yet. The bound +// only ends the asking: then the start is refused (fail closed), naming the +// masters that did not answer. There is no clock in the verdict: a chunk +// volume does not start while any master is unreachable. func (c *SWFS) CheckSingleFiler(ctx context.Context) error { ctx, cancel := context.WithTimeout(ctx, singleFilerWait) defer cancel() @@ -57,50 +59,49 @@ func (c *SWFS) CheckSingleFiler(ctx context.Context) error { if len(cfg.GetMasters()) == 0 { return fmt.Errorf("%w: filer %s reports no master, cannot list its group", ErrNotSingleFiler, c.cfg.FilerGRPC) } - var tried []string + var tried, unanswered []string for { - ok, err := c.singleFilerRound(ctx, cfg, &tried) + ok, err := c.singleFilerRound(ctx, cfg, &tried, &unanswered) if ok || err != nil { return err } select { case <-ctx.Done(): - return fmt.Errorf("%w: could not confirm filer %s as the only filer within %s: %s", ErrNotSingleFiler, c.cfg.FilerGRPC, singleFilerWait, strings.Join(tried, "; ")) + missing := "" + if len(unanswered) > 0 { + missing = "; every master must answer, these did not: " + strings.Join(unanswered, ", ") + " (rerun when they answer)" + } + return fmt.Errorf("%w: could not confirm filer %s as the only filer within %s: %s%s", ErrNotSingleFiler, c.cfg.FilerGRPC, singleFilerWait, strings.Join(tried, "; "), missing) case <-time.After(singleFilerRetry): } } } // singleFilerRound asks every master once. It refuses when any master lists -// several filers or a filer other than the target; it passes when no master -// refuses and at least one lists exactly the target; otherwise it asks for -// another round (tried keeps each master's answer). -func (c *SWFS) singleFilerRound(ctx context.Context, cfg *filerpb.GetFilerConfigurationResponse, tried *[]string) (bool, error) { - *tried = (*tried)[:0] +// several filers or a filer other than the target; it passes only when every +// master answered, none refused, and at least one lists exactly the target; +// otherwise it asks for another round. tried keeps each master's answer, +// unanswered the masters that did not answer in this round. +func (c *SWFS) singleFilerRound(ctx context.Context, cfg *filerpb.GetFilerConfigurationResponse, tried, unanswered *[]string) (bool, error) { + *tried, *unanswered = (*tried)[:0], (*unanswered)[:0] group := cfg.GetFilerGroup() sawTarget := false for _, m := range cfg.GetMasters() { addr, err := grpcAddress(m) if err != nil { *tried = append(*tried, fmt.Sprintf("%s: %v", m, err)) + *unanswered = append(*unanswered, m) continue } - // When the bound, not the attempt, ends this call, a deadline answer - // is the bound's: a gRPC server's deadline is the timeout the client - // sent, so it runs out first and answers DeadlineExceeded while ctx - // is not done yet. - dl, bounded := ctx.Deadline() - byBound := bounded && time.Until(dl) <= singleFilerAttempt actx, cancel := context.WithTimeout(ctx, singleFilerAttempt) nodes, err := listFilers(actx, addr, group) cancel() if err != nil { - *tried = append(*tried, fmt.Sprintf("%s: %v", addr, err)) - if byBound && (ctx.Err() != nil || errors.Is(err, context.DeadlineExceeded) || status.Code(err) == codes.DeadlineExceeded) { - // Cut by the bound: this master's view is unknown, so an - // earlier favourable answer does not count. - return false, nil - } + // Its view is unknown, whatever the reason and whenever it + // happened: the round is not confirmed (asking the rest still + // finds a refusal). + *tried = append(*tried, fmt.Sprintf("%s: no answer: %v", addr, err)) + *unanswered = append(*unanswered, addr) continue } switch { @@ -114,13 +115,7 @@ func (c *SWFS) singleFilerRound(ctx context.Context, cfg *filerpb.GetFilerConfig *tried = append(*tried, addr+": lists no filer in group "+strconv.Quote(group)+" yet") } } - if ctx.Err() != nil { - // The bound passed while a later master was still being asked: - // its view is unknown, so an earlier favourable answer does not - // count. - return false, nil - } - return sawTarget, nil + return sawTarget && len(*unanswered) == 0, nil } // isTarget reports whether a filer address listed by a master is the diff --git a/seaweed-block/core/frontend/chunkstore/single_filer_test.go b/seaweed-block/core/frontend/chunkstore/single_filer_test.go index b1c5862ad7..ab30c59a61 100644 --- a/seaweed-block/core/frontend/chunkstore/single_filer_test.go +++ b/seaweed-block/core/frontend/chunkstore/single_filer_test.go @@ -189,7 +189,8 @@ func refusedAtBound(t *testing.T, what string, c *SWFS) { } // The configured filer passes only as the one filer of its group, listed by -// a master; also when it registers only after a few rounds. +// a master, in a round where every master answered; also when it registers +// only after a few rounds. func TestCheckSingleFilerPasses(t *testing.T) { shortBounds(t) c, ms := cluster(t, "g1", lists("self")) @@ -203,20 +204,10 @@ func TestCheckSingleFilerPasses(t *testing.T) { if err := c.CheckSingleFiler(context.Background()); err != nil || ms[0].callCount() != 4 { t.Fatalf("target registered on the 4th round: %v after %d calls; want a pass", err, ms[0].callCount()) } - // A dead or stalled first master does not hide a healthy second one, and - // a stalled call costs one attempt, not the whole bound. - c, _ = cluster(t, "", func(string) *nodesMaster { return &nodesMaster{stall: true} }, lists("self")) - start := time.Now() - if err := c.CheckSingleFiler(context.Background()); err != nil || time.Since(start) > 2*singleFilerAttempt { - t.Fatalf("stalled first master: %v after %s; want a pass within one attempt", err, time.Since(start)) - } - f := &configFiler{} - c, self := startConfigFiler(t, f) - f.mu.Lock() - f.masters = []string{deadAddress(t), startMaster(t, &nodesMaster{filers: []string{self}})} - f.mu.Unlock() + // Two masters, both listing the target: a pass (every master answered). + c, _ = cluster(t, "", lists("self"), lists("self")) if err := c.CheckSingleFiler(context.Background()); err != nil { - t.Fatalf("dead first master: %v", err) + t.Fatalf("two masters, both favourable: %v", err) } } @@ -261,35 +252,47 @@ func TestIsTargetComparesScopedAddresses(t *testing.T) { } } -// A favourable first master does not pass once the bound has run out while -// a later master is still being asked: that master's view is unknown. -func TestCheckSingleFilerFavourableThenStalledRefuses(t *testing.T) { - w, r, a := singleFilerWait, singleFilerRetry, singleFilerAttempt - singleFilerWait, singleFilerRetry, singleFilerAttempt = 800*time.Millisecond, 100*time.Millisecond, 5*time.Second - t.Cleanup(func() { singleFilerWait, singleFilerRetry, singleFilerAttempt = w, r, a }) - c, _ := cluster(t, "", lists("self"), func(string) *nodesMaster { return &nodesMaster{stall: true} }) - start := time.Now() - err := c.CheckSingleFiler(context.Background()) - if !errors.Is(err, ErrNotSingleFiler) || time.Since(start) < singleFilerWait { - t.Fatalf("favourable then stalled past the bound: %v after %s; want a refusal at the bound", err, time.Since(start)) - } -} - -// The same, when the stalled master's answer arrives just BEFORE the bound -// on the caller's clock: a gRPC server's deadline is the client's timeout -// sent with the call, so it runs out first and answers DeadlineExceeded -// while the caller's context is not done yet. That answer is cut by the -// bound, not a master's view: the earlier favourable answer must not pass -// alone (the flake of TestCheckSingleFilerFavourableThenStalledRefuses). -func TestCheckSingleFilerFavourableThenCutByTheBoundRefuses(t *testing.T) { - w, r, a := singleFilerWait, singleFilerRetry, singleFilerAttempt - singleFilerWait, singleFilerRetry, singleFilerAttempt = 800*time.Millisecond, 100*time.Millisecond, 5*time.Second - t.Cleanup(func() { singleFilerWait, singleFilerRetry, singleFilerAttempt = w, r, a }) - c, _ := cluster(t, "", lists("self"), func(string) *nodesMaster { return &nodesMaster{stall: true, early: 100 * time.Millisecond} }) - start := time.Now() - err := c.CheckSingleFiler(context.Background()) - if !errors.Is(err, ErrNotSingleFiler) { - t.Fatalf("favourable, then a master cut by the bound just before it: %v after %s; want a refusal", err, time.Since(start)) +// A round passes only when every master answered: with one favourable +// master, any other master that does not answer (it stalls past its attempt, +// answers DeadlineExceeded before its deadline, is down, or its address does +// not parse), in either order, leaves every round unconfirmed, and the start +// is refused at the bound naming it. No clock decides: the same master is a +// refusal whenever it does not answer. +func TestCheckSingleFilerNeedsEveryMasterToAnswer(t *testing.T) { + shortBounds(t) + for _, c := range []struct { + name string + other func(t *testing.T) (string, func(string) *nodesMaster) + }{ + {"stalled", func(t *testing.T) (string, func(string) *nodesMaster) { + return "", func(string) *nodesMaster { return &nodesMaster{stall: true} } + }}, + {"deadline answer before its deadline", func(t *testing.T) (string, func(string) *nodesMaster) { + return "", func(string) *nodesMaster { return &nodesMaster{stall: true, early: 100 * time.Millisecond} } + }}, + {"down", func(t *testing.T) (string, func(string) *nodesMaster) { return deadAddress(t), nil }}, + {"address does not parse", func(t *testing.T) (string, func(string) *nodesMaster) { return "m1:9333.notaport", nil }}, + } { + for _, otherFirst := range []bool{false, true} { + f := &configFiler{} + cl, self := startConfigFiler(t, f) + good := startMaster(t, &nodesMaster{filers: []string{self}}) + bad, mk := c.other(t) + if mk != nil { + bad = startMaster(t, mk(self)) + } + masters := []string{good, bad} + if otherFirst { + masters = []string{bad, good} + } + f.mu.Lock() + f.masters = masters + f.mu.Unlock() + err := cl.CheckSingleFiler(context.Background()) + if !errors.Is(err, ErrNotSingleFiler) || !strings.Contains(err.Error(), "every master must answer") || !strings.Contains(err.Error(), "rerun when they answer") { + t.Errorf("%s (other first %v): %v; want a refusal naming the master that did not answer", c.name, otherFirst, err) + } + } } } diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index def91c2568..2d64ae11f0 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -823,7 +823,7 @@ Notes: | `INV-VOL-GEOMETRY-FLAGS-RECORD-001` | A create-mode start whose `--durable-blocks`/`--durable-blocksize` disagree with the volume record exits 78 before any local store exists; an existing store is checked only at attach; an absent record keeps the create path; an unread record exits 75 (filer failed) or 78 (corrupt). | blockvolume startup (before the durable provider) | `cmd/blockvolume/main.go`; `cmd/blockvolume/chunk_remote.go` `geometryFlagsRefusal`; `core/frontend/chunkstore/layout/volume.go`, `start.go`, `create.go`; `core/frontend/chunkstore/catalog_b.go` | `TestGeometryFlagsRefusalComparesTheRecord`, `TestVolumeRequiresGeometry`, `TestVolumeGeometryChecksAndIsImmutable`, `TestStartGeometryMismatchPrecedesClaim`, `TestBCatalogRestoreCopiesVolumeGeometry`, `TestVolumeGeometryLivesOnlyInVolume` | K3: immutable required VOLUME geometry; Start checks the local persisted shape before claim; restores copy source geometry; no resize or compatibility path. | ACTIVE | | `INV-WS-STALE-LOCAL-ROOT-001` | An old or rolled-back local root is refused by `layout.Start` before serving; synchronous and deferred attach end without retry and report the typed stale-root error and cure. A fresh root at its own epoch likewise ends with `ErrMissingLocalRoot` and its cure. | M4 Claiming → Refused | `core/frontend/durable/provider_layout.go` `startLayoutRoot`; `core/frontend/durable/provider_chunk.go` `recoverChunk`, `chunkAuthorityWait`; `core/frontend/durable/provider_startup.go` `endChunkStartup` | `TestLayoutProviderRefusesEmptyOwnEpochAndRemountedOldRoot`, `TestFinalLayoutStartRefusalTable` (`core/frontend/durable/provider_layout_test.go`, `provider_layout_start_class_test.go`) | — | ACTIVE | | `INV-WS-ONE-PROCESS-PER-HOST-001` | One host runs at most one live blockvolume per volume: each takes the volume's exclusive host lock before its store opens and holds it until the store closes; a second is refused before it opens anything, whatever its `--durable-root`. | M4 (start) → Attaching, Refused | `core/frontend/durable/provider.go` `DurableProvider.lockVolumeOnHost` | `TestOneLiveProcessPerVolumePerHost`, `TestHostVolumeLockDirectoryCreatedOrRefused`, `TestHostVolumeLockRefusesNonDirectoryLockPath`, `TestHostVolumeLockTakesReadOnlyLockFile` (`core/frontend/durable/one_process_per_volume_test.go`); `TestHostVolumeLockCreatesParent0755AndLocks0700`, `TestHostVolumeLockKeepsExistingDirectoryModes` (`core/frontend/durable/volume_lock_parent_mode_test.go`); `TestVolumeLockDirTestOnlyRefusedUnderSystemd` (`cmd/blockvolume/volume_lock_flag_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`, `TestCheckSingleFilerFavourableThenCutByTheBoundRefuses`, `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`), `TestOpsChunk_VerifyRunsTheSingleFilerCheck` (`cmd/sw-block/chunk_verify_test.go`) | — | ACTIVE | +| `INV-WS-SINGLE-FILER-001` | A chunk volume starts only when `--chunk-filer` is its group's only filer, confirmed in a round where every master answered: a master listing another filer or several refuses at once; an unanswered master leaves the round unconfirmed; 60 s unconfirmed is refused. A startup census, not a fence. | M4 (startup) → Refused | `core/frontend/chunkstore/single_filer.go` `CheckSingleFiler`; `cmd/blockvolume/chunk_remote.go` `newChunkWiring`; `cmd/sw-block/chunk.go` `refuseUnlessSingleFiler` | `TestCheckSingleFilerRefusesAnotherOrSeveralFilers`, `TestCheckSingleFilerFailsClosed`, `TestCheckSingleFilerNeedsEveryMasterToAnswer`, `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`), `TestOpsChunk_VerifyRunsTheSingleFilerCheck` (`cmd/sw-block/chunk_verify_test.go`) | — | ACTIVE | | `INV-WS-LEASE-REGRANT-001` | Re-grant after expiry keeps the same holder and epoch, with no pending move or revocation, a certain store, and one durable write after the horizon/deadline plus skew barrier. Restart begins a new session and voids the old; ordinary reopen keeps HEAD identity (epoch, holder, claim) unchanged. | M2 Expired → Granted; M9 Start | `core/authority/workspace_lease_view.go` `regrantWorkspaceLease`; `core/authority/workspace.go` `forcedMoveBarrierLocked`; `core/frontend/chunkstore/layout/start.go` `Start` | `TestWorkspaceLeaseExpiredSameHolderRegrantIsDurable`, `TestWorkspaceLeaseRegrantWaitsForFullSkew`, `TestWorkspaceLeaseRegrantDefeatsAlreadyPassedForcedPreflight`, `TestWorkspaceLeaseForcedIssueDefeatsOldHolderRegrant`, `TestWorkspaceLeaseRegrantWaitsForDurabilityAndLatchesFailure` (`core/authority/workspace_regrant_test.go`); `TestWorkspaceLeaseRegrantPreservesLocalLogThroughFirstCut` (`core/authority/workspace_regrant_storage_test.go`); `TestStart_OwnEpochFreshRootRefusedBeforeClaim`, `TestStart_OwnEpochSameRootContinues` (`core/frontend/chunkstore/layout/start_test.go`); `TestClaim_WritesItsClaimID`, `TestRunClaim_VoidsTheOldSession` (`core/frontend/chunkstore/layout/claim_test.go`); `TestLocalReopenClaimsBeforeServing` (`core/frontend/durable/provider_reopen_belt_test.go`) | `WorkspaceRegrant` `NoDualServingInc`, `RegrantNoLoss`, `DiskCoversMemory`; `INV-SNAPK-START-ROOT-001` | 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`); `TestWorkspaceRevocationAckOutlastsTheMastersBoundedSteps`, `TestWorkspaceRevocationAckIsCutAtTheLeaseDeadline` (`core/host/volume/workspace_ack_budget_test.go`); `TestWorkspaceRevocationAckIsAskedAgainAfterALostAnswer`, `TestWorkspaceCloseoutFitsTheUnitsStopBudget` (`cmd/blockvolume/process_exit_ack_retry_test.go`) | `WorkspaceTakeover` `CleanNoLoss` | ACTIVE | diff --git a/seaweed-block/docs/standalone-deployment.md b/seaweed-block/docs/standalone-deployment.md index 93174828dd..6835f14c53 100644 --- a/seaweed-block/docs/standalone-deployment.md +++ b/seaweed-block/docs/standalone-deployment.md @@ -438,12 +438,15 @@ The `--chunk-*` and `--workspace-lease` flags are refused unless `--chunk-filer` must name the only filer of its filer group. At startup blockvolume asks that filer for its masters, then every master for the -filers in the group. It starts only when a master lists exactly that filer. -Any master listing another filer, or several, refuses at once. If no master -lists it yet (a fresh filer registers with its master some seconds after it -serves), or none answers, blockvolume asks again every second until 60 s, -then refuses. This is checked at startup only; do not add filers to the -group while volumes run. +filers in the group. It starts only when, in one round, every master +answers, none lists another filer or several, and at least one lists +exactly that filer. Any master listing another filer, or several, refuses at +once. If a master does not answer, or no master lists the filer yet (a fresh +filer registers with its master some seconds after it serves), blockvolume +asks again every second until 60 s, then refuses, naming the masters that +did not answer. A chunk volume therefore does not start while any master of +the filer's group is unreachable: start it again once they answer. This is +checked at startup only; do not add filers to the group while volumes run. ## Notes