99
1010package org .elasticsearch .cluster .routing .allocation .allocator ;
1111
12- import org .elasticsearch .TransportVersion ;
13- import org .elasticsearch .TransportVersions ;
1412import org .elasticsearch .cluster .ClusterInfo ;
1513import org .elasticsearch .cluster .ClusterName ;
1614import org .elasticsearch .cluster .ClusterState ;
2321import org .elasticsearch .cluster .routing .RoutingTable ;
2422import org .elasticsearch .cluster .routing .allocation .allocator .ClusterBalanceStats .MetricStats ;
2523import 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 ;
3024import org .elasticsearch .core .Nullable ;
3125import org .elasticsearch .core .Tuple ;
3226import org .elasticsearch .index .IndexVersion ;
3327import 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 ;
3929import java .util .HashMap ;
4030import java .util .List ;
4131import 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 (
0 commit comments