diff --git a/seaweed-block/core/host/volume/host.go b/seaweed-block/core/host/volume/host.go index 660b05f6be..71afa9d6d5 100644 --- a/seaweed-block/core/host/volume/host.go +++ b/seaweed-block/core/host/volume/host.go @@ -168,12 +168,9 @@ type Host struct { // even on a host that was healthy at its prior epoch; only a later // successful role call for the current role and epoch clears it. roleAdmissionFailed atomic.Value // string reason; "" = not failed - // storesAwaited: the primary fact's (awaited<<32 | initial) replica - // store count (ReplicaStoresAwaited). - storesAwaited atomic.Uint64 - // storesAwaitedEpoch: the epoch of the primary fact storesAwaited came - // from. - storesAwaitedEpoch atomic.Uint64 + // storeWait: the primary fact's replica-store wait (ReplicaStoresAwaited, + // replicaStoresAwaitedEpoch), one value under its own lock. + storeWait storeWait // roleRetry: the last replica role call failed with an error a retry // may outlast (a floor that could not be persisted); runRoleRetry // re-applies the current role until it succeeds or is refused for good. @@ -778,6 +775,7 @@ func (h *Host) runHeartbeat(ctx context.Context) { send := func() { rpcCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() + h.noteFrontendServing() _, err := h.obsCli.ReportHeartbeat(rpcCtx, h.buildReport()) if ctx.Err() == nil { h.mw.call(err) diff --git a/seaweed-block/core/host/volume/probe_round.go b/seaweed-block/core/host/volume/probe_round.go index 732cd53ebe..3b2f0955cd 100644 --- a/seaweed-block/core/host/volume/probe_round.go +++ b/seaweed-block/core/host/volume/probe_round.go @@ -154,8 +154,6 @@ func (h *Host) probeAnswer(round uint64) *control.ProbeAnswer { // Not serving only because the creation wait of this line still holds: // said so, with the line's epoch, so the master does not take the // not-ready answer for a failure (and no other line can borrow it). - if !a.PrimaryReady { - a.AwaitingReplicaStoresEpoch = h.replicaStoresAwaitedEpoch() - } + a.AwaitingReplicaStoresEpoch = h.replicaStoresAwaitedEpoch(a.PrimaryReady) return a } diff --git a/seaweed-block/core/host/volume/replica_stores_wait_test.go b/seaweed-block/core/host/volume/replica_stores_wait_test.go index ea5b50c1c7..4b9d3e4103 100644 --- a/seaweed-block/core/host/volume/replica_stores_wait_test.go +++ b/seaweed-block/core/host/volume/replica_stores_wait_test.go @@ -75,9 +75,9 @@ func TestReplicaStoresWaitOnlyWhereStoresAreRequired(t *testing.T) { } } -// The probe answer says the primary awaits replica stores, with the epoch -// of the assignment that awaits them, only while that wait holds; once the -// assignment carries every store it says nothing. +// The probe answer says the primary awaits replica stores with the epoch +// of the line whose assignment awaits them; a new line (another epoch) that +// never waited says nothing. func TestProbeAnswerNamesTheLineAwaitingReplicaStores(t *testing.T) { h, _, rep := newStubbedHost(t, "vol1", "rB", &orderingRecorder{}) h.replicationMu.Lock() @@ -89,8 +89,38 @@ func TestProbeAnswerNamesTheLineAwaitingReplicaStores(t *testing.T) { if got := h.probeAnswer(1).GetAwaitingReplicaStoresEpoch(); got != 3 { t.Fatalf("awaiting epoch %d inside the wait; want 3 (the line's epoch)", got) } - h.noteReplicaStores(selfFact(3)) // the re-sent assignment carries every store + h.noteReplicaStores(selfFact(4)) // a new line, which never waited if got := h.probeAnswer(2).GetAwaitingReplicaStoresEpoch(); got != 0 { - t.Fatalf("awaiting epoch %d after the wait; want 0", got) + t.Fatalf("awaiting epoch %d on a line that never waited; want 0", got) + } +} + +// The stores-complete assignment arrives before the frontend opens (it opens +// only after): a probe in between must still say the line awaits replica +// stores, or the master takes the not-ready answer for a failure. Once the +// frontend has opened on the line the report ends for good: a later +// not-ready answer is a real one. +func TestProbeAnswerKeepsAwaitingUntilTheFrontendOpens(t *testing.T) { + h, _, rep := newStubbedHost(t, "vol1", "rB", &orderingRecorder{}) + h.replicationMu.Lock() + h.setReplicationLocked(requiringReplication{rep}) + h.replicationMu.Unlock() + open := false + h.SetProbeRoleSource(func() (bool, bool) { return false, open }) + + fact := selfFact(3) + fact.ReplicaStoresAwaited, fact.ReplicaStoresInitial = 1, 3 + h.noteReplicaStores(fact) + h.noteReplicaStores(selfFact(3)) // the stores-complete assignment, at the same epoch + if got := h.probeAnswer(1).GetAwaitingReplicaStoresEpoch(); got != 3 { + t.Fatalf("probe between the stores-complete assignment and the frontend: awaiting epoch %d; want 3", got) + } + open = true + if a := h.probeAnswer(2); !a.GetPrimaryReady() || a.GetAwaitingReplicaStoresEpoch() != 0 { + t.Fatalf("frontend open: ready=%t awaiting=%d; want ready, 0", a.GetPrimaryReady(), a.GetAwaitingReplicaStoresEpoch()) + } + open = false + if got := h.probeAnswer(3).GetAwaitingReplicaStoresEpoch(); got != 0 { + t.Fatalf("not ready after the frontend opened on the line: awaiting epoch %d; want 0 (a real not-ready)", got) } } diff --git a/seaweed-block/core/host/volume/workspace_startup.go b/seaweed-block/core/host/volume/workspace_startup.go index 617f29ef91..8a7ec76795 100644 --- a/seaweed-block/core/host/volume/workspace_startup.go +++ b/seaweed-block/core/host/volume/workspace_startup.go @@ -2,24 +2,70 @@ package volume import ( "fmt" + "sync" control "github.com/seaweedfs/seaweed-block/core/rpc/control" ) +// storeWait is the primary fact's replica-store wait, read and written as +// one value: the fact's epoch, its counts, and the line still starting from +// that wait. +type storeWait struct { + mu sync.Mutex + epoch uint64 + awaited, initial uint32 + // starting: the epoch of a line whose assignment awaited replica stores + // and whose frontend has not opened on it yet (0: none). It outlives the + // stores-complete assignment until the frontend opens. + starting uint64 +} + // noteReplicaStores keeps the primary fact's count of a new volume's initial // replicas whose store the master has not recorded yet. func (h *Host) noteReplicaStores(f *control.AssignmentFact) { - h.storesAwaitedEpoch.Store(f.GetEpoch()) - h.storesAwaited.Store(uint64(f.GetReplicaStoresAwaited())<<32 | uint64(f.GetReplicaStoresInitial())) + w := &h.storeWait + w.mu.Lock() + defer w.mu.Unlock() + w.epoch, w.awaited, w.initial = f.GetEpoch(), f.GetReplicaStoresAwaited(), f.GetReplicaStoresInitial() + switch { + case w.awaited > 0: + w.starting = w.epoch + case w.starting != w.epoch: + w.starting = 0 // another line: it never started from a wait + } } -// replicaStoresAwaitedEpoch is the epoch of the primary assignment whose -// creation wait holds (0: none), for the probe answer. -func (h *Host) replicaStoresAwaitedEpoch() uint64 { - if h.ReplicaStoresAwaited() == "" { +// replicaStoresAwaitedEpoch is, for the probe answer, the epoch of the line +// that still starts from its creation wait: its assignment awaits replica +// stores, or carried them but the frontend has not opened on it yet. 0 +// once the frontend is open (primaryReady), for good on that line. +func (h *Host) replicaStoresAwaitedEpoch(primaryReady bool) uint64 { + if _, ok := h.getReplication().(storeRequirer); !ok { return 0 } - return h.storesAwaitedEpoch.Load() + w := &h.storeWait + w.mu.Lock() + defer w.mu.Unlock() + if w.starting == 0 || w.starting != w.epoch { + return 0 + } + if primaryReady { + w.starting = 0 + return 0 + } + return w.starting +} + +// noteFrontendServing ends the line's start once its frontend is open: the +// heartbeat calls it, so the start ends without waiting for a probe. +func (h *Host) noteFrontendServing() { + roles, ok := h.probeRoles.Load().(probeRoleSource) + if !ok || roles == nil { + return + } + if _, ready := roles(); ready { + h.replicaStoresAwaitedEpoch(true) + } } // ReplicaStoresAwaited is the reason a new volume's primary serves nothing @@ -30,8 +76,10 @@ func (h *Host) ReplicaStoresAwaited() string { if _, ok := h.getReplication().(storeRequirer); !ok { return "" } - v := h.storesAwaited.Load() - waiting, initial := v>>32, v&0xffffffff + w := &h.storeWait + w.mu.Lock() + waiting, initial := w.awaited, w.initial + w.mu.Unlock() if waiting == 0 { return "" }