Files
seaweedfs/weed/topology/started_at_test.go
T
Chris LuandClaude Opus 5.5 df59c81a8c master: record when the cluster started (#13121)
* topology: record when the cluster started

StartedAt rides on MaxVolumeIdCommand; the first non-zero value wins so
replayed or racing entries cannot move it, and the max volume id mirror
file keeps it across raft state loss.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: carry StartedAt through raft apply and snapshots

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* master: leader records StartedAt once per cluster

The leader records it right after the topology id. A cluster that predates
the record gets the time its first upgraded leader runs. /dir/status shows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: use current StartedAt values

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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>

* topology: let the latest applied StartedAt win

With first-wins, a master that restored an older-version snapshot without
the field could record a second value while the others kept the first.
Following the raft order makes every master converge on the same value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 23:12:46 -08:00

68 lines
2.0 KiB
Go

package topology
import (
"path/filepath"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
)
func TestSetStartedAtFollowsLatestValue(t *testing.T) {
topo := NewTopology("topo", nil, 0, 5, false)
first := time.Now().Unix()
topo.SetStartedAt(first)
topo.SetStartedAt(0)
if got := topo.GetStartedAt(); got != first {
t.Fatalf("StartedAt = %d after zero, want %d", got, first)
}
topo.SetStartedAt(first + 60)
if got := topo.GetStartedAt(); got != first+60 {
t.Fatalf("StartedAt = %d, want the latest value %d", got, first+60)
}
}
func TestMaxVolumeIdFileRestoresStartedAt(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(3)); err != nil {
t.Fatalf("persist: %v", err)
}
startedAt := time.Now().Unix()
topo.SetStartedAt(startedAt)
if err := topo.PersistMaxVolumeId(needle.VolumeId(3)); err != nil {
t.Fatalf("persist after StartedAt: %v", err)
}
restarted := NewTopology("topo", nil, 0, 5, false)
restarted.SetMaxVolumeIdFile(file)
if got := restarted.GetStartedAt(); got != startedAt {
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")
}
}