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

pick host randomly when distance map is empty #294

Merged
merged 2 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions services/controllerhost/controllerhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,22 @@ func (s *McpSuite) listInputHostExtents(dstUUID string, inputHostUUID string) (*
mReq := &m.ListInputHostExtentsStatsRequest{
DestinationUUID: common.StringPtr(dstUUID),
InputHostUUID: common.StringPtr(inputHostUUID),
Status: common.MetadataExtentStatusPtr(shared.ExtentStatus_OPEN),
}
return s.mClient.ListInputHostExtentsStats(nil, mReq)
}

func (s *McpSuite) updateExtentStatus(dstID, extID string, status shared.ExtentStatus) error {
mReq := &m.UpdateExtentStatsRequest{
DestinationUUID: common.StringPtr(dstID),
ExtentUUID: common.StringPtr(extID),
Status: shared.ExtentStatusPtr(status),
}

_, err := s.mClient.UpdateExtentStats(nil, mReq)
return err
}

func (s *McpSuite) createConsumerGroup(dstPath, cgName string) (*shared.ConsumerGroupDescription, error) {
return s.createConsumerGroupWithDLQ(dstPath, cgName)
}
Expand Down Expand Up @@ -314,9 +326,13 @@ func (s *McpSuite) TestGetInputHosts() {
s.Nil(err, "GetInputHosts() created an extent with un-known store uuid")
}

// seal extent
s.mcp.context.extentSeals.inProgress.PutIfNotExist(extent.GetExtentUUID(), Boolean(true))
sealedExtents++
err = s.updateExtentStatus(dstUUID, extent.GetExtentUUID(), shared.ExtentStatus_SEALED)
s.Nil(err)

// expect a new extent created
resp, err = s.mcp.GetInputHosts(nil, &c.GetInputHostsRequest{DestinationUUID: dstDesc.DestinationUUID})
s.Nil(err)
s.Equal(1, len(resp.InputHostIds), "GetInputHosts() returned an extent that's in progress for seal")
Expand Down Expand Up @@ -618,7 +634,7 @@ func (s *McpSuite) TestGetOutputHosts() {
s.Equal(1, len(resp.GetOutputHostIds()), "GetOutputHosts() returned more than one out host")

cge, err = s.mClient.ReadConsumerGroupExtents(nil,
&shared.ReadConsumerGroupExtentsRequest{DestinationUUID: common.StringPtr(dstUUID), ConsumerGroupUUID: common.StringPtr(cgUUID), OutputHostUUID: common.StringPtr(outputHost.UUID), MaxResults: common.Int32Ptr(10)})
&shared.ReadConsumerGroupExtentsRequest{DestinationUUID: common.StringPtr(dstUUID), ConsumerGroupUUID: common.StringPtr(cgUUID), MaxResults: common.Int32Ptr(10)})
s.Nil(err, "Failed to find consumer group extent entry for outputhost")
s.Equal(2, len(cge.GetExtents()), "Wrong number of extents for consumer group")
for _, cge := range cge.Extents {
Expand Down Expand Up @@ -657,7 +673,6 @@ func (s *McpSuite) TestGetOutputHosts() {
break
}
}
s.True(ok, "Unknown out host %v found in consumer group extent table", e.GetOutputHostUUID())
}

s.mockrpm.Remove(common.OutputServiceName, toKill)
Expand Down
30 changes: 19 additions & 11 deletions services/controllerhost/placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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 @@ -77,7 +78,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 poolHosts[:count], nil
return p.pickRandomHosts(poolHosts, count)
}

sourceResources := toResources(sourceHosts)
Expand Down Expand Up @@ -204,16 +205,8 @@ func (p *DistancePlacement) PickStoreHosts(count int) ([]*common.HostInfo, error
return hosts, nil
}
}
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 p.pickRandomHosts(storeHosts, count)
}

return nil, errNoStoreHosts
Expand Down Expand Up @@ -304,3 +297,18 @@ 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
}