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>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-29 19:06:17 -08:00
1 parent 604d391012
commit 968b501b05
3 files changed
+69 -1

No files matched your search

+4
View File
@@ -27,6 +27,9 @@ type MaxVolumeIdCommand struct {
// written before the binding existed; -1 binds the ageless generation
// left by freeze entries written before instants existed.
GrowthUnfreezeFor int64 `json:"growthUnfreezeFor,omitempty"`
// StartedAt is when the cluster started, in unix seconds. The first
// value applied wins. Zero means no change.
StartedAt int64 `json:"startedAt,omitempty"`
}
func NewMaxVolumeIdCommand(value needle.VolumeId, topologyId string) *MaxVolumeIdCommand {
@@ -60,6 +63,7 @@ func (c *MaxVolumeIdCommand) Apply(server raft.Server) (interface{}, error) {
if c.GrowthFrozen != nil {
topo.SetGrowthFrozen(*c.GrowthFrozen, c.GrowthFrozenAt, c.GrowthUnfreezeFor)
}
topo.SetStartedAt(c.StartedAt)
if err := topo.PersistMaxVolumeId(c.MaxVolumeId); err != nil {
glog.Warningf("persist max volume id %d: %v", c.MaxVolumeId, err)
}
+41
View File
@@ -0,0 +1,41 @@
package topology
import (
"path/filepath"
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
)
func TestSetStartedAtKeepsFirstValue(t *testing.T) {
topo := NewTopology("topo", nil, 0, 5, false)
topo.SetStartedAt(0)
if got := topo.GetStartedAt(); got != 0 {
t.Fatalf("StartedAt = %d after zero, want 0", got)
}
topo.SetStartedAt(100)
topo.SetStartedAt(200)
if got := topo.GetStartedAt(); got != 100 {
t.Fatalf("StartedAt = %d, want the first value 100", got)
}
}
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)
}
topo.SetStartedAt(1700000000)
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 != 1700000000 {
t.Fatalf("restored StartedAt = %d, want 1700000000", got)
}
}
+24 -1
View File
@@ -73,6 +73,7 @@ type Topology struct {
topologyId string
topologyIdLock sync.RWMutex
startedAt atomic.Int64
lastLeaderChangeTime time.Time
hadVolumesAtLeaderChange bool
@@ -91,6 +92,7 @@ type Topology struct {
lastPersistedMaxVolumeId atomic.Uint32
lastPersistedTopologyId atomic.Value
lastPersistedGrowthFrozen atomic.Bool
lastPersistedStartedAt atomic.Int64
// growthFrozenState packs the freeze flag and its generation into one
// value: 0 = unfrozen, -1 = frozen without a recorded instant (entries
@@ -1611,6 +1613,21 @@ func (t *Topology) SetTopologyId(topologyId string) {
}
}
// GetStartedAt returns when the cluster started, in unix seconds, or 0 if not
// recorded yet.
func (t *Topology) GetStartedAt() int64 {
return t.startedAt.Load()
}
// SetStartedAt records when the cluster started. Only the first non-zero
// value is kept, so replayed or racing entries cannot move it.
func (t *Topology) SetStartedAt(startedAt int64) {
if startedAt <= 0 || !t.startedAt.CompareAndSwap(0, startedAt) {
return
}
glog.V(0).Infof("cluster StartedAt: %s", time.Unix(startedAt, 0).UTC().Format(time.RFC3339))
}
func (t *Topology) SetGrowthFreezeConfig(freezeOnMismatch bool, recoveryWait time.Duration) {
t.freezeOnMismatch.Store(freezeOnMismatch)
t.stateLossRecoveryWait.Store(int64(recoveryWait))
@@ -2019,6 +2036,8 @@ func (t *Topology) SetMaxVolumeIdFile(file string) {
t.lastPersistedGrowthFrozen.Store(*cmd.GrowthFrozen)
t.SetGrowthFrozen(*cmd.GrowthFrozen, cmd.GrowthFrozenAt, 0)
}
t.lastPersistedStartedAt.Store(cmd.StartedAt)
t.SetStartedAt(cmd.StartedAt)
}
// PersistMaxVolumeId writes the bound to the mirror file. Called by the
@@ -2027,8 +2046,9 @@ func (t *Topology) SetMaxVolumeIdFile(file string) {
func (t *Topology) PersistMaxVolumeId(vid needle.VolumeId) error {
topoId := t.GetTopologyId()
frozen, frozenAt := t.GrowthFreezeState()
startedAt := t.GetStartedAt()
mirrored := func() bool {
return t.lastPersistedMaxVolumeId.Load() >= uint32(vid) && t.lastPersistedTopologyId.Load() == topoId && t.lastPersistedGrowthFrozen.Load() == frozen
return t.lastPersistedMaxVolumeId.Load() >= uint32(vid) && t.lastPersistedTopologyId.Load() == topoId && t.lastPersistedGrowthFrozen.Load() == frozen && t.lastPersistedStartedAt.Load() == startedAt
}
if mirrored() {
return nil
@@ -2042,6 +2062,7 @@ func (t *Topology) PersistMaxVolumeId(vid needle.VolumeId) error {
t.lastPersistedMaxVolumeId.Store(uint32(vid))
t.lastPersistedTopologyId.Store(topoId)
t.lastPersistedGrowthFrozen.Store(frozen)
t.lastPersistedStartedAt.Store(startedAt)
return nil
}
cmd := NewMaxVolumeIdCommand(vid, topoId)
@@ -2049,6 +2070,7 @@ func (t *Topology) PersistMaxVolumeId(vid needle.VolumeId) error {
if frozen {
cmd.GrowthFrozenAt = frozenAt
}
cmd.StartedAt = startedAt
b, err := json.Marshal(cmd)
if err != nil {
return err
@@ -2078,5 +2100,6 @@ func (t *Topology) PersistMaxVolumeId(vid needle.VolumeId) error {
t.lastPersistedMaxVolumeId.Store(uint32(vid))
t.lastPersistedTopologyId.Store(topoId)
t.lastPersistedGrowthFrozen.Store(frozen)
t.lastPersistedStartedAt.Store(startedAt)
return nil
}