diff --git a/weed/ec/ec_common.go b/weed/ec/ec_common.go index f5ec17ad94..0c50a16669 100644 --- a/weed/ec/ec_common.go +++ b/weed/ec/ec_common.go @@ -1463,10 +1463,47 @@ func (ecb *ecBalancer) applyShardMoveRPC(src, dst *EcNode, collection string, vi // separates generations living on different disks; same-disk mixing is prevented // upstream by the pre-encode artifact wipe and the cross-run read guard. func CountExistingEcShardsForVolume(topologyInfo *master_pb.TopologyInfo, volumeID uint32, collection string) int { - if topologyInfo == nil { - return 0 + best := 0 + for _, gen := range ecShardGenerations(topologyInfo, volumeID, collection) { + if c := gen.bits.Count(); c > best { + best = c + } } - perGeneration := make(map[int64]erasure_coding.ShardBits) + return best +} + +// LargestEcGenerationHolders returns the nodes holding shards of the +// generation CountExistingEcShardsForVolume counts, or nil when no generation +// is strictly largest and so none can be named the EC volume. +func LargestEcGenerationHolders(topologyInfo *master_pb.TopologyInfo, volumeID uint32, collection string) []string { + var largest *ecShardGeneration + tied := false + for _, gen := range ecShardGenerations(topologyInfo, volumeID, collection) { + switch { + case largest == nil || gen.bits.Count() > largest.bits.Count(): + largest, tied = gen, false + case gen.bits.Count() == largest.bits.Count(): + tied = true + } + } + if largest == nil || tied { + return nil + } + return largest.holders +} + +type ecShardGeneration struct { + bits erasure_coding.ShardBits + holders []string +} + +// ecShardGenerations groups the (volumeID, collection) shards in the topology +// by encode generation, recording each generation's shard bits and holders. +func ecShardGenerations(topologyInfo *master_pb.TopologyInfo, volumeID uint32, collection string) map[int64]*ecShardGeneration { + if topologyInfo == nil { + return nil + } + perGeneration := make(map[int64]*ecShardGeneration) for _, dc := range topologyInfo.DataCenterInfos { for _, rack := range dc.RackInfos { for _, node := range rack.DataNodeInfos { @@ -1481,19 +1518,22 @@ func CountExistingEcShardsForVolume(topologyInfo *master_pb.TopologyInfo, volume if ecShardInfo.Id != volumeID || ecShardInfo.Collection != collection { continue } - perGeneration[ecShardInfo.EncodeTsNs] |= erasure_coding.ShardBits(ecShardInfo.EcIndexBits) + gen := perGeneration[ecShardInfo.EncodeTsNs] + if gen == nil { + gen = &ecShardGeneration{} + perGeneration[ecShardInfo.EncodeTsNs] = gen + } + bits := erasure_coding.ShardBits(ecShardInfo.EcIndexBits) + gen.bits |= bits + if bits.Count() > 0 && !slices.Contains(gen.holders, node.Id) { + gen.holders = append(gen.holders, node.Id) + } } } } } } - best := 0 - for _, bits := range perGeneration { - if c := bits.Count(); c > best { - best = c - } - } - return best + return perGeneration } // ParseVolumeIdsFlag parses a comma-separated -volumeIds flag value, dropping diff --git a/weed/worker/tasks/erasure_coding/detection_ec_generation_test.go b/weed/worker/tasks/erasure_coding/detection_ec_generation_test.go index bd735d4533..de81dbcff2 100644 --- a/weed/worker/tasks/erasure_coding/detection_ec_generation_test.go +++ b/weed/worker/tasks/erasure_coding/detection_ec_generation_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/seaweedfs/seaweedfs/weed/admin/topology" + "github.com/seaweedfs/seaweedfs/weed/ec" "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" "github.com/stretchr/testify/require" ) @@ -90,3 +91,23 @@ func TestCountExistingEcShards_PreUpgradeZeroGeneration(t *testing.T) { }) require.Equal(t, 14, countExistingEcShardsForVolume(at, vid, "")) } + +// The .ecx used to vet a source replica must come from a holder of the +// generation that is counted as the EC volume, never from a stale run, and no +// holder is named when two generations tie. +func TestLargestEcGenerationHolders(t *testing.T) { + const vid = uint32(45) + at := buildEcTopology(t, vid, "", []ecShardSet{ + {bits: ecBits(0, 1, 2), encodeTsNs: 1}, + {bits: ecBits(0, 1, 2, 3, 4, 5, 6), encodeTsNs: 2}, + {bits: ecBits(7, 8, 9, 10, 11, 12, 13), encodeTsNs: 2}, + }) + require.Equal(t, []string{"10.0.0.2:8080", "10.0.0.3:8080"}, ec.LargestEcGenerationHolders(at.GetTopologyInfo(), vid, "")) + + tied := buildEcTopology(t, vid, "", []ecShardSet{ + {bits: ecBits(0, 1, 2), encodeTsNs: 1}, + {bits: ecBits(3, 4, 5), encodeTsNs: 2}, + }) + require.Nil(t, ec.LargestEcGenerationHolders(tied.GetTopologyInfo(), vid, "")) + require.Nil(t, ec.LargestEcGenerationHolders(nil, vid, "")) +}