diff --git a/seaweed-block/core/host/master/format_gate.go b/seaweed-block/core/host/master/format_gate.go index c17f5817a5..28cbbf3107 100644 --- a/seaweed-block/core/host/master/format_gate.go +++ b/seaweed-block/core/host/master/format_gate.go @@ -484,6 +484,9 @@ func (h *Host) recordReadsLevel(server string) error { if !ok || h.lifecycle == nil { return nil } + if hook := h.testAfterReadsSettle; hook != nil { + hook() + } if _, err := h.lifecycle.Nodes.RecordReadsLevel(server, level); err != nil { if h.readsUnkept == nil { h.readsUnkept = map[string]bool{} diff --git a/seaweed-block/core/host/master/format_gate_test.go b/seaweed-block/core/host/master/format_gate_test.go index b932cec5c2..1204bb5a5f 100644 --- a/seaweed-block/core/host/master/format_gate_test.go +++ b/seaweed-block/core/host/master/format_gate_test.go @@ -851,27 +851,49 @@ func TestTheLastReadLevelIsKeptInOrderAndFailsClosed(t *testing.T) { return *rec.ReadsLevel, true } - // Ordering: the level-1 write is held inside its persist (a barrier says - // so); a level-0 report comes meanwhile and is kept after it. + // Ordering: the level-1 keeper is held after settling its level, before + // it writes (a barrier ahead of the node store's own lock). A level-0 + // report comes meanwhile; its keeper must wait for the first, so 0 is the + // level left on the record. Without that ordering the second keeper + // writes 0 at once and the held first then writes 1 over it. entered, gate := make(chan struct{}), make(chan struct{}) - var once sync.Once - nodes.SetPersistHookForTest(func() error { - once.Do(func() { close(entered); <-gate }) - return nil - }) + var once, opened sync.Once + openGate := func() { opened.Do(func() { close(gate) }) } + r.m.testAfterReadsSettle = func() { once.Do(func() { close(entered); <-gate }) } + first, second := make(chan error, 1), make(chan error, 1) + var waiting []chan error + defer func() { // on every exit: open the gate and join the keepers + openGate() + for _, ch := range waiting { + select { + case <-ch: + case <-time.After(10 * time.Second): + t.Error("a keeper did not return") + } + } + r.m.testAfterReadsSettle = nil + }() r.m.reads.note("node-b", "", "p1", 1) - first := make(chan error, 1) + waiting = append(waiting, first) go func() { first <- r.m.recordReadsLevel("node-b") }() select { case <-entered: case <-time.After(10 * time.Second): - t.Fatal("the first keeper never reached its write") + t.Fatal("the first keeper never settled its level") } r.m.reads.note("node-b", "", "p1", 0) - second := make(chan error, 1) + waiting = append(waiting, second) go func() { second <- r.m.recordReadsLevel("node-b") }() - close(gate) - for _, ch := range []chan error{first, second} { + select { + case err := <-second: // only an unordered keeper gets here first + waiting = waiting[:1] + if err != nil { + t.Fatal(err) + } + case <-time.After(200 * time.Millisecond): // the second waits for the first + } + openGate() + for _, ch := range waiting { select { case err := <-ch: if err != nil { @@ -881,7 +903,7 @@ func TestTheLastReadLevelIsKeptInOrderAndFailsClosed(t *testing.T) { t.Fatal("a keeper did not return") } } - nodes.SetPersistHookForTest(nil) + waiting = nil if l, ok := level(); !ok || l != 0 { t.Fatalf("node record level %d (%v) after reports 1 then 0, want 0", l, ok) } @@ -912,6 +934,17 @@ func TestTheLastReadLevelIsKeptInOrderAndFailsClosed(t *testing.T) { if err := r.fgHeartbeat("node-b", 1); err != nil { t.Fatalf("heartbeat while its level cannot be kept: %v, want accepted", err) } + // The heartbeat's facts were ingested, not only acknowledged: a new + // slot it reports is in the observation store. + if err := r.fgProcessHeartbeat("node-b", "proc-new", "seen-unkept", 1); err != nil { + t.Fatalf("heartbeat while its level cannot be kept: %v, want accepted", err) + } + if _, ok := r.m.ObservationHost().Store().SlotFactOnServer("node-b", "seen-unkept", "r-proc-new"); !ok { + t.Fatal("a heartbeat accepted while the level write failed was not ingested") + } + if !strings.Contains(r.logs.String(), "keeping its read level") { + t.Fatalf("the failed level write was not logged: %q", r.logs.String()) + } if _, err := r.ws.RenewWorkspaceLease(r.ctx, leaseID); err != nil { t.Fatalf("lease renewal while node-record writes fail: %v, want renewed", err) } diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index fa485ddf71..c015a61094 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -163,6 +163,9 @@ type Host struct { // kept; the finalize refuses them until a later write succeeds. readsKeepMu sync.Mutex readsUnkept map[string]bool + // testAfterReadsSettle runs after a level is settled, before it is + // written (under readsKeepMu). Tests only. + testAfterReadsSettle func() // testGrantWaitsRead, when a test sets it, runs after a status snapshot // has read its placements, still holding grantWaitMu. testGrantWaitsRead func() diff --git a/seaweed-block/docs/releases/v1.1.md b/seaweed-block/docs/releases/v1.1.md index ff426c22cb..bb6ca77d51 100644 --- a/seaweed-block/docs/releases/v1.1.md +++ b/seaweed-block/docs/releases/v1.1.md @@ -48,7 +48,9 @@ These notes collect v1.1's changes as they land; v1.1 is not released. lease or an assignment) or that has reported does not read v1.1, and names each one: upgrade it, or retire it (`sw-block cluster retire-host --server --confirm --master `, refused while the host still holds a volume). A spare host with no volume and no report does not block the finalize; if it later runs an old binary, it - is refused (`binary_too_old`) when it asks for its first grant. + is refused (`binary_too_old`) when it asks for its first grant. A host with no volume is judged by the last level it + reported, kept on its node record; after a master restart, finalize uses each idle host's last recorded level until + its next heartbeat. - Before the finalize, going back to v1.0.x works: stop the v1.1 binaries and start v1.0.x. Name-only snapshot deletes keep v1.0's semantics until then (a retry after a lost answer may act twice). - The finalize is one-way. Afterwards a v1.0.x blockmaster refuses to start on the store, and a v1.0.x holder that