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

96 lines
3.2 KiB
Go

package weed_server
import (
"encoding/json"
"time"
"github.com/google/uuid"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/topology"
)
// recoverTopologyIdFromState restores the TopologyId from serialized FSM
// state bytes (JSON-encoded MaxVolumeIdCommand). Both raft implementations
// call this after reading their snapshot in their own format.
func recoverTopologyIdFromState(fsmState []byte, topo *topology.Topology) {
if topo.GetTopologyId() != "" && topo.GetStartedAt() != 0 {
return
}
var cmd topology.MaxVolumeIdCommand
if err := json.Unmarshal(fsmState, &cmd); err != nil {
return
}
if topo.GetStartedAt() == 0 {
topo.SetStartedAt(cmd.StartedAt)
}
if cmd.TopologyId != "" && topo.GetTopologyId() == "" {
topo.SetTopologyId(cmd.TopologyId)
glog.V(0).Infof("Recovered TopologyId from snapshot: %s", cmd.TopologyId)
}
}
// EnsureTopologyId ensures that a TopologyId is generated and persisted if it's currently missing.
// It uses the provided checkLeaderFn to verify leadership and persistFn to save the new ID.
func EnsureTopologyId(topo *topology.Topology, checkLeaderFn func() bool, persistFn func(string) error) {
if topo.GetTopologyId() != "" {
// The id arrived through raft replay or the mirror file; reports
// stashed while it was unknown can now be checked.
topo.SetTopologyInitPending(false)
return
}
topo.SetTopologyInitPending(true)
defer topo.SetTopologyInitPending(false)
topologyId := uuid.New().String()
for {
if !checkLeaderFn() {
glog.V(0).Infof("lost leadership while saving topologyId")
return
}
// Another concurrent operation may have set the ID between generation and now.
if latestId := topo.GetTopologyId(); latestId != "" {
glog.V(1).Infof("topologyId was set concurrently to %s, aborting generation", latestId)
return
}
if err := persistFn(topologyId); err != nil {
glog.Errorf("failed to save topologyId, will retry: %v", err)
time.Sleep(time.Second)
continue
}
// Verify that the topology ID was actually applied as expected.
appliedId := topo.GetTopologyId()
if appliedId != "" && appliedId != topologyId {
glog.V(0).Infof("TopologyId generation race: expected %s, but current TopologyId is %s", topologyId, appliedId)
} else {
glog.V(0).Infof("TopologyId generated: %s", topologyId)
}
// A recovered bound means cluster state survived without a
// TopologyId, so the mint is not proof of an empty cluster.
if topo.GetMaxVolumeId() == 0 {
topo.MarkFreshMint()
}
topo.DeferAllocation(topo.AllocationDeferralDuration())
break
}
}
// EnsureStartedAt records when the cluster started, once. A cluster that
// predates the record gets the time its first upgraded leader runs this.
func EnsureStartedAt(topo *topology.Topology, checkLeaderFn func() bool, persistFn func(*topology.MaxVolumeIdCommand) error) {
for topo.GetStartedAt() == 0 {
if !checkLeaderFn() {
return
}
command := topology.NewMaxVolumeIdCommand(topo.GetMaxVolumeId(), topo.GetTopologyId())
command.StartedAt = time.Now().Unix()
if err := persistFn(command); err != nil {
glog.Errorf("failed to record cluster StartedAt, will retry: %v", err)
time.Sleep(time.Second)
}
}
}