@@ -256,6 +256,40 @@ public void testInitializingPrimaryRemovesInitializingReplicaWithSameAID() {
256
256
257
257
}
258
258
259
+ public void testRecoveryFailures () {
260
+ disableRandomFailures ();
261
+ String index = "index_" + randomAlphaOfLength (8 ).toLowerCase (Locale .ROOT );
262
+ ClusterState state = ClusterStateCreationUtils .state (index , randomBoolean (),
263
+ ShardRoutingState .STARTED , ShardRoutingState .INITIALIZING );
264
+
265
+ // the initial state which is derived from the newly created cluster state but doesn't contain the index
266
+ ClusterState previousState = ClusterState .builder (state )
267
+ .metaData (MetaData .builder (state .metaData ()).remove (index ))
268
+ .routingTable (RoutingTable .builder ().build ())
269
+ .build ();
270
+
271
+ // pick a data node to simulate the adding an index cluster state change event on, that has shards assigned to it
272
+ final ShardRouting shardRouting = state .routingTable ().index (index ).shard (0 ).replicaShards ().get (0 );
273
+ final ShardId shardId = shardRouting .shardId ();
274
+ DiscoveryNode node = state .nodes ().get (shardRouting .currentNodeId ());
275
+
276
+ // simulate the cluster state change on the node
277
+ ClusterState localState = adaptClusterStateToLocalNode (state , node );
278
+ ClusterState previousLocalState = adaptClusterStateToLocalNode (previousState , node );
279
+ IndicesClusterStateService indicesCSSvc = createIndicesClusterStateService (node , RecordingIndicesService ::new );
280
+ indicesCSSvc .start ();
281
+ indicesCSSvc .applyClusterState (new ClusterChangedEvent ("cluster state change that adds the index" , localState , previousLocalState ));
282
+
283
+ assertNotNull (indicesCSSvc .indicesService .getShardOrNull (shardId ));
284
+
285
+ // check that failing unrelated allocation does not remove shard
286
+ indicesCSSvc .handleRecoveryFailure (shardRouting .reinitializeReplicaShard (), false , new Exception ("dummy" ));
287
+ assertNotNull (indicesCSSvc .indicesService .getShardOrNull (shardId ));
288
+
289
+ indicesCSSvc .handleRecoveryFailure (shardRouting , false , new Exception ("dummy" ));
290
+ assertNull (indicesCSSvc .indicesService .getShardOrNull (shardId ));
291
+ }
292
+
259
293
public ClusterState randomInitialClusterState (Map <DiscoveryNode , IndicesClusterStateService > clusterStateServiceMap ,
260
294
Supplier <MockIndicesService > indicesServiceSupplier ) {
261
295
List <DiscoveryNode > allNodes = new ArrayList <>();
0 commit comments