-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Adds Cluster Balance Stats Tests #132936
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
Merged
joshua-adams-1
merged 8 commits into
elastic:main
from
joshua-adams-1:cluster-balance-stats-tests
Aug 22, 2025
Merged
Adds Cluster Balance Stats Tests #132936
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8567b14
Add ClusterBalanceStats Serialization Tests
joshua-adams-1 76ee3e8
Merge branch 'main' into cluster-balance-stats-tests
joshua-adams-1 6d7242c
Merge branch 'main' into cluster-balance-stats-tests
joshua-adams-1 6ad7be0
Remove unnecessary comments
joshua-adams-1 18f56e0
Merge branch 'main' into cluster-balance-stats-tests
joshua-adams-1 7e17962
Merge branch 'cluster-balance-stats-tests' of github.com:joshua-adams…
joshua-adams-1 3d5bde7
Addresses nit comments
joshua-adams-1 ed88def
Merge branch 'main' into cluster-balance-stats-tests
joshua-adams-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...rc/test/java/org/elasticsearch/cluster/routing/allocation/allocator/MetricStatsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.cluster.routing.allocation.allocator; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
| import org.elasticsearch.xcontent.ToXContent; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class MetricStatsTests extends AbstractWireSerializingTestCase<ClusterBalanceStats.MetricStats> { | ||
|
|
||
| @Override | ||
| protected Writeable.Reader<ClusterBalanceStats.MetricStats> instanceReader() { | ||
| return ClusterBalanceStats.MetricStats::readFrom; | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBalanceStats.MetricStats createTestInstance() { | ||
| return createRandomMetricStats(); | ||
| } | ||
|
|
||
| public static ClusterBalanceStats.MetricStats createRandomMetricStats() { | ||
| return new ClusterBalanceStats.MetricStats(randomDouble(), randomDouble(), randomDouble(), randomDouble(), randomDouble()); | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBalanceStats.MetricStats mutateInstance(ClusterBalanceStats.MetricStats instance) throws IOException { | ||
| return createTestInstance(); | ||
| } | ||
|
|
||
| public void testToXContent() throws IOException { | ||
| double total = randomDouble(); | ||
| double min = randomDouble(); | ||
| double max = randomDouble(); | ||
| double average = randomDouble(); | ||
| double stdDev = randomDouble(); | ||
| ClusterBalanceStats.MetricStats stats = new ClusterBalanceStats.MetricStats(total, min, max, average, stdDev); | ||
|
|
||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| builder = stats.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
| // Convert to map for easy assertions | ||
| Map<String, Object> map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); | ||
|
|
||
| assertThat(map.get("total"), equalTo(total)); | ||
| assertThat(map.get("min"), equalTo(min)); | ||
| assertThat(map.get("max"), equalTo(max)); | ||
| assertThat(map.get("average"), equalTo(average)); | ||
| assertThat(map.get("std_dev"), equalTo(stdDev)); | ||
| } | ||
| } | ||
124 changes: 124 additions & 0 deletions
124
...st/java/org/elasticsearch/cluster/routing/allocation/allocator/TierBalanceStatsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.cluster.routing.allocation.allocator; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
| import org.elasticsearch.xcontent.ToXContent; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.cluster.routing.allocation.allocator.MetricStatsTests.createRandomMetricStats; | ||
| import static org.hamcrest.Matchers.containsInAnyOrder; | ||
|
|
||
| public class TierBalanceStatsTests extends AbstractWireSerializingTestCase<ClusterBalanceStats.TierBalanceStats> { | ||
|
|
||
| @Override | ||
| protected Writeable.Reader<ClusterBalanceStats.TierBalanceStats> instanceReader() { | ||
| return ClusterBalanceStats.TierBalanceStats::readFrom; | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBalanceStats.TierBalanceStats createTestInstance() { | ||
| return createRandomTierBalanceStats(); | ||
| } | ||
|
|
||
| private ClusterBalanceStats.TierBalanceStats createRandomTierBalanceStats() { | ||
| return new ClusterBalanceStats.TierBalanceStats( | ||
| createRandomMetricStats(), | ||
| createRandomMetricStats(), | ||
| createRandomMetricStats(), | ||
| createRandomMetricStats(), | ||
| createRandomMetricStats() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBalanceStats.TierBalanceStats mutateInstance(ClusterBalanceStats.TierBalanceStats instance) throws IOException { | ||
| return createTestInstance(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public void testToXContent() throws IOException { | ||
| ClusterBalanceStats.MetricStats shardCount = createRandomMetricStats(); | ||
| ClusterBalanceStats.MetricStats undesiredShardAllocations = createRandomMetricStats(); | ||
| ClusterBalanceStats.MetricStats forecastWriteLoad = createRandomMetricStats(); | ||
| ClusterBalanceStats.MetricStats forecastDiskUsage = createRandomMetricStats(); | ||
| ClusterBalanceStats.MetricStats actualDiskUsage = createRandomMetricStats(); | ||
| ClusterBalanceStats.TierBalanceStats tierBalanceStats = new ClusterBalanceStats.TierBalanceStats( | ||
| shardCount, | ||
| undesiredShardAllocations, | ||
| forecastWriteLoad, | ||
| forecastDiskUsage, | ||
| actualDiskUsage | ||
| ); | ||
|
||
|
|
||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| builder = tierBalanceStats.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
| // Convert to map for easy assertions | ||
| Map<String, Object> map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); | ||
|
|
||
| assertThat( | ||
| map.keySet(), | ||
| containsInAnyOrder( | ||
| "shard_count", | ||
| "undesired_shard_allocation_count", | ||
| "forecast_write_load", | ||
| "forecast_disk_usage", | ||
| "actual_disk_usage" | ||
| ) | ||
| ); | ||
|
|
||
| Map<String, Object> shardCountStats = (Map<String, Object>) map.get("shard_count"); | ||
| assertThat(shardCountStats.keySet(), containsInAnyOrder("total", "average", "min", "max", "std_dev")); | ||
| assertEquals(shardCountStats.get("total"), shardCount.total()); | ||
| assertEquals(shardCountStats.get("average"), shardCount.average()); | ||
| assertEquals(shardCountStats.get("min"), shardCount.min()); | ||
| assertEquals(shardCountStats.get("max"), shardCount.max()); | ||
| assertEquals(shardCountStats.get("std_dev"), shardCount.stdDev()); | ||
|
|
||
| Map<String, Object> undesiredShardAllocationCountStats = (Map<String, Object>) map.get("undesired_shard_allocation_count"); | ||
| assertThat(undesiredShardAllocationCountStats.keySet(), containsInAnyOrder("total", "average", "min", "max", "std_dev")); | ||
| assertEquals(undesiredShardAllocationCountStats.get("total"), undesiredShardAllocations.total()); | ||
| assertEquals(undesiredShardAllocationCountStats.get("average"), undesiredShardAllocations.average()); | ||
| assertEquals(undesiredShardAllocationCountStats.get("min"), undesiredShardAllocations.min()); | ||
| assertEquals(undesiredShardAllocationCountStats.get("max"), undesiredShardAllocations.max()); | ||
| assertEquals(undesiredShardAllocationCountStats.get("std_dev"), undesiredShardAllocations.stdDev()); | ||
|
|
||
| Map<String, Object> forecastWriteLoadStats = (Map<String, Object>) map.get("forecast_write_load"); | ||
| assertThat(forecastWriteLoadStats.keySet(), containsInAnyOrder("total", "average", "min", "max", "std_dev")); | ||
| assertEquals(forecastWriteLoadStats.get("total"), forecastWriteLoad.total()); | ||
| assertEquals(forecastWriteLoadStats.get("average"), forecastWriteLoad.average()); | ||
| assertEquals(forecastWriteLoadStats.get("min"), forecastWriteLoad.min()); | ||
| assertEquals(forecastWriteLoadStats.get("max"), forecastWriteLoad.max()); | ||
| assertEquals(forecastWriteLoadStats.get("std_dev"), forecastWriteLoad.stdDev()); | ||
|
|
||
| Map<String, Object> forecastDiskUsageStats = (Map<String, Object>) map.get("forecast_disk_usage"); | ||
| assertThat(forecastDiskUsageStats.keySet(), containsInAnyOrder("total", "average", "min", "max", "std_dev")); | ||
| assertEquals(forecastDiskUsageStats.get("total"), forecastDiskUsage.total()); | ||
| assertEquals(forecastDiskUsageStats.get("average"), forecastDiskUsage.average()); | ||
| assertEquals(forecastDiskUsageStats.get("min"), forecastDiskUsage.min()); | ||
| assertEquals(forecastDiskUsageStats.get("max"), forecastDiskUsage.max()); | ||
| assertEquals(forecastDiskUsageStats.get("std_dev"), forecastDiskUsage.stdDev()); | ||
|
|
||
| Map<String, Object> actualDiskUsageStats = (Map<String, Object>) map.get("actual_disk_usage"); | ||
| assertThat(actualDiskUsageStats.keySet(), containsInAnyOrder("total", "average", "min", "max", "std_dev")); | ||
| assertEquals(actualDiskUsageStats.get("total"), actualDiskUsage.total()); | ||
| assertEquals(actualDiskUsageStats.get("average"), actualDiskUsage.average()); | ||
| assertEquals(actualDiskUsageStats.get("min"), actualDiskUsage.min()); | ||
| assertEquals(actualDiskUsageStats.get("max"), actualDiskUsage.max()); | ||
| assertEquals(actualDiskUsageStats.get("std_dev"), actualDiskUsage.stdDev()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think these can be replaced by calling
createRandomMetricStats()and the later assertions can compare withstats.total()etc?