98
98
import static org .elasticsearch .indices .cluster .IndicesClusterStateService .AllocatedIndices .IndexRemovalReason .DELETED ;
99
99
import static org .elasticsearch .indices .cluster .IndicesClusterStateService .AllocatedIndices .IndexRemovalReason .FAILURE ;
100
100
import static org .elasticsearch .indices .cluster .IndicesClusterStateService .AllocatedIndices .IndexRemovalReason .NO_LONGER_ASSIGNED ;
101
+ import static org .elasticsearch .indices .cluster .IndicesClusterStateService .AllocatedIndices .IndexRemovalReason .REOPENED ;
101
102
102
103
public class IndicesClusterStateService extends AbstractLifecycleComponent implements ClusterStateApplier {
103
104
private static final Logger logger = LogManager .getLogger (IndicesClusterStateService .class );
@@ -240,7 +241,7 @@ public synchronized void applyClusterState(final ClusterChangedEvent event) {
240
241
241
242
deleteIndices (event ); // also deletes shards of deleted indices
242
243
243
- removeUnallocatedIndices (event ); // also removes shards of removed indices
244
+ removeIndices (event ); // also removes shards of removed indices
244
245
245
246
failMissingShards (state );
246
247
@@ -352,17 +353,18 @@ protected void doRun() throws Exception {
352
353
}
353
354
354
355
/**
355
- * Removes indices that have no shards allocated to this node. This does not delete the shard data as we wait for enough
356
- * shard copies to exist in the cluster before deleting shard data (triggered by {@link org.elasticsearch.indices.store.IndicesStore}).
356
+ * Removes indices that have no shards allocated to this node or indices whose state has changed. This does not delete the shard data
357
+ * as we wait for enough shard copies to exist in the cluster before deleting shard data (triggered by
358
+ * {@link org.elasticsearch.indices.store.IndicesStore}).
357
359
*
358
360
* @param event the cluster changed event
359
361
*/
360
- private void removeUnallocatedIndices (final ClusterChangedEvent event ) {
362
+ private void removeIndices (final ClusterChangedEvent event ) {
361
363
final ClusterState state = event .state ();
362
364
final String localNodeId = state .nodes ().getLocalNodeId ();
363
365
assert localNodeId != null ;
364
366
365
- Set <Index > indicesWithShards = new HashSet <>();
367
+ final Set <Index > indicesWithShards = new HashSet <>();
366
368
RoutingNode localRoutingNode = state .getRoutingNodes ().node (localNodeId );
367
369
if (localRoutingNode != null ) { // null e.g. if we are not a data node
368
370
for (ShardRouting shardRouting : localRoutingNode ) {
@@ -371,20 +373,27 @@ private void removeUnallocatedIndices(final ClusterChangedEvent event) {
371
373
}
372
374
373
375
for (AllocatedIndex <? extends Shard > indexService : indicesService ) {
374
- Index index = indexService .index ();
375
- if (indicesWithShards .contains (index ) == false ) {
376
+ final Index index = indexService .index ();
377
+ final IndexMetaData indexMetaData = state .metaData ().index (index );
378
+ final IndexMetaData existingMetaData = indexService .getIndexSettings ().getIndexMetaData ();
379
+
380
+ AllocatedIndices .IndexRemovalReason reason = null ;
381
+ if (indexMetaData != null && indexMetaData .getState () != existingMetaData .getState ()) {
382
+ reason = indexMetaData .getState () == IndexMetaData .State .CLOSE ? CLOSED : REOPENED ;
383
+ } else if (indicesWithShards .contains (index ) == false ) {
376
384
// if the cluster change indicates a brand new cluster, we only want
377
385
// to remove the in-memory structures for the index and not delete the
378
386
// contents on disk because the index will later be re-imported as a
379
387
// dangling index
380
- final IndexMetaData indexMetaData = state .metaData ().index (index );
381
388
assert indexMetaData != null || event .isNewCluster () :
382
389
"index " + index + " does not exist in the cluster state, it should either " +
383
390
"have been deleted or the cluster must be new" ;
384
- final AllocatedIndices .IndexRemovalReason reason =
385
- indexMetaData != null && indexMetaData .getState () == IndexMetaData .State .CLOSE ? CLOSED : NO_LONGER_ASSIGNED ;
386
- logger .debug ("{} removing index, [{}]" , index , reason );
387
- indicesService .removeIndex (index , reason , "removing index (no shards allocated)" );
391
+ reason = indexMetaData != null && indexMetaData .getState () == IndexMetaData .State .CLOSE ? CLOSED : NO_LONGER_ASSIGNED ;
392
+ }
393
+
394
+ if (reason != null ) {
395
+ logger .debug ("{} removing index ({})" , index , reason );
396
+ indicesService .removeIndex (index , reason , "removing index (" + reason + ")" );
388
397
}
389
398
}
390
399
}
@@ -595,7 +604,7 @@ private void updateShard(DiscoveryNodes nodes, ShardRouting shardRouting, Shard
595
604
ClusterState clusterState ) {
596
605
final ShardRouting currentRoutingEntry = shard .routingEntry ();
597
606
assert currentRoutingEntry .isSameAllocation (shardRouting ) :
598
- "local shard has a different allocation id but wasn't cleaning by removeShards. "
607
+ "local shard has a different allocation id but wasn't cleaned by removeShards. "
599
608
+ "cluster state: " + shardRouting + " local: " + currentRoutingEntry ;
600
609
601
610
final long primaryTerm ;
@@ -730,7 +739,7 @@ private void failAndRemoveShard(ShardRouting shardRouting, boolean sendShardFail
730
739
private void sendFailShard (ShardRouting shardRouting , String message , @ Nullable Exception failure , ClusterState state ) {
731
740
try {
732
741
logger .warn (() -> new ParameterizedMessage (
733
- "[{}] marking and sending shard failed due to [{}]" , shardRouting .shardId (), message ), failure );
742
+ "{} marking and sending shard failed due to [{}]" , shardRouting .shardId (), message ), failure );
734
743
failedShardsCache .put (shardRouting .shardId (), shardRouting );
735
744
shardStateAction .localShardFailed (shardRouting , message , failure , SHARD_STATE_ACTION_LISTENER , state );
736
745
} catch (Exception inner ) {
@@ -931,7 +940,7 @@ enum IndexRemovalReason {
931
940
DELETED ,
932
941
933
942
/**
934
- * The index have been closed. The index should be removed and all associated resources released. Persistent parts of the index
943
+ * The index has been closed. The index should be removed and all associated resources released. Persistent parts of the index
935
944
* like the shards files, state and transaction logs are kept around in the case of a disaster recovery.
936
945
*/
937
946
CLOSED ,
@@ -941,7 +950,13 @@ enum IndexRemovalReason {
941
950
* Persistent parts of the index like the shards files, state and transaction logs are kept around in the
942
951
* case of a disaster recovery.
943
952
*/
944
- FAILURE
953
+ FAILURE ,
954
+
955
+ /**
956
+ * The index has been reopened. The index should be removed and all associated resources released. Persistent parts of the index
957
+ * like the shards files, state and transaction logs are kept around in the case of a disaster recovery.
958
+ */
959
+ REOPENED ,
945
960
}
946
961
}
947
962
}
0 commit comments