Skip to content

More efficient sort in tryRelocateShard #128063

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

Conversation

DaveCTurner
Copy link
Contributor

No need to do this via an allocation-heavy Stream, we can just put the
objects straight into an array, sort them in-place, and keep hold of the
array to avoid having to allocate anything on the next iteration.

Also slims down BY_DESCENDING_SHARD_ID: it's always sorting the same
index so we don't need to look at ShardId#index in the comparison, nor
do we really need multiple layers of vtable lookups, we can just compare
the shard IDs directly.

Relates #128021

No need to do this via an allocation-heavy `Stream`, we can just put the
objects straight into an array, sort them in-place, and keep hold of the
array to avoid having to allocate anything on the next iteration.

Also slims down `BY_DESCENDING_SHARD_ID`: it's always sorting the same
index so we don't need to look at `ShardId#index` in the comparison, nor
do we really need multiple layers of vtable lookups, we can just compare
the shard IDs directly.

Relates elastic#128021
@DaveCTurner DaveCTurner added >enhancement :Distributed Coordination/Allocation All issues relating to the decision making around placing a shard (both master logic & on the nodes) v8.19.0 v9.1.0 labels May 14, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-distributed-coordination (Team:Distributed Coordination)

@elasticsearchmachine elasticsearchmachine added the Team:Distributed Coordination Meta label for Distributed Coordination team label May 14, 2025
@elasticsearchmachine
Copy link
Collaborator

Hi @DaveCTurner, I've created a changelog YAML for you.

@DaveCTurner DaveCTurner added the auto-backport Automatically create backport pull requests when merged label May 14, 2025
Copy link
Contributor

@DiannaHohensee DiannaHohensee left a comment

Choose a reason for hiding this comment

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

lgtm 👍

To confirm, this change essentially

  • removes the use of Stream, which isn't performant
  • shortens the comparison method
  • reuses allocated memory

Though this last one sacrifices memory in favor of performance -- if a user has one index with a stupid number of shards, we're keep that array memory allocation for the duration of a balancing round? Though even a large number of shards will take insignificant memory.

for (final var shardRouting : index) {
if (shardRouting.started()) { // cannot rebalance unassigned, initializing or relocating shards anyway
shardRoutingsOnMaxWeightNode[startedShards] = shardRouting;
startedShards += 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

opt: ++startedShards or shardRoutingsOnMaxWeightNode[startedShards++] = shardRouting;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO unary increments are harder on the reader than spelling out a += 1 so I'd rather leave it like this.

@DaveCTurner DaveCTurner added the auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label May 19, 2025
@DaveCTurner
Copy link
Contributor Author

DaveCTurner commented May 19, 2025

if a user has one index with a stupid number of shards,

Yeah the maximum is 1024, if they all end up on one node for some reason then we allocate an array of length 2048 which is still tiny and probably not worth GC'ing.

We probably allocate more than 2048*8=16kiB today just by using streams.

@elasticsearchmachine elasticsearchmachine merged commit a84dff8 into elastic:main May 19, 2025
17 checks passed
@DaveCTurner DaveCTurner deleted the 2025/05/14/BalancedShardsAllocator-slimmer-sort branch May 19, 2025 19:45
DaveCTurner added a commit to DaveCTurner/elasticsearch that referenced this pull request May 19, 2025
No need to do this via an allocation-heavy `Stream`, we can just put the
objects straight into an array, sort them in-place, and keep hold of the
array to avoid having to allocate anything on the next iteration.

Also slims down `BY_DESCENDING_SHARD_ID`: it's always sorting the same
index so we don't need to look at `ShardId#index` in the comparison, nor
do we really need multiple layers of vtable lookups, we can just compare
the shard IDs directly.

Relates elastic#128021
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.19

elasticsearchmachine pushed a commit that referenced this pull request May 19, 2025
No need to do this via an allocation-heavy `Stream`, we can just put the
objects straight into an array, sort them in-place, and keep hold of the
array to avoid having to allocate anything on the next iteration.

Also slims down `BY_DESCENDING_SHARD_ID`: it's always sorting the same
index so we don't need to look at `ShardId#index` in the comparison, nor
do we really need multiple layers of vtable lookups, we can just compare
the shard IDs directly.

Relates #128021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) :Distributed Coordination/Allocation All issues relating to the decision making around placing a shard (both master logic & on the nodes) >enhancement Team:Distributed Coordination Meta label for Distributed Coordination team v8.19.0 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants