From 84941f16e960e240b06bf75bb4d727e98475cb65 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 5 Dec 2026 09:40:29 -0800 Subject: [PATCH] master: write a recovered TopologyId into the hashicorp raft log on leadership A hashicorp raft leader whose TopologyId came from the max_volume_id mirror never wrote it to the raft log, so followers kept an empty id. The next leader then minted a new one, and the first master crashed with split-brain on its next restart when it read the old id back from its mirror. Sync the id after the barrier, as the goraft path already does. Co-Authored-By: Claude Opus 5.5 --- weed/server/raft_hashicorp.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/weed/server/raft_hashicorp.go b/weed/server/raft_hashicorp.go index 987755a450..25221770e8 100644 --- a/weed/server/raft_hashicorp.go +++ b/weed/server/raft_hashicorp.go @@ -61,7 +61,9 @@ func (s *RaftServer) monitorLeaderLoop(updatePeers bool) { updatePeers = false } - s.topo.DoBarrier() + if s.topo.DoBarrier() { + s.syncRaftForTopologyId() + } EnsureTopologyId(s.topo, func() bool { return s.RaftHashicorp.State() == hashicorpRaft.Leader @@ -93,6 +95,28 @@ func (s *RaftServer) monitorLeaderLoop(updatePeers bool) { } } +// syncRaftForTopologyId writes the leader's TopologyId into the raft log. An id +// restored from the max_volume_id mirror is otherwise known to this master +// only, and the next leader would mint a different one. +func (s *RaftServer) syncRaftForTopologyId() { + topologyId := s.topo.GetTopologyId() + if topologyId == "" { + return + } + for s.RaftHashicorp.State() == hashicorpRaft.Leader { + b, err := json.Marshal(topology.NewMaxVolumeIdCommand(s.topo.GetMaxVolumeId(), topologyId)) + if err != nil { + glog.Errorf("marshal TopologyId %s: %v", topologyId, err) + return + } + if err = s.RaftHashicorp.Apply(b, 5*time.Second).Error(); err == nil { + return + } + glog.Errorf("failed to sync raft for topologyId %s, retrying in 1s: %v", topologyId, err) + time.Sleep(time.Second) + } +} + func (s *RaftServer) updatePeers() { peerLeader := raftServerID(s.serverAddr) desiredPeers := make(map[string]pb.ServerAddress, len(s.peers))