Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Revert "pick host randomly when distance map is empty (#285)" (#287)
Browse files Browse the repository at this point in the history
This reverts commit 08d5bef.
  • Loading branch information
datoug committed Sep 1, 2017
1 parent 9790e57 commit 85ea27e
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions services/controllerhost/placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
)

var errNoHosts = errors.New("Unable to find healthy hosts")
var errNotEnoughHosts = errors.New("Unable to find enough hosts")
var errNoInputHosts = errors.New("Unable to find healthy input host")
var errNoOutputHosts = errors.New("Unable to find healthy output host")
var errNoStoreHosts = errors.New("Unable to find healthy store hosts")
Expand Down Expand Up @@ -78,7 +77,7 @@ func toResources(hosts []*common.HostInfo) []string {
// Helper function to pick hosts based on the predicates
func (p *DistancePlacement) pickHosts(service string, poolHosts, sourceHosts []*common.HostInfo, count int, minDistance, maxDistance uint16) ([]*common.HostInfo, error) {
if p.distMap == nil {
return p.pickRandomHosts(poolHosts, count)
return poolHosts[:count], nil
}

sourceResources := toResources(sourceHosts)
Expand Down Expand Up @@ -205,8 +204,16 @@ func (p *DistancePlacement) PickStoreHosts(count int) ([]*common.HostInfo, error
return hosts, nil
}
}

return p.pickRandomHosts(storeHosts, count)
if cnt := len(storeHosts); cnt >= count {
start := rand.Intn(cnt)
end := start + count
if end > cnt {
end = cnt
}
hosts := append([]*common.HostInfo{}, storeHosts[start:end]...)
hosts = append(hosts, storeHosts[:count-len(hosts)]...)
return hosts, nil
}
}

return nil, errNoStoreHosts
Expand Down Expand Up @@ -297,18 +304,3 @@ func (p *DistancePlacement) findEligibleStoreHosts() ([]*common.HostInfo, error)

return result, nil
}

func (p *DistancePlacement) pickRandomHosts(source []*common.HostInfo, count int) ([]*common.HostInfo, error) {
if len(source) < count {
return nil, errNotEnoughHosts
}

pickedHosts := make([]*common.HostInfo, count)
for i, j := range rand.Perm(len(source)) {
if i >= count {
break
}
pickedHosts[i] = source[j]
}
return pickedHosts, nil
}

0 comments on commit 85ea27e

Please sign in to comment.