Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cluster] Fix unbalanced initial shard allocation for sharded placement #4020

Merged
merged 7 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 12 additions & 5 deletions src/cluster/placement/algo/sharded_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ func (ph *helper) returnInitializingShardsToSource(

func (ph *helper) mostUnderLoadedInstance() (placement.Instance, bool) {
var (
res placement.Instance
maxLoadGap int
res placement.Instance
maxLoadGap int
totalLoadSurplus int
)

Expand Down Expand Up @@ -656,12 +656,19 @@ func (h *instanceHeap) Less(i, j int) bool {
leftLoadOnJ := h.targetLoadForInstance(instanceJ.ID()) - loadOnInstance(instanceJ)
// If both instance has tokens to be filled, prefer the one from bigger isolation group
// since it tends to be more picky in accepting shards
if leftLoadOnI > 0 && leftLoadOnJ > 0 {
if instanceI.IsolationGroup() != instanceJ.IsolationGroup() {
return h.igToWeightMap[instanceI.IsolationGroup()] > h.igToWeightMap[instanceJ.IsolationGroup()]
if leftLoadOnI > 0 && leftLoadOnJ > 0 && instanceI.IsolationGroup() != instanceJ.IsolationGroup() {
var (
igWeightI = h.igToWeightMap[instanceI.IsolationGroup()]
igWeightJ = h.igToWeightMap[instanceJ.IsolationGroup()]
)
if igWeightI != igWeightJ {
return igWeightI > igWeightJ
}
}
// compare left capacity on both instances
if leftLoadOnI == leftLoadOnJ {
return instanceI.ID() < instanceJ.ID()
}
if h.capacityAscending {
return leftLoadOnI > leftLoadOnJ
}
Expand Down
36 changes: 36 additions & 0 deletions src/cluster/placement/algo/sharded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,42 @@ func TestBalanceShardsForShardedWhenImbalanced(t *testing.T) {
assert.Equal(t, expectedInstances, balancedPlacement.Instances())
}

func TestInitialPlacementIsBalanced(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be useful to apply property based testing in this case? https://pkg.go.dev/testing/quick
To be more confident we generate well balanced shards.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a matter of fact we are using github.com/leanovate/gopter.

instances := make([]placement.Instance, 0)

for i := 0; i < 10; i++ {
instances = append(instances,
placement.NewEmptyInstance(fmt.Sprintf("instance-0-%03d", i), "iso0", "zone", "endpoint", 1),
placement.NewEmptyInstance(fmt.Sprintf("instance-1-%03d", i), "iso1", "zone", "endpoint", 1),
)
}

ids := make([]uint32, 1024)
for i := range ids {
ids[i] = uint32(i)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From this code I can't get what those ids are. Are they shard ids? Instance ids? Some other ids? Perhaps some naming would help here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


algo := newShardedAlgorithm(placement.NewOptions())
p, err := algo.InitialPlacement(instances, ids, 2)
require.NoError(t, err)

var (
min = math.MaxInt32
max = math.MinInt32
)
for _, instance := range p.Instances() {
n := instance.Shards().NumShards()
if n < min {
min = n
}
if n > max {
max = n
}
}
require.True(t, max-min <= 1,
"The number of shards per instance differs by more than 1, min=%d max=%d", min, max)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a unit test for a single concrete case, I would simplify it by asserting that n is within certain range.
What is done here would be more appropriate for a property based test that @Antanukas has suggested (if you decide to go with that approach).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

func verifyAllShardsInAvailableState(t *testing.T, p placement.Placement) {
for _, instance := range p.Instances() {
s := instance.Shards()
Expand Down