diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/TransportRestoreSnapshotAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/TransportRestoreSnapshotAction.java index 56fcba85167e7..b362be49b10ab 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/TransportRestoreSnapshotAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/TransportRestoreSnapshotAction.java @@ -82,12 +82,7 @@ protected ClusterBlockException checkBlock(RestoreSnapshotRequest request, Clust @Override protected void masterOperation(final RestoreSnapshotRequest request, final ClusterState state, final ActionListener listener) { - RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(request.repository(), request.snapshot(), - request.indices(), request.indicesOptions(), request.renamePattern(), request.renameReplacement(), - request.settings(), request.masterNodeTimeout(), request.includeGlobalState(), request.partial(), request.includeAliases(), - request.indexSettings(), request.ignoreIndexSettings(), "restore_snapshot[" + request.snapshot() + "]"); - - restoreService.restoreSnapshot(restoreRequest, new ActionListener() { + restoreService.restoreSnapshot(request, new ActionListener() { @Override public void onResponse(RestoreCompletionResponse restoreCompletionResponse) { if (restoreCompletionResponse.getRestoreInfo() == null && request.waitForCompletion()) { diff --git a/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java b/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java index eecac92d63e95..b8fa8c6f1a9c8 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java +++ b/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java @@ -27,7 +27,7 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest; import org.elasticsearch.cluster.ClusterChangedEvent; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateApplier; @@ -78,7 +78,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; @@ -100,7 +99,7 @@ *

* Restore operation is performed in several stages. *

- * First {@link #restoreSnapshot(RestoreRequest, org.elasticsearch.action.ActionListener)} + * First {@link #restoreSnapshot(RestoreSnapshotRequest, org.elasticsearch.action.ActionListener)} * method reads information about snapshot and metadata from repository. In update cluster state task it checks restore * preconditions, restores global state if needed, creates {@link RestoreInProgress} record with list of shards that needs * to be restored and adds this shard to the routing table using @@ -172,28 +171,30 @@ public RestoreService(ClusterService clusterService, RepositoriesService reposit * @param request restore request * @param listener restore listener */ - public void restoreSnapshot(final RestoreRequest request, final ActionListener listener) { + public void restoreSnapshot(final RestoreSnapshotRequest request, final ActionListener listener) { try { // Read snapshot info and metadata from the repository - Repository repository = repositoriesService.repository(request.repositoryName); + final String repositoryName = request.repository(); + Repository repository = repositoriesService.repository(repositoryName); final RepositoryData repositoryData = repository.getRepositoryData(); + final String snapshotName = request.snapshot(); final Optional incompatibleSnapshotId = - repositoryData.getIncompatibleSnapshotIds().stream().filter(s -> request.snapshotName.equals(s.getName())).findFirst(); + repositoryData.getIncompatibleSnapshotIds().stream().filter(s -> snapshotName.equals(s.getName())).findFirst(); if (incompatibleSnapshotId.isPresent()) { - throw new SnapshotRestoreException(request.repositoryName, request.snapshotName, "cannot restore incompatible snapshot"); + throw new SnapshotRestoreException(repositoryName, snapshotName, "cannot restore incompatible snapshot"); } final Optional matchingSnapshotId = repositoryData.getSnapshotIds().stream() - .filter(s -> request.snapshotName.equals(s.getName())).findFirst(); + .filter(s -> snapshotName.equals(s.getName())).findFirst(); if (matchingSnapshotId.isPresent() == false) { - throw new SnapshotRestoreException(request.repositoryName, request.snapshotName, "snapshot does not exist"); + throw new SnapshotRestoreException(repositoryName, snapshotName, "snapshot does not exist"); } final SnapshotId snapshotId = matchingSnapshotId.get(); final SnapshotInfo snapshotInfo = repository.getSnapshotInfo(snapshotId); - final Snapshot snapshot = new Snapshot(request.repositoryName, snapshotId); + final Snapshot snapshot = new Snapshot(repositoryName, snapshotId); // Make sure that we can restore from this snapshot - validateSnapshotRestorable(request.repositoryName, snapshotInfo); + validateSnapshotRestorable(repositoryName, snapshotInfo); // Resolve the indices from the snapshot that need to be restored final List indicesInSnapshot = filterIndices(snapshotInfo.indices(), request.indices(), request.indicesOptions()); @@ -218,7 +219,7 @@ public void restoreSnapshot(final RestoreRequest request, final ActionListener new ParameterizedMessage("[{}] failed to restore snapshot", - request.repositoryName + ":" + request.snapshotName), e); + request.repository() + ":" + request.snapshot()), e); listener.onFailure(e); } } @@ -820,7 +821,7 @@ public static int failedShards(ImmutableOpenMap renamedIndices(RestoreRequest request, List filteredIndices) { + private Map renamedIndices(RestoreSnapshotRequest request, List filteredIndices) { Map renamedIndices = new HashMap<>(); for (String index : filteredIndices) { String renamedIndex = index; @@ -829,7 +830,7 @@ private Map renamedIndices(RestoreRequest request, List } String previousIndex = renamedIndices.put(renamedIndex, index); if (previousIndex != null) { - throw new SnapshotRestoreException(request.repositoryName, request.snapshotName, + throw new SnapshotRestoreException(request.repository(), request.snapshot(), "indices [" + index + "] and [" + previousIndex + "] are renamed into the same index [" + renamedIndex + "]"); } } @@ -919,203 +920,4 @@ public static boolean isRepositoryInUse(ClusterState clusterState, String reposi } return false; } - - /** - * Restore snapshot request - */ - public static class RestoreRequest { - - private final String cause; - - private final String repositoryName; - - private final String snapshotName; - - private final String[] indices; - - private final String renamePattern; - - private final String renameReplacement; - - private final IndicesOptions indicesOptions; - - private final Settings settings; - - private final TimeValue masterNodeTimeout; - - private final boolean includeGlobalState; - - private final boolean partial; - - private final boolean includeAliases; - - private final Settings indexSettings; - - private final String[] ignoreIndexSettings; - - /** - * Constructs new restore request - * - * @param repositoryName repositoryName - * @param snapshotName snapshotName - * @param indices list of indices to restore - * @param indicesOptions indices options - * @param renamePattern pattern to rename indices - * @param renameReplacement replacement for renamed indices - * @param settings repository specific restore settings - * @param masterNodeTimeout master node timeout - * @param includeGlobalState include global state into restore - * @param partial allow partial restore - * @param indexSettings index settings that should be changed on restore - * @param ignoreIndexSettings index settings that shouldn't be restored - * @param cause cause for restoring the snapshot - */ - public RestoreRequest(String repositoryName, String snapshotName, String[] indices, IndicesOptions indicesOptions, - String renamePattern, String renameReplacement, Settings settings, - TimeValue masterNodeTimeout, boolean includeGlobalState, boolean partial, boolean includeAliases, - Settings indexSettings, String[] ignoreIndexSettings, String cause) { - this.repositoryName = Objects.requireNonNull(repositoryName); - this.snapshotName = Objects.requireNonNull(snapshotName); - this.indices = indices; - this.renamePattern = renamePattern; - this.renameReplacement = renameReplacement; - this.indicesOptions = indicesOptions; - this.settings = settings; - this.masterNodeTimeout = masterNodeTimeout; - this.includeGlobalState = includeGlobalState; - this.partial = partial; - this.includeAliases = includeAliases; - this.indexSettings = indexSettings; - this.ignoreIndexSettings = ignoreIndexSettings; - this.cause = cause; - } - - /** - * Returns restore operation cause - * - * @return restore operation cause - */ - public String cause() { - return cause; - } - - /** - * Returns repository name - * - * @return repository name - */ - public String repositoryName() { - return repositoryName; - } - - /** - * Returns snapshot name - * - * @return snapshot name - */ - public String snapshotName() { - return snapshotName; - } - - /** - * Return the list of indices to be restored - * - * @return the list of indices - */ - public String[] indices() { - return indices; - } - - /** - * Returns indices option flags - * - * @return indices options flags - */ - public IndicesOptions indicesOptions() { - return indicesOptions; - } - - /** - * Returns rename pattern - * - * @return rename pattern - */ - public String renamePattern() { - return renamePattern; - } - - /** - * Returns replacement pattern - * - * @return replacement pattern - */ - public String renameReplacement() { - return renameReplacement; - } - - /** - * Returns repository-specific restore settings - * - * @return restore settings - */ - public Settings settings() { - return settings; - } - - /** - * Returns true if global state should be restore during this restore operation - * - * @return restore global state flag - */ - public boolean includeGlobalState() { - return includeGlobalState; - } - - /** - * Returns true if incomplete indices will be restored - * - * @return partial indices restore flag - */ - public boolean partial() { - return partial; - } - - /** - * Returns true if aliases should be restore during this restore operation - * - * @return restore aliases state flag - */ - public boolean includeAliases() { - return includeAliases; - } - - /** - * Returns index settings that should be changed on restore - * - * @return restore aliases state flag - */ - public Settings indexSettings() { - return indexSettings; - } - - /** - * Returns index settings that that shouldn't be restored - * - * @return restore aliases state flag - */ - public String[] ignoreIndexSettings() { - return ignoreIndexSettings; - } - - - /** - * Return master node timeout - * - * @return master node timeout - */ - public TimeValue masterNodeTimeout() { - return masterNodeTimeout; - } - - } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java index 36e1027dc5f87..825520d2f1541 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java @@ -8,6 +8,7 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest; import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.get.GetResponse; @@ -115,11 +116,10 @@ public void testThatRepositoryRecoversEmptyIndexBasedOnLeaderSettings() throws I Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(leaderClusterRepoName, - CcrRepository.LATEST, new String[]{leaderIndex}, indicesOptions, - "^(.*)$", followerIndex, Settings.EMPTY, new TimeValue(1, TimeUnit.HOURS), false, - false, true, settingsBuilder.build(), new String[0], - "restore_snapshot[" + leaderClusterRepoName + ":" + leaderIndex + "]"); + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) + .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") + .renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) + .indexSettings(settingsBuilder); PlainActionFuture future = PlainActionFuture.newFuture(); restoreService.restoreSnapshot(restoreRequest, waitForRestore(clusterService, future)); @@ -215,11 +215,10 @@ public void testDocsAreRecovered() throws Exception { Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(leaderClusterRepoName, - CcrRepository.LATEST, new String[]{leaderIndex}, indicesOptions, - "^(.*)$", followerIndex, Settings.EMPTY, new TimeValue(1, TimeUnit.HOURS), false, - false, true, settingsBuilder.build(), new String[0], - "restore_snapshot[" + leaderClusterRepoName + ":" + leaderIndex + "]"); + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) + .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") + .renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) + .indexSettings(settingsBuilder); PlainActionFuture future = PlainActionFuture.newFuture(); restoreService.restoreSnapshot(restoreRequest, waitForRestore(clusterService, future)); @@ -252,11 +251,10 @@ public void testFollowerMappingIsUpdated() throws IOException { Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(leaderClusterRepoName, - CcrRepository.LATEST, new String[]{leaderIndex}, indicesOptions, - "^(.*)$", followerIndex, Settings.EMPTY, new TimeValue(1, TimeUnit.HOURS), false, - false, true, settingsBuilder.build(), new String[0], - "restore_snapshot[" + leaderClusterRepoName + ":" + leaderIndex + "]"); + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) + .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") + .renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) + .indexSettings(settingsBuilder); // TODO: Eventually when the file recovery work is complete, we should test updated mappings by // indexing to the leader while the recovery is happening. However, into order to that test mappings