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

Revert "pick host randomly when distance map is empty (#285)" #287

Merged
merged 1 commit into from
Sep 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}