Skip to content

Commit 4a7ec19

Browse files
Yang Comments
1 parent cd5e8f4 commit 4a7ec19

File tree

3 files changed

+90
-28
lines changed

3 files changed

+90
-28
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterBalanceStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private static NodeBalanceStats createFrom(
242242
}
243243
}
244244

245-
Double nodeWeight = desiredBalance == null || desiredBalance.weightsPerNode().isEmpty()
245+
Double nodeWeight = desiredBalance.weightsPerNode().isEmpty()
246246
? null
247247
: desiredBalance.weightsPerNode().get(routingNode.node()).nodeWeight();
248248

server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterBalanceStatsTests.java

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
package org.elasticsearch.cluster.routing.allocation.allocator;
1111

12-
import org.elasticsearch.TransportVersion;
13-
import org.elasticsearch.TransportVersions;
1412
import org.elasticsearch.cluster.ClusterInfo;
1513
import org.elasticsearch.cluster.ClusterName;
1614
import org.elasticsearch.cluster.ClusterState;
@@ -23,19 +21,11 @@
2321
import org.elasticsearch.cluster.routing.RoutingTable;
2422
import org.elasticsearch.cluster.routing.allocation.allocator.ClusterBalanceStats.MetricStats;
2523
import org.elasticsearch.cluster.routing.allocation.allocator.ClusterBalanceStats.NodeBalanceStats;
26-
import org.elasticsearch.common.bytes.BytesReference;
27-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
28-
import org.elasticsearch.common.io.stream.StreamInput;
29-
import org.elasticsearch.common.xcontent.XContentHelper;
3024
import org.elasticsearch.core.Nullable;
3125
import org.elasticsearch.core.Tuple;
3226
import org.elasticsearch.index.IndexVersion;
3327
import org.elasticsearch.index.shard.ShardId;
34-
import org.elasticsearch.xcontent.ToXContent;
35-
import org.elasticsearch.xcontent.XContentBuilder;
36-
import org.elasticsearch.xcontent.XContentFactory;
3728

38-
import java.io.IOException;
3929
import java.util.HashMap;
4030
import java.util.List;
4131
import java.util.Map;
@@ -262,7 +252,49 @@ public void testStatsForNoIndicesInTier() {
262252
var clusterState = createClusterState(List.of(NODE1, NODE2, NODE3), List.of());
263253
var clusterInfo = createClusterInfo(List.of());
264254

265-
var stats = ClusterBalanceStats.createFrom(clusterState, null, clusterInfo, TEST_WRITE_LOAD_FORECASTER);
255+
double nodeWeight = randomDoubleBetween(-1, 1, true);
256+
var stats = ClusterBalanceStats.createFrom(clusterState, createDesiredBalance(clusterState, nodeWeight), clusterInfo, TEST_WRITE_LOAD_FORECASTER);
257+
258+
assertThat(
259+
stats,
260+
equalTo(
261+
new ClusterBalanceStats(
262+
0,
263+
0,
264+
Map.of(
265+
DATA_CONTENT_NODE_ROLE.roleName(),
266+
new ClusterBalanceStats.TierBalanceStats(
267+
new MetricStats(0.0, 0.0, 0.0, 0.0, 0.0),
268+
new MetricStats(0.0, 0.0, 0.0, 0.0, 0.0),
269+
new MetricStats(0.0, 0.0, 0.0, 0.0, 0.0),
270+
new MetricStats(0.0, 0.0, 0.0, 0.0, 0.0),
271+
new MetricStats(0.0, 0.0, 0.0, 0.0, 0.0)
272+
)
273+
),
274+
Map.ofEntries(
275+
Map.entry(
276+
"node-1",
277+
new NodeBalanceStats("node-1", List.of(DATA_CONTENT_NODE_ROLE.roleName()), 0, 0, 0.0, 0L, 0L, nodeWeight)
278+
),
279+
Map.entry(
280+
"node-2",
281+
new NodeBalanceStats("node-2", List.of(DATA_CONTENT_NODE_ROLE.roleName()), 0, 0, 0.0, 0L, 0L, nodeWeight)
282+
),
283+
Map.entry(
284+
"node-3",
285+
new NodeBalanceStats("node-3", List.of(DATA_CONTENT_NODE_ROLE.roleName()), 0, 0, 0.0, 0L, 0L, nodeWeight)
286+
)
287+
)
288+
)
289+
)
290+
);
291+
}
292+
293+
public void testStatsForDesiredBalanceWithEmptyWeightsPerNodeMap() {
294+
var clusterState = createClusterState(List.of(NODE1, NODE2, NODE3), List.of());
295+
var clusterInfo = createClusterInfo(List.of());
296+
297+
var stats = ClusterBalanceStats.createFrom(clusterState, createDesiredBalanceWithEmptyNodeWeights(clusterState), clusterInfo, TEST_WRITE_LOAD_FORECASTER);
266298

267299
assertThat(
268300
stats,
@@ -330,7 +362,15 @@ private static ClusterState createClusterState(List<DiscoveryNode> nodes, List<T
330362
.build();
331363
}
332364

333-
private static DesiredBalance createDesiredBalance(ClusterState state, Double nodeWeight) {
365+
private static DesiredBalance createDesiredBalanceWithEmptyNodeWeights(ClusterState state) {
366+
return createDesiredBalance(state, randomDoubleBetween(-1, 1, true), true);
367+
}
368+
369+
private static DesiredBalance createDesiredBalance(ClusterState state, double nodeWeight) {
370+
return createDesiredBalance(state, nodeWeight, false);
371+
}
372+
373+
private static DesiredBalance createDesiredBalance(ClusterState state, double nodeWeight, boolean emptyNodeWeights) {
334374
var assignments = new HashMap<ShardId, ShardAssignment>();
335375
Map<String, Long> shardCounts = new HashMap<>();
336376
for (var indexRoutingTable : state.getRoutingTable()) {
@@ -342,6 +382,10 @@ private static DesiredBalance createDesiredBalance(ClusterState state, Double no
342382
}
343383
}
344384

385+
if (emptyNodeWeights) {
386+
return new DesiredBalance(1, assignments, new HashMap<>(), DesiredBalance.ComputationFinishReason.CONVERGED);
387+
}
388+
345389
final var nodeWeights = state.nodes()
346390
.stream()
347391
.collect(

server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/NodeBalanceStatsTests.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.common.io.stream.Writeable;
1616
import org.elasticsearch.common.xcontent.XContentHelper;
1717
import org.elasticsearch.test.AbstractWireSerializingTestCase;
18+
import org.elasticsearch.test.TransportVersionUtils;
1819
import org.elasticsearch.xcontent.ToXContent;
1920
import org.elasticsearch.xcontent.XContentBuilder;
2021
import org.elasticsearch.xcontent.XContentFactory;
@@ -58,7 +59,11 @@ protected ClusterBalanceStats.NodeBalanceStats mutateInstance(ClusterBalanceStat
5859
public void testSerializationWithTransportVersionV_8_7_0() throws IOException {
5960
ClusterBalanceStats.NodeBalanceStats instance = createTestInstance();
6061
// Serialization changes based on this version
61-
TransportVersion oldVersion = TransportVersions.V_8_7_0;
62+
final var oldVersion = TransportVersionUtils.randomVersionBetween(
63+
random(),
64+
TransportVersions.V_8_0_0,
65+
TransportVersionUtils.getPreviousVersion(TransportVersions.V_8_8_0)
66+
);
6267
ClusterBalanceStats.NodeBalanceStats deserialized = copyInstance(instance, oldVersion);
6368

6469
// Assert the default values are as expected
@@ -71,12 +76,16 @@ public void testSerializationWithTransportVersionV_8_7_0() throws IOException {
7176
public void testSerializationWithTransportVersionV_8_8_0() throws IOException {
7277
ClusterBalanceStats.NodeBalanceStats instance = createTestInstance();
7378
// Serialization changes based on this version
74-
TransportVersion oldVersion = TransportVersions.V_8_8_0;
79+
final var oldVersion = TransportVersionUtils.randomVersionBetween(
80+
random(),
81+
TransportVersions.V_8_8_0,
82+
TransportVersionUtils.getPreviousVersion(TransportVersions.V_8_12_0)
83+
);
7584
ClusterBalanceStats.NodeBalanceStats deserialized = copyInstance(instance, oldVersion);
7685

77-
// Assert the default values have not been used
78-
assertNotEquals(UNKNOWN, deserialized.nodeId());
79-
assertNotEquals(List.of(), deserialized.roles());
86+
// Assert the values are as expected
87+
assertEquals(instance.nodeId(), deserialized.nodeId());
88+
assertEquals(instance.roles(), deserialized.roles());
8089

8190
// Assert the default values are as expected
8291
assertEquals(UNDESIRED_SHARD_ALLOCATION_DEFAULT_VALUE, deserialized.undesiredShardAllocations());
@@ -86,13 +95,17 @@ public void testSerializationWithTransportVersionV_8_8_0() throws IOException {
8695
public void testSerializationWithTransportVersionV_8_12_0() throws IOException {
8796
ClusterBalanceStats.NodeBalanceStats instance = createTestInstance();
8897
// Serialization changes based on this version
89-
TransportVersion oldVersion = TransportVersions.V_8_12_0;
98+
final var oldVersion = TransportVersionUtils.randomVersionBetween(
99+
random(),
100+
TransportVersions.V_8_12_0,
101+
TransportVersionUtils.getPreviousVersion(TransportVersions.NODE_WEIGHTS_ADDED_TO_NODE_BALANCE_STATS)
102+
);
90103
ClusterBalanceStats.NodeBalanceStats deserialized = copyInstance(instance, oldVersion);
91104

92-
// Assert the default values have not been used
93-
assertNotEquals(UNKNOWN, deserialized.nodeId());
94-
assertNotEquals(List.of(), deserialized.roles());
95-
assertNotEquals(UNDESIRED_SHARD_ALLOCATION_DEFAULT_VALUE, deserialized.undesiredShardAllocations());
105+
// Assert the values are as expected
106+
assertEquals(instance.nodeId(), deserialized.nodeId());
107+
assertEquals(instance.roles(), deserialized.roles());
108+
assertEquals(instance.undesiredShardAllocations(), deserialized.undesiredShardAllocations());
96109

97110
// Assert the default values are as expected
98111
assertNull(deserialized.nodeWeight());
@@ -101,13 +114,18 @@ public void testSerializationWithTransportVersionV_8_12_0() throws IOException {
101114
public void testSerializationWithTransportVersionNodeWeightsAddedToNodeBalanceStats() throws IOException {
102115
ClusterBalanceStats.NodeBalanceStats instance = createTestInstance();
103116
// Serialization changes based on this version
104-
TransportVersion oldVersion = TransportVersions.NODE_WEIGHTS_ADDED_TO_NODE_BALANCE_STATS;
117+
final var oldVersion = TransportVersionUtils.randomVersionBetween(
118+
random(),
119+
TransportVersions.NODE_WEIGHTS_ADDED_TO_NODE_BALANCE_STATS,
120+
TransportVersion.current()
121+
);
105122
ClusterBalanceStats.NodeBalanceStats deserialized = copyInstance(instance, oldVersion);
106123

107-
// Assert the default values have not been used
108-
assertNotEquals(UNKNOWN, deserialized.nodeId());
109-
assertNotEquals(List.of(), deserialized.roles());
110-
assertNotEquals(UNDESIRED_SHARD_ALLOCATION_DEFAULT_VALUE, deserialized.undesiredShardAllocations());
124+
// Assert the values are as expected
125+
assertEquals(instance.nodeId(), deserialized.nodeId());
126+
assertEquals(instance.roles(), deserialized.roles());
127+
assertEquals(instance.undesiredShardAllocations(), deserialized.undesiredShardAllocations());
128+
assertEquals(instance.nodeWeight(), deserialized.nodeWeight());
111129
}
112130

113131
public void testToXContentWithoutHumanReadableNames() throws IOException {

0 commit comments

Comments
 (0)