From f9383aa726771b9bd9027cf5e9d62fcaecad9cee Mon Sep 17 00:00:00 2001 From: chrislu Date: Sun, 9 Oct 2022 18:58:10 -0700 Subject: [PATCH] refactor to change capacity data type --- weed/shell/command_ec_common.go | 4 ---- weed/shell/command_volume_balance.go | 18 +++++++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/weed/shell/command_ec_common.go b/weed/shell/command_ec_common.go index 325e504c4..5ca77785f 100644 --- a/weed/shell/command_ec_common.go +++ b/weed/shell/command_ec_common.go @@ -282,10 +282,6 @@ func mountEcShards(grpcDialOption grpc.DialOption, collection string, volumeId n }) } -func divide(total, n int) float64 { - return float64(total) / float64(n) -} - func ceilDivide(total, n int) int { return int(math.Ceil(float64(total) / float64(n))) } diff --git a/weed/shell/command_volume_balance.go b/weed/shell/command_volume_balance.go index bc0adfc5f..980a8d7d1 100644 --- a/weed/shell/command_volume_balance.go +++ b/weed/shell/command_volume_balance.go @@ -183,34 +183,34 @@ type Node struct { rack string } -type CapacityFunc func(*master_pb.DataNodeInfo) int +type CapacityFunc func(*master_pb.DataNodeInfo) float64 func capacityByMaxVolumeCount(diskType types.DiskType) CapacityFunc { - return func(info *master_pb.DataNodeInfo) int { + return func(info *master_pb.DataNodeInfo) float64 { diskInfo, found := info.DiskInfos[string(diskType)] if !found { return 0 } - return int(diskInfo.MaxVolumeCount) + return float64(diskInfo.MaxVolumeCount) } } func capacityByFreeVolumeCount(diskType types.DiskType) CapacityFunc { - return func(info *master_pb.DataNodeInfo) int { + return func(info *master_pb.DataNodeInfo) float64 { diskInfo, found := info.DiskInfos[string(diskType)] if !found { return 0 } - return int(diskInfo.MaxVolumeCount - diskInfo.VolumeCount) + return float64(diskInfo.MaxVolumeCount - diskInfo.VolumeCount) } } func (n *Node) localVolumeRatio(capacityFunc CapacityFunc) float64 { - return divide(len(n.selectedVolumes), capacityFunc(n.info)) + return float64(len(n.selectedVolumes)) / capacityFunc(n.info) } func (n *Node) localVolumeNextRatio(capacityFunc CapacityFunc) float64 { - return divide(len(n.selectedVolumes)+1, capacityFunc(n.info)) + return float64(len(n.selectedVolumes)+1) / capacityFunc(n.info) } func (n *Node) isOneVolumeOnly() bool { @@ -243,7 +243,7 @@ func sortWritableVolumes(volumes []*master_pb.VolumeInformationMessage) { } func balanceSelectedVolume(commandEnv *CommandEnv, diskType types.DiskType, volumeReplicas map[uint32][]*VolumeReplica, nodes []*Node, capacityFunc CapacityFunc, sortCandidatesFn func(volumes []*master_pb.VolumeInformationMessage), applyBalancing bool) (err error) { - selectedVolumeCount, volumeMaxCount := 0, 0 + selectedVolumeCount, volumeMaxCount := 0, float64(0) var nodesWithCapacity []*Node for _, dn := range nodes { selectedVolumeCount += len(dn.selectedVolumes) @@ -254,7 +254,7 @@ func balanceSelectedVolume(commandEnv *CommandEnv, diskType types.DiskType, volu volumeMaxCount += capacity } - idealVolumeRatio := divide(selectedVolumeCount, volumeMaxCount) + idealVolumeRatio := float64(selectedVolumeCount) / volumeMaxCount hasMoved := true