diff --git a/weed/server/raft_server.go b/weed/server/raft_server.go index 585ad7b132..951446ffc9 100644 --- a/weed/server/raft_server.go +++ b/weed/server/raft_server.go @@ -87,7 +87,7 @@ func (s StateMachine) Recovery(data []byte) error { s.topo.SetGrowthFrozen(*state.GrowthFrozen, state.GrowthFrozenAt, state.GrowthUnfreezeFor) } s.topo.SetStartedAt(state.StartedAt) - s.topo.RaiseFileIdCeiling(state.FileIdCeiling) + s.topo.RaiseFileIdCeiling(state.FileIdCeiling, "") if err := s.topo.PersistMaxVolumeId(state.MaxVolumeId); err != nil { glog.Warningf("persist max volume id %d: %v", state.MaxVolumeId, err) } @@ -114,7 +114,7 @@ func (s *StateMachine) Apply(l *hashicorpRaft.Log) interface{} { s.topo.SetGrowthFrozen(*state.GrowthFrozen, state.GrowthFrozenAt, state.GrowthUnfreezeFor) } s.topo.SetStartedAt(state.StartedAt) - s.topo.RaiseFileIdCeiling(state.FileIdCeiling) + s.topo.RaiseFileIdCeiling(state.FileIdCeiling, state.FileIdCeilingBy) if err := s.topo.PersistMaxVolumeId(state.MaxVolumeId); err != nil { glog.Warningf("persist max volume id %d: %v", state.MaxVolumeId, err) } diff --git a/weed/server/raft_server_file_id_ceiling_test.go b/weed/server/raft_server_file_id_ceiling_test.go index b8427f3cf2..895259854c 100644 --- a/weed/server/raft_server_file_id_ceiling_test.go +++ b/weed/server/raft_server_file_id_ceiling_test.go @@ -18,7 +18,7 @@ func TestRaftStateKeepsTheFileIdCeiling(t *testing.T) { } recorded := newTopology() - recorded.RaiseFileIdCeiling(5000) + recorded.RaiseFileIdCeiling(5000, "") snapshot, err := StateMachine{topo: recorded}.Save() if err != nil { t.Fatalf("save the raft state: %v", err) diff --git a/weed/topology/cluster_commands.go b/weed/topology/cluster_commands.go index 40c6662eb2..d569b61863 100644 --- a/weed/topology/cluster_commands.go +++ b/weed/topology/cluster_commands.go @@ -32,7 +32,8 @@ type MaxVolumeIdCommand struct { StartedAt int64 `json:"startedAt,omitempty"` // FileIdCeiling is the end of the file ids recorded as handed out: see // Topology.RaiseFileIdCeiling. Zero means no change. - FileIdCeiling uint64 `json:"fileIdCeiling,omitempty"` + FileIdCeiling uint64 `json:"fileIdCeiling,omitempty"` + FileIdCeilingBy string `json:"fileIdCeilingBy,omitempty"` } func NewMaxVolumeIdCommand(value needle.VolumeId, topologyId string) *MaxVolumeIdCommand { @@ -67,7 +68,7 @@ func (c *MaxVolumeIdCommand) Apply(server raft.Server) (interface{}, error) { topo.SetGrowthFrozen(*c.GrowthFrozen, c.GrowthFrozenAt, c.GrowthUnfreezeFor) } topo.SetStartedAt(c.StartedAt) - topo.RaiseFileIdCeiling(c.FileIdCeiling) + topo.RaiseFileIdCeiling(c.FileIdCeiling, c.FileIdCeilingBy) if err := topo.PersistMaxVolumeId(c.MaxVolumeId); err != nil { glog.Warningf("persist max volume id %d: %v", c.MaxVolumeId, err) } diff --git a/weed/topology/topology.go b/weed/topology/topology.go index 567863cd6c..47cb6d795b 100644 --- a/weed/topology/topology.go +++ b/weed/topology/topology.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "slices" + "strconv" "strings" "sync" "sync/atomic" @@ -163,9 +164,11 @@ type Topology struct { QuarantineVolumeFn func(dn *DataNode, vid needle.VolumeId, readOnly bool) error // fileIdCeiling is the end of the file ids recorded through raft as - // handed out, and ownFileIdCeiling the one this master recorded last. + // handed out, and ownFileIdCeiling the one this master recorded last, + // under fileIdCeilingBy: another master can record the same value. fileIdCeiling atomic.Uint64 ownFileIdCeiling atomic.Uint64 + fileIdCeilingBy string fileIdCeilingLock sync.Mutex } @@ -361,6 +364,7 @@ func NewTopology(id string, seq sequence.Sequencer, volumeSizeLimit uint64, puls t.replicationAsMin = replicationAsMin t.Sequence = seq + t.fileIdCeilingBy = strconv.FormatUint(rand.Uint64(), 36) t.chanFullVolumes = make(chan storage.VolumeInfo) t.chanCrowdedVolumes = make(chan storage.VolumeInfo) @@ -801,6 +805,7 @@ func (t *Topology) recordFileIdCeiling(seen, ceiling uint64) error { command := NewMaxVolumeIdCommand(t.GetMaxVolumeId(), t.GetTopologyId()) command.FileIdCeiling = ceiling + command.FileIdCeilingBy = t.fileIdCeilingBy t.ownFileIdCeiling.Store(ceiling) t.RaftServerAccessLock.RLock() @@ -818,7 +823,7 @@ func (t *Topology) recordFileIdCeiling(seen, ceiling uint64) error { t.ownFileIdCeiling.Store(0) return err } - t.RaiseFileIdCeiling(ceiling) + t.RaiseFileIdCeiling(ceiling, t.fileIdCeilingBy) return nil } @@ -826,7 +831,7 @@ func (t *Topology) recordFileIdCeiling(seen, ceiling uint64) error { // out a file id at or past it without recording a higher one first. A master // that did not record it itself, or did before it restarted, moves its // sequence up to it, which is where it goes on from when it takes over. -func (t *Topology) RaiseFileIdCeiling(ceiling uint64) { +func (t *Topology) RaiseFileIdCeiling(ceiling uint64, by string) { if ceiling == 0 { return } @@ -836,7 +841,7 @@ func (t *Topology) RaiseFileIdCeiling(ceiling uint64) { break } } - if t.ownFileIdCeiling.Load() != ceiling { + if by == "" || by != t.fileIdCeilingBy || t.ownFileIdCeiling.Load() != ceiling { t.Sequence.SetMax(ceiling - 1) } } diff --git a/weed/topology/topology_file_id_ceiling_test.go b/weed/topology/topology_file_id_ceiling_test.go index 0b8314796e..45bb31fcbb 100644 --- a/weed/topology/topology_file_id_ceiling_test.go +++ b/weed/topology/topology_file_id_ceiling_test.go @@ -53,8 +53,8 @@ func TestMasterTakingOverStartsPastTheFileIdsHandedOut(t *testing.T) { // None of the three uploads has landed: the servers still report 1000. for master := 2; master <= 3; master++ { next := newFileIdTestTopology() - next.RaiseFileIdCeiling(recorded) // replayed from the raft log - next.Sequence.SetMax(written) // reported by the volume servers + next.RaiseFileIdCeiling(recorded, "") // replayed from the raft log + next.Sequence.SetMax(written) // reported by the volume servers for i := 0; i < 3; i++ { id := handOut(t, next) if handedOut[id] != 0 { @@ -105,10 +105,11 @@ type catchingUpRaftServer struct { func (s *catchingUpRaftServer) Do(command raft.Command) (interface{}, error) { if s.pending != 0 { - s.topo.RaiseFileIdCeiling(s.pending) + s.topo.RaiseFileIdCeiling(s.pending, "previous") s.pending = 0 } - s.topo.RaiseFileIdCeiling(command.(*MaxVolumeIdCommand).FileIdCeiling) + c := command.(*MaxVolumeIdCommand) + s.topo.RaiseFileIdCeiling(c.FileIdCeiling, c.FileIdCeilingBy) return nil, nil } @@ -119,7 +120,7 @@ func TestMasterTakingOverDropsAFileIdDrawnBeforeItCaughtUp(t *testing.T) { const applied, pending = 5000, 9000 // the previous master handed out ids up to 9000 topo := newFileIdTestTopology() - topo.RaiseFileIdCeiling(applied) + topo.RaiseFileIdCeiling(applied, "") topo.RaftServer = &catchingUpRaftServer{topo: topo, pending: pending} for i := 0; i < 3; i++ { @@ -196,7 +197,8 @@ type countingRaftServer struct { func (s *countingRaftServer) Do(command raft.Command) (interface{}, error) { s.count.Add(1) - s.topo.RaiseFileIdCeiling(command.(*MaxVolumeIdCommand).FileIdCeiling) + c := command.(*MaxVolumeIdCommand) + s.topo.RaiseFileIdCeiling(c.FileIdCeiling, c.FileIdCeilingBy) return nil, nil } @@ -226,3 +228,37 @@ func TestFileIdDrawLargerThanTheCeilingStep(t *testing.T) { t.Fatalf("handed out more file ids at once than the sequence can be checked against") } } + +// sameCeilingRaftServer is a master that has just taken over and proposes, as +// its first ceiling, the very one the master before it recorded last and it +// has not applied yet. +type sameCeilingRaftServer struct { + raft.Server + topo *Topology + previous uint64 +} + +func (s *sameCeilingRaftServer) Do(command raft.Command) (interface{}, error) { + c := command.(*MaxVolumeIdCommand) + if s.previous == 0 { + s.previous = c.FileIdCeiling + s.topo.RaiseFileIdCeiling(s.previous, "previous") + } + s.topo.RaiseFileIdCeiling(c.FileIdCeiling, c.FileIdCeilingBy) + return nil, nil +} + +// The ceiling alone does not tell whose record it is: the one the master +// before recorded moves the sequence even when this master proposed the same. +func TestMasterTakingOverWithTheSameFileIdCeilingAsTheOneBefore(t *testing.T) { + topo := newFileIdTestTopology() + topo.RaiseFileIdCeiling(5000, "") + + raftServer := &sameCeilingRaftServer{topo: topo} + topo.RaftServer = raftServer + for i := 0; i < 3; i++ { + if id := handOut(t, topo); id < raftServer.previous { + t.Fatalf("file id %d handed out, below the %d the master before recorded as handed out", id, raftServer.previous) + } + } +}