3838import org .elasticsearch .env .NodeEnvironment ;
3939import org .elasticsearch .index .IndexService ;
4040import org .elasticsearch .index .IndexSettings ;
41+ import org .elasticsearch .index .engine .Engine ;
4142import org .elasticsearch .index .seqno .ReplicationTracker ;
4243import org .elasticsearch .index .seqno .RetentionLease ;
4344import org .elasticsearch .index .shard .IndexEventListener ;
@@ -179,16 +180,17 @@ public void testSimpleRelocationWithIndexingPaused() throws Exception {
179180 logger .info ("--> creating test index ..." );
180181 prepareCreate ("test" , indexSettings (1 , 0 )).get ();
181182
182- logger .info ("--> index 10 docs" );
183- for (int i = 0 ; i < 10 ; i ++) {
183+ logger .info ("--> index docs" );
184+ int numDocs = between (1 ,10 );
185+ for (int i = 0 ; i < numDocs ; i ++) {
184186 prepareIndex ("test" ).setId (Integer .toString (i )).setSource ("field" , "value" + i ).get ();
185187 }
186188 logger .info ("--> flush so we have an actual index" );
187189 indicesAdmin ().prepareFlush ().get ();
188190
189191 logger .info ("--> verifying count" );
190192 indicesAdmin ().prepareRefresh ().get ();
191- assertHitCount (prepareSearch ("test" ).setSize (0 ), 10L );
193+ assertHitCount (prepareSearch ("test" ).setSize (0 ), numDocs );
192194
193195 logger .info ("--> start another node" );
194196 final String node_2 = internalCluster ().startNode ();
@@ -203,20 +205,21 @@ public void testSimpleRelocationWithIndexingPaused() throws Exception {
203205 IndexShard shard = indicesService .indexServiceSafe (resolveIndex ("test" )).getShard (0 );
204206 shard .activateThrottling ();
205207 // Verify that indexing is paused for the throttled shard
206- assertThat (shard .isIndexingPaused (), equalTo (true ));
208+ Engine engine = shard .getEngineOrNull ();
209+ assertThat (engine != null && engine .isThrottled (), equalTo (true ));
207210 // Try to index a document into the "test" index which is currently throttled
208211 logger .info ("--> Try to index a doc while indexing is paused" );
209212 IndexRequestBuilder indexRequestBuilder = prepareIndex ("test" ).setId (Integer .toString (20 )).setSource ("field" , "value" + 20 );
210213 var future = indexRequestBuilder .execute ();
211- expectThrows (ElasticsearchException .class , () -> future .actionGet (10 , TimeUnit .SECONDS ));
214+ expectThrows (ElasticsearchException .class , () -> future .actionGet (500 , TimeUnit .MILLISECONDS ));
212215 // Verify that the new document has not been indexed indicating that the indexing thread is paused.
213216 logger .info ("--> verifying count is unchanged..." );
214217 indicesAdmin ().prepareRefresh ().get ();
215- assertHitCount (prepareSearch ("test" ).setSize (0 ), 10 );
218+ assertHitCount (prepareSearch ("test" ).setSize (0 ), numDocs );
216219
217220 logger .info ("--> relocate the shard from node1 to node2" );
218- ClusterRerouteUtils . reroute ( client (), new MoveAllocationCommand ( "test " , 0 , node_1 , node_2 ));
219- // updateIndexSettings(Settings.builder().put("index.routing.allocation.include._id", node_2) , "test");
221+ updateIndexSettings ( Settings . builder (). put ( "index.routing.allocation.include._name " , node_2 ), "test" );
222+ ensureGreen ( ACCEPTABLE_RELOCATION_TIME , "test" );
220223
221224 // Relocation will suspend throttling for the paused shard, allow the indexing thread to proceed, thereby releasing
222225 // the indexing permit it holds, in turn allowing relocation to acquire the permits and proceed.
@@ -230,7 +233,7 @@ public void testSimpleRelocationWithIndexingPaused() throws Exception {
230233 logger .info ("--> verifying count after relocation ..." );
231234 future .actionGet ();
232235 indicesAdmin ().prepareRefresh ().get ();
233- assertHitCount (prepareSearch ("test" ).setSize (0 ), 11 );
236+ assertHitCount (prepareSearch ("test" ).setSize (0 ), numDocs + 1 );
234237 }
235238
236239 public void testRelocationWhileIndexingRandom () throws Exception {
@@ -283,7 +286,8 @@ public void testRelocationWhileIndexingRandom() throws Exception {
283286 // Activate index throttling on "test" index primary shard
284287 shard .activateThrottling ();
285288 // Verify that indexing is throttled for this shard
286- assertThat (shard .getEngineOrNull ().isThrottled (), equalTo (true ));
289+ Engine engine = shard .getEngineOrNull ();
290+ assertThat (engine != null && engine .isThrottled (), equalTo (true ));
287291 }
288292 logger .info ("--> starting relocations..." );
289293 int nodeShiftBased = numberOfReplicas ; // if we have replicas shift those
@@ -296,10 +300,9 @@ public void testRelocationWhileIndexingRandom() throws Exception {
296300 logger .debug ("--> Allow indexer to index [{}] documents" , numDocs );
297301 indexer .continueIndexing (numDocs );
298302 logger .info ("--> START relocate the shard from {} to {}" , nodes [fromNode ], nodes [toNode ]);
299- ClusterRerouteUtils .reroute (client (), new MoveAllocationCommand ("test" , 0 , nodes [fromNode ], nodes [toNode ]));
300303
301- // updateIndexSettings(Settings.builder().put("index.routing.allocation.include._id ", nodes[toNode]), "test");
302- // ensureGreen(ACCEPTABLE_RELOCATION_TIME, "test");
304+ updateIndexSettings (Settings .builder ().put ("index.routing.allocation.include._name " , nodes [toNode ]), "test" );
305+ ensureGreen (ACCEPTABLE_RELOCATION_TIME , "test" );
303306
304307 if (rarely ()) {
305308 logger .debug ("--> flushing" );
@@ -321,7 +324,8 @@ public void testRelocationWhileIndexingRandom() throws Exception {
321324 shard = indicesService .indexServiceSafe (resolveIndex ("test" )).getShard (0 );
322325 shard .activateThrottling ();
323326 // Verify that indexing is throttled for this shard
324- assertThat (shard .getEngineOrNull ().isThrottled (), equalTo (true ));
327+ Engine engine = shard .getEngineOrNull ();
328+ assertThat (engine != null && engine .isThrottled (), equalTo (true ));
325329 }
326330 }
327331 logger .info ("--> done relocations" );
0 commit comments