1313import org .elasticsearch .cluster .ClusterInfo ;
1414import org .elasticsearch .cluster .ClusterState ;
1515import org .elasticsearch .cluster .NodeUsageStatsForThreadPools ;
16+ import org .elasticsearch .cluster .metadata .ProjectId ;
1617import org .elasticsearch .cluster .routing .allocation .RoutingAllocation ;
1718import org .elasticsearch .cluster .routing .allocation .decider .AllocationDeciders ;
19+ import org .elasticsearch .index .shard .ShardId ;
1820import org .elasticsearch .snapshots .SnapshotShardSizeInfo ;
1921import org .elasticsearch .test .ESTestCase ;
2022import org .hamcrest .Matchers ;
2123
24+ import java .util .Arrays ;
2225import java .util .HashMap ;
26+ import java .util .HashSet ;
2327import java .util .List ;
2428import java .util .Map ;
29+ import java .util .Set ;
30+ import java .util .stream .Collectors ;
31+ import java .util .stream .IntStream ;
2532import java .util .stream .StreamSupport ;
2633
2734import static org .hamcrest .Matchers .equalTo ;
@@ -33,14 +40,15 @@ public class WriteLoadPerShardSimulatorTests extends ESTestCase {
3340
3441 private static final RoutingChangesObserver NOOP = new RoutingChangesObserver () {
3542 };
43+ public static final String [] INDICES = { "indexOne" , "indexTwo" , "indexThree" };
3644
3745 /**
3846 * We should not adjust the values if there's no movement
3947 */
4048 public void testNoShardMovement () {
4149 final var originalNode0WriteLoadStats = randomUsageStats ();
4250 final var originalNode1WriteLoadStats = randomUsageStats ();
43- final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats );
51+ final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats , Set . of () );
4452
4553 final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator (allocation );
4654 final var calculatedNodeUsageStates = writeLoadPerShardSimulator .nodeUsageStatsForThreadPools ();
@@ -58,7 +66,7 @@ public void testNoShardMovement() {
5866 public void testMovementOfAShardWillReduceThreadPoolUtilisation () {
5967 final var originalNode0WriteLoadStats = randomUsageStats ();
6068 final var originalNode1WriteLoadStats = randomUsageStats ();
61- final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats );
69+ final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats , Set . of () );
6270 final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator (allocation );
6371
6472 // Relocate a random shard from node_0 to node_1
@@ -83,7 +91,7 @@ public void testMovementOfAShardWillReduceThreadPoolUtilisation() {
8391 public void testMovementFollowedByMovementBackWillNotChangeAnything () {
8492 final var originalNode0WriteLoadStats = randomUsageStats ();
8593 final var originalNode1WriteLoadStats = randomUsageStats ();
86- final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats );
94+ final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats , Set . of () );
8795 final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator (allocation );
8896
8997 // Relocate a random shard from node_0 to node_1
@@ -122,7 +130,11 @@ public void testMovementFollowedByMovementBackWillNotChangeAnything() {
122130 public void testMovementBetweenNodesWithNoThreadPoolStats () {
123131 final var originalNode0WriteLoadStats = randomBoolean () ? randomUsageStats () : null ;
124132 final var originalNode1WriteLoadStats = randomBoolean () ? randomUsageStats () : null ;
125- final var allocation = createRoutingAllocation (originalNode0WriteLoadStats , originalNode1WriteLoadStats );
133+ final var allocation = createRoutingAllocation (
134+ originalNode0WriteLoadStats ,
135+ originalNode1WriteLoadStats ,
136+ new HashSet <>(randomSubsetOf (Arrays .asList (INDICES )))
137+ );
126138 final var writeLoadPerShardSimulator = new WriteLoadPerShardSimulator (allocation );
127139
128140 // Relocate a random shard from node_0 to node_1
@@ -153,7 +165,8 @@ private NodeUsageStatsForThreadPools.ThreadPoolUsageStats randomUsageStats() {
153165
154166 private RoutingAllocation createRoutingAllocation (
155167 NodeUsageStatsForThreadPools .ThreadPoolUsageStats node0WriteLoadStats ,
156- NodeUsageStatsForThreadPools .ThreadPoolUsageStats node1WriteLoadStats
168+ NodeUsageStatsForThreadPools .ThreadPoolUsageStats node1WriteLoadStats ,
169+ Set <String > indicesWithNoWriteLoad
157170 ) {
158171 final Map <String , NodeUsageStatsForThreadPools > nodeUsageStats = new HashMap <>();
159172 if (node0WriteLoadStats != null ) {
@@ -163,16 +176,29 @@ private RoutingAllocation createRoutingAllocation(
163176 nodeUsageStats .put ("node_1" , new NodeUsageStatsForThreadPools ("node_1" , Map .of ("write" , node1WriteLoadStats )));
164177 }
165178
179+ final ClusterState clusterState = createClusterState ();
180+ final ClusterInfo clusterInfo = ClusterInfo .builder ()
181+ .nodeUsageStatsForThreadPools (nodeUsageStats )
182+ .shardWriteLoads (
183+ clusterState .metadata ()
184+ .getProject (ProjectId .DEFAULT )
185+ .stream ()
186+ .filter (index -> indicesWithNoWriteLoad .contains (index .getIndex ().getName ()) == false )
187+ .flatMap (index -> IntStream .range (0 , 3 ).mapToObj (shardNum -> new ShardId (index .getIndex (), shardNum )))
188+ .collect (Collectors .toUnmodifiableMap (shardId -> shardId , shardId -> randomDoubleBetween (0.1 , 5.0 , true )))
189+ )
190+ .build ();
191+
166192 return new RoutingAllocation (
167193 new AllocationDeciders (List .of ()),
168- createClusterState () ,
169- ClusterInfo . builder (). nodeUsageStatsForThreadPools ( nodeUsageStats ). build () ,
194+ clusterState ,
195+ clusterInfo ,
170196 SnapshotShardSizeInfo .EMPTY ,
171197 System .nanoTime ()
172198 ).mutableCloneForSimulation ();
173199 }
174200
175201 private ClusterState createClusterState () {
176- return ClusterStateCreationUtils .stateWithAssignedPrimariesAndReplicas (new String [] { "indexOne" , "indexTwo" , "indexThree" } , 3 , 0 );
202+ return ClusterStateCreationUtils .stateWithAssignedPrimariesAndReplicas (INDICES , 3 , 0 );
177203 }
178204}
0 commit comments