Skip to content

Conversation

@nicktindall
Copy link
Contributor

@nicktindall nicktindall commented Jun 25, 2025

When the number of shards changes in a data-stream, whether due to auto-sharding or manual intervention, it impacts the write-load forecast used for the new write index. This is because the forecast is calculated as a weighted average per-shard write load. So if the number of shards increased in the new write index, the indexes total write load will increase correspondingly, likewise if the number of shards decreased. This PR shifts to calculating a per-index write load, so it can be correctly adjusted when the shard count changes.

Explanation

Write load is calculated per shard, it is equal to
WRITE_LOAD(shard) = WRITE_TIME(shard) / UPTIME(shard)

To calculate the forecasted write load of the shards of a new index on DS rollover, we calculate the weighted average write load of all the shards of all the "recent" indices, e.g.
FORECAST_WRITE_LOAD(shard) = sum(WRITE_LOAD(shard)) / sum(UPTIME(shard)) | all shard in RECENT_INDEX

We use the forecasted write load quite heavily in balancing.

To calculate the write load for a node, we add the write loads of all the shards in all the allocated indices
FORECAST_WRITE_LOAD(node) = sum(FORECAST_WRITE_LOAD(shard)) | all shards on node
(see org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator.ModelNode#(addShard|removeShard))

And to calculate the write load for an index, we add the write loads of all the shards in the index
FORECAST_WRITE_LOAD(index) = sum(FORECAST_WRITE_LOAD(shard)) | all shards in index (taking into account replicas)
(see org.elasticsearch.cluster.routing.allocation.allocator.WeightFunction#getIndexWriteLoad)

This means when auto-sharding decides to increase the number of shards, the total write load in the cluster increases, because
e.g. an index that went from 3 shards to 6 shards goes from 3 * FORECAST_WRITE_LOAD(shard) to 6 * FORECAST_WRITE_LOAD(shard)
Conversely when auto-sharding decides to decrease the number of shards, the total write load in the cluster will decrease

Why is this a problem?

If we auto-shard down from 63 to 38 shards in a rollover, like we did the scale testing environment, the balancer will use the forecasted write-load that was calculated for 63 shards, but with only 38 shards. That load from 63 shards will be compressed into the 38 shards that replace them. So the balancer will under-estimate the write load for each shard by (1 - (38/36)) = ~40%. This will make it think it's in a desired state, but will leave nodes that have a greater proportion of the scaled-down (hence under-estimated) index's shards overloaded.

@nicktindall nicktindall added >bug :Distributed Coordination/Allocation All issues relating to the decision making around placing a shard (both master logic & on the nodes) labels Jun 25, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Distributed Coordination Meta label for Distributed Coordination team label Jun 25, 2025
@elasticsearchmachine
Copy link
Collaborator

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

* If the auto sharding service thinks the number of shards must be changed but it can't recommend a change due to the cooldown
* period not lapsing, the result will be of type {@link AutoShardingType#COOLDOWN_PREVENTED_INCREASE} or
* {@link AutoShardingType#COOLDOWN_PREVENTED_INCREASE} with the remaining cooldown configured and the number of shards that should
* {@link AutoShardingType#COOLDOWN_PREVENTED_DECREASE} with the remaining cooldown configured and the number of shards that should
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed a typo too

@elasticsearchmachine
Copy link
Collaborator

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

Copy link
Contributor

@mhl-b mhl-b left a comment

Choose a reason for hiding this comment

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

I found testWriteLoadForecast covers too many cases, hard to follow which test validates your change. Can you add tests for these two cases when number of shards goes up and down and how it impacts forecast, please?

closeTo(
originalWriteIndexMetadata.getNumberOfShards() * writeLoadForecaster.getForecastedWriteLoad(originalWriteIndexMetadata)
.getAsDouble(),
0.01
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are sometimes small rounding errors

@nicktindall nicktindall changed the title Make forecast write load accurate when shard numbers change Improve accuracy of write load forecast when shard numbers change Jun 26, 2025
Copy link
Contributor

@mhl-b mhl-b left a comment

Choose a reason for hiding this comment

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

LGTM

@nicktindall nicktindall merged commit 77b459c into elastic:main Jun 27, 2025
32 checks passed
@nicktindall nicktindall deleted the fix_type_ds_autosharding branch June 27, 2025 03:04
mridula-s109 pushed a commit to mridula-s109/elasticsearch that referenced this pull request Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>bug :Distributed Coordination/Allocation All issues relating to the decision making around placing a shard (both master logic & on the nodes) Team:Distributed Coordination Meta label for Distributed Coordination team v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants