Files
seaweedfs/weed/server/raft_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

77 lines
2.2 KiB
Go

package weed_server
import (
"encoding/json"
"errors"
"testing"
"time"
hashicorpRaft "github.com/hashicorp/raft"
"github.com/seaweedfs/seaweedfs/weed/topology"
)
func TestEnsureStartedAtRecordsOnce(t *testing.T) {
topo := topology.NewTopology("topo", nil, 0, 5, false)
fsm := &StateMachine{topo: topo}
persists := 0
persist := func(command *topology.MaxVolumeIdCommand) error {
persists++
if persists == 1 {
return errors.New("not applied")
}
b, err := json.Marshal(command)
if err != nil {
return err
}
if res := fsm.Apply(&hashicorpRaft.Log{Data: b}); res != nil {
return res.(error)
}
return nil
}
isLeader := func() bool { return true }
EnsureStartedAt(topo, isLeader, persist)
first := topo.GetStartedAt()
if first == 0 || persists != 2 {
t.Fatalf("StartedAt = %d after %d persists, want it recorded on the retry", first, persists)
}
EnsureStartedAt(topo, isLeader, persist)
if persists != 2 || topo.GetStartedAt() != first {
t.Fatalf("second run persisted again or moved StartedAt: persists=%d StartedAt=%d", persists, topo.GetStartedAt())
}
}
func TestEnsureStartedAtSkipsFollower(t *testing.T) {
topo := topology.NewTopology("topo", nil, 0, 5, false)
EnsureStartedAt(topo, func() bool { return false }, func(*topology.MaxVolumeIdCommand) error {
t.Fatal("follower must not record StartedAt")
return nil
})
}
func TestStartedAtSurvivesSnapshot(t *testing.T) {
topo := topology.NewTopology("topo", nil, 0, 5, false)
topo.SetTopologyId("cluster-a")
startedAt := time.Now().Unix()
topo.SetStartedAt(startedAt)
state, err := StateMachine{topo: topo}.Save()
if err != nil {
t.Fatalf("save: %v", err)
}
restored := topology.NewTopology("topo", nil, 0, 5, false)
if err := (StateMachine{topo: restored}).Recovery(state); err != nil {
t.Fatalf("recovery: %v", err)
}
if got := restored.GetStartedAt(); got != startedAt {
t.Fatalf("recovered StartedAt = %d, want %d", got, startedAt)
}
fromState := topology.NewTopology("topo", nil, 0, 5, false)
fromState.SetTopologyId("cluster-a")
recoverTopologyIdFromState(state, fromState)
if got := fromState.GetStartedAt(); got != startedAt {
t.Fatalf("StartedAt from snapshot state = %d, want %d", got, startedAt)
}
}