28
28
import org .elasticsearch .common .settings .Settings ;
29
29
import org .elasticsearch .common .util .set .Sets ;
30
30
import org .elasticsearch .index .Index ;
31
- import org .elasticsearch .repositories .RepositoryMissingException ;
32
31
import org .elasticsearch .snapshots .RestoreService ;
33
32
import org .elasticsearch .snapshots .SearchableSnapshotsSettings ;
34
33
import org .elasticsearch .snapshots .SnapshotId ;
38
37
import java .util .Arrays ;
39
38
import java .util .HashMap ;
40
39
import java .util .HashSet ;
41
- import java .util .List ;
42
40
import java .util .Map ;
43
41
import java .util .Objects ;
44
42
import java .util .Set ;
45
43
46
- import static java .util .Collections .emptyList ;
47
44
import static org .elasticsearch .snapshots .SearchableSnapshotsSettings .SEARCHABLE_SNAPSHOTS_DELETE_SNAPSHOT_ON_INDEX_DELETION ;
48
45
import static org .elasticsearch .snapshots .SearchableSnapshotsSettings .SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY ;
49
46
import static org .elasticsearch .snapshots .SearchableSnapshotsSettings .SEARCHABLE_SNAPSHOTS_REPOSITORY_UUID_SETTING_KEY ;
@@ -74,13 +71,15 @@ public void deleteIndices(final DeleteIndexClusterStateUpdateRequest request, fi
74
71
throw new IllegalArgumentException ("Index name is required" );
75
72
}
76
73
77
- clusterService .submitStateUpdateTask ("delete-index " + Arrays .toString (request .indices ()),
74
+ clusterService .submitStateUpdateTask (
75
+ "delete-index " + Arrays .toString (request .indices ()),
78
76
new AckedClusterStateUpdateTask (Priority .URGENT , request , listener ) {
79
77
@ Override
80
78
public ClusterState execute (final ClusterState currentState ) {
81
79
return deleteIndices (currentState , Sets .newHashSet (request .indices ()));
82
80
}
83
- });
81
+ }
82
+ );
84
83
}
85
84
86
85
/**
@@ -95,8 +94,13 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
95
94
IndexAbstraction .DataStream parent = meta .getIndicesLookup ().get (im .getIndex ().getName ()).getParentDataStream ();
96
95
if (parent != null ) {
97
96
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
+ );
100
104
} else {
101
105
backingIndices .put (index , parent .getDataStream ());
102
106
}
@@ -107,8 +111,11 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
107
111
// Check if index deletion conflicts with any running snapshots
108
112
Set <Index > snapshottingIndices = SnapshotsService .snapshottingIndices (currentState , indicesToDelete );
109
113
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
+ );
112
119
}
113
120
114
121
RoutingTable .Builder routingTableBuilder = RoutingTable .builder (currentState .routingTable ());
@@ -131,8 +138,12 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
131
138
// add tombstones to the cluster state for each deleted index
132
139
final IndexGraveyard currentGraveyard = graveyardBuilder .addTombstones (indices ).build (settings );
133
140
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
+ );
136
147
137
148
// add snapshot(s) marked as to delete to the cluster state
138
149
final Map <String , Set <SnapshotId >> snapshotsToDelete = listOfSnapshotsToDelete (currentState , indicesToDelete );
@@ -158,13 +169,14 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices)
158
169
}
159
170
160
171
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
+ );
168
180
}
169
181
170
182
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
184
196
185
197
// TODO change this to an assertion once it becomes impossible to delete a snapshot that is mounted as an index
186
198
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 )))) {
188
202
continue ; // this snapshot is part of an existing snapshot deletion in progress, nothing to do
189
203
}
190
204
@@ -214,15 +228,14 @@ private static Map<String, Set<SnapshotId>> listOfSnapshotsToDelete(final Cluste
214
228
215
229
private static String repositoryNameFromIndexSettings (ClusterState currentState , Settings indexSettings ) {
216
230
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
+ }
219
238
}
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 );
227
240
}
228
241
}
0 commit comments