diff --git a/weed/topology/cluster_commands.go b/weed/topology/cluster_commands.go index c12528a2db..5abd922f1d 100644 --- a/weed/topology/cluster_commands.go +++ b/weed/topology/cluster_commands.go @@ -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) } diff --git a/weed/topology/started_at_test.go b/weed/topology/started_at_test.go new file mode 100644 index 0000000000..7a624f8543 --- /dev/null +++ b/weed/topology/started_at_test.go @@ -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) + } +} diff --git a/weed/topology/topology.go b/weed/topology/topology.go index 8392c998f0..a46d936d9a 100644 --- a/weed/topology/topology.go +++ b/weed/topology/topology.go @@ -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 }