From 2e7fa3a07b554ccbdc90b8d953aa3521f721c335 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 29 Nov 2026 19:22:31 -0800 Subject: [PATCH] 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 --- weed/topology/started_at_test.go | 23 +++++++++++++++++++++++ weed/topology/topology.go | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/weed/topology/started_at_test.go b/weed/topology/started_at_test.go index 429067b35c..a93155862a 100644 --- a/weed/topology/started_at_test.go +++ b/weed/topology/started_at_test.go @@ -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") + } +} diff --git a/weed/topology/topology.go b/weed/topology/topology.go index a46d936d9a..fc75649a08 100644 --- a/weed/topology/topology.go +++ b/weed/topology/topology.go @@ -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)