mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-23 06:20:48 +02:00
* ec distribution: parse EC and replication configs into a shard plan NewECConfig/DefaultECConfig and NewReplicationConfig(FromString) turn an EC ratio and a replica placement into ECConfig/ReplicationConfig values, and CalculateDistribution derives the target and maximum shard counts per data center, rack, and node. ECDistribution.Summary and FaultToleranceAnalysis describe, per topology level, whether losing one failure domain leaves enough shards to reconstruct the volume, so the EC durability guarantees of a given setup can be answered directly instead of only documented informally. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: analyze topology and plan shard moves TopologyAnalysis records nodes and shard locations per DC/rack/node, and Rebalancer.PlanRebalance emits the moves that level the layout: data centers first, then racks within each DC, then nodes within each rack, moving parity shards before data shards and honoring per-node free slots. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: cover the plan math and rebalancing with tests Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: keep AddShardLocation and node ShardIDs in sync AddShardLocation updated the count maps but never the node's ShardIDs — yet every rebalancer count reads len(node.ShardIDs), so the documented AddNode-then-AddShardLocation flow produced an empty rebalance plan. It now appends to the node's shard list and skips shards the node already has, making the two registration styles equivalent and idempotent. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: key rack-scoped maps by dc/rack Rack names are only unique within a data center — two DCs can both hold a "rack1" — so ShardsByRack, RackToShards, and RackToNodes keyed by the bare rack name merged distinct domains and corrupted both the DC-level shard counts and rack-level balancing. Rack lookups now use the dc/rack pair; DCToRacks still lists bare rack names. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: never move a shard onto a node already holding it The move picker selected the destination node before the shard, so a node already holding the chosen shard could be targeted. Pick the shard first (parity-first) and choose among destination nodes that lack it. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: tolerate zero-value replication configs A ReplicationConfig{} with zeroed minimums made maxShare divide by zero; clamp each level's unit count to at least one. Covers the new behaviors with tests. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: keep rack keys distinct when names contain "/" rackKey joined data center and rack with "/", so dc "a"/rack "b/c" and dc "a/b"/rack "c" shared one key and were counted as the same failure domain. Separate the parts with a NUL byte, which cannot appear in a name. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: plan rebalances on a copy of the topology recordMove mutated the caller's TopologyAnalysis while planning, so an interrupted or partial execution left the input describing moves that never happened, and replanning on it returned nothing. PlanRebalance now simulates on a cloned analysis; plan.Analysis reports the layout the moves would produce and the caller's input is left untouched. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: balance nodes against the rack's current shard count The node-level target still read the original analysis, so a rack that had just sent or received shards during DC/rack balancing aimed at a stale total and left its nodes unbalanced. * ec distribution: register each node id once Adding the same node twice appended it to RackToNodes twice, so every count that walks rack nodes double-counted its shards. * ec distribution: normalize replication counts in the stored plan A zero-value ReplicationConfig computed targets against one domain per level while Summary still reported zero DCs, racks, and nodes. * ec distribution: cap a survivable level's max at the loss budget * ec distribution: skip shards already inside the destination domain * ec distribution: register nodes implied by shard locations * ec distribution: merge repeat node registrations A shard location now auto-registers an unknown node with no free slots; a later AddNode carrying the real capacity was then dropped entirely. Repeat registration now updates the node's capacity and adopts any new shards, while the first registration keeps the topology placement. * ec distribution: try other overloaded sources before stopping When the most overloaded domain could not shed any shard, balance returned early and skipped every other source that still had movable shards. A source that cannot move now gets set aside for the rest of the pass. * ec distribution: collapse chained shard moves to the final destination Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ec distribution: schedule collapsed moves behind the departures that free slots Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
151 lines
5.1 KiB
Go
151 lines
5.1 KiB
Go
package distribution
|
|
|
|
// NewTopologyAnalysis creates an empty topology analysis.
|
|
func NewTopologyAnalysis() *TopologyAnalysis {
|
|
return &TopologyAnalysis{
|
|
ShardsByDC: make(map[string]int),
|
|
ShardsByRack: make(map[string]int),
|
|
ShardsByNode: make(map[string]int),
|
|
DCToShards: make(map[string][]int),
|
|
RackToShards: make(map[string][]int),
|
|
NodeToShards: make(map[string][]int),
|
|
DCToRacks: make(map[string][]string),
|
|
RackToNodes: make(map[string][]*TopologyNode),
|
|
AllNodes: make(map[string]*TopologyNode),
|
|
}
|
|
}
|
|
|
|
// AddNode registers a node in the topology, including any shards it already
|
|
// holds for the volume being analyzed. A node id may only register once;
|
|
// registering it again updates its capacity and adds any new shards. The
|
|
// first registration wins for the node's place in the topology.
|
|
func (a *TopologyAnalysis) AddNode(node *TopologyNode) {
|
|
registered, ok := a.AllNodes[node.NodeID]
|
|
if !ok {
|
|
registered = node
|
|
a.AllNodes[node.NodeID] = node
|
|
a.RackToNodes[rackKey(node.DataCenter, node.Rack)] = append(a.RackToNodes[rackKey(node.DataCenter, node.Rack)], node)
|
|
if !contains(a.DCToRacks[node.DataCenter], node.Rack) {
|
|
a.DCToRacks[node.DataCenter] = append(a.DCToRacks[node.DataCenter], node.Rack)
|
|
}
|
|
} else {
|
|
registered.FreeSlots = node.FreeSlots
|
|
registered.TotalShards = node.TotalShards
|
|
}
|
|
seen := node.ShardIDs[:0]
|
|
for _, shardID := range node.ShardIDs {
|
|
if !contains(seen, shardID) {
|
|
seen = append(seen, shardID)
|
|
}
|
|
}
|
|
node.ShardIDs = seen
|
|
for _, shardID := range node.ShardIDs {
|
|
a.AddShardLocation(ShardLocation{
|
|
ShardID: shardID,
|
|
NodeID: registered.NodeID,
|
|
DataCenter: registered.DataCenter,
|
|
Rack: registered.Rack,
|
|
})
|
|
}
|
|
}
|
|
|
|
// AddShardLocation records one shard's placement in the topology. Repeated
|
|
// calls for a shard already known to its node are ignored, so callers may
|
|
// either set ShardIDs on the TopologyNode or register each shard here. A
|
|
// location naming an unregistered node registers that node so the rebalancer
|
|
// can see the shard; for a registered node the node's own dc/rack win.
|
|
func (a *TopologyAnalysis) AddShardLocation(loc ShardLocation) {
|
|
node, ok := a.AllNodes[loc.NodeID]
|
|
if !ok {
|
|
node = &TopologyNode{NodeID: loc.NodeID, DataCenter: loc.DataCenter, Rack: loc.Rack}
|
|
a.AddNode(node)
|
|
}
|
|
if contains(a.NodeToShards[loc.NodeID], loc.ShardID) {
|
|
return
|
|
}
|
|
a.ShardsByDC[node.DataCenter]++
|
|
a.ShardsByRack[rackKey(node.DataCenter, node.Rack)]++
|
|
a.ShardsByNode[loc.NodeID]++
|
|
a.DCToShards[node.DataCenter] = append(a.DCToShards[node.DataCenter], loc.ShardID)
|
|
a.RackToShards[rackKey(node.DataCenter, node.Rack)] = append(a.RackToShards[rackKey(node.DataCenter, node.Rack)], loc.ShardID)
|
|
a.NodeToShards[loc.NodeID] = append(a.NodeToShards[loc.NodeID], loc.ShardID)
|
|
a.TotalShards++
|
|
if !contains(node.ShardIDs, loc.ShardID) {
|
|
node.ShardIDs = append(node.ShardIDs, loc.ShardID)
|
|
}
|
|
}
|
|
|
|
// rackKey qualifies a rack name with its data center: rack names are only
|
|
// unique within a DC, so two DCs can both hold a "rack1". NUL separates the
|
|
// parts so a "/" inside either name cannot merge distinct racks.
|
|
func rackKey(dc, rack string) string {
|
|
return dc + "\x00" + rack
|
|
}
|
|
|
|
// Finalize computes topology statistics after all nodes and shards are added.
|
|
func (a *TopologyAnalysis) Finalize() {
|
|
a.TotalNodes = len(a.AllNodes)
|
|
a.TotalRacks = len(a.RackToNodes)
|
|
a.TotalDCs = len(a.DCToRacks)
|
|
}
|
|
|
|
func contains[T comparable](list []T, s T) bool {
|
|
for _, item := range list {
|
|
if item == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ShardLocation represents where a shard is located in the topology
|
|
type ShardLocation struct {
|
|
ShardID int
|
|
NodeID string
|
|
DataCenter string
|
|
Rack string
|
|
}
|
|
|
|
// TopologyNode represents a node in the topology that can hold EC shards
|
|
type TopologyNode struct {
|
|
NodeID string
|
|
DataCenter string
|
|
Rack string
|
|
FreeSlots int // Available slots for new shards
|
|
ShardIDs []int // Shard IDs currently on this node for a specific volume
|
|
TotalShards int // Total shards on this node (for all volumes)
|
|
}
|
|
|
|
// TopologyAnalysis holds the current shard distribution analysis for a volume
|
|
type TopologyAnalysis struct {
|
|
// Shard counts at each level
|
|
ShardsByDC map[string]int
|
|
ShardsByRack map[string]int
|
|
ShardsByNode map[string]int
|
|
|
|
// Detailed shard locations
|
|
DCToShards map[string][]int // DC -> list of shard IDs
|
|
RackToShards map[string][]int // Rack -> list of shard IDs
|
|
NodeToShards map[string][]int // NodeID -> list of shard IDs
|
|
|
|
// Topology structure
|
|
DCToRacks map[string][]string // DC -> list of rack IDs
|
|
RackToNodes map[string][]*TopologyNode // Rack -> list of nodes
|
|
AllNodes map[string]*TopologyNode // NodeID -> node info
|
|
|
|
// Statistics
|
|
TotalShards int
|
|
TotalNodes int
|
|
TotalRacks int
|
|
TotalDCs int
|
|
}
|
|
|
|
// TopologyExcess represents a topology level (DC/rack/node) with excess shards
|
|
type TopologyExcess struct {
|
|
ID string // DC/rack/node ID
|
|
Level string // "dc", "rack", or "node"
|
|
Excess int // Number of excess shards (above target)
|
|
Shards []int // Shard IDs at this level
|
|
Nodes []*TopologyNode // Nodes at this level (for finding sources)
|
|
}
|