Skip to content

Commit 3e8dc65

Browse files
committed
rework snapshot deletion
1 parent 4df53d7 commit 3e8dc65

File tree

6 files changed

+367
-282
lines changed

6 files changed

+367
-282
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataDeleteIndexService.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.common.settings.Settings;
2929
import org.elasticsearch.common.util.set.Sets;
3030
import org.elasticsearch.index.Index;
31-
import org.elasticsearch.repositories.RepositoryMissingException;
3231
import org.elasticsearch.snapshots.RestoreService;
3332
import org.elasticsearch.snapshots.SearchableSnapshotsSettings;
3433
import org.elasticsearch.snapshots.SnapshotId;
@@ -38,12 +37,10 @@
3837
import java.util.Arrays;
3938
import java.util.HashMap;
4039
import java.util.HashSet;
41-
import java.util.List;
4240
import java.util.Map;
4341
import java.util.Objects;
4442
import java.util.Set;
4543

46-
import static java.util.Collections.emptyList;
4744
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_DELETE_SNAPSHOT_ON_INDEX_DELETION;
4845
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY;
4946
import 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
}

server/src/main/java/org/elasticsearch/cluster/metadata/RepositoryMetadata.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ public RepositoryMetadata withSettings(Settings settings) {
246246
return new RepositoryMetadata(name, uuid, type, settings, generation, pendingGeneration, snapshotsToDelete);
247247
}
248248

249+
public RepositoryMetadata withSnapshotsToDelete(List<SnapshotId> snapshotsToDelete) {
250+
return new RepositoryMetadata(name, uuid, type, settings, generation, pendingGeneration, snapshotsToDelete);
251+
}
252+
249253
public RepositoryMetadata addSnapshotsToDelete(Collection<SnapshotId> snapshotsToDelete) {
250254
final List<SnapshotId> snapshots = new ArrayList<>(this.snapshotsToDelete);
251255
for (SnapshotId snapshotToDelete : snapshotsToDelete) {

server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ public void apply(Settings value, Settings current, Settings previous) {
478478
HandshakingTransportAddressConnector.PROBE_CONNECT_TIMEOUT_SETTING,
479479
HandshakingTransportAddressConnector.PROBE_HANDSHAKE_TIMEOUT_SETTING,
480480
SnapshotsService.MAX_CONCURRENT_SNAPSHOT_OPERATIONS_SETTING,
481-
SnapshotsService.MAX_CONCURRENT_SNAPSHOT_TO_DELETE_PER_REPOSITORY_SETTING,
482481
SnapshotsService.SNAPSHOT_TO_DELETE_RETRY_INTERVAL_SETTING,
483482
RestoreService.REFRESH_REPO_UUID_ON_RESTORE_SETTING,
484483
FsHealthService.ENABLED_SETTING,

server/src/main/java/org/elasticsearch/repositories/RepositoriesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public ClusterState execute(ClusterState currentState) {
217217
updatedMetadata = repositoryMetadata.withSettings(newRepositoryMetadata.settings());
218218
} else {
219219
ensureRepositoryNotInUse(currentState, request.name());
220-
updatedMetadata = newRepositoryMetadata;
220+
updatedMetadata = newRepositoryMetadata.withSnapshotsToDelete(repositoryMetadata.snapshotsToDelete());
221221
}
222222
found = true;
223223
repositoriesMetadata.add(updatedMetadata);

0 commit comments

Comments
 (0)