Skip to content

Commit 381d035

Browse files
Remove Redundant RestoreRequest Class (elastic#37535)
* Same as elastic#37464 but for the restore side
1 parent a0c504e commit 381d035

File tree

3 files changed

+31
-236
lines changed

3 files changed

+31
-236
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/TransportRestoreSnapshotAction.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ protected ClusterBlockException checkBlock(RestoreSnapshotRequest request, Clust
8282
@Override
8383
protected void masterOperation(final RestoreSnapshotRequest request, final ClusterState state,
8484
final ActionListener<RestoreSnapshotResponse> listener) {
85-
RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(request.repository(), request.snapshot(),
86-
request.indices(), request.indicesOptions(), request.renamePattern(), request.renameReplacement(),
87-
request.settings(), request.masterNodeTimeout(), request.includeGlobalState(), request.partial(), request.includeAliases(),
88-
request.indexSettings(), request.ignoreIndexSettings(), "restore_snapshot[" + request.snapshot() + "]");
89-
90-
restoreService.restoreSnapshot(restoreRequest, new ActionListener<RestoreCompletionResponse>() {
85+
restoreService.restoreSnapshot(request, new ActionListener<RestoreCompletionResponse>() {
9186
@Override
9287
public void onResponse(RestoreCompletionResponse restoreCompletionResponse) {
9388
if (restoreCompletionResponse.getRestoreInfo() == null && request.waitForCompletion()) {

server/src/main/java/org/elasticsearch/snapshots/RestoreService.java

Lines changed: 17 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.apache.logging.log4j.message.ParameterizedMessage;
2828
import org.elasticsearch.Version;
2929
import org.elasticsearch.action.ActionListener;
30-
import org.elasticsearch.action.support.IndicesOptions;
30+
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest;
3131
import org.elasticsearch.cluster.ClusterChangedEvent;
3232
import org.elasticsearch.cluster.ClusterState;
3333
import org.elasticsearch.cluster.ClusterStateApplier;
@@ -78,7 +78,6 @@
7878
import java.util.HashSet;
7979
import java.util.List;
8080
import java.util.Map;
81-
import java.util.Objects;
8281
import java.util.Optional;
8382
import java.util.Set;
8483
import java.util.function.Predicate;
@@ -100,7 +99,7 @@
10099
* <p>
101100
* Restore operation is performed in several stages.
102101
* <p>
103-
* First {@link #restoreSnapshot(RestoreRequest, org.elasticsearch.action.ActionListener)}
102+
* First {@link #restoreSnapshot(RestoreSnapshotRequest, org.elasticsearch.action.ActionListener)}
104103
* method reads information about snapshot and metadata from repository. In update cluster state task it checks restore
105104
* preconditions, restores global state if needed, creates {@link RestoreInProgress} record with list of shards that needs
106105
* to be restored and adds this shard to the routing table using
@@ -172,28 +171,30 @@ public RestoreService(ClusterService clusterService, RepositoriesService reposit
172171
* @param request restore request
173172
* @param listener restore listener
174173
*/
175-
public void restoreSnapshot(final RestoreRequest request, final ActionListener<RestoreCompletionResponse> listener) {
174+
public void restoreSnapshot(final RestoreSnapshotRequest request, final ActionListener<RestoreCompletionResponse> listener) {
176175
try {
177176
// Read snapshot info and metadata from the repository
178-
Repository repository = repositoriesService.repository(request.repositoryName);
177+
final String repositoryName = request.repository();
178+
Repository repository = repositoriesService.repository(repositoryName);
179179
final RepositoryData repositoryData = repository.getRepositoryData();
180+
final String snapshotName = request.snapshot();
180181
final Optional<SnapshotId> incompatibleSnapshotId =
181-
repositoryData.getIncompatibleSnapshotIds().stream().filter(s -> request.snapshotName.equals(s.getName())).findFirst();
182+
repositoryData.getIncompatibleSnapshotIds().stream().filter(s -> snapshotName.equals(s.getName())).findFirst();
182183
if (incompatibleSnapshotId.isPresent()) {
183-
throw new SnapshotRestoreException(request.repositoryName, request.snapshotName, "cannot restore incompatible snapshot");
184+
throw new SnapshotRestoreException(repositoryName, snapshotName, "cannot restore incompatible snapshot");
184185
}
185186
final Optional<SnapshotId> matchingSnapshotId = repositoryData.getSnapshotIds().stream()
186-
.filter(s -> request.snapshotName.equals(s.getName())).findFirst();
187+
.filter(s -> snapshotName.equals(s.getName())).findFirst();
187188
if (matchingSnapshotId.isPresent() == false) {
188-
throw new SnapshotRestoreException(request.repositoryName, request.snapshotName, "snapshot does not exist");
189+
throw new SnapshotRestoreException(repositoryName, snapshotName, "snapshot does not exist");
189190
}
190191

191192
final SnapshotId snapshotId = matchingSnapshotId.get();
192193
final SnapshotInfo snapshotInfo = repository.getSnapshotInfo(snapshotId);
193-
final Snapshot snapshot = new Snapshot(request.repositoryName, snapshotId);
194+
final Snapshot snapshot = new Snapshot(repositoryName, snapshotId);
194195

195196
// Make sure that we can restore from this snapshot
196-
validateSnapshotRestorable(request.repositoryName, snapshotInfo);
197+
validateSnapshotRestorable(repositoryName, snapshotInfo);
197198

198199
// Resolve the indices from the snapshot that need to be restored
199200
final List<String> indicesInSnapshot = filterIndices(snapshotInfo.indices(), request.indices(), request.indicesOptions());
@@ -218,7 +219,7 @@ public void restoreSnapshot(final RestoreRequest request, final ActionListener<R
218219

219220
// Now we can start the actual restore process by adding shards to be recovered in the cluster state
220221
// and updating cluster metadata (global and index) as needed
221-
clusterService.submitStateUpdateTask(request.cause(), new ClusterStateUpdateTask() {
222+
clusterService.submitStateUpdateTask("restore_snapshot[" + snapshotName + ']', new ClusterStateUpdateTask() {
222223
String restoreUUID = UUIDs.randomBase64UUID();
223224
RestoreInfo restoreInfo = null;
224225

@@ -261,7 +262,7 @@ public ClusterState execute(ClusterState currentState) {
261262
String renamedIndexName = indexEntry.getKey();
262263
IndexMetaData snapshotIndexMetaData = metaData.index(index);
263264
snapshotIndexMetaData = updateIndexSettings(snapshotIndexMetaData,
264-
request.indexSettings, request.ignoreIndexSettings);
265+
request.indexSettings(), request.ignoreIndexSettings());
265266
try {
266267
snapshotIndexMetaData = metaDataIndexUpgradeService.upgradeIndexMetaData(snapshotIndexMetaData,
267268
minIndexCompatibilityVersion);
@@ -535,7 +536,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
535536

536537
} catch (Exception e) {
537538
logger.warn(() -> new ParameterizedMessage("[{}] failed to restore snapshot",
538-
request.repositoryName + ":" + request.snapshotName), e);
539+
request.repository() + ":" + request.snapshot()), e);
539540
listener.onFailure(e);
540541
}
541542
}
@@ -820,7 +821,7 @@ public static int failedShards(ImmutableOpenMap<ShardId, RestoreInProgress.Shard
820821
return failedShards;
821822
}
822823

823-
private Map<String, String> renamedIndices(RestoreRequest request, List<String> filteredIndices) {
824+
private Map<String, String> renamedIndices(RestoreSnapshotRequest request, List<String> filteredIndices) {
824825
Map<String, String> renamedIndices = new HashMap<>();
825826
for (String index : filteredIndices) {
826827
String renamedIndex = index;
@@ -829,7 +830,7 @@ private Map<String, String> renamedIndices(RestoreRequest request, List<String>
829830
}
830831
String previousIndex = renamedIndices.put(renamedIndex, index);
831832
if (previousIndex != null) {
832-
throw new SnapshotRestoreException(request.repositoryName, request.snapshotName,
833+
throw new SnapshotRestoreException(request.repository(), request.snapshot(),
833834
"indices [" + index + "] and [" + previousIndex + "] are renamed into the same index [" + renamedIndex + "]");
834835
}
835836
}
@@ -919,203 +920,4 @@ public static boolean isRepositoryInUse(ClusterState clusterState, String reposi
919920
}
920921
return false;
921922
}
922-
923-
/**
924-
* Restore snapshot request
925-
*/
926-
public static class RestoreRequest {
927-
928-
private final String cause;
929-
930-
private final String repositoryName;
931-
932-
private final String snapshotName;
933-
934-
private final String[] indices;
935-
936-
private final String renamePattern;
937-
938-
private final String renameReplacement;
939-
940-
private final IndicesOptions indicesOptions;
941-
942-
private final Settings settings;
943-
944-
private final TimeValue masterNodeTimeout;
945-
946-
private final boolean includeGlobalState;
947-
948-
private final boolean partial;
949-
950-
private final boolean includeAliases;
951-
952-
private final Settings indexSettings;
953-
954-
private final String[] ignoreIndexSettings;
955-
956-
/**
957-
* Constructs new restore request
958-
*
959-
* @param repositoryName repositoryName
960-
* @param snapshotName snapshotName
961-
* @param indices list of indices to restore
962-
* @param indicesOptions indices options
963-
* @param renamePattern pattern to rename indices
964-
* @param renameReplacement replacement for renamed indices
965-
* @param settings repository specific restore settings
966-
* @param masterNodeTimeout master node timeout
967-
* @param includeGlobalState include global state into restore
968-
* @param partial allow partial restore
969-
* @param indexSettings index settings that should be changed on restore
970-
* @param ignoreIndexSettings index settings that shouldn't be restored
971-
* @param cause cause for restoring the snapshot
972-
*/
973-
public RestoreRequest(String repositoryName, String snapshotName, String[] indices, IndicesOptions indicesOptions,
974-
String renamePattern, String renameReplacement, Settings settings,
975-
TimeValue masterNodeTimeout, boolean includeGlobalState, boolean partial, boolean includeAliases,
976-
Settings indexSettings, String[] ignoreIndexSettings, String cause) {
977-
this.repositoryName = Objects.requireNonNull(repositoryName);
978-
this.snapshotName = Objects.requireNonNull(snapshotName);
979-
this.indices = indices;
980-
this.renamePattern = renamePattern;
981-
this.renameReplacement = renameReplacement;
982-
this.indicesOptions = indicesOptions;
983-
this.settings = settings;
984-
this.masterNodeTimeout = masterNodeTimeout;
985-
this.includeGlobalState = includeGlobalState;
986-
this.partial = partial;
987-
this.includeAliases = includeAliases;
988-
this.indexSettings = indexSettings;
989-
this.ignoreIndexSettings = ignoreIndexSettings;
990-
this.cause = cause;
991-
}
992-
993-
/**
994-
* Returns restore operation cause
995-
*
996-
* @return restore operation cause
997-
*/
998-
public String cause() {
999-
return cause;
1000-
}
1001-
1002-
/**
1003-
* Returns repository name
1004-
*
1005-
* @return repository name
1006-
*/
1007-
public String repositoryName() {
1008-
return repositoryName;
1009-
}
1010-
1011-
/**
1012-
* Returns snapshot name
1013-
*
1014-
* @return snapshot name
1015-
*/
1016-
public String snapshotName() {
1017-
return snapshotName;
1018-
}
1019-
1020-
/**
1021-
* Return the list of indices to be restored
1022-
*
1023-
* @return the list of indices
1024-
*/
1025-
public String[] indices() {
1026-
return indices;
1027-
}
1028-
1029-
/**
1030-
* Returns indices option flags
1031-
*
1032-
* @return indices options flags
1033-
*/
1034-
public IndicesOptions indicesOptions() {
1035-
return indicesOptions;
1036-
}
1037-
1038-
/**
1039-
* Returns rename pattern
1040-
*
1041-
* @return rename pattern
1042-
*/
1043-
public String renamePattern() {
1044-
return renamePattern;
1045-
}
1046-
1047-
/**
1048-
* Returns replacement pattern
1049-
*
1050-
* @return replacement pattern
1051-
*/
1052-
public String renameReplacement() {
1053-
return renameReplacement;
1054-
}
1055-
1056-
/**
1057-
* Returns repository-specific restore settings
1058-
*
1059-
* @return restore settings
1060-
*/
1061-
public Settings settings() {
1062-
return settings;
1063-
}
1064-
1065-
/**
1066-
* Returns true if global state should be restore during this restore operation
1067-
*
1068-
* @return restore global state flag
1069-
*/
1070-
public boolean includeGlobalState() {
1071-
return includeGlobalState;
1072-
}
1073-
1074-
/**
1075-
* Returns true if incomplete indices will be restored
1076-
*
1077-
* @return partial indices restore flag
1078-
*/
1079-
public boolean partial() {
1080-
return partial;
1081-
}
1082-
1083-
/**
1084-
* Returns true if aliases should be restore during this restore operation
1085-
*
1086-
* @return restore aliases state flag
1087-
*/
1088-
public boolean includeAliases() {
1089-
return includeAliases;
1090-
}
1091-
1092-
/**
1093-
* Returns index settings that should be changed on restore
1094-
*
1095-
* @return restore aliases state flag
1096-
*/
1097-
public Settings indexSettings() {
1098-
return indexSettings;
1099-
}
1100-
1101-
/**
1102-
* Returns index settings that that shouldn't be restored
1103-
*
1104-
* @return restore aliases state flag
1105-
*/
1106-
public String[] ignoreIndexSettings() {
1107-
return ignoreIndexSettings;
1108-
}
1109-
1110-
1111-
/**
1112-
* Return master node timeout
1113-
*
1114-
* @return master node timeout
1115-
*/
1116-
public TimeValue masterNodeTimeout() {
1117-
return masterNodeTimeout;
1118-
}
1119-
1120-
}
1121923
}

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.elasticsearch.action.ActionListener;
1010
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
11+
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest;
1112
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
1213
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
1314
import org.elasticsearch.action.get.GetResponse;
@@ -115,11 +116,10 @@ public void testThatRepositoryRecoversEmptyIndexBasedOnLeaderSettings() throws I
115116
Settings.Builder settingsBuilder = Settings.builder()
116117
.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followerIndex)
117118
.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true);
118-
RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(leaderClusterRepoName,
119-
CcrRepository.LATEST, new String[]{leaderIndex}, indicesOptions,
120-
"^(.*)$", followerIndex, Settings.EMPTY, new TimeValue(1, TimeUnit.HOURS), false,
121-
false, true, settingsBuilder.build(), new String[0],
122-
"restore_snapshot[" + leaderClusterRepoName + ":" + leaderIndex + "]");
119+
RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST)
120+
.indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$")
121+
.renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS))
122+
.indexSettings(settingsBuilder);
123123

124124
PlainActionFuture<RestoreInfo> future = PlainActionFuture.newFuture();
125125
restoreService.restoreSnapshot(restoreRequest, waitForRestore(clusterService, future));
@@ -215,11 +215,10 @@ public void testDocsAreRecovered() throws Exception {
215215
Settings.Builder settingsBuilder = Settings.builder()
216216
.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followerIndex)
217217
.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true);
218-
RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(leaderClusterRepoName,
219-
CcrRepository.LATEST, new String[]{leaderIndex}, indicesOptions,
220-
"^(.*)$", followerIndex, Settings.EMPTY, new TimeValue(1, TimeUnit.HOURS), false,
221-
false, true, settingsBuilder.build(), new String[0],
222-
"restore_snapshot[" + leaderClusterRepoName + ":" + leaderIndex + "]");
218+
RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST)
219+
.indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$")
220+
.renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS))
221+
.indexSettings(settingsBuilder);
223222

224223
PlainActionFuture<RestoreInfo> future = PlainActionFuture.newFuture();
225224
restoreService.restoreSnapshot(restoreRequest, waitForRestore(clusterService, future));
@@ -252,11 +251,10 @@ public void testFollowerMappingIsUpdated() throws IOException {
252251
Settings.Builder settingsBuilder = Settings.builder()
253252
.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followerIndex)
254253
.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true);
255-
RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(leaderClusterRepoName,
256-
CcrRepository.LATEST, new String[]{leaderIndex}, indicesOptions,
257-
"^(.*)$", followerIndex, Settings.EMPTY, new TimeValue(1, TimeUnit.HOURS), false,
258-
false, true, settingsBuilder.build(), new String[0],
259-
"restore_snapshot[" + leaderClusterRepoName + ":" + leaderIndex + "]");
254+
RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST)
255+
.indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$")
256+
.renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS))
257+
.indexSettings(settingsBuilder);
260258

261259
// TODO: Eventually when the file recovery work is complete, we should test updated mappings by
262260
// indexing to the leader while the recovery is happening. However, into order to that test mappings

0 commit comments

Comments
 (0)