topology: never lower the mirrored max volume id

A mirror rewrite for a StartedAt change could carry a bound older than
one already persisted. Keep the highest bound, and read StartedAt under
the file lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-29 19:22:31 -08:00
1 parent 17e5684e80
commit 2e7fa3a07b
2 files changed
+27

No files matched your search

+23
View File
@@ -42,3 +42,26 @@ func TestMaxVolumeIdFileRestoresStartedAt(t *testing.T) {
t.Fatalf("restored StartedAt = %d, want %d", got, startedAt)
}
}
func TestStartedAtMirrorKeepsHighestVolumeId(t *testing.T) {
file := filepath.Join(t.TempDir(), "max_volume_id")
topo := NewTopology("topo", nil, 0, 5, false)
topo.SetMaxVolumeIdFile(file)
if err := topo.PersistMaxVolumeId(needle.VolumeId(10)); err != nil {
t.Fatalf("persist: %v", err)
}
topo.SetStartedAt(time.Now().Unix())
if err := topo.PersistMaxVolumeId(needle.VolumeId(9)); err != nil {
t.Fatalf("persist an older bound with StartedAt: %v", err)
}
restarted := NewTopology("topo", nil, 0, 5, false)
restarted.SetMaxVolumeIdFile(file)
if got := restarted.GetMaxVolumeId(); got != needle.VolumeId(10) {
t.Fatalf("restored max volume id = %d, want 10", got)
}
if restarted.GetStartedAt() == 0 {
t.Fatal("StartedAt not mirrored")
}
}
+4
View File
@@ -2055,9 +2055,13 @@ func (t *Topology) PersistMaxVolumeId(vid needle.VolumeId) error {
}
t.maxVolumeIdFileLock.Lock()
defer t.maxVolumeIdFileLock.Unlock()
startedAt = t.GetStartedAt()
if mirrored() {
return nil
}
if persisted := needle.VolumeId(t.lastPersistedMaxVolumeId.Load()); persisted > vid {
vid = persisted
}
if t.maxVolumeIdFile == "" {
t.lastPersistedMaxVolumeId.Store(uint32(vid))
t.lastPersistedTopologyId.Store(topoId)