diff --git a/seaweed-block/core/host/master/format_gate.go b/seaweed-block/core/host/master/format_gate.go index e7c318652c..2b9ab0ac0b 100644 --- a/seaweed-block/core/host/master/format_gate.go +++ b/seaweed-block/core/host/master/format_gate.go @@ -196,6 +196,13 @@ type grantWaitEntry struct { func (h *Host) grantWaits(placements []lifecycle.PlacementIntent, now time.Time) map[string][]grantWaitEntry { out := map[string][]grantWaitEntry{} live := map[string]bool{} + // One snapshot at a time computes the set and reconciles against it, so + // a concurrent snapshot never drops an entry this one just started. + h.grantWaitMu.Lock() + defer h.grantWaitMu.Unlock() + if h.grantWait == nil { + h.grantWait = map[string]time.Time{} + } if h.formatLevel() > 0 { window := h.grantStartupWindow() for _, p := range placements { @@ -211,18 +218,21 @@ func (h *Host) grantWaits(placements []lifecycle.PlacementIntent, now time.Time) } key := slot.ServerID + "\x00" + p.VolumeID live[key] = true - v, _ := h.grantWait.LoadOrStore(key, now) - waited := now.Sub(v.(time.Time)) + first, seen := h.grantWait[key] + if !seen { + first = now + h.grantWait[key] = now + } + waited := now.Sub(first) out[slot.ServerID] = append(out[slot.ServerID], grantWaitEntry{volume: p.VolumeID, waited: waited, late: waited > window}) } } } - h.grantWait.Range(func(k, _ any) bool { - if !live[k.(string)] { - h.grantWait.Delete(k) + for k := range h.grantWait { + if !live[k] { + delete(h.grantWait, k) } - return true - }) + } for server := range out { sort.Slice(out[server], func(i, j int) bool { return out[server][i].volume < out[server][j].volume }) } diff --git a/seaweed-block/core/host/master/format_level_new_cluster_test.go b/seaweed-block/core/host/master/format_level_new_cluster_test.go index 98db73caad..6ee8607687 100644 --- a/seaweed-block/core/host/master/format_level_new_cluster_test.go +++ b/seaweed-block/core/host/master/format_level_new_cluster_test.go @@ -289,10 +289,18 @@ func TestGrantWaitsAreReconciledPerServerAndVolume(t *testing.T) { if c, ok := cond(late, "node-c"); ok { t.Fatalf("node-c after vc's process reported: %+v; want no wait", c) } - if _, kept := r.m.grantWait.Load("node-c\x00vc"); kept { + if kept := r.m.grantWaitKept("node-c\x00vc"); kept { t.Fatal("vc's wait timer outlived its process's report") } - if _, kept := r.m.grantWait.Load("node-b\x00vb"); !kept { + if kept := r.m.grantWaitKept("node-b\x00vb"); !kept { t.Fatal("vb's wait timer was dropped by another node's reconcile") } } + +// grantWaitKept reports whether the master holds a first-seen time for key. +func (h *Host) grantWaitKept(key string) bool { + h.grantWaitMu.Lock() + defer h.grantWaitMu.Unlock() + _, ok := h.grantWait[key] + return ok +} diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index 13ee8ed0b2..40e151d623 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -152,10 +152,11 @@ type Host struct { statusLn net.Listener statusHTTP *http.Server journal *journalIsolation - reads *readsLevels // each server's reported format read level (format_gate.go) - grantWait sync.Map // "server\x00volume" -> time.Time: first seen waiting for its grant without its process's report (format_gate.go grantWaits) - format formatLevelState // the cluster's format level (format_gate.go) - finalizeMu sync.Mutex // serializes the format finalize and host retires (format_gate.go) + reads *readsLevels // each server's reported format read level (format_gate.go) + grantWaitMu sync.Mutex // one status snapshot reconciles grantWait at a time + grantWait map[string]time.Time // "server\x00volume" -> first seen waiting for its grant without its process's report (format_gate.go grantWaits) + format formatLevelState // the cluster's format level (format_gate.go) + finalizeMu sync.Mutex // serializes the format finalize and host retires (format_gate.go) // testPublicationAdmission, when a test sets it before Start, wraps each // publication admission: next runs the admission itself. testPublicationAdmission func(ask authority.AssignmentAsk, next func() error) error