-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Delete backing snapshot when searchable snapshot index is deleted #75565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fdf484c
add snapshots to delete to repository metadata
tlrx ba6b73b
add snapshots to delete metadata
tlrx a446683
prevent snapshots to delete to be mounted/restored/cloned
tlrx 704c8ea
trigger snapshot deletions
tlrx 955a5fc
add settings
tlrx 982d6ea
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx 4e641ab
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx c799368
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx e1995d7
Remove other repository name check
tlrx e13a11a
Do not re read repository name
tlrx 4df53d7
Delete snapshots using UUIDs
tlrx 3e8dc65
rework snapshot deletion
tlrx e1f17d5
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx b7bcf80
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx 0bcaf26
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx 83dfa73
fix random int in test
tlrx eb0e89e
Merge branch 'master' into delete-snap-on-searchable-index-deletion
tlrx b1f36f8
Update docs/changelog/75565.yaml
tlrx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 75565 | ||
summary: Delete backing snapshot when searchable snapshot index is deleted | ||
area: Snapshot/Restore | ||
type: enhancement | ||
issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,26 +16,37 @@ | |
import org.elasticsearch.cluster.AckedClusterStateUpdateTask; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.RestoreInProgress; | ||
import org.elasticsearch.cluster.SnapshotDeletionsInProgress; | ||
import org.elasticsearch.cluster.block.ClusterBlocks; | ||
import org.elasticsearch.cluster.routing.RoutingTable; | ||
import org.elasticsearch.cluster.routing.allocation.AllocationService; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.Priority; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.collect.ImmutableOpenMap; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.set.Sets; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.snapshots.RestoreService; | ||
import org.elasticsearch.snapshots.SearchableSnapshotsSettings; | ||
import org.elasticsearch.snapshots.SnapshotId; | ||
import org.elasticsearch.snapshots.SnapshotInProgressException; | ||
import org.elasticsearch.snapshots.SnapshotsService; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_DELETE_SNAPSHOT_ON_INDEX_DELETION; | ||
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY; | ||
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_REPOSITORY_UUID_SETTING_KEY; | ||
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_SNAPSHOT_NAME_SETTING_KEY; | ||
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOTS_SNAPSHOT_UUID_SETTING_KEY; | ||
|
||
/** | ||
* Deletes indices. | ||
*/ | ||
|
@@ -60,13 +71,15 @@ public void deleteIndices(final DeleteIndexClusterStateUpdateRequest request, fi | |
throw new IllegalArgumentException("Index name is required"); | ||
} | ||
|
||
clusterService.submitStateUpdateTask("delete-index " + Arrays.toString(request.indices()), | ||
clusterService.submitStateUpdateTask( | ||
"delete-index " + Arrays.toString(request.indices()), | ||
new AckedClusterStateUpdateTask(Priority.URGENT, request, listener) { | ||
@Override | ||
public ClusterState execute(final ClusterState currentState) { | ||
return deleteIndices(currentState, Sets.newHashSet(request.indices())); | ||
} | ||
}); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -81,8 +94,13 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) | |
IndexAbstraction.DataStream parent = meta.getIndicesLookup().get(im.getIndex().getName()).getParentDataStream(); | ||
if (parent != null) { | ||
if (parent.getWriteIndex().equals(im)) { | ||
throw new IllegalArgumentException("index [" + index.getName() + "] is the write index for data stream [" + | ||
parent.getName() + "] and cannot be deleted"); | ||
throw new IllegalArgumentException( | ||
"index [" | ||
+ index.getName() | ||
+ "] is the write index for data stream [" | ||
+ parent.getName() | ||
+ "] and cannot be deleted" | ||
); | ||
} else { | ||
backingIndices.put(index, parent.getDataStream()); | ||
} | ||
|
@@ -93,8 +111,11 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) | |
// Check if index deletion conflicts with any running snapshots | ||
Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentState, indicesToDelete); | ||
if (snapshottingIndices.isEmpty() == false) { | ||
throw new SnapshotInProgressException("Cannot delete indices that are being snapshotted: " + snapshottingIndices + | ||
". Try again after snapshot finishes or cancel the currently running snapshot."); | ||
throw new SnapshotInProgressException( | ||
"Cannot delete indices that are being snapshotted: " | ||
+ snapshottingIndices | ||
+ ". Try again after snapshot finishes or cancel the currently running snapshot." | ||
); | ||
} | ||
|
||
RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable()); | ||
|
@@ -117,8 +138,22 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) | |
// add tombstones to the cluster state for each deleted index | ||
final IndexGraveyard currentGraveyard = graveyardBuilder.addTombstones(indices).build(settings); | ||
metadataBuilder.indexGraveyard(currentGraveyard); // the new graveyard set on the metadata | ||
logger.trace("{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}.", | ||
graveyardBuilder.getNumPurged(), previousGraveyardSize, currentGraveyard.getTombstones().size()); | ||
logger.trace( | ||
"{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}.", | ||
graveyardBuilder.getNumPurged(), | ||
previousGraveyardSize, | ||
currentGraveyard.getTombstones().size() | ||
); | ||
|
||
// add snapshot(s) marked as to delete to the cluster state | ||
final Map<String, Set<SnapshotId>> snapshotsToDelete = listOfSnapshotsToDelete(currentState, indicesToDelete); | ||
if (snapshotsToDelete.isEmpty() == false) { | ||
RepositoriesMetadata repositories = currentState.metadata().custom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY); | ||
for (Map.Entry<String, Set<SnapshotId>> snapshotToDelete : snapshotsToDelete.entrySet()) { | ||
repositories = repositories.addSnapshotsToDelete(snapshotToDelete.getKey(), snapshotToDelete.getValue()); | ||
} | ||
metadataBuilder.putCustom(RepositoriesMetadata.TYPE, repositories); | ||
} | ||
|
||
Metadata newMetadata = metadataBuilder.build(); | ||
ClusterBlocks blocks = clusterBlocksBuilder.build(); | ||
|
@@ -134,12 +169,73 @@ public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) | |
} | ||
|
||
return allocationService.reroute( | ||
ClusterState.builder(currentState) | ||
.routingTable(routingTableBuilder.build()) | ||
.metadata(newMetadata) | ||
.blocks(blocks) | ||
.customs(customs) | ||
.build(), | ||
"deleted indices [" + indices + "]"); | ||
ClusterState.builder(currentState) | ||
.routingTable(routingTableBuilder.build()) | ||
.metadata(newMetadata) | ||
.blocks(blocks) | ||
.customs(customs) | ||
.build(), | ||
"deleted indices [" + indices + "]" | ||
); | ||
} | ||
|
||
private static Map<String, Set<SnapshotId>> listOfSnapshotsToDelete(final ClusterState currentState, final Set<Index> indicesToDelete) { | ||
final Map<String, Set<SnapshotId>> snapshotsToDelete = new HashMap<>(); | ||
|
||
for (Index indexToDelete : indicesToDelete) { | ||
final Settings indexSettings = currentState.metadata().getIndexSafe(indexToDelete).getSettings(); | ||
if (SearchableSnapshotsSettings.isSearchableSnapshotIndexWithSnapshotDeletion(indexSettings) == false) { | ||
continue; | ||
} | ||
|
||
final String repositoryName = repositoryNameFromIndexSettings(currentState, indexSettings); | ||
final String snapshotName = indexSettings.get(SEARCHABLE_SNAPSHOTS_SNAPSHOT_NAME_SETTING_KEY); | ||
final String snapshotUuid = indexSettings.get(SEARCHABLE_SNAPSHOTS_SNAPSHOT_UUID_SETTING_KEY); | ||
|
||
boolean canDeleteSnapshot = true; | ||
|
||
// TODO change this to an assertion once it becomes impossible to delete a snapshot that is mounted as an index | ||
if (currentState.custom(SnapshotDeletionsInProgress.TYPE, SnapshotDeletionsInProgress.EMPTY) | ||
.getEntries() | ||
.stream() | ||
.anyMatch(entry -> entry.getSnapshots().contains(new SnapshotId(snapshotName, snapshotUuid)))) { | ||
continue; // this snapshot is part of an existing snapshot deletion in progress, nothing to do | ||
} | ||
|
||
for (IndexMetadata other : currentState.metadata()) { | ||
if (indicesToDelete.contains(other.getIndex())) { | ||
continue; // do not check indices that are going to be deleted | ||
} | ||
final Settings otherSettings = other.getSettings(); | ||
if (SearchableSnapshotsSettings.isSearchableSnapshotStore(otherSettings) == false) { | ||
continue; // other index is not a searchable snapshot index, skip | ||
} | ||
final String otherSnapshotUuid = otherSettings.get(SEARCHABLE_SNAPSHOTS_SNAPSHOT_UUID_SETTING_KEY); | ||
if (Objects.equals(snapshotUuid, otherSnapshotUuid) == false) { | ||
continue; // other index is backed by a different snapshot, skip | ||
} | ||
assert otherSettings.getAsBoolean(SEARCHABLE_SNAPSHOTS_DELETE_SNAPSHOT_ON_INDEX_DELETION, false) : other; | ||
DaveCTurner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
canDeleteSnapshot = false; // another index is using the same snapshot, do not delete | ||
break; | ||
} | ||
if (canDeleteSnapshot) { | ||
snapshotsToDelete.computeIfAbsent(repositoryName, r -> new HashSet<>()) | ||
.add(new SnapshotId(indexSettings.get(SEARCHABLE_SNAPSHOTS_SNAPSHOT_NAME_SETTING_KEY), snapshotUuid)); | ||
} | ||
} | ||
return snapshotsToDelete; | ||
} | ||
|
||
private static String repositoryNameFromIndexSettings(ClusterState currentState, Settings indexSettings) { | ||
final String repositoryUuid = indexSettings.get(SEARCHABLE_SNAPSHOTS_REPOSITORY_UUID_SETTING_KEY); | ||
if (Strings.hasLength(repositoryUuid)) { | ||
final RepositoriesMetadata repositories = currentState.metadata().custom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY); | ||
for (RepositoryMetadata repository : repositories.repositories()) { | ||
if (repositoryUuid.equals(repository.uuid())) { | ||
return repository.name(); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should return null in case there is a repo-uuid on the index but no such repo was found? This works differently from |
||
return indexSettings.get(SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we risk getting an
IllegalArgumentException
fromRepositoriesMetadata.withUpdate
when the repo has been deleted while deleting the index.