diff --git a/seaweed-block/core/authority/workspace_restart_grace_test.go b/seaweed-block/core/authority/workspace_restart_grace_test.go index 2ab7e7b274..a923b41719 100644 --- a/seaweed-block/core/authority/workspace_restart_grace_test.go +++ b/seaweed-block/core/authority/workspace_restart_grace_test.go @@ -58,10 +58,12 @@ func TestForcedAdmissionIgnoresAPersistedHorizon(t *testing.T) { } } -// The authority record's envelope is version 2: a v1.0.x master, which -// trusts the persisted horizon a v1.1 master no longer keeps current, -// refuses every envelope version but 1. Today's decoder reads both. -func TestARecordWrittenNowIsRefusedByAV100Master(t *testing.T) { +// The authority record's envelope is version 2: v1.0.x's decoder refuses +// every envelope version but 1 (its loader then skips the record, so this +// alone does not stop a v1.0.x master: the store's attestation envelope +// fails its whole start, TestAV100MasterRefusesTheAttestationEnvelope in +// core/host/master). Today's decoder reads both. +func TestARecordWrittenNowFailsTheV100Decoder(t *testing.T) { r := DurableRecord{VolumeID: "v", ReplicaID: "r", Epoch: 1} raw, err := encodeEnvelope(r) if err != nil { @@ -75,7 +77,7 @@ func TestARecordWrittenNowIsRefusedByAV100Master(t *testing.T) { } // v1.0.x's decodeEnvelope: "if env.Version != 1 { ErrCorruptRecord }". if env.Version == storeEnvelopeVersionV100 { - t.Fatalf("a record written now carries envelope version %d, which a v1.0.x master accepts", env.Version) + t.Fatalf("a record written now carries envelope version %d, which v1.0.x's decoder accepts", env.Version) } if _, err := decodeEnvelope(raw, "v"); err != nil { t.Fatalf("today's decoder refuses today's record: %v", err) diff --git a/seaweed-block/core/host/master/golden_v100_test.go b/seaweed-block/core/host/master/golden_v100_test.go index 90cc402640..f37cfa8ec3 100644 --- a/seaweed-block/core/host/master/golden_v100_test.go +++ b/seaweed-block/core/host/master/golden_v100_test.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/sha1" "encoding/hex" + "encoding/json" "fmt" "os" "path/filepath" @@ -76,19 +77,74 @@ func TestAttestationGoldenFileIsTheV100File(t *testing.T) { } } -// Today's store writes the v1.0.0 file for the same steps. -func TestAttestationsWriteTheV100File(t *testing.T) { +// Today's store writes the v1.0.0 file's volumes for the same steps, inside +// this version's envelope: {"format":2,"volumes":}. The +// one change since v1.0.0, by the format path: a v1.0.x master refuses the +// envelope at start (TestAV100MasterRefusesTheAttestationEnvelope). +func TestAttestationsWriteTheV100FileInTheEnvelope(t *testing.T) { dir := t.TempDir() goldenAttestations(t, dir) got, err := os.ReadFile(filepath.Join(dir, storeAttestationFile)) if err != nil { t.Fatal(err) } - if want := readGoldenAttestations(t); !bytes.Equal(got, want) { + want := append(append([]byte(`{"format":2,"volumes":`), readGoldenAttestations(t)...), '}') + if !bytes.Equal(got, want) { t.Fatalf("%s\n got %s\n want %s", goldenAttestationFix, got, want) } } +// A v1.0.x master decodes store-attestations.json as a bare map of volume +// records (json.Unmarshal into map[string]attestedStores, its +// loadStoreAttestations), and its New returns that error before it listens +// or publishes. The envelope fails that decode; today's master reads both +// forms, and a master started on a v1.0.x store rewrites it as the envelope +// before it serves. +func TestAV100MasterRefusesTheAttestationEnvelope(t *testing.T) { + dir := t.TempDir() + goldenAttestations(t, dir) + b, err := os.ReadFile(filepath.Join(dir, storeAttestationFile)) + if err != nil { + t.Fatal(err) + } + var v100 map[string]attestedStores // v1.0.x's decode, verbatim + if err := json.Unmarshal(b, &v100); err == nil { + t.Fatalf("a v1.0.x master's decode accepted the file written now: %v", v100) + } + if bad := readAttestations(dir); len(bad) != 0 { + t.Fatalf("today's master does not read its own envelope: %v", bad) + } + // A v1.0.x store (the bare map) is read, and ensureEnvelope rewrites it. + old := t.TempDir() + if err := os.WriteFile(filepath.Join(old, storeAttestationFile), readGoldenAttestations(t), 0o644); err != nil { + t.Fatal(err) + } + s, err := loadStoreAttestations(old) + if err != nil { + t.Fatal(err) + } + if bad := readAttestations(old); len(bad) != 0 { + t.Fatalf("today's master does not read the v1.0.x form: %v", bad) + } + if err := s.ensureEnvelope(); err != nil { + t.Fatal(err) + } + b, err = os.ReadFile(filepath.Join(old, storeAttestationFile)) + if err != nil { + t.Fatal(err) + } + if err := json.Unmarshal(b, &v100); err == nil { + t.Fatal("after ensureEnvelope, a v1.0.x master's decode still accepts the store") + } + // An envelope of a later format is refused by this master too. + if err := os.WriteFile(filepath.Join(old, storeAttestationFile), []byte(`{"format":3,"volumes":{}}`), 0o644); err != nil { + t.Fatal(err) + } + if _, err := loadStoreAttestations(old); err == nil { + t.Fatal("this master accepted attestation format 3") + } +} + // wantAttestations is the v1.0.0 file's content, written out independently // of the store: every volume's primary, epoch, stores and initial replicas. var wantAttestations = map[string]attestedStores{ diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index e2979f3207..802c61cdcf 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -282,6 +282,12 @@ func New(cfg Config) (*Host, error) { if h.attest, err = loadStoreAttestations(cfg.AuthorityStoreDir); err != nil { return nil, fmt.Errorf("master.New: %w", err) } + // Under the authority store's lock and before anything is served: the + // attestation file in this version's envelope, which a v1.0.x master + // refuses at start (store_attestation.go). + if err := h.attest.ensureEnvelope(); err != nil { + return nil, fmt.Errorf("master.New: %w", err) + } var sink authority.ControllerSink = ctrl if len(cfg.Topology.Volumes) == 0 { diff --git a/seaweed-block/core/host/master/store_attestation.go b/seaweed-block/core/host/master/store_attestation.go index 5195f2e107..90c24f2bde 100644 --- a/seaweed-block/core/host/master/store_attestation.go +++ b/seaweed-block/core/host/master/store_attestation.go @@ -19,6 +19,19 @@ import ( // write (temp file, fsync, rename, directory fsync). const storeAttestationFile = "store-attestations.json" +// storeAttestationFormat is the envelope this master writes: +// {"format": 2, "volumes": {...}}. A v1.0.x master decodes the file as a +// bare map of volume records, so the number under "format" fails its whole +// start (before any listener or authority publication): the downgrade guard +// for what this version stops keeping current (the lease horizon). The +// bare map (v1.0.x's form) is still read. +const storeAttestationFormat = 2 + +type storeAttestationEnvelope struct { + Format int `json:"format"` + Volumes map[string]attestedStores `json:"volumes"` +} + // attestedStores is one volume's last attestation: the primary line that // made it and, per replica, the store that counted toward its write ACKs. type attestedStores struct { @@ -55,6 +68,9 @@ type storeAttestations struct { mu sync.Mutex path string // "" = memory only vols map[string]attestedStores + // enveloped: the file on disk is this version's envelope (persist always + // writes it; ensureEnvelope writes it at start). + enveloped bool // changed[vol] is closed (and dropped) when vol's record changes. changed map[string]chan struct{} } @@ -72,15 +88,61 @@ func loadStoreAttestations(dir string) (*storeAttestations, error) { if err != nil { return nil, fmt.Errorf("store attestations: %w", err) } - if err := json.Unmarshal(b, &s.vols); err != nil { + vols, enveloped, err := decodeStoreAttestations(b) + if err != nil { return nil, fmt.Errorf("store attestations %s: %w", s.path, err) } - if s.vols == nil { - s.vols = map[string]attestedStores{} - } + s.vols, s.enveloped = vols, enveloped return s, nil } +// decodeStoreAttestations reads either form: the envelope (a number under +// "format": this master reads format 2 only) or v1.0.x's bare map. +func decodeStoreAttestations(b []byte) (map[string]attestedStores, bool, error) { + var probe map[string]json.RawMessage + if err := json.Unmarshal(b, &probe); err != nil { + return nil, false, err + } + if raw, ok := probe["format"]; ok { + var format int + if json.Unmarshal(raw, &format) == nil { + if format != storeAttestationFormat { + return nil, false, fmt.Errorf("format %d, this master reads format %d", format, storeAttestationFormat) + } + var env storeAttestationEnvelope + if err := json.Unmarshal(b, &env); err != nil { + return nil, false, err + } + if env.Volumes == nil { + env.Volumes = map[string]attestedStores{} + } + return env.Volumes, true, nil + } + } + vols := map[string]attestedStores{} + if err := json.Unmarshal(b, &vols); err != nil { + return nil, false, err + } + return vols, false, nil +} + +// ensureEnvelope writes the file in this version's envelope, durably, when +// it is absent or in v1.0.x's form: the master calls it before it serves +// anything, so a v1.0.x master started on this store afterwards refuses its +// whole start. +func (s *storeAttestations) ensureEnvelope() error { + s.mu.Lock() + defer s.mu.Unlock() + if s.path == "" || s.enveloped { + return nil + } + if err := s.persist(s.vols); err != nil { + return err + } + s.enveloped = true + return nil +} + // lookup returns the store a primary of the volume last attested for // replica; false when none ever did. A replica dropped from the write-ACK // set since keeps it. @@ -334,7 +396,7 @@ func (s *storeAttestations) persist(vols map[string]attestedStores) error { if s.path == "" { return nil } - payload, err := json.Marshal(vols) + payload, err := json.Marshal(storeAttestationEnvelope{Format: storeAttestationFormat, Volumes: vols}) if err != nil { return err } diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 0ea5dcc666..bd5251ffff 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -813,7 +813,7 @@ Notes: | `INV-WS-LEASE-HORIZON-MONO-001` | Retired with the lease horizon (`INV-WS-RESTART-GRACE-001`). | legacy M2 Granted → Granted | — | — | — | ARCHIVED | | `INV-WS-RENEW-NONBLOCKING-001` | Live renewals take only the leaf lease lock and lock-free lifecycle snapshots; they never wait on publisher/store locks. First grants and expired re-grants persist synchronously; an expired re-grant refuses immediately if the publisher lock is busy. | M2 Granted → Granted | `core/authority/workspace_lease_view.go` `RenewWorkspaceLease`; `core/host/master/workspace_service.go` `workspaceVolumeForRenewal` ; `core/lifecycle/placement_intent.go` `GetPlacementSnapshot`; `core/host/master/workspace_service.go` `RenewWorkspaceLease`; `core/lifecycle/node_inventory.go` `GetNodeSnapshot` (the holder host check) | `TestWorkspaceRenewalDoesNotWaitForSlowStore`, `TestWorkspaceConcurrentRenewalsDoNotSerializeOnStore`, `TestWorkspaceRenewalDoesNotWaitForUnrelatedHeldWrite` (`core/authority/workspace_renewal_slowstore_test.go`); `TestWorkspaceLeaseGuardSurvivesSlowAuthorityStore`, `TestWorkspaceRenewalRPCDoesNotWaitForHeldReservationWrite` (`core/host/master/workspace_renewal_slowstore_test.go`) ; `TestPlacementSnapshotDoesNotWaitForWriter`, `TestPlacementSnapshotFailedCommitStaysCommitted` (`core/lifecycle/placement_snapshot_test.go`); `TestNodeSnapshotDoesNotWaitForARegistrationWrite` (`core/lifecycle/node_snapshot_test.go`); `TestLiveRenewalDoesNotWaitForAHeldRegistrationWrite` (`core/host/master/server_host_identity_test.go`) | — | ACTIVE | | `INV-WS-RESTART-GRACE-001` | A renewal writes nothing durable. A forced move, an expired re-grant and a delete's no-live-lease proof wait for this process's deadline plus skew: its latest grant, or after a restart the restart floor (restart + TTL, own monotonic clock); no lease value an earlier process wrote is read. | M2 Granted → Granted; Expired → Moved (forced); Expired → Granted; any → DeleteReserved | `core/authority/workspace_lease_view.go` `RenewWorkspaceLease`, `regrantWorkspaceLease`, `newWorkspaceLease`; `core/authority/workspace.go` `forcedMoveBarrierLocked`, `AckWorkspaceRevocation`; `core/authority/authority.go` `reloadFromStore` (the restart floor) | `TestRenewalWritesNothingDurable`, `TestForcedAdmissionIgnoresAPersistedHorizon` (`core/authority/workspace_restart_grace_test.go`); `TestWorkspaceLeaseGuardServesThroughAStalledStore` (`core/host/master/workspace_lease_horizon_test.go`); `TestAfterARestartADifferentHolderIsRefusedUntilTheRestartFloor`, `TestAfterARestartTheSameLiveHolderRenewsAtOnce` (`core/authority/workspace_restart_barrier_test.go`) | `LeaseRestart` `OneServer` (grace design) | ACTIVE | -| `INV-WS-AUTH-RECORD-V2-001` | An authority record this version writes has envelope version 2, which a v1.0.x master refuses (it would trust the record's stale lease expiry); this version reads versions 1 and 2. | M2 every workspace write | `core/authority/authority_store.go` `storeEnvelopeVersion`, `encodeEnvelope`, `decodeEnvelope` | `TestARecordWrittenNowIsRefusedByAV100Master` (`core/authority/workspace_restart_grace_test.go`); `TestAuthorityStoreWritesTheV100Files`, `TestAuthorityStoreReadsTheV100Files` (`core/authority/golden_v100_test.go`) | — | ACTIVE | +| `INV-WS-AUTH-RECORD-V2-001` | Before it serves, this master writes the store's attestation file as format 2, on which a v1.0.x master refuses its whole start; its authority records are envelope version 2 (a v1.0.x master would skip them). It reads both forms of each. | M2 every workspace write; master start | `core/host/master/store_attestation.go` `ensureEnvelope`, `decodeStoreAttestations`; `core/host/master/host.go` `New`; `core/authority/authority_store.go` `storeEnvelopeVersion`, `encodeEnvelope`, `decodeEnvelope` | `TestAV100MasterRefusesTheAttestationEnvelope`, `TestAttestationsWriteTheV100FileInTheEnvelope`, `TestAttestationsReadTheV100File` (`core/host/master/golden_v100_test.go`); `TestARecordWrittenNowFailsTheV100Decoder` (`core/authority/workspace_restart_grace_test.go`); `TestAuthorityStoreWritesTheV100Files`, `TestAuthorityStoreReadsTheV100Files` (`core/authority/golden_v100_test.go`) | — | ACTIVE | | `INV-LIFECYCLE-SNAPSHOT-001` | The Volumes and deletion-reservation snapshots read by the renewal path equal the store records after every change of the records, including load, delete and a protective reservation kept in memory on an unknown write outcome; a published snapshot is never mutated. | M2 Bound → Deleting (renewal reads) | `core/lifecycle/store.go` `publishLocked`, `GetVolumeSnapshot`; `core/lifecycle/deletion_reservations.go` `publishLocked`, `ReservedSnapshot` | `TestDeletionReservationSnapshotFollowsRecords`, `TestFileStoreSnapshotFollowsRecords`, `TestLifecycleSnapshotReadsDoNotTakeStoreMutex`, `TestLifecycleSnapshotConcurrentWriters` (`core/lifecycle/renewal_snapshot_test.go`); `TestWorkspaceRenewalRPCDuringHeldSameVolumeReservation` (`core/host/master/workspace_lease_horizon_test.go`) | — | ACTIVE | | `INV-LIFECYCLE-STORE-DURABLE-001` | Node registration, volume record and placement-intent puts/replacements are atomic and durable: after a crash the record file is the complete old or the complete new record; a put that returned nil has fsynced the file and its directory. | M2 lifecycle record publication | `core/lifecycle/record_file.go` `writeRecordDurably`; `node_inventory.go`, `store.go`, `placement_intent.go` `putLocked` | `TestNodeInventoryCrashKeepsCompleteRegistration` (`core/lifecycle/node_inventory_crash_test.go`); `TestFileStoreCrashKeepsCompleteRecord`, `TestPlacementStoreCrashKeepsCompleteRecord`, `TestLifecyclePutSyncOrderAndFailure` (`core/lifecycle/store_crash_test.go`); `TestLifecycleStoreParentSync` (`core/lifecycle/store_parent_sync_test.go`) | � | ACTIVE | | `INV-VOL-ID-NO-REUSE-001` | A deleted volume id is never created or attached again. | M2 Deleting; M6 purging → deleted | `core/host/master/services.go` `MarkVolumeDeleting`; `core/frontend/chunkstore/layout/create.go` `createVolume` | `TestMarkVolumeDeleting_ReservesUnknownIdsAndSurvivesDeleteAndRestart` (`core/host/master/volume_deleting_test.go`); `TestCreateVolume_OverAnExistingHeadByState` (`core/frontend/chunkstore/layout/create_test.go`) | `ChunkLifecycleFixed` `NoIdReuse` | ACTIVE |