diff --git a/seaweed-block/core/host/volume/host.go b/seaweed-block/core/host/volume/host.go index eb8cdc3361..9b976d4e49 100644 --- a/seaweed-block/core/host/volume/host.go +++ b/seaweed-block/core/host/volume/host.go @@ -626,6 +626,9 @@ func (h *Host) runHeartbeat(ctx context.Context) { _, err := h.obsCli.ReportHeartbeat(rpcCtx, h.buildReport()) if ctx.Err() == nil { h.mw.call(err) + // The wait's reason reaches the journal on a change and every + // 10 min from here, whether or not anyone reads /status. + _ = h.MasterWaitStatus() } if err != nil { h.log.Printf("blockvolume: heartbeat: %v", err) diff --git a/seaweed-block/core/host/volume/master_wait_test.go b/seaweed-block/core/host/volume/master_wait_test.go index f18d3c3394..20d27cb460 100644 --- a/seaweed-block/core/host/volume/master_wait_test.go +++ b/seaweed-block/core/host/volume/master_wait_test.go @@ -3,6 +3,10 @@ package volume import ( "context" "errors" + "fmt" + "log" + "strings" + "sync" "testing" "time" @@ -190,3 +194,81 @@ func TestStatusShowsTheMasterWait(t *testing.T) { t.Fatalf("MasterWait %+v shown without a lease wait", got.MasterWait) } } + +// Refused credentials for an hour: no bound, reason credentials, and the log +// line at the start and every 10 min, driven by the heartbeat cadence alone +// (no /status read) (F1). +func TestCredentialsRefusedForAnHourWaitsAndLogs(t *testing.T) { + now := time.Unix(1_800_000_000, 0) + var lines []string + w := newMasterWait(func() time.Time { return now }, func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) }) + denied := status.Error(codes.PermissionDenied, "node certificate revoked") + for i := 0; i < 1800; i++ { // one heartbeat every 2 s for 60 min + w.call(denied) + _ = w.snapshot(true) + now = now.Add(2 * time.Second) + } + if d, final := w.state(); !d.IsZero() || final != "" { + t.Fatalf("after an hour of refused credentials: bound %v final %q; want none", d, final) + } + if len(lines) != 6 { + t.Fatalf("%d log lines in 60 min; want 6 (at 0 and every 10 min): %q", len(lines), lines) + } + for _, l := range lines { + if !strings.Contains(l, "waiting for the master: credentials since") || !strings.Contains(l, "(no bound)") { + t.Fatalf("log line %q; want the credentials reason without a bound", l) + } + } +} + +// The heartbeat loop itself logs the wait's reason: a workspace host whose +// master is unreachable states it with no /status read (F1 wiring). +func TestHeartbeatLogsTheMasterWait(t *testing.T) { + var buf safeBuffer + h, err := New(Config{MasterAddr: "127.0.0.1:1", ServerID: "s1", VolumeID: "v1", ReplicaID: "r1", + DataAddr: "127.0.0.1:19303", CtrlAddr: "127.0.0.1:19304", WorkspaceLease: true, + HeartbeatInterval: 20 * time.Millisecond, Logger: log.New(&buf, "", 0)}) + if err != nil { + t.Fatal(err) + } + h.Start() + t.Cleanup(func() { _ = h.Close() }) + deadline := time.Now().Add(10 * time.Second) + for !strings.Contains(buf.String(), "waiting for the master: unreachable since") { + if time.Now().After(deadline) { + t.Fatalf("no wait reason logged from the heartbeat loop; log:\n%s", buf.String()) + } + time.Sleep(20 * time.Millisecond) + } +} + +type safeBuffer struct { + mu sync.Mutex + b strings.Builder +} + +func (s *safeBuffer) Write(p []byte) (int, error) { s.mu.Lock(); defer s.mu.Unlock(); return s.b.Write(p) } +func (s *safeBuffer) String() string { s.mu.Lock(); defer s.mu.Unlock(); return s.b.String() } + +// A flapping master (an answer, then no reply, every 10 s for 10 min) never +// ends the wait early: each answer starts a new streak, so the bound is never +// before the latest contact + 30 s. It postpones the bound for as long as the +// flapping lasts, also for an obsolete unit (documented: F7). +func TestFlappingMasterNeverExpiresEarly(t *testing.T) { + now := time.Unix(1_800_000_000, 0) + w := newMasterWait(func() time.Time { return now }, func(string, ...any) {}) + var lastContact time.Time + for i := 0; i < 60; i++ { + if i%2 == 0 { + w.call(nil) + lastContact = now + } else { + w.call(status.Error(codes.Unavailable, "flap")) + } + d, _ := w.state() + if !d.IsZero() && (d.Before(lastContact.Add(MasterWaitLimit)) || !now.Before(d)) { + t.Fatalf("step %d: bound %v ends the wait early (latest contact %v, now %v)", i, d, lastContact, now) + } + now = now.Add(10 * time.Second) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 9e21840ce7..0c734aa40e 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -777,6 +777,7 @@ Notes: | `INV-WS-MASTER-WAIT-001` | A workspace primary's startup wait has no bound while blockmaster gives no reply or refuses its credentials; once it answers, the wait ends 30 s after max(contact streak start, the master's barrier time), and a typed MOVED or DELETED lease refusal ends it at once with 78. | M2 Awaiting assignment → Process exited | `core/host/volume/master_wait.go` `masterWait` (contact from heartbeat, stream and renewal results; the refusal kind from the status detail only); `core/frontend/durable/provider_chunk.go` `chunkAuthorityWait` (`ChunkMasterWait`); `cmd/blockvolume/process_exit.go` `chooseChunkStartupExit`; `core/host/master/workspace_service.go` `leaseRefusal`, `renewalMoved`; `core/authority/workspace_lease_view.go` `LeaseBarrierError`; `core/host/volume/workspace_lease.go` `renewDelay` (sleeps until the barrier time); `core/rpc/link/link.go` `MasterReconnect` | `TestChunkAuthorityWaitStartsAtFirstContact`, `TestLeaseGuardSleepsUntilTheBarrier`, `TestStatusShowsTheMasterWait` (`core/host/volume/master_wait_test.go`); `TestMasterLinkReconnectsWithinTwoSeconds` (`core/host/volume/master_reconnect_test.go`); `TestChunkAuthorityWaitFollowsTheMasterWait` (`core/frontend/durable/provider_master_wait_test.go`); `TestLeaseRefusalKinds` (`core/host/master/workspace_lease_refusal_kind_test.go`) | — | ACTIVE | - `INV-WS-STARTUP-RETRY-001`: exit 78 is permitted by a NEWER observed master fact for this volume naming another holder (compared with nonzero observed-own assignment; never-assigned zero retries75), or by the exact refused HEAD claim image proving another holder at or above the assigned epoch. The provider binds the latter proof to its recovery generation. Both existing-root checks and claim CAS readback use the same epoch-qualified supersede classifier. A lower-epoch foreign HEAD on an existing root retains the move-back diagnostic; a displaced lower-epoch claim on a fresh root retries 75 without serving. A same-holder higher-epoch HEAD fence retries 75. Unknown/no line, same replica at a higher epoch, and status codes alone mean retry 75. A wrong retry costs a bounded restart loop; a wrong 78 causes an outage until an explicit operator start. Sealed same-epoch recovery remains up for read-only replay after assignment; stale local roots, corrupt WAL and I/O faults retain diagnostic wait. The first chosen outcome wins. Process cleanup reuses `WorkspaceDrainTimeout` (5 s), including a blocked Close; only main exits the process. +- `INV-WS-MASTER-WAIT-001`: every reply produced by blockmaster's handler starts or continues a contact streak; a transport failure (`Unavailable`, `DeadlineExceeded`, `Canceled`) ends it. So a master that answers and drops in turn postpones the 30 s bound for as long as it flaps, also for an obsolete unit; it never ends the wait early (`TestFlappingMasterNeverExpiresEarly`). The barrier time travels as a duration the master computes from the same two predicates as admission, added by the volume to its own monotonic request start; the master re-checks the barrier on every re-grant, so a wrong duration only moves the 75 exit. The status codes alone still mean retry 75 (`INV-WS-STARTUP-RETRY-001`); only the typed `WorkspaceLeaseRefusal` detail (MOVED, DELETED) permits the early 78. - Part T lineage evidence: `recordSelfAssignment` records the newest identity-validated observed-own assignment before local-readiness admission; `EngineProjection` is the adopted-own lineage. Exit compares the former; fencing compares the latter. Both use `IsSuperseded` with explicit lineage; zero lineage selects retry75 and no fence; neither fact grants I/O. `TestWorkspaceExitAndFenceUseNamedLineages`, `TestWorkspaceExitLineageIncludesBlockedOwnAssignment`, and `TestWorkspaceObservedLineageNeverTrailsAdoption` (`core/host/volume/workspace_startup_test.go`) keep the distinction causal. - Part T evidence scope: `workspaceRetryOutcome` is the one retry/no-retry decision. Startup `chooseChunkStartupExit` supplies the recovery-generation-bound refused-claim HEAD proof; runtime lease-end supplies none. Both combine that input with a foreign master line newer than nonzero observed-own; absent proof means retry 75. - Part T fence ownership: `Host.FenceWorkspaceLease` uses `WorkspaceLeaseGuard.fenceLocked`, including before runtime publication; the zero-validity terminal guard preserves the first cause through installation and can grant no I/O. No second host fence latch exists. `TestWorkspaceHostFencePublishesOneTerminalCause`, `TestWorkspaceInstalledHostFenceRecordsCause`, `TestWorkspaceFenceBeforeHeldInstallIsTerminal` (`core/host/volume/workspace_terminal_test.go`).