diff --git a/seaweed-block/core/host/master/observation_snapshot.go b/seaweed-block/core/host/master/observation_snapshot.go index ec632ac33c..5a9ddd28a3 100644 --- a/seaweed-block/core/host/master/observation_snapshot.go +++ b/seaweed-block/core/host/master/observation_snapshot.go @@ -175,7 +175,7 @@ func (h *Host) observationVolume(volumeID string, rec lifecycle.VolumeRecord, pl if h.attest != nil { // A condition only: the wait is a normal step of creating a volume, // not a degradation (the primary's own status says it serves nothing). - if waiting, initial := h.attest.awaited(volumeID); waiting > 0 { + if waiting, initial := h.attest.awaited(volumeID, h.replicaSlotsFor(volumeID)); waiting > 0 { volume.Conditions = append(volume.Conditions, ops.ObservationCondition{ Type: "ReplicaStoresAttested", Status: "false", diff --git a/seaweed-block/core/host/master/services.go b/seaweed-block/core/host/master/services.go index cc08132839..cc65942a56 100644 --- a/seaweed-block/core/host/master/services.go +++ b/seaweed-block/core/host/master/services.go @@ -1050,7 +1050,7 @@ func (s *services) assignmentFact(info adapter.AssignmentInfo) *control.Assignme fact.Peers = s.collectPeers(info.VolumeID, info) fact.PeerSetGeneration = (info.Epoch << 32) | info.EndpointVersion if s.host.attest != nil { - waiting, initial := s.host.attest.awaited(info.VolumeID) + waiting, initial := s.host.attest.awaited(info.VolumeID, s.host.replicaSlotsFor(info.VolumeID)) fact.ReplicaStoresAwaited, fact.ReplicaStoresInitial = uint32(waiting), uint32(initial) } return fact diff --git a/seaweed-block/core/host/master/store_attestation.go b/seaweed-block/core/host/master/store_attestation.go index ebeae914a9..2a93bdf4e7 100644 --- a/seaweed-block/core/host/master/store_attestation.go +++ b/seaweed-block/core/host/master/store_attestation.go @@ -25,8 +25,11 @@ type attestedStores struct { Primary string `json:"primary"` Epoch uint64 `json:"epoch"` Stores map[string]string `json:"stores"` // replica id -> store id (hex) - // Initial is a new volume's replica set at its first bind, while any of - // them has no recorded store; cleared once all have one. + // Initial is a new volume's replica set at its first bind; non-empty + // while the volume is creating (waiting for its replicas' stores). The + // wait itself follows the volume's current replicas (a replica replaced + // meanwhile is not waited for); it is cleared, for good, the first time + // every current replica has a recorded store. Initial []string `json:"initial,omitempty"` } @@ -129,18 +132,40 @@ func (s *storeAttestations) open(volumeID string, replicas []string) error { return nil } -// awaited reports how many of a new volume's initial replicas have no -// recorded store yet, and how many initial replicas it has. -func (s *storeAttestations) awaited(volumeID string) (waiting, initial int) { +// awaited reports, for a volume still creating, how many of its current +// replicas have no recorded store yet and how many it has; 0, 0 once the +// volume left creating (or never waited). When nothing is awaited any more +// the creation wait is cleared durably, so a later replacement of a +// replica never closes the volume again. +func (s *storeAttestations) awaited(volumeID string, current []string) (waiting, replicas int) { s.mu.Lock() defer s.mu.Unlock() - v := s.vols[volumeID] - for _, id := range v.Initial { + v, had := s.vols[volumeID] + if !had || len(v.Initial) == 0 { + return 0, 0 + } + for _, id := range current { if _, ok := v.Stores[id]; !ok { waiting++ } } - return waiting, len(v.Initial) + if waiting > 0 || len(current) == 0 { + return waiting, len(current) + } + vols := make(map[string]attestedStores, len(s.vols)) + for k, x := range s.vols { + vols[k] = x + } + v.Initial = nil + vols[volumeID] = v + if err := s.persist(vols); err != nil { + // Not cleared: the volume keeps answering from its stores (nothing + // awaited); the next call retries the write. + return 0, len(current) + } + s.vols = vols + s.notifyLocked(volumeID) + return 0, len(current) } // record merges att into the volume's record, in one write: each replica it @@ -175,12 +200,7 @@ func (s *storeAttestations) record(att *control.StoreAttestation) error { next.Stores[id] = sid } } - for _, id := range cur.Initial { - if _, ok := next.Stores[id]; !ok { - next.Initial = cur.Initial // still waiting - break - } - } + next.Initial = cur.Initial // cleared only by awaited if had && sameAttestation(cur, next) { return nil } diff --git a/seaweed-block/core/host/master/store_record_wait_test.go b/seaweed-block/core/host/master/store_record_wait_test.go index 6b3c4988e9..b7ba09058a 100644 --- a/seaweed-block/core/host/master/store_record_wait_test.go +++ b/seaweed-block/core/host/master/store_record_wait_test.go @@ -73,7 +73,7 @@ func TestNewVolumeAssignmentWaitsForReplicaAttestations(t *testing.T) { if err != nil { t.Fatal(err) } - if waiting, initial := reloaded.awaited("vol-rf3"); waiting != 0 || initial != 0 { + if waiting, initial := reloaded.awaited("vol-rf3", []string{"r1", "r2", "r3"}); waiting != 0 || initial != 0 { t.Fatalf("reloaded: %d of %d awaited; want the wait cleared", waiting, initial) } } @@ -85,7 +85,7 @@ func TestUpgradedVolumeAwaitsNoAttestation(t *testing.T) { if err != nil { t.Fatal(err) } - if waiting, initial := s.awaited("v-old"); waiting != 0 || initial != 0 { + if waiting, initial := s.awaited("v-old", []string{"r1", "r2"}); waiting != 0 || initial != 0 { t.Fatalf("no record: %d of %d", waiting, initial) } if err := s.open("v-new", []string{"r1", "r2"}); err != nil { @@ -94,7 +94,7 @@ func TestUpgradedVolumeAwaitsNoAttestation(t *testing.T) { if err := s.open("v-new", []string{"r1", "r2", "r3"}); err != nil { // a second open changes nothing t.Fatal(err) } - if waiting, initial := s.awaited("v-new"); waiting != 2 || initial != 2 { + if waiting, initial := s.awaited("v-new", []string{"r1", "r2"}); waiting != 2 || initial != 2 { t.Fatalf("new: %d of %d awaited; want 2 of 2", waiting, initial) } if err := s.record(attestation("v-old", "r1", 3, map[string]storage.StoreID{"r2": storeID(2)})); err != nil { @@ -103,7 +103,39 @@ func TestUpgradedVolumeAwaitsNoAttestation(t *testing.T) { if err := s.open("v-old", []string{"r1", "r2"}); err != nil { t.Fatal(err) } - if waiting, _ := s.awaited("v-old"); waiting != 0 { + if waiting, _ := s.awaited("v-old", []string{"r1", "r2"}); waiting != 0 { t.Fatalf("an attested volume opened again waits for %d", waiting) } } + +// An initial replica replaced during the creation wait (a new replica id in +// the placement) must not keep the volume waiting for the old id, which +// never attests: the wait follows the volume's current replicas. +func TestCreationWaitFollowsAReplacedInitialReplica(t *testing.T) { + s, err := loadStoreAttestations(t.TempDir()) + if err != nil { + t.Fatal(err) + } + if err := s.open("v", []string{"r1", "r2", "r3"}); err != nil { + t.Fatal(err) + } + // r3 is replaced by r4 before it attests; the primary attests r1, r2, r4. + if err := s.record(attestation("v", "r1", 1, map[string]storage.StoreID{"r1": storeID(1), "r2": storeID(2), "r4": storeID(4)})); err != nil { + t.Fatal(err) + } + if waiting, initial := s.awaited("v", []string{"r1", "r2", "r4"}); waiting != 0 { + t.Fatalf("after r3 was replaced by r4 and r1, r2, r4 attested: %d of %d awaited; want 0", waiting, initial) + } + // The wait is over for good: a later replacement (r5, no store yet) does + // not close the volume again; its replica just does not count. + if waiting, _ := s.awaited("v", []string{"r1", "r2", "r5"}); waiting != 0 { + t.Fatalf("a replacement after the wait ended re-opened it: %d awaited", waiting) + } + // Before r4 attests, the wait names r4, not r3. + s2, _ := loadStoreAttestations(t.TempDir()) + _ = s2.open("v", []string{"r1", "r2", "r3"}) + _ = s2.record(attestation("v", "r1", 1, map[string]storage.StoreID{"r1": storeID(1), "r2": storeID(2)})) + if waiting, initial := s2.awaited("v", []string{"r1", "r2", "r4"}); waiting != 1 || initial != 3 { + t.Fatalf("r4 not yet attested: %d of %d; want 1 of 3", waiting, initial) + } +}