Skip to content

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

Merged
vpranckaitis merged 7 commits into
masterfrom
vilus/fix_unbalanced_shards
Dec 22, 2021
Merged

[cluster] Fix unbalanced initial shard allocation for sharded placement#4020
vpranckaitis merged 7 commits into
masterfrom
vilus/fix_unbalanced_shards

Conversation

@vpranckaitis

Copy link
Copy Markdown
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

assert.Equal(t, expectedInstances, balancedPlacement.Instances())
}

func TestInitialPlacementIsBalanced(t *testing.T) {

Copy link
Copy Markdown
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
Copy Markdown
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.

@Antanukas Antanukas left a comment

Copy link
Copy Markdown
Collaborator

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.

@linasm linasm left a comment

Copy link
Copy Markdown
Collaborator

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?

assert.Equal(t, expectedInstances, balancedPlacement.Instances())
}

func TestInitialPlacementIsBalanced(t *testing.T) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@vpranckaitis vpranckaitis left a comment

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
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

@linasm linasm left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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

@linasm linasm left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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