refactor to change capacity data type

This commit is contained in:
chrislu 2022-10-09 18:58:10 -07:00
parent 0f522da367
commit f9383aa726
2 changed files with 9 additions and 13 deletions

View file

@ -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)))
}

View file

@ -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