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

Conversation

vpranckaitis
Copy link
Collaborator

What this PR does / why we need it:

Fixes unbalanced shard assignment to instances when initializing sharded placement. This was caused by nondeterministic comparison function used in a heap.

Special notes for your reviewer:

Does this PR introduce a user-facing and/or backwards incompatible change?:

NONE

Does this PR require updating code package or user-facing documentation?:

NONE

@@ -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.

Copy link
Collaborator

@Antanukas Antanukas left a comment

Choose a reason for hiding this comment

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

LGTM. With an optional idea about property based testing.

Copy link
Collaborator

@linasm linasm left a comment

Choose a reason for hiding this comment

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

Two questions:

  1. Is this the code that also generates the placements for m3aggregator?
  2. Is this code used during initial placement generation only, or also when moving shards around during cluster resizing?

@@ -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.

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

Comment on lines 1319 to 1322
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.

Comment on lines 1334 to 1342
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.

Copy link
Collaborator Author

@vpranckaitis vpranckaitis left a comment

Choose a reason for hiding this comment

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

Two questions:
1. Is this the code that also generates the placements for m3aggregator?

The same handler is used for initializing m3db, m3coordinator and m3aggregator placements

var (
initHandler = NewInitHandler(opts)
initFn = applyMiddleware(initHandler.ServeHTTP, defaults)
routes []Route
)
routes = append(routes, Route{
Paths: []string{
M3DBInitURL,
M3AggInitURL,
M3CoordinatorInitURL,
},
Handler: initFn,
Methods: []string{InitHTTPMethod},
})

There are non_sharded, sharded and mirrored algorithms (I suppose different algorithms are used for dbnodes and aggregators). non_sharded is straigthforward, and mirrored depends on sharded.

2. Is this code used during initial placement generation only, or also when moving shards around during cluster resizing?

Yes. placeShards(), which uses the heap, also is used for other operations.

paveikslas

Comment on lines 1319 to 1322
ids := make([]uint32, 1024)
for i := range ids {
ids[i] = uint32(i)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines 1334 to 1342
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 Author

Choose a reason for hiding this comment

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

@vpranckaitis vpranckaitis requested a review from linasm December 21, 2021 09:47
Copy link
Collaborator

@linasm linasm left a comment

Choose a reason for hiding this comment

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

LGTM but I agree with @Antanukas that a property based test feels like a good fit here, as we have a good property to assert on (max - min <= 1), also there are other clear properties to check (such as whether every shard has been included and there are no duplicates).

Comment on lines 1341 to 1342
require.Equal(t, 102, min)
require.Equal(t, 103, max)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: just do the range assertions in the loop and drop 10 LoC.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Migrated to property test instead: ebc054b

Copy link
Collaborator

@linasm linasm left a comment

Choose a reason for hiding this comment

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

LGTM with one suggestion.

testInitialPlacementIsBalanced,
gen.IntRange(1, 3),
gen.IntRange(1, 20),
gen.IntRange(128, 1024),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's cover a broader shard range based on what we've seen (maybe 64 to 3072).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vpranckaitis vpranckaitis enabled auto-merge (squash) December 22, 2021 10:30
@vpranckaitis vpranckaitis merged commit 17096be into master Dec 22, 2021
@vpranckaitis vpranckaitis deleted the vilus/fix_unbalanced_shards branch December 22, 2021 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants