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)