diff --git a/seaweed-block/cmd/sparrow/bootstrap_test.go b/seaweed-block/cmd/sparrow/bootstrap_test.go index 5f226fbd33..84d855faaa 100644 --- a/seaweed-block/cmd/sparrow/bootstrap_test.go +++ b/seaweed-block/cmd/sparrow/bootstrap_test.go @@ -430,3 +430,10 @@ func TestBootstrap_Close_Idempotent(t *testing.T) { t.Fatalf("second Close must be idempotent: %v", err) } } + +func (s *indexFailingAuthorityStore) PutWorkspaceLostRanges(string, []authority.WorkspaceLostRange) error { + return nil +} +func (s *indexFailingAuthorityStore) LoadWorkspaceLostRanges() (map[string][]authority.WorkspaceLostRange, []error, error) { + return nil, nil, nil +} diff --git a/seaweed-block/core/authority/authority.go b/seaweed-block/core/authority/authority.go index 321165423b..52f8d7fe1f 100644 --- a/seaweed-block/core/authority/authority.go +++ b/seaweed-block/core/authority/authority.go @@ -285,14 +285,12 @@ func (p *Publisher) reloadFromStore() { } } p.loadErrs = append(p.loadErrs, skips...) - if ls, ok := p.store.(WorkspaceLostRangeStore); ok { - ranges, lskips, lerr := ls.LoadWorkspaceLostRanges() - if lerr != nil { - panic(fmt.Sprintf("authority: lost range load failure: %v", lerr)) - } - p.lostRanges = ranges - p.loadErrs = append(p.loadErrs, lskips...) + ranges, lskips, lerr := p.store.LoadWorkspaceLostRanges() + if lerr != nil { + panic(fmt.Sprintf("authority: lost range load failure: %v", lerr)) } + p.lostRanges = ranges + p.loadErrs = append(p.loadErrs, lskips...) for _, rec := range records { p.refreshLeaseViewLocked(rec.VolumeID) if rec.Workspace != nil { diff --git a/seaweed-block/core/authority/authority_restart_test.go b/seaweed-block/core/authority/authority_restart_test.go index eb0cf7e578..8a152c4e24 100644 --- a/seaweed-block/core/authority/authority_restart_test.go +++ b/seaweed-block/core/authority/authority_restart_test.go @@ -369,3 +369,8 @@ func (f *failingStore) Close() error { return nil } // Silence unused import warning if adapter is not referenced // directly in this file's test bodies. var _ = adapter.AssignmentInfo{} + +func (f *failingStore) PutWorkspaceLostRanges(string, []WorkspaceLostRange) error { return nil } +func (f *failingStore) LoadWorkspaceLostRanges() (map[string][]WorkspaceLostRange, []error, error) { + return nil, nil, nil +} diff --git a/seaweed-block/core/authority/authority_store.go b/seaweed-block/core/authority/authority_store.go index 680fb2a087..8a3864ac62 100644 --- a/seaweed-block/core/authority/authority_store.go +++ b/seaweed-block/core/authority/authority_store.go @@ -80,6 +80,16 @@ type AuthorityStore interface { // Publisher.apply). Put(record DurableRecord) error + // PutWorkspaceLostRanges replaces a volume's forced-move lost ranges + // atomically, beside (never inside) its authority record, so the record + // stays readable by binaries that predate them. Every store must record + // them: a forced move does not issue without its range. + PutWorkspaceLostRanges(volumeID string, ranges []WorkspaceLostRange) error + + // LoadWorkspaceLostRanges returns every volume's lost ranges, with + // per-file skip errors; only an index-level failure returns err. + LoadWorkspaceLostRanges() (map[string][]WorkspaceLostRange, []error, error) + // Close releases store resources. Idempotent. Close() error } diff --git a/seaweed-block/core/authority/workspace.go b/seaweed-block/core/authority/workspace.go index ce4342dcb7..6d6301e1b3 100644 --- a/seaweed-block/core/authority/workspace.go +++ b/seaweed-block/core/authority/workspace.go @@ -302,6 +302,15 @@ func (p *Publisher) prepareWorkspaceMoveLocked(ask AssignmentAsk, g workspaceGra if r.Issued || !sameWorkspaceRequest(r, q) { return nil, 0, fmt.Errorf("workspace move reservation differs") } + // A forced reservation issues only with the exact range recorded + // before it; a missing one (a reservation older than lost ranges, + // or a skipped side file) is refused, never recomputed: a new + // request records a fresh range. + if r.Kind == "forced" { + if _, ok := p.lostRangeLocked(q.VolumeID, r.RequestID, r.NewEpoch); !ok { + return nil, 0, fmt.Errorf("workspace forced move reservation %s has no recorded lost range; retry with a new request id", r.RequestID) + } + } s.Moves[idx].Issued = true s.Lease = nil s.Ack = nil diff --git a/seaweed-block/core/authority/workspace_lost_range.go b/seaweed-block/core/authority/workspace_lost_range.go index dab767f3fd..40d26cd5ae 100644 --- a/seaweed-block/core/authority/workspace_lost_range.go +++ b/seaweed-block/core/authority/workspace_lost_range.go @@ -25,15 +25,6 @@ type WorkspaceLostRange struct { LostUntilNS int64 } -// WorkspaceLostRangeStore is an optional AuthorityStore capability. The -// ranges live beside the authority record, never in it, so the record body -// and its checksum stay readable by binaries that predate them (those load -// only *.v3auth.json). A store without it records no lost range. -type WorkspaceLostRangeStore interface { - PutWorkspaceLostRanges(volumeID string, ranges []WorkspaceLostRange) error - LoadWorkspaceLostRanges() (map[string][]WorkspaceLostRange, []error, error) -} - // lostRangeFileExtension does not end in storeFileExtension: authority // loaders, old and new, never read these files. const lostRangeFileExtension = ".lostrange.json" @@ -82,6 +73,9 @@ func (s *FileAuthorityStore) PutWorkspaceLostRanges(volumeID string, ranges []Wo if werr == nil { werr = f.Sync() } + if werr == nil { + werr = testLostRangeFault("before-rename") + } if cerr := f.Close(); werr == nil { werr = cerr } @@ -98,6 +92,9 @@ func (s *FileAuthorityStore) PutWorkspaceLostRanges(volumeID string, ranges []Wo return fmt.Errorf("authority_store: lost range directory open: %w", err) } serr := d.Sync() + if serr == nil { + serr = testLostRangeFault("directory-sync") + } if cerr := d.Close(); serr == nil { serr = cerr } @@ -149,14 +146,34 @@ func (s *FileAuthorityStore) LoadWorkspaceLostRanges() (map[string][]WorkspaceLo return out, skips, nil } +// testLostRangeFaultHook, test-only, fails PutWorkspaceLostRanges at a named +// step ("before-rename", "directory-sync") when it returns an error. +var testLostRangeFaultHook func(step string) error + +func testLostRangeFault(step string) error { + if h := testLostRangeFaultHook; h != nil { + return h(step) + } + return nil +} + +// lostRangeLocked returns volume's recorded range for (requestID, newEpoch). +// Called under p.mu. +func (p *Publisher) lostRangeLocked(volume, requestID string, newEpoch uint64) (WorkspaceLostRange, bool) { + for _, e := range p.lostRanges[volume] { + if e.RequestID == requestID && e.NewEpoch == newEpoch { + return e, true + } + } + return WorkspaceLostRange{}, false +} + // recordLostRangeLocked durably records r for volume before the move record -// that issues it is written; a store without the capability records nothing. -// An entry whose move never issues is an orphan, ignored on read. Called -// under p.mu. +// that issues it is written. An entry whose move never issues is an orphan, +// ignored on read. Called under p.mu. func (p *Publisher) recordLostRangeLocked(volume string, r WorkspaceLostRange) error { - ls, ok := p.store.(WorkspaceLostRangeStore) - if !ok { - return nil + if p.store == nil { + return fmt.Errorf("workspace forced move: no store to record the lost range") } next := make([]WorkspaceLostRange, 0, len(p.lostRanges[volume])+1) for _, e := range p.lostRanges[volume] { @@ -165,7 +182,7 @@ func (p *Publisher) recordLostRangeLocked(volume string, r WorkspaceLostRange) e } } next = append(next, r) - if err := ls.PutWorkspaceLostRanges(volume, next); err != nil { + if err := p.store.PutWorkspaceLostRanges(volume, next); err != nil { return fmt.Errorf("workspace forced move: record lost range: %w", err) } p.lostRanges[volume] = next diff --git a/seaweed-block/core/authority/workspace_lost_range_test.go b/seaweed-block/core/authority/workspace_lost_range_test.go index 096f6f5141..4be162bd56 100644 --- a/seaweed-block/core/authority/workspace_lost_range_test.go +++ b/seaweed-block/core/authority/workspace_lost_range_test.go @@ -175,3 +175,165 @@ func TestWorkspaceForcedMoveLostRangeOrdering(t *testing.T) { } }) } + +// issueFailStore fails the authority write that would issue a move, leaving +// its reservation pending on disk (a crash between reservation and issue). +type issueFailStore struct { + *FileAuthorityStore + failIssue bool +} + +func (s *issueFailStore) Put(r DurableRecord) error { + if s.failIssue && r.Workspace != nil { + for _, m := range r.Workspace.Moves { + if m.Issued { + return errInjected + } + } + } + return s.FileAuthorityStore.Put(r) +} + +// A pending forced reservation resumes only with the exact range recorded +// before it: after a reload the retry issues it with that range (not one +// recomputed at the retry); without it the retry is refused, and a new +// request records a fresh range. +func TestWorkspaceForcedReservationResumeNeedsItsRange(t *testing.T) { + for _, missing := range []bool{false, true} { + t.Run(map[bool]string{false: "range kept", true: "range missing"}[missing], func(t *testing.T) { + disk, dir := newTempStore(t) + store := &issueFailStore{FileAuthorityStore: disk, failIssue: true} + p, now, barrier := forcedLostRangeFixture(t, store, false) + *now = barrier.Add(time.Second) + if _, err := p.MoveWorkspace(context.Background(), forcedLostRangeReq, forcedLostRangeHead); err == nil { + t.Fatal("fixture: the issue write did not fail") + } + st := NewPublisher(nil, WithStore(disk)).WorkspaceState("v") + if st == nil || len(st.Moves) != 1 || st.Moves[0].Issued { + t.Fatalf("fixture: no pending reservation on disk: %+v", st) + } + want := WorkspaceLostRange{RequestID: "f", NewEpoch: st.Moves[0].NewEpoch, OldReplicaID: "old", OldEpoch: 1, LostAfterLSN: 42, LostUntilNS: barrier.UnixNano()} + if missing { + if err := os.Remove(filepath.Join(dir, "v"+lostRangeFileExtension)); err != nil { + t.Fatal(err) + } + } + later := barrier.Add(time.Hour) // a recomputed bound would differ + q := NewPublisher(nil, WithStore(disk), WithWorkspaceLeaseConfig(time.Second, func() time.Time { return later })) + later = later.Add(time.Minute) // past the reload's own restart deadline + r, err := q.MoveWorkspace(context.Background(), forcedLostRangeReq, forcedLostRangeHead) + if missing { + if err == nil || !strings.Contains(err.Error(), "no recorded lost range") { + t.Fatalf("resume without its range: %v; want refused", err) + } + if line, _ := q.VolumeAuthorityLine("v"); line.Epoch != 1 { + t.Fatalf("epoch issued without a range: %+v", line) + } + fresh := forcedLostRangeReq + fresh.RequestID = "f2" + r2, err := q.MoveWorkspace(context.Background(), fresh, forcedLostRangeHead) + if err != nil { + t.Fatalf("a new request after the refused resume: %v", err) + } + if got := q.WorkspaceLostRanges("v"); len(got) != 1 || got[0].RequestID != "f2" || got[0].NewEpoch != r2.NewEpoch { + t.Fatalf("new request range %+v", got) + } + return + } + if err != nil || !r.Issued || r.NewEpoch != want.NewEpoch { + t.Fatalf("resume: %+v %v", r, err) + } + if got := q.WorkspaceLostRanges("v"); len(got) != 1 || got[0] != want { + t.Fatalf("resumed range %+v; want the recorded [%+v]", got, want) + } + }) + } +} + +// Failures inside the file store: before the rename nothing is published +// and no temp file is left; after the rename (directory sync) the range may +// be on disk as an orphan. Either way the move is refused with no epoch, the +// orphan is not reported, and a retry issues with exactly one range. +func TestWorkspaceLostRangeFileFaults(t *testing.T) { + for _, step := range []string{"before-rename", "directory-sync"} { + t.Run(step, func(t *testing.T) { + disk, dir := newTempStore(t) + p, now, barrier := forcedLostRangeFixture(t, disk, false) + *now = barrier.Add(time.Second) + testLostRangeFaultHook = func(s string) error { + if s == step { + return errInjected + } + return nil + } + t.Cleanup(func() { testLostRangeFaultHook = nil }) + if _, err := p.MoveWorkspace(context.Background(), forcedLostRangeReq, forcedLostRangeHead); !errors.Is(err, errInjected) { + t.Fatalf("move with a failed range write: %v; want refused", err) + } + if line, _ := p.VolumeAuthorityLine("v"); line.Epoch != 1 { + t.Fatalf("epoch issued: %+v", line) + } + _, statErr := os.Stat(filepath.Join(dir, "v"+lostRangeFileExtension)) + temps, _ := filepath.Glob(filepath.Join(dir, "*"+lostRangeFileExtension+tempSuffix)) + if len(temps) != 0 { + t.Fatalf("temp file left: %v", temps) + } + if step == "before-rename" && statErr == nil { + t.Fatal("range published although the write failed before the rename") + } + if step == "directory-sync" && statErr != nil { + t.Fatalf("fixture: the rename did not land: %v", statErr) + } + if got := NewPublisher(nil, WithStore(disk)).WorkspaceLostRanges("v"); len(got) != 0 { + t.Fatalf("orphan reported: %+v", got) + } + testLostRangeFaultHook = nil + r, err := p.MoveWorkspace(context.Background(), forcedLostRangeReq, forcedLostRangeHead) + if err != nil { + t.Fatal("retry:", err) + } + if got := NewPublisher(nil, WithStore(disk)).WorkspaceLostRanges("v"); len(got) != 1 || got[0].NewEpoch != r.NewEpoch { + t.Fatalf("after the retry: %+v; want one range", got) + } + }) + } +} + +// A store wrapper that embeds AuthorityStore forwards the lost ranges: a +// forced move through it records its range. +func TestWorkspaceLostRangeThroughAStoreWrapper(t *testing.T) { + disk, _ := newTempStore(t) + wrapped := &workspaceFailStore{AuthorityStore: disk} + p, now, barrier := forcedLostRangeFixture(t, wrapped, false) + *now = barrier.Add(time.Second) + if _, err := p.MoveWorkspace(context.Background(), forcedLostRangeReq, forcedLostRangeHead); err != nil { + t.Fatal(err) + } + if got := NewPublisher(nil, WithStore(disk)).WorkspaceLostRanges("v"); len(got) != 1 { + t.Fatalf("range through a wrapper: %+v", got) + } +} + +// Budget evidence (logged, not asserted): the added critical-section write, +// one file of the volume's whole range history plus two fsyncs, for a short +// and a long history. +func TestWorkspaceLostRangeWriteCost(t *testing.T) { + disk, _ := newTempStore(t) + for _, n := range []int{1, 1000} { + ranges := make([]WorkspaceLostRange, n) + for i := range ranges { + ranges[i] = WorkspaceLostRange{RequestID: "request-" + strings.Repeat("x", 24), NewEpoch: uint64(i + 2), OldReplicaID: "r1", OldEpoch: uint64(i + 1), LostAfterLSN: 1 << 40, LostUntilNS: 1 << 60} + } + var worst time.Duration + for i := 0; i < 5; i++ { + start := time.Now() + if err := disk.PutWorkspaceLostRanges("v", ranges); err != nil { + t.Fatal(err) + } + if d := time.Since(start); d > worst { + worst = d + } + } + t.Logf("lost range write: %d entries, worst of 5 = %v", n, worst) + } +}