a cleaner fix

This commit is contained in:
Chris Lu 2020-12-18 10:39:30 -08:00
parent b7fef8f693
commit 2260864b45

View file

@ -244,9 +244,14 @@ func sortReadOnlyVolumes(volumes []*master_pb.VolumeInformationMessage) {
func balanceSelectedVolume(commandEnv *CommandEnv, volumeReplicas map[uint32][]*VolumeReplica, nodes []*Node, capacityFunc CapacityFunc, sortCandidatesFn func(volumes []*master_pb.VolumeInformationMessage), applyBalancing bool) (err error) { func balanceSelectedVolume(commandEnv *CommandEnv, 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, 0
var nodesWithCapacity []*Node
for _, dn := range nodes { for _, dn := range nodes {
selectedVolumeCount += len(dn.selectedVolumes) selectedVolumeCount += len(dn.selectedVolumes)
volumeMaxCount += capacityFunc(dn.info) capacity := capacityFunc(dn.info)
if capacity > 0 {
nodesWithCapacity = append(nodesWithCapacity, dn)
}
volumeMaxCount += capacity
} }
idealVolumeRatio := divide(selectedVolumeCount, volumeMaxCount) idealVolumeRatio := divide(selectedVolumeCount, volumeMaxCount)
@ -257,22 +262,19 @@ func balanceSelectedVolume(commandEnv *CommandEnv, volumeReplicas map[uint32][]*
for hasMoved { for hasMoved {
hasMoved = false hasMoved = false
sort.Slice(nodes, func(i, j int) bool { sort.Slice(nodesWithCapacity, func(i, j int) bool {
return nodes[i].localVolumeRatio(capacityFunc) < nodes[j].localVolumeRatio(capacityFunc) return nodesWithCapacity[i].localVolumeRatio(capacityFunc) < nodesWithCapacity[j].localVolumeRatio(capacityFunc)
}) })
fullNode := nodes[len(nodes)-1] fullNode := nodesWithCapacity[len(nodesWithCapacity)-1]
var candidateVolumes []*master_pb.VolumeInformationMessage var candidateVolumes []*master_pb.VolumeInformationMessage
for _, v := range fullNode.selectedVolumes { for _, v := range fullNode.selectedVolumes {
candidateVolumes = append(candidateVolumes, v) candidateVolumes = append(candidateVolumes, v)
} }
sortCandidatesFn(candidateVolumes) sortCandidatesFn(candidateVolumes)
for i := 0; i < len(nodes)-1; i++ { for i := 0; i < len(nodesWithCapacity)-1; i++ {
emptyNode := nodes[i] emptyNode := nodesWithCapacity[i]
if capacityFunc(emptyNode.info) == 0 {
continue
}
if !(fullNode.localVolumeRatio(capacityFunc) > idealVolumeRatio && emptyNode.localVolumeNextRatio(capacityFunc) <= idealVolumeRatio) { if !(fullNode.localVolumeRatio(capacityFunc) > idealVolumeRatio && emptyNode.localVolumeNextRatio(capacityFunc) <= idealVolumeRatio) {
// no more volume servers with empty slots // no more volume servers with empty slots
break break