133133import org .elasticsearch .index .shard .ShardId ;
134134import org .elasticsearch .index .translog .TranslogStats ;
135135import org .elasticsearch .indices .breaker .CircuitBreakerService ;
136+ import org .elasticsearch .indices .cluster .IndexRemovalReason ;
136137import org .elasticsearch .indices .cluster .IndicesClusterStateService ;
137138import org .elasticsearch .indices .fielddata .cache .IndicesFieldDataCache ;
138139import org .elasticsearch .indices .recovery .PeerRecoveryTargetService ;
@@ -1021,7 +1022,7 @@ public void removeIndex(
10211022 listener .afterIndexRemoved (indexService .index (), indexSettings , reason );
10221023 if (reason == IndexRemovalReason .DELETED ) {
10231024 // now we are done - try to wipe data on disk if possible
1024- deleteIndexStore (extraInfo , indexService .index (), indexSettings );
1025+ deleteIndexStore (extraInfo , indexService .index (), indexSettings , reason );
10251026 }
10261027 }));
10271028 });
@@ -1105,7 +1106,7 @@ public void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata,
11051106 + "]"
11061107 );
11071108 }
1108- deleteIndexStore (reason , oldIndexMetadata );
1109+ deleteIndexStore (reason , oldIndexMetadata , IndexRemovalReason . DELETED );
11091110 } catch (Exception e ) {
11101111 logger .warn (() -> format ("[%s] failed to delete unassigned index (reason [%s])" , oldIndexMetadata .getIndex (), reason ), e );
11111112 }
@@ -1118,7 +1119,7 @@ public void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata,
11181119 *
11191120 * Package private for testing
11201121 */
1121- void deleteIndexStore (String reason , IndexMetadata metadata ) throws IOException {
1122+ void deleteIndexStore (String reasonText , IndexMetadata metadata , IndexRemovalReason reason ) throws IOException {
11221123 if (nodeEnv .hasNodeFile ()) {
11231124 synchronized (this ) {
11241125 Index index = metadata .getIndex ();
@@ -1136,33 +1137,35 @@ void deleteIndexStore(String reason, IndexMetadata metadata) throws IOException
11361137 }
11371138 }
11381139 final IndexSettings indexSettings = buildIndexSettings (metadata );
1139- deleteIndexStore (reason , indexSettings .getIndex (), indexSettings );
1140+ deleteIndexStore (reasonText , indexSettings .getIndex (), indexSettings , reason );
11401141 }
11411142 }
11421143
1143- private void deleteIndexStore (String reason , Index index , IndexSettings indexSettings ) throws IOException {
1144- deleteIndexStoreIfDeletionAllowed (reason , index , indexSettings , DEFAULT_INDEX_DELETION_PREDICATE );
1144+ private void deleteIndexStore (String reasonText , Index index , IndexSettings indexSettings , IndexRemovalReason reason )
1145+ throws IOException {
1146+ deleteIndexStoreIfDeletionAllowed (reasonText , index , indexSettings , DEFAULT_INDEX_DELETION_PREDICATE , reason );
11451147 }
11461148
11471149 private void deleteIndexStoreIfDeletionAllowed (
1148- final String reason ,
1150+ final String reasonText ,
11491151 final Index index ,
11501152 final IndexSettings indexSettings ,
1151- final IndexDeletionAllowedPredicate predicate
1153+ final IndexDeletionAllowedPredicate predicate ,
1154+ final IndexRemovalReason reason
11521155 ) throws IOException {
11531156 boolean success = false ;
11541157 try {
11551158 // we are trying to delete the index store here - not a big deal if the lock can't be obtained
11561159 // the store metadata gets wiped anyway even without the lock this is just best effort since
11571160 // every shards deletes its content under the shard lock it owns.
1158- logger .debug ("{} deleting index store reason [{}]" , index , reason );
1161+ logger .debug ("{} deleting index store reason [{}]" , index , reasonText );
11591162 if (predicate .apply (index , indexSettings )) {
11601163 // its safe to delete all index metadata and shard data
11611164 nodeEnv .deleteIndexDirectorySafe (
11621165 index ,
11631166 0 ,
11641167 indexSettings ,
1165- paths -> indexFoldersDeletionListeners .beforeIndexFoldersDeleted (index , indexSettings , paths )
1168+ paths -> indexFoldersDeletionListeners .beforeIndexFoldersDeleted (index , indexSettings , paths , reason )
11661169 );
11671170 }
11681171 success = true ;
@@ -1172,7 +1175,7 @@ private void deleteIndexStoreIfDeletionAllowed(
11721175 logger .warn (() -> format ("%s failed to delete index" , index ), ex );
11731176 } finally {
11741177 if (success == false ) {
1175- addPendingDelete (index , indexSettings );
1178+ addPendingDelete (index , indexSettings , reason );
11761179 }
11771180 // this is a pure protection to make sure this index doesn't get re-imported as a dangling index.
11781181 // we should in the future rather write a tombstone rather than wiping the metadata.
@@ -1182,19 +1185,21 @@ private void deleteIndexStoreIfDeletionAllowed(
11821185
11831186 /**
11841187 * Deletes the shard with an already acquired shard lock.
1185- * @param reason the reason for the shard deletion
1188+ * @param reasonText the reason for the shard deletion
11861189 * @param lock the lock of the shard to delete
11871190 * @param indexSettings the shards index settings.
1191+ * @param reason the reason for the deletion (as an enum)
11881192 * @throws IOException if an IOException occurs
11891193 */
11901194 @ Override
1191- public void deleteShardStore (String reason , ShardLock lock , IndexSettings indexSettings ) throws IOException {
1195+ public void deleteShardStore (String reasonText , ShardLock lock , IndexSettings indexSettings , IndexRemovalReason reason )
1196+ throws IOException {
11921197 ShardId shardId = lock .getShardId ();
1193- logger .trace ("{} deleting shard reason [{}]" , shardId , reason );
1198+ logger .trace ("{} deleting shard reason [{}]" , shardId , reasonText );
11941199 nodeEnv .deleteShardDirectoryUnderLock (
11951200 lock ,
11961201 indexSettings ,
1197- paths -> indexFoldersDeletionListeners .beforeShardFoldersDeleted (shardId , indexSettings , paths )
1202+ paths -> indexFoldersDeletionListeners .beforeShardFoldersDeleted (shardId , indexSettings , paths , reason )
11981203 );
11991204 }
12001205
@@ -1206,13 +1211,14 @@ public void deleteShardStore(String reason, ShardLock lock, IndexSettings indexS
12061211 * On data nodes, if the deleted shard is the last shard folder in its index, the method will attempt to remove
12071212 * the index folder as well.
12081213 *
1209- * @param reason the reason for the shard deletion
1214+ * @param reasonText the reason for the shard deletion
12101215 * @param shardId the shards ID to delete
12111216 * @param clusterState . This is required to access the indexes settings etc.
1217+ * @param reason The reason for the deletion (as an enum)
12121218 * @throws IOException if an IOException occurs
12131219 */
1214- public void deleteShardStore (String reason , ShardId shardId , ClusterState clusterState ) throws IOException ,
1215- ShardLockObtainFailedException {
1220+ public void deleteShardStore (String reasonText , ShardId shardId , ClusterState clusterState , IndexRemovalReason reason )
1221+ throws IOException , ShardLockObtainFailedException {
12161222 final IndexMetadata metadata = clusterState .getMetadata ().getProject ().indices ().get (shardId .getIndexName ());
12171223
12181224 final IndexSettings indexSettings = buildIndexSettings (metadata );
@@ -1223,15 +1229,15 @@ public void deleteShardStore(String reason, ShardId shardId, ClusterState cluste
12231229 nodeEnv .deleteShardDirectorySafe (
12241230 shardId ,
12251231 indexSettings ,
1226- paths -> indexFoldersDeletionListeners .beforeShardFoldersDeleted (shardId , indexSettings , paths )
1232+ paths -> indexFoldersDeletionListeners .beforeShardFoldersDeleted (shardId , indexSettings , paths , reason )
12271233 );
1228- logger .debug ("{} deleted shard reason [{}]" , shardId , reason );
1234+ logger .debug ("{} deleted shard reason [{}]" , shardId , reasonText );
12291235
12301236 if (canDeleteIndexContents (shardId .getIndex ())) {
12311237 if (nodeEnv .findAllShardIds (shardId .getIndex ()).isEmpty ()) {
12321238 try {
12331239 // note that deleteIndexStore have more safety checks and may throw an exception if index was concurrently created.
1234- deleteIndexStore ("no longer used" , metadata );
1240+ deleteIndexStore ("no longer used" , metadata , reason );
12351241 } catch (Exception e ) {
12361242 // wrap the exception to indicate we already deleted the shard
12371243 throw new ElasticsearchException ("failed to delete unused index after deleting its last shard (" + shardId + ")" , e );
@@ -1287,7 +1293,7 @@ public IndexMetadata verifyIndexIsDeleted(final Index index, final ClusterState
12871293 }
12881294 final IndexSettings indexSettings = buildIndexSettings (metadata );
12891295 try {
1290- deleteIndexStoreIfDeletionAllowed ("stale deleted index" , index , indexSettings , ALWAYS_TRUE );
1296+ deleteIndexStoreIfDeletionAllowed ("stale deleted index" , index , indexSettings , ALWAYS_TRUE , IndexRemovalReason . DELETED );
12911297 } catch (Exception e ) {
12921298 // we just warn about the exception here because if deleteIndexStoreIfDeletionAllowed
12931299 // throws an exception, it gets added to the list of pending deletes to be tried again
@@ -1345,22 +1351,22 @@ private IndexSettings buildIndexSettings(IndexMetadata metadata) {
13451351 * Adds a pending delete for the given index shard.
13461352 */
13471353 @ Override
1348- public void addPendingDelete (ShardId shardId , IndexSettings settings ) {
1354+ public void addPendingDelete (ShardId shardId , IndexSettings settings , IndexRemovalReason reason ) {
13491355 if (shardId == null ) {
13501356 throw new IllegalArgumentException ("shardId must not be null" );
13511357 }
13521358 if (settings == null ) {
13531359 throw new IllegalArgumentException ("settings must not be null" );
13541360 }
1355- PendingDelete pendingDelete = new PendingDelete (shardId , settings );
1361+ PendingDelete pendingDelete = new PendingDelete (shardId , settings , reason );
13561362 addPendingDelete (shardId .getIndex (), pendingDelete );
13571363 }
13581364
13591365 /**
13601366 * Adds a pending delete for the given index.
13611367 */
1362- public void addPendingDelete (Index index , IndexSettings settings ) {
1363- PendingDelete pendingDelete = new PendingDelete (index , settings );
1368+ public void addPendingDelete (Index index , IndexSettings settings , IndexRemovalReason reason ) {
1369+ PendingDelete pendingDelete = new PendingDelete (index , settings , reason );
13641370 addPendingDelete (index , pendingDelete );
13651371 }
13661372
@@ -1376,25 +1382,28 @@ private static final class PendingDelete implements Comparable<PendingDelete> {
13761382 final int shardId ;
13771383 final IndexSettings settings ;
13781384 final boolean deleteIndex ;
1385+ final IndexRemovalReason reason ;
13791386
13801387 /**
13811388 * Creates a new pending delete of an index
13821389 */
1383- PendingDelete (ShardId shardId , IndexSettings settings ) {
1390+ PendingDelete (ShardId shardId , IndexSettings settings , IndexRemovalReason reason ) {
13841391 this .index = shardId .getIndex ();
13851392 this .shardId = shardId .getId ();
13861393 this .settings = settings ;
13871394 this .deleteIndex = false ;
1395+ this .reason = reason ;
13881396 }
13891397
13901398 /**
13911399 * Creates a new pending delete of a shard
13921400 */
1393- PendingDelete (Index index , IndexSettings settings ) {
1401+ PendingDelete (Index index , IndexSettings settings , IndexRemovalReason reason ) {
13941402 this .index = index ;
13951403 this .shardId = -1 ;
13961404 this .settings = settings ;
13971405 this .deleteIndex = true ;
1406+ this .reason = reason ;
13981407 }
13991408
14001409 @ Override
@@ -1458,7 +1467,12 @@ public void processPendingDeletes(Index index, IndexSettings indexSettings, Time
14581467 nodeEnv .deleteIndexDirectoryUnderLock (
14591468 index ,
14601469 indexSettings ,
1461- paths -> indexFoldersDeletionListeners .beforeIndexFoldersDeleted (index , indexSettings , paths )
1470+ paths -> indexFoldersDeletionListeners .beforeIndexFoldersDeleted (
1471+ index ,
1472+ indexSettings ,
1473+ paths ,
1474+ delete .reason
1475+ )
14621476 );
14631477 iterator .remove ();
14641478 } catch (IOException ex ) {
@@ -1470,7 +1484,7 @@ public void processPendingDeletes(Index index, IndexSettings indexSettings, Time
14701484 final ShardLock shardLock = locks .get (shardId );
14711485 if (shardLock != null ) {
14721486 try {
1473- deleteShardStore ("pending delete" , shardLock , delete .settings );
1487+ deleteShardStore ("pending delete" , shardLock , delete .settings , delete . reason );
14741488 iterator .remove ();
14751489 } catch (IOException ex ) {
14761490 logger .debug (() -> format ("%s retry pending delete" , shardLock .getShardId ()), ex );
0 commit comments