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

fix potential ArgumentException during shard rebalancing #7367

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ protected bool IsAGoodTimeToRebalance(IEnumerable<RegionEntry> regionEntries)

protected ImmutableList<RegionEntry> RegionEntriesFor(IImmutableDictionary<IActorRef, IImmutableList<string>> currentShardAllocations)
{
var addressToMember = ClusterState.Members.ToImmutableDictionary(m => m.Address, m => m);
Copy link
Member Author

Choose a reason for hiding this comment

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

Left some comments that explain the change

// switched to using `GroupBy` instead just ToImmutableDictionary due to https://github.com/akkadotnet/akka.net/issues/7365
// it's very rare, but possible, that there can be two members with the same address in the ClusterState. This can happen
// when a node quickly reboots and re-uses its old address, but the old incarnation hasn't been downed yet.
var addressToMember = ClusterState.Members
.GroupBy(m => m.Address)
// using Last or First here is non-deterministic since the UID that appears in the UniqueAddress sort order is random
.ToImmutableDictionary(g => g.Key, g => g.First());

return currentShardAllocations.Select(i =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ public override Task<IImmutableSet<ShardId>> Rebalance(IImmutableDictionary<IAct
var sortedRegionEntries = RegionEntriesFor(currentShardAllocations).OrderBy(i => i, ShardSuitabilityOrdering.Instance).ToImmutableList();
if (IsAGoodTimeToRebalance(sortedRegionEntries))
{
var (_, Shards) = MostSuitableRegion(sortedRegionEntries);
var (_, shards) = MostSuitableRegion(sortedRegionEntries);
// even if it is to another new node.
var mostShards = sortedRegionEntries.Select(r => r.ShardIds.Where(s => !rebalanceInProgress.Contains(s))).MaxBy(i => i.Count())?.ToArray() ?? Array.Empty<string>();

var difference = mostShards.Length - Shards.Count;
var difference = mostShards.Length - shards.Count;
if (difference >= _rebalanceThreshold)
{
var n = Math.Min(
Expand Down