diff --git a/server/src/main/java/org/elasticsearch/cluster/SnapshotDeletionsInProgress.java b/server/src/main/java/org/elasticsearch/cluster/SnapshotDeletionsInProgress.java index 95b9f06e12e5e..4d6c938e6e1ca 100644 --- a/server/src/main/java/org/elasticsearch/cluster/SnapshotDeletionsInProgress.java +++ b/server/src/main/java/org/elasticsearch/cluster/SnapshotDeletionsInProgress.java @@ -100,6 +100,20 @@ public List getEntries() { return entries; } + /** + * Checks if there is an actively executing delete operation for the given repository + * + * @param repository repository name + */ + public boolean hasExecutingDeletion(String repository) { + for (Entry entry : entries) { + if (entry.state() == State.STARTED && entry.repository().equals(repository)) { + return true; + } + } + return false; + } + /** * Returns {@code true} if there are snapshot deletions in progress in the cluster, * returns {@code false} otherwise. diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java index 4c04b50268476..b20a6e4f6f8d2 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java @@ -550,8 +550,7 @@ public ClusterState execute(ClusterState currentState) { final ImmutableOpenMap.Builder clonesBuilder = ImmutableOpenMap.builder(); final boolean readyToExecute = currentState.custom( - SnapshotDeletionsInProgress.TYPE, SnapshotDeletionsInProgress.EMPTY).getEntries().stream() - .noneMatch(e -> e.repository().equals(repoName) && e.state() == SnapshotDeletionsInProgress.State.STARTED); + SnapshotDeletionsInProgress.TYPE, SnapshotDeletionsInProgress.EMPTY).hasExecutingDeletion(repoName) == false; final InFlightShardSnapshotStates inFlightShardStates; if (readyToExecute) { inFlightShardStates = InFlightShardSnapshotStates.forRepo(repoName, snapshotsInProgress.entries()); @@ -1691,8 +1690,7 @@ public ClusterState execute(ClusterState currentState) { repositoryData.getGenId(), updatedSnapshots.entries().stream().filter(entry -> repoName.equals(entry.repository())).noneMatch( SnapshotsService::isWritingToRepository) - && deletionsInProgress.getEntries().stream().noneMatch(entry -> - repoName.equals(entry.repository()) && entry.state() == SnapshotDeletionsInProgress.State.STARTED) + && deletionsInProgress.hasExecutingDeletion(repoName) == false ? SnapshotDeletionsInProgress.State.STARTED : SnapshotDeletionsInProgress.State.WAITING); } else { newDelete = replacedEntry.withAddedSnapshots(snapshotIdsRequiringCleanup); @@ -2123,8 +2121,7 @@ && alreadyReassigned(value.key.indexName(), value.key.shardId(), reassignedShard } // TODO: the below logic is very similar to that in #startCloning and both could be dried up against each other // also the code for standard snapshots could make use of this breakout as well - if (canBeUpdated.isEmpty() || updatedDeletions.getEntries().stream().anyMatch( - e -> e.repository().equals(repoName) && e.state() == SnapshotDeletionsInProgress.State.STARTED)) { + if (canBeUpdated.isEmpty() || updatedDeletions.hasExecutingDeletion(repoName)) { // No shards can be updated in this snapshot so we just add it as is again snapshotEntries.add(entry); } else { @@ -2261,15 +2258,14 @@ private static void completeListenersIgnoringException(@Nullable List shards( - SnapshotsInProgress snapshotsInProgress, @Nullable SnapshotDeletionsInProgress deletionsInProgress, + SnapshotsInProgress snapshotsInProgress, SnapshotDeletionsInProgress deletionsInProgress, Metadata metadata, RoutingTable routingTable, List indices, boolean useShardGenerations, RepositoryData repositoryData, String repoName) { ImmutableOpenMap.Builder builder = ImmutableOpenMap.builder(); final ShardGenerations shardGenerations = repositoryData.shardGenerations(); final InFlightShardSnapshotStates inFlightShardStates = InFlightShardSnapshotStates.forRepo(repoName, snapshotsInProgress.entries()); - final boolean readyToExecute = deletionsInProgress == null || deletionsInProgress.getEntries().stream() - .noneMatch(entry -> entry.repository().equals(repoName) && entry.state() == SnapshotDeletionsInProgress.State.STARTED); + final boolean readyToExecute = deletionsInProgress.hasExecutingDeletion(repoName) == false; for (IndexId index : indices) { final String indexName = index.getName(); final boolean isNewIndex = repositoryData.getIndices().containsKey(indexName) == false; @@ -2296,22 +2292,10 @@ private static ImmutableOpenMapbuilder().successes(tasks).build(currentState); }; - /** - * Creates a {@link ShardSnapshotStatus} entry for a snapshot after the shard has become available for snapshotting as a result - * of a snapshot clone completing. - * - * @param currentState current cluster state - * @param shardGeneration shard generation of the shard in the repository - * @param shardId shard id of the shard that just finished cloning - * @return shard snapshot status - */ - private static ShardSnapshotStatus startShardSnapshotAfterClone(ClusterState currentState, String shardGeneration, ShardId shardId) { - final ShardRouting primary = currentState.routingTable().index(shardId.getIndex()).shard(shardId.id()).primaryShard(); - final ShardSnapshotStatus shardSnapshotStatus; - if (primary == null || primary.assignedToNode() == false) { - shardSnapshotStatus = new ShardSnapshotStatus( - null, ShardState.MISSING, "primary shard is not allocated", shardGeneration); - } else if (primary.relocating() || primary.initializing()) { - shardSnapshotStatus = - new ShardSnapshotStatus(primary.currentNodeId(), ShardState.WAITING, shardGeneration); - } else if (primary.started() == false) { - shardSnapshotStatus = new ShardSnapshotStatus(primary.currentNodeId(), ShardState.MISSING, - "primary shard hasn't been started yet", shardGeneration); - } else { - shardSnapshotStatus = new ShardSnapshotStatus(primary.currentNodeId(), shardGeneration); - } - return shardSnapshotStatus; - } - /** * An update to the snapshot state of a shard. *