diff --git a/seaweed-block/core/host/master/format_gate.go b/seaweed-block/core/host/master/format_gate.go index d4fd49add8..0390c5cba2 100644 --- a/seaweed-block/core/host/master/format_gate.go +++ b/seaweed-block/core/host/master/format_gate.go @@ -16,52 +16,70 @@ import ( "google.golang.org/grpc/status" ) -// readsLevels holds the format read level each reporting process of a -// server sent (its latest heartbeat's reads_level), in memory only. One -// server id is shared by its per-volume processes, which may run different -// binaries, so a server reads the lowest level any of its processes reported -// within the freshness window (a process silent longer has gone). A -// registered server with no such report reads level 0: unknown counts as -// old, so a silent host never lets the format be finalized past it. +// readsLevels holds the format read level each of a server's processes +// reported (its latest heartbeat's reads_level), in memory only, one entry +// per volume: a server runs one process per volume, so a later report for +// the same volume is that volume's process (restarted or replaced) and +// supersedes the earlier one. A server reads the lowest level of its +// entries. Silence is not proof that a process is gone: an entry whose +// volume the server still serves (a placement, a live line) stays, however +// long its process has been silent; only a silent entry of a volume the +// server no longer serves leaves after the freshness window. A registered +// server with no entry reads level 0: unknown counts as old. type readsLevels struct { mu sync.Mutex window time.Duration now func() time.Time - procs map[string]map[string]procLevel // server -> process -> report + procs map[string]map[string]procLevel // server -> volume -> report } type procLevel struct { - level uint32 - seen time.Time + level uint32 + process string + seen time.Time } func newReadsLevels(window time.Duration) *readsLevels { return &readsLevels{window: window, now: time.Now, procs: map[string]map[string]procLevel{}} } -// note records the level process (of server) reported now. -func (r *readsLevels) note(server, process string, level uint32) { +// note records the level the process holding volume (on server) reported. +func (r *readsLevels) note(server, volume, process string, level uint32) { r.mu.Lock() defer r.mu.Unlock() if r.procs[server] == nil { r.procs[server] = map[string]procLevel{} } - r.procs[server][process] = procLevel{level: level, seen: r.now()} + r.procs[server][volume] = procLevel{level: level, process: process, seen: r.now()} } -// of is the lowest level server's processes reported within the window, -// and whether any did. -func (r *readsLevels) of(server string) (uint32, bool) { +// forget drops every entry of server (a retired host). +func (r *readsLevels) forget(server string) { r.mu.Lock() defer r.mu.Unlock() + delete(r.procs, server) +} + +// liveLocked returns server's entries that still count: every entry of a +// served volume, and the others reported within the window (a silent +// entry of an unserved volume is dropped). +func (r *readsLevels) liveLocked(server string, served map[string]bool) map[string]procLevel { cutoff := r.now().Add(-r.window) + for v, e := range r.procs[server] { + if e.seen.Before(cutoff) && !served[v] { + delete(r.procs[server], v) + } + } + return r.procs[server] +} + +// of is the lowest level of server's live entries, and whether it has any. +func (r *readsLevels) of(server string, served map[string]bool) (uint32, bool) { + r.mu.Lock() + defer r.mu.Unlock() var low uint32 found := false - for p, e := range r.procs[server] { - if e.seen.Before(cutoff) { - delete(r.procs[server], p) - continue - } + for _, e := range r.liveLocked(server, served) { if !found || e.level < low { low, found = e.level, true } @@ -69,36 +87,43 @@ func (r *readsLevels) of(server string) (uint32, bool) { return low, found } -// below names server's processes that reported a level below level within -// the window, sorted, each as "process (reads level N)". -func (r *readsLevels) below(server string, level uint32) []string { +// below names server's live entries below level, sorted, each as +// "process of volume (reads level N)". +func (r *readsLevels) below(server string, level uint32, served map[string]bool) []string { r.mu.Lock() defer r.mu.Unlock() - cutoff := r.now().Add(-r.window) var out []string - for p, e := range r.procs[server] { - if !e.seen.Before(cutoff) && e.level < level { - out = append(out, fmt.Sprintf("%s (reads level %d)", p, e.level)) + for v, e := range r.liveLocked(server, served) { + if e.level < level { + out = append(out, fmt.Sprintf("%s of %s (reads level %d)", e.process, v, e.level)) } } sort.Strings(out) return out } -// heartbeatProcess names the reporting process of a heartbeat: its slot's -// incarnation (new on every process start), else its slot's data address. -func heartbeatProcess(slots []*control.HeartbeatSlot) string { +// heartbeatProcess names a heartbeat's volume and reporting process: its +// slot's volume, and the slot's incarnation (new on every process start), +// else its data address. +func heartbeatProcess(slots []*control.HeartbeatSlot) (volume, process string) { for _, s := range slots { - if s.GetIncarnation() != "" { - return s.GetIncarnation() + volume = s.GetVolumeId() + process = s.GetIncarnation() + if process == "" { + process = s.GetDataAddr() } + return volume, process } - for _, s := range slots { - if s.GetDataAddr() != "" { - return s.GetDataAddr() - } + return "", "" +} + +// servedBy is the set of volumes server holds work for (volumesServedBy). +func (h *Host) servedBy(server string) map[string]bool { + out := map[string]bool{} + for _, v := range h.volumesServedBy(server) { + out[v] = true } - return "" + return out } // formatLevelState is the cluster's format level in memory: 0 until the @@ -117,7 +142,7 @@ func (h *Host) formatEligible(server string) bool { if level == 0 { return true } - reads, ok := h.reads.of(server) + reads, ok := h.reads.of(server, h.servedBy(server)) return ok && reads >= level } @@ -126,7 +151,7 @@ func (h *Host) formatTooOld(server string) string { if h.formatEligible(server) { return "" } - reads, ok := h.reads.of(server) + reads, ok := h.reads.of(server, h.servedBy(server)) if !ok { return fmt.Sprintf("node %q has not reported its format read level; the cluster's format level is %d", server, h.formatLevel()) } @@ -138,12 +163,13 @@ func (h *Host) formatTooOld(server string) string { func (h *Host) belowLevel(servers []string, level uint32) []string { var out []string for _, s := range servers { - reads, ok := h.reads.of(s) + served := h.servedBy(s) + reads, ok := h.reads.of(s, served) switch { case !ok: out = append(out, fmt.Sprintf("%s (no report)", s)) case reads < level: - out = append(out, fmt.Sprintf("%s (process %s)", s, strings.Join(h.reads.below(s, level), ", "))) + out = append(out, fmt.Sprintf("%s (process %s)", s, strings.Join(h.reads.below(s, level, served), ", "))) } } sort.Strings(out) @@ -225,6 +251,7 @@ func (h *Host) RetireHost(server, confirm string) error { if err := h.lifecycle.Nodes.RemoveNode(server); err != nil { return fmt.Errorf("retire: %w", err) } + h.reads.forget(server) h.log.Printf("blockmaster: host %s retired from the node inventory", server) return nil } diff --git a/seaweed-block/core/host/master/format_gate_test.go b/seaweed-block/core/host/master/format_gate_test.go index 5458934e3c..605e3050d2 100644 --- a/seaweed-block/core/host/master/format_gate_test.go +++ b/seaweed-block/core/host/master/format_gate_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/seaweedfs/seaweed-block/core/adapter" + "github.com/seaweedfs/seaweed-block/core/authority" "github.com/seaweedfs/seaweed-block/core/lifecycle" control "github.com/seaweedfs/seaweed-block/core/rpc/control" "github.com/seaweedfs/seaweed-block/core/rpc/lifecycleclient" @@ -176,7 +177,7 @@ func TestFinalizeFormatNeedsEveryRegisteredHostAndIsDurable(t *testing.T) { for _, step := range []struct { reads *uint32 want string - }{{nil, "node-b (no report)"}, {ptr(uint32(0)), "node-b (process 127.0.0.1:19311 (reads level 0))"}} { + }{{nil, "node-b (no report)"}, {ptr(uint32(0)), "node-b (process 127.0.0.1:19311 of hb-node-b (reads level 0))"}} { if step.reads != nil { if err := r.fgHeartbeat("node-b", *step.reads); err != nil { t.Fatal(err) @@ -308,7 +309,7 @@ func TestAServerReadsTheLowestLevelOfItsProcesses(t *testing.T) { t.Fatal(err) } _, err := r.lc.FinalizeFormat(r.ctx, &control.FinalizeFormatRequest{Level: 1}) - if reasonOf(err) != lifecycleclient.ReasonFormatHostsTooOld || !strings.Contains(status.Convert(err).Message(), "old-proc (reads level 0)") { + if reasonOf(err) != lifecycleclient.ReasonFormatHostsTooOld || !strings.Contains(status.Convert(err).Message(), "old-proc of va (reads level 0)") { t.Fatalf("finalize with an old process beside a new one on node-b: %v", err) } r.m.format.v.Store(1) @@ -323,56 +324,97 @@ func TestAServerReadsTheLowestLevelOfItsProcesses(t *testing.T) { } } -// A process silent longer than the freshness window has gone: it no longer -// holds its server's level down. -func TestAGoneProcessNoLongerHoldsItsServersLevel(t *testing.T) { +// Silence is not proof that a process is gone: an entry of a volume its +// server still serves stays, however long its process is silent; an +// unserved volume's silent entry leaves after the window; a later report +// for the same volume (its restarted or replaced process) supersedes it. +func TestAServerKeepsASilentProcessThatStillHoldsItsVolume(t *testing.T) { rl := newReadsLevels(time.Minute) now := time.Unix(1000, 0) rl.now = func() time.Time { return now } - rl.note("s", "old", 0) - rl.note("s", "new", 1) - if l, ok := rl.of("s"); !ok || l != 0 { - t.Fatalf("both live: level %d %v, want 0", l, ok) - } + rl.note("s", "va", "old", 0) + rl.note("s", "vb", "new", 1) now = now.Add(2 * time.Minute) - rl.note("s", "new", 1) - if l, ok := rl.of("s"); !ok || l != 1 { - t.Fatalf("the old process silent past the window: level %d %v, want 1", l, ok) + rl.note("s", "vb", "new", 1) + if l, ok := rl.of("s", map[string]bool{"va": true}); !ok || l != 0 { + t.Fatalf("va still served, its process silent past the window: level %d %v, want 0", l, ok) + } + if l, ok := rl.of("s", map[string]bool{}); !ok || l != 1 { + t.Fatalf("va no longer served and silent: level %d %v, want 1", l, ok) + } + rl.note("s", "va", "old", 0) + rl.note("s", "va", "restarted", 1) + if l, ok := rl.of("s", map[string]bool{"va": true}); !ok || l != 1 { + t.Fatalf("va's process replaced by one reading 1: level %d %v, want 1", l, ok) } } -// A bind decided on a server's observations is not published once another -// of that server's processes reports a lower read level: the publication -// boundary judges the server's current capability (codex01's AMEND P1 2). -// The control, with no old process, binds. -func TestAPublicationToAServerThatTurnedOutOldIsRefused(t *testing.T) { +// codex01's schedule: an old process holds a placed volume, a new sibling +// keeps reporting, the old one falls silent past the freshness window. The +// finalize stays refused naming the old process, and at level 1 a placement +// or a move onto the server stays refused. +func TestASilentOldHolderKeepsTheFinalizeAndNewWorkRefused(t *testing.T) { + r := newIsolationRig(t, false, t.TempDir(), t.TempDir()) + now := time.Now() + r.m.reads.now = func() time.Time { return now } + if err := r.fgHeartbeat("node-a", 1); err != nil { + t.Fatal(err) + } + r.createVolume(t, "va") + r.place(t, "va", "node-b") + if err := r.fgProcessHeartbeat("node-b", "old-proc", "va", 0); err != nil { + t.Fatal(err) + } + now = now.Add(time.Hour) // past any freshness window: the old process is silent + if err := r.fgHeartbeat("node-a", 1); err != nil { + t.Fatal(err) + } + if err := r.fgProcessHeartbeat("node-b", "new-proc", "vb", 1); err != nil { + t.Fatal(err) + } + _, err := r.lc.FinalizeFormat(r.ctx, &control.FinalizeFormatRequest{Level: 1}) + if reasonOf(err) != lifecycleclient.ReasonFormatHostsTooOld || !strings.Contains(status.Convert(err).Message(), "old-proc of va (reads level 0)") { + t.Fatalf("finalize with a silent old holder of va: %v", err) + } + r.m.format.v.Store(1) + if why := r.m.formatTooOld("node-b"); why == "" { + t.Fatal("level 1: node-b takes new work while its silent old process still holds va") + } + r.createVolume(t, "fresh") + if _, err := r.ws.RegisterWorkspaceBinding(r.ctx, &control.WorkspaceBinding{VolumeId: "fresh", ServerId: "node-b", DataAddr: "127.0.0.1:19321", CtrlAddr: "127.0.0.1:19322"}); err != nil { + t.Fatal(err) + } + if _, err := r.ws.PlaceWorkspace(r.ctx, &control.WorkspacePlacement{VolumeId: "fresh", ServerId: "node-b", ReplicaId: "r1"}); status.Code(err) != codes.FailedPrecondition { + t.Fatalf("level 1: a placement onto node-b: %v, want refused", err) + } +} + +// The publication boundary judges the server's level when the ask is +// published: an ask whose server reads below the cluster's level is refused +// there and its publish never runs; the control publishes (codex01's AMEND +// P2: the admission itself, called with a recording publish). +func TestThePublicationBoundaryRefusesAnAskForAnOldServer(t *testing.T) { for _, old := range []bool{true, false} { t.Run(map[bool]string{true: "old-process", false: "control"}[old], func(t *testing.T) { r := newIsolationRig(t, false, t.TempDir(), t.TempDir()) - if old { - if err := r.fgProcessHeartbeat("node-b", "old-proc", "other", 0); err != nil { - t.Fatal(err) - } - } - r.m.format.v.Store(1) r.createVolume(t, "v") r.place(t, "v", "node-b") - if _, err := r.obs.ReportHeartbeat(r.ctx, &control.HeartbeatReport{ServerId: "node-b", SentAt: timestamppb.Now(), Reachable: true, Eligible: true, ReadsLevel: 1, - Slots: []*control.HeartbeatSlot{{VolumeId: "v", ReplicaId: "r1", DataAddr: "127.0.0.1:19301", CtrlAddr: "127.0.0.1:19302", Incarnation: "new-proc", Reachable: true, Eligible: true}}}); err != nil { + level := uint32(1) + if old { + level = 0 + } + if err := r.fgProcessHeartbeat("node-b", "proc", "v", level); err != nil { t.Fatal(err) } - if _, err := r.m.RunLifecycleProductTick(); err != nil { - t.Fatal(err) + r.m.format.v.Store(1) + published := false + err := r.m.admitWorkspacePublication(authority.AssignmentAsk{VolumeID: "v", ReplicaID: "r1", DataAddr: "127.0.0.1:19301", CtrlAddr: "127.0.0.1:19302", Intent: authority.IntentBind}, + func() error { published = true; return nil }) + if old && (err == nil || published || !strings.Contains(err.Error(), "binary_too_old")) { + t.Fatalf("level 1, node-b's process reads 0: err %v, published %v; want refused before the publish", err, published) } - bound := false - for end := time.Now().Add(3 * time.Second); time.Now().Before(end) && !bound; time.Sleep(50 * time.Millisecond) { - _, bound = r.m.Publisher().VolumeAuthorityLine("v") - } - if old && bound { - t.Fatal("v was bound on node-b while an old process of node-b is live at level 1") - } - if !old && !bound { - t.Fatal("control: v was not bound on node-b, whose only process reads level 1") + if !old && (err != nil || !published) { + t.Fatalf("control: err %v, published %v; want published", err, published) } }) } diff --git a/seaweed-block/core/host/master/services.go b/seaweed-block/core/host/master/services.go index bac154e131..ce1faea551 100644 --- a/seaweed-block/core/host/master/services.go +++ b/seaweed-block/core/host/master/services.go @@ -541,7 +541,8 @@ func (s *services) ReportHeartbeat(ctx context.Context, r *control.HeartbeatRepo // it is judged (its server then reads at most that level for new work); // a process whose binary cannot read the cluster's format level is // refused, so it is never observed as a candidate. - s.host.reads.note(r.GetServerId(), heartbeatProcess(r.GetSlots()), r.GetReadsLevel()) + volume, process := heartbeatProcess(r.GetSlots()) + s.host.reads.note(r.GetServerId(), volume, process, r.GetReadsLevel()) if level := s.host.formatLevel(); r.GetReadsLevel() < level { why := fmt.Sprintf("node %q process reads format level %d; the cluster's format level is %d (binary_too_old)", r.GetServerId(), r.GetReadsLevel(), level) return nil, lifecycleclient.WithReason(status.New(codes.FailedPrecondition, "heartbeat refused: "+why), lifecycleclient.ReasonBinaryTooOld).Err()