Skip to content

Commit c34bee4

Browse files
dbwiddismispencer
andauthored
Add new parameters to snapshot restore to rename the restored aliases… (#16292) (#16447)
* Add new parameters to snapshot restore to rename the restored aliases similar to the existing parameters to rename indexes * Fix comment. Update changelog. * New parameters needs to only used for new version * Add missing equals and hash implemenation for new parameters * Add some tests * Add some more tests * Use CountDownLatch * Add two more tests. Refactoring and cleanup. * Use CURRENT version to pass backward compatibility tests. Change to V2.18 later once it is backported into that version. * Refactoring * Overwriting aliases variable causes test failures for reasons I do not understand. Also some refactoring. * Convert to paramaterized tests --------- Signed-off-by: Spencer G. Jones <[email protected]> Signed-off-by: Daniel Widdis <[email protected]> Co-authored-by: Spencer G. Jones <[email protected]>
1 parent 56551fd commit c34bee4

File tree

8 files changed

+506
-14
lines changed

8 files changed

+506
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2828
- Add _list/shards API as paginated alternate to _cat/shards ([#14641](https://github.com/opensearch-project/OpenSearch/pull/14641))
2929
- [Star Tree - Search] Add support for metric aggregations with/without term query ([15289](https://github.com/opensearch-project/OpenSearch/pull/15289))
3030
- URI path filtering support in cluster stats API ([#15938](https://github.com/opensearch-project/OpenSearch/pull/15938))
31-
31+
- Add support for renaming aliases during snapshot restore ([#16292](https://github.com/opensearch-project/OpenSearch/pull/16292))
3232

3333
### Dependencies
3434
- Bump `org.apache.logging.log4j:log4j-core` from 2.23.1 to 2.24.0 ([#15858](https://github.com/opensearch-project/OpenSearch/pull/15858))

server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ private static StorageType fromString(String string) {
113113
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
114114
private String renamePattern;
115115
private String renameReplacement;
116+
private String renameAliasPattern;
117+
private String renameAliasReplacement;
116118
private boolean waitForCompletion;
117119
private boolean includeGlobalState = false;
118120
private boolean partial = false;
@@ -170,6 +172,13 @@ public RestoreSnapshotRequest(StreamInput in) throws IOException {
170172
if (in.getVersion().onOrAfter(Version.V_2_17_0)) {
171173
sourceRemoteTranslogRepository = in.readOptionalString();
172174
}
175+
// TODO: change to V_2_18_0 once this is backported into that version
176+
if (in.getVersion().onOrAfter(Version.CURRENT)) {
177+
renameAliasPattern = in.readOptionalString();
178+
}
179+
if (in.getVersion().onOrAfter(Version.CURRENT)) {
180+
renameAliasReplacement = in.readOptionalString();
181+
}
173182
}
174183

175184
@Override
@@ -206,6 +215,13 @@ public void writeTo(StreamOutput out) throws IOException {
206215
if (out.getVersion().onOrAfter(Version.V_2_17_0)) {
207216
out.writeOptionalString(sourceRemoteTranslogRepository);
208217
}
218+
// TODO: change to V_2_18_0 once this is backported into that version
219+
if (out.getVersion().onOrAfter(Version.CURRENT)) {
220+
out.writeOptionalString(renameAliasPattern);
221+
}
222+
if (out.getVersion().onOrAfter(Version.CURRENT)) {
223+
out.writeOptionalString(renameAliasReplacement);
224+
}
209225
}
210226

211227
@Override
@@ -376,6 +392,51 @@ public String renameReplacement() {
376392
return renameReplacement;
377393
}
378394

395+
/**
396+
* Sets rename pattern that should be applied to restored indices' alias.
397+
* <p>
398+
* Alias that match the rename pattern will be renamed according to {@link #renameAliasReplacement(String)}. The
399+
* rename pattern is applied according to the {@link java.util.regex.Matcher#appendReplacement(StringBuffer, String)}
400+
* If two or more aliases are renamed into the same name, they will be merged.
401+
*
402+
* @param renameAliasPattern rename pattern
403+
* @return this request
404+
*/
405+
public RestoreSnapshotRequest renameAliasPattern(String renameAliasPattern) {
406+
this.renameAliasPattern = renameAliasPattern;
407+
return this;
408+
}
409+
410+
/**
411+
* Returns rename alias pattern
412+
*
413+
* @return rename alias pattern
414+
*/
415+
public String renameAliasPattern() {
416+
return renameAliasPattern;
417+
}
418+
419+
/**
420+
* Sets rename alias replacement
421+
* <p>
422+
* See {@link #renameAliasPattern(String)} for more information.
423+
*
424+
* @param renameAliasReplacement rename replacement
425+
*/
426+
public RestoreSnapshotRequest renameAliasReplacement(String renameAliasReplacement) {
427+
this.renameAliasReplacement = renameAliasReplacement;
428+
return this;
429+
}
430+
431+
/**
432+
* Returns rename alias replacement
433+
*
434+
* @return rename alias replacement
435+
*/
436+
public String renameAliasReplacement() {
437+
return renameAliasReplacement;
438+
}
439+
379440
/**
380441
* If this parameter is set to true the operation will wait for completion of restore process before returning.
381442
*
@@ -640,6 +701,18 @@ public RestoreSnapshotRequest source(Map<String, Object> source) {
640701
} else {
641702
throw new IllegalArgumentException("malformed rename_replacement");
642703
}
704+
} else if (name.equals("rename_alias_pattern")) {
705+
if (entry.getValue() instanceof String) {
706+
renameAliasPattern((String) entry.getValue());
707+
} else {
708+
throw new IllegalArgumentException("malformed rename_alias_pattern");
709+
}
710+
} else if (name.equals("rename_alias_replacement")) {
711+
if (entry.getValue() instanceof String) {
712+
renameAliasReplacement((String) entry.getValue());
713+
} else {
714+
throw new IllegalArgumentException("malformed rename_alias_replacement");
715+
}
643716
} else if (name.equals("index_settings")) {
644717
if (!(entry.getValue() instanceof Map)) {
645718
throw new IllegalArgumentException("malformed index_settings section");
@@ -700,6 +773,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
700773
if (renameReplacement != null) {
701774
builder.field("rename_replacement", renameReplacement);
702775
}
776+
if (renameAliasPattern != null) {
777+
builder.field("rename_alias_pattern", renameAliasPattern);
778+
}
779+
if (renameAliasReplacement != null) {
780+
builder.field("rename_alias_replacement", renameAliasReplacement);
781+
}
703782
builder.field("include_global_state", includeGlobalState);
704783
builder.field("partial", partial);
705784
builder.field("include_aliases", includeAliases);
@@ -748,6 +827,8 @@ public boolean equals(Object o) {
748827
&& Objects.equals(indicesOptions, that.indicesOptions)
749828
&& Objects.equals(renamePattern, that.renamePattern)
750829
&& Objects.equals(renameReplacement, that.renameReplacement)
830+
&& Objects.equals(renameAliasPattern, that.renameAliasPattern)
831+
&& Objects.equals(renameAliasReplacement, that.renameAliasReplacement)
751832
&& Objects.equals(indexSettings, that.indexSettings)
752833
&& Arrays.equals(ignoreIndexSettings, that.ignoreIndexSettings)
753834
&& Objects.equals(snapshotUuid, that.snapshotUuid)
@@ -766,6 +847,8 @@ public int hashCode() {
766847
indicesOptions,
767848
renamePattern,
768849
renameReplacement,
850+
renameAliasPattern,
851+
renameAliasReplacement,
769852
waitForCompletion,
770853
includeGlobalState,
771854
partial,

server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestBuilder.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,34 @@ public RestoreSnapshotRequestBuilder setRenameReplacement(String renameReplaceme
144144
return this;
145145
}
146146

147+
/**
148+
* Sets rename pattern that should be applied to restored indices' aliases.
149+
* <p>
150+
* Aliases that match the rename pattern will be renamed according to {@link #setRenameAliasReplacement(String)}. The
151+
* rename pattern is applied according to the {@link java.util.regex.Matcher#appendReplacement(StringBuffer, String)}
152+
* The request will fail if two or more alias will be renamed into the same name.
153+
*
154+
* @param renameAliasPattern rename alias pattern
155+
* @return this builder
156+
*/
157+
public RestoreSnapshotRequestBuilder setRenameAliasPattern(String renameAliasPattern) {
158+
request.renameAliasPattern(renameAliasPattern);
159+
return this;
160+
}
161+
162+
/**
163+
* Sets rename replacement
164+
* <p>
165+
* See {@link #setRenameAliasPattern(String)} for more information.
166+
*
167+
* @param renameAliasReplacement rename alias replacement
168+
* @return this builder
169+
*/
170+
public RestoreSnapshotRequestBuilder setRenameAliasReplacement(String renameAliasReplacement) {
171+
request.renameAliasReplacement(renameAliasReplacement);
172+
return this;
173+
}
174+
147175
/**
148176
* If this parameter is set to true the operation will wait for completion of restore process before returning.
149177
*

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
import java.util.function.Function;
113113
import java.util.function.Predicate;
114114
import java.util.function.Supplier;
115+
import java.util.regex.Pattern;
115116
import java.util.stream.Collectors;
116117

117118
import static java.util.Collections.unmodifiableSet;
@@ -498,9 +499,7 @@ public ClusterState execute(ClusterState currentState) {
498499
// Remove all aliases - they shouldn't be restored
499500
indexMdBuilder.removeAllAliases();
500501
} else {
501-
for (final String alias : snapshotIndexMetadata.getAliases().keySet()) {
502-
aliases.add(alias);
503-
}
502+
applyAliasesWithRename(snapshotIndexMetadata, indexMdBuilder, aliases);
504503
}
505504
IndexMetadata updatedIndexMetadata = indexMdBuilder.build();
506505
if (partial) {
@@ -545,9 +544,7 @@ public ClusterState execute(ClusterState currentState) {
545544
indexMdBuilder.putAlias(alias);
546545
}
547546
} else {
548-
for (final String alias : snapshotIndexMetadata.getAliases().keySet()) {
549-
aliases.add(alias);
550-
}
547+
applyAliasesWithRename(snapshotIndexMetadata, indexMdBuilder, aliases);
551548
}
552549
final Settings.Builder indexSettingsBuilder = Settings.builder()
553550
.put(snapshotIndexMetadata.getSettings())
@@ -682,6 +679,27 @@ private void checkAliasNameConflicts(Map<String, String> renamedIndices, Set<Str
682679
}
683680
}
684681

682+
private void applyAliasesWithRename(
683+
IndexMetadata snapshotIndexMetadata,
684+
IndexMetadata.Builder indexMdBuilder,
685+
Set<String> aliases
686+
) {
687+
if (request.renameAliasPattern() == null || request.renameAliasReplacement() == null) {
688+
aliases.addAll(snapshotIndexMetadata.getAliases().keySet());
689+
} else {
690+
Pattern renameAliasPattern = Pattern.compile(request.renameAliasPattern());
691+
for (final Map.Entry<String, AliasMetadata> alias : snapshotIndexMetadata.getAliases().entrySet()) {
692+
String currentAliasName = alias.getKey();
693+
indexMdBuilder.removeAlias(currentAliasName);
694+
String newAliasName = renameAliasPattern.matcher(currentAliasName)
695+
.replaceAll(request.renameAliasReplacement());
696+
AliasMetadata newAlias = AliasMetadata.newAliasMetadata(alias.getValue(), newAliasName);
697+
indexMdBuilder.putAlias(newAlias);
698+
aliases.add(newAliasName);
699+
}
700+
}
701+
}
702+
685703
private String[] getIgnoreSettingsInternal() {
686704
// for non-remote store enabled domain, we will remove all the remote store
687705
// related index settings present in the snapshot.

server/src/test/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ private RestoreSnapshotRequest randomState(RestoreSnapshotRequest instance) {
7171
if (randomBoolean()) {
7272
instance.renameReplacement(randomUnicodeOfLengthBetween(1, 100));
7373
}
74+
if (randomBoolean()) {
75+
instance.renameAliasPattern(randomUnicodeOfLengthBetween(1, 100));
76+
}
77+
if (randomBoolean()) {
78+
instance.renameAliasReplacement(randomUnicodeOfLengthBetween(1, 100));
79+
}
7480
instance.partial(randomBoolean());
7581
instance.includeAliases(randomBoolean());
7682

0 commit comments

Comments
 (0)