2828import org .elasticsearch .common .settings .Settings ;
2929import org .elasticsearch .common .util .set .Sets ;
3030import org .elasticsearch .index .Index ;
31- import org .elasticsearch .repositories .RepositoryMissingException ;
3231import org .elasticsearch .snapshots .RestoreService ;
3332import org .elasticsearch .snapshots .SearchableSnapshotsSettings ;
3433import org .elasticsearch .snapshots .SnapshotId ;
3837import java .util .Arrays ;
3938import java .util .HashMap ;
4039import java .util .HashSet ;
41- import java .util .List ;
4240import java .util .Map ;
4341import java .util .Objects ;
4442import java .util .Set ;
4543
46- import static java .util .Collections .emptyList ;
4744import static org .elasticsearch .snapshots .SearchableSnapshotsSettings .SEARCHABLE_SNAPSHOTS_DELETE_SNAPSHOT_ON_INDEX_DELETION ;
4845import static org .elasticsearch .snapshots .SearchableSnapshotsSettings .SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY ;
4946import static org .elasticsearch .snapshots .SearchableSnapshotsSettings .SEARCHABLE_SNAPSHOTS_REPOSITORY_UUID_SETTING_KEY ;
@@ -74,13 +71,15 @@ public void deleteIndices(final DeleteIndexClusterStateUpdateRequest request, fi
7471 throw new IllegalArgumentException ("Index name is required" );
7572 }
7673
77- clusterService .submitStateUpdateTask ("delete-index " + Arrays .toString (request .indices ()),
74+ clusterService .submitStateUpdateTask (
75+ "delete-index " + Arrays .toString (request .indices ()),
7876 new AckedClusterStateUpdateTask (Priority .URGENT , request , listener ) {
7977 @ Override
8078 public ClusterState execute (final ClusterState currentState ) {
8179 return deleteIndices (currentState , Sets .newHashSet (request .indices ()));
8280 }
83- });
81+ }
82+ );
8483 }
8584
8685 /**
@@ -95,8 +94,13 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
9594 IndexAbstraction .DataStream parent = meta .getIndicesLookup ().get (im .getIndex ().getName ()).getParentDataStream ();
9695 if (parent != null ) {
9796 if (parent .getWriteIndex ().equals (im )) {
98- throw new IllegalArgumentException ("index [" + index .getName () + "] is the write index for data stream [" +
99- parent .getName () + "] and cannot be deleted" );
97+ throw new IllegalArgumentException (
98+ "index ["
99+ + index .getName ()
100+ + "] is the write index for data stream ["
101+ + parent .getName ()
102+ + "] and cannot be deleted"
103+ );
100104 } else {
101105 backingIndices .put (index , parent .getDataStream ());
102106 }
@@ -107,8 +111,11 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
107111 // Check if index deletion conflicts with any running snapshots
108112 Set <Index > snapshottingIndices = SnapshotsService .snapshottingIndices (currentState , indicesToDelete );
109113 if (snapshottingIndices .isEmpty () == false ) {
110- throw new SnapshotInProgressException ("Cannot delete indices that are being snapshotted: " + snapshottingIndices +
111- ". Try again after snapshot finishes or cancel the currently running snapshot." );
114+ throw new SnapshotInProgressException (
115+ "Cannot delete indices that are being snapshotted: "
116+ + snapshottingIndices
117+ + ". Try again after snapshot finishes or cancel the currently running snapshot."
118+ );
112119 }
113120
114121 RoutingTable .Builder routingTableBuilder = RoutingTable .builder (currentState .routingTable ());
@@ -131,8 +138,12 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
131138 // add tombstones to the cluster state for each deleted index
132139 final IndexGraveyard currentGraveyard = graveyardBuilder .addTombstones (indices ).build (settings );
133140 metadataBuilder .indexGraveyard (currentGraveyard ); // the new graveyard set on the metadata
134- logger .trace ("{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}." ,
135- graveyardBuilder .getNumPurged (), previousGraveyardSize , currentGraveyard .getTombstones ().size ());
141+ logger .trace (
142+ "{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}." ,
143+ graveyardBuilder .getNumPurged (),
144+ previousGraveyardSize ,
145+ currentGraveyard .getTombstones ().size ()
146+ );
136147
137148 // add snapshot(s) marked as to delete to the cluster state
138149 final Map <String , Set <SnapshotId >> snapshotsToDelete = listOfSnapshotsToDelete (currentState , indicesToDelete );
@@ -158,13 +169,14 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
158169 }
159170
160171 return allocationService .reroute (
161- ClusterState .builder (currentState )
162- .routingTable (routingTableBuilder .build ())
163- .metadata (newMetadata )
164- .blocks (blocks )
165- .customs (customs )
166- .build (),
167- "deleted indices [" + indices + "]" );
172+ ClusterState .builder (currentState )
173+ .routingTable (routingTableBuilder .build ())
174+ .metadata (newMetadata )
175+ .blocks (blocks )
176+ .customs (customs )
177+ .build (),
178+ "deleted indices [" + indices + "]"
179+ );
168180 }
169181
170182 private static Map <String , Set <SnapshotId >> listOfSnapshotsToDelete (final ClusterState currentState , final Set <Index > indicesToDelete ) {
@@ -184,7 +196,9 @@ private static Map<String, Set<SnapshotId>> listOfSnapshotsToDelete(final Cluste
184196
185197 // TODO change this to an assertion once it becomes impossible to delete a snapshot that is mounted as an index
186198 if (currentState .custom (SnapshotDeletionsInProgress .TYPE , SnapshotDeletionsInProgress .EMPTY )
187- .getEntries ().stream ().anyMatch (entry -> entry .getSnapshots ().contains (new SnapshotId (snapshotName , snapshotUuid )))) {
199+ .getEntries ()
200+ .stream ()
201+ .anyMatch (entry -> entry .getSnapshots ().contains (new SnapshotId (snapshotName , snapshotUuid )))) {
188202 continue ; // this snapshot is part of an existing snapshot deletion in progress, nothing to do
189203 }
190204
@@ -214,15 +228,14 @@ private static Map<String, Set<SnapshotId>> listOfSnapshotsToDelete(final Cluste
214228
215229 private static String repositoryNameFromIndexSettings (ClusterState currentState , Settings indexSettings ) {
216230 final String repositoryUuid = indexSettings .get (SEARCHABLE_SNAPSHOTS_REPOSITORY_UUID_SETTING_KEY );
217- if (Strings .hasLength (repositoryUuid ) == false ) {
218- return indexSettings .get (SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY );
231+ if (Strings .hasLength (repositoryUuid )) {
232+ final RepositoriesMetadata repositories = currentState .metadata ().custom (RepositoriesMetadata .TYPE , RepositoriesMetadata .EMPTY );
233+ for (RepositoryMetadata repository : repositories .repositories ()) {
234+ if (repositoryUuid .equals (repository .uuid ())) {
235+ return repository .name ();
236+ }
237+ }
219238 }
220- final RepositoriesMetadata repoMetadata = currentState .metadata ().custom (RepositoriesMetadata .TYPE );
221- final List <RepositoryMetadata > repositories = repoMetadata == null ? emptyList () : repoMetadata .repositories ();
222- return repositories .stream ()
223- .filter (r -> repositoryUuid .equals (r .uuid ()))
224- .map (RepositoryMetadata ::name )
225- .findFirst ()
226- .orElseThrow (() -> new RepositoryMissingException (repositoryUuid ));
239+ return indexSettings .get (SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY );
227240 }
228241}
0 commit comments