Skip to content

Commit

Permalink
Add new parameters to snapshot restore to rename the restored aliases… (
Browse files Browse the repository at this point in the history
#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 <spencer.jones2@tylertech.com>
Signed-off-by: Daniel Widdis <widdis@gmail.com>
Co-authored-by: Spencer G. Jones <spencer.jones2@tylertech.com>
  • Loading branch information
dbwiddis and mispencer authored Oct 23, 2024
1 parent 56551fd commit c34bee4
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add _list/shards API as paginated alternate to _cat/shards ([#14641](https://github.com/opensearch-project/OpenSearch/pull/14641))
- [Star Tree - Search] Add support for metric aggregations with/without term query ([15289](https://github.com/opensearch-project/OpenSearch/pull/15289))
- URI path filtering support in cluster stats API ([#15938](https://github.com/opensearch-project/OpenSearch/pull/15938))

- Add support for renaming aliases during snapshot restore ([#16292](https://github.com/opensearch-project/OpenSearch/pull/16292))

### Dependencies
- Bump `org.apache.logging.log4j:log4j-core` from 2.23.1 to 2.24.0 ([#15858](https://github.com/opensearch-project/OpenSearch/pull/15858))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ private static StorageType fromString(String string) {
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
private String renamePattern;
private String renameReplacement;
private String renameAliasPattern;
private String renameAliasReplacement;
private boolean waitForCompletion;
private boolean includeGlobalState = false;
private boolean partial = false;
Expand Down Expand Up @@ -170,6 +172,13 @@ public RestoreSnapshotRequest(StreamInput in) throws IOException {
if (in.getVersion().onOrAfter(Version.V_2_17_0)) {
sourceRemoteTranslogRepository = in.readOptionalString();
}
// TODO: change to V_2_18_0 once this is backported into that version
if (in.getVersion().onOrAfter(Version.CURRENT)) {
renameAliasPattern = in.readOptionalString();
}
if (in.getVersion().onOrAfter(Version.CURRENT)) {
renameAliasReplacement = in.readOptionalString();
}
}

@Override
Expand Down Expand Up @@ -206,6 +215,13 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_2_17_0)) {
out.writeOptionalString(sourceRemoteTranslogRepository);
}
// TODO: change to V_2_18_0 once this is backported into that version
if (out.getVersion().onOrAfter(Version.CURRENT)) {
out.writeOptionalString(renameAliasPattern);
}
if (out.getVersion().onOrAfter(Version.CURRENT)) {
out.writeOptionalString(renameAliasReplacement);
}
}

@Override
Expand Down Expand Up @@ -376,6 +392,51 @@ public String renameReplacement() {
return renameReplacement;
}

/**
* Sets rename pattern that should be applied to restored indices' alias.
* <p>
* Alias that match the rename pattern will be renamed according to {@link #renameAliasReplacement(String)}. The
* rename pattern is applied according to the {@link java.util.regex.Matcher#appendReplacement(StringBuffer, String)}
* If two or more aliases are renamed into the same name, they will be merged.
*
* @param renameAliasPattern rename pattern
* @return this request
*/
public RestoreSnapshotRequest renameAliasPattern(String renameAliasPattern) {
this.renameAliasPattern = renameAliasPattern;
return this;
}

/**
* Returns rename alias pattern
*
* @return rename alias pattern
*/
public String renameAliasPattern() {
return renameAliasPattern;
}

/**
* Sets rename alias replacement
* <p>
* See {@link #renameAliasPattern(String)} for more information.
*
* @param renameAliasReplacement rename replacement
*/
public RestoreSnapshotRequest renameAliasReplacement(String renameAliasReplacement) {
this.renameAliasReplacement = renameAliasReplacement;
return this;
}

/**
* Returns rename alias replacement
*
* @return rename alias replacement
*/
public String renameAliasReplacement() {
return renameAliasReplacement;
}

/**
* If this parameter is set to true the operation will wait for completion of restore process before returning.
*
Expand Down Expand Up @@ -640,6 +701,18 @@ public RestoreSnapshotRequest source(Map<String, Object> source) {
} else {
throw new IllegalArgumentException("malformed rename_replacement");
}
} else if (name.equals("rename_alias_pattern")) {
if (entry.getValue() instanceof String) {
renameAliasPattern((String) entry.getValue());
} else {
throw new IllegalArgumentException("malformed rename_alias_pattern");
}
} else if (name.equals("rename_alias_replacement")) {
if (entry.getValue() instanceof String) {
renameAliasReplacement((String) entry.getValue());
} else {
throw new IllegalArgumentException("malformed rename_alias_replacement");
}
} else if (name.equals("index_settings")) {
if (!(entry.getValue() instanceof Map)) {
throw new IllegalArgumentException("malformed index_settings section");
Expand Down Expand Up @@ -700,6 +773,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (renameReplacement != null) {
builder.field("rename_replacement", renameReplacement);
}
if (renameAliasPattern != null) {
builder.field("rename_alias_pattern", renameAliasPattern);
}
if (renameAliasReplacement != null) {
builder.field("rename_alias_replacement", renameAliasReplacement);
}
builder.field("include_global_state", includeGlobalState);
builder.field("partial", partial);
builder.field("include_aliases", includeAliases);
Expand Down Expand Up @@ -748,6 +827,8 @@ public boolean equals(Object o) {
&& Objects.equals(indicesOptions, that.indicesOptions)
&& Objects.equals(renamePattern, that.renamePattern)
&& Objects.equals(renameReplacement, that.renameReplacement)
&& Objects.equals(renameAliasPattern, that.renameAliasPattern)
&& Objects.equals(renameAliasReplacement, that.renameAliasReplacement)
&& Objects.equals(indexSettings, that.indexSettings)
&& Arrays.equals(ignoreIndexSettings, that.ignoreIndexSettings)
&& Objects.equals(snapshotUuid, that.snapshotUuid)
Expand All @@ -766,6 +847,8 @@ public int hashCode() {
indicesOptions,
renamePattern,
renameReplacement,
renameAliasPattern,
renameAliasReplacement,
waitForCompletion,
includeGlobalState,
partial,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ public RestoreSnapshotRequestBuilder setRenameReplacement(String renameReplaceme
return this;
}

/**
* Sets rename pattern that should be applied to restored indices' aliases.
* <p>
* Aliases that match the rename pattern will be renamed according to {@link #setRenameAliasReplacement(String)}. The
* rename pattern is applied according to the {@link java.util.regex.Matcher#appendReplacement(StringBuffer, String)}
* The request will fail if two or more alias will be renamed into the same name.
*
* @param renameAliasPattern rename alias pattern
* @return this builder
*/
public RestoreSnapshotRequestBuilder setRenameAliasPattern(String renameAliasPattern) {
request.renameAliasPattern(renameAliasPattern);
return this;
}

/**
* Sets rename replacement
* <p>
* See {@link #setRenameAliasPattern(String)} for more information.
*
* @param renameAliasReplacement rename alias replacement
* @return this builder
*/
public RestoreSnapshotRequestBuilder setRenameAliasReplacement(String renameAliasReplacement) {
request.renameAliasReplacement(renameAliasReplacement);
return this;
}

/**
* If this parameter is set to true the operation will wait for completion of restore process before returning.
*
Expand Down
30 changes: 24 additions & 6 deletions server/src/main/java/org/opensearch/snapshots/RestoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.Collections.unmodifiableSet;
Expand Down Expand Up @@ -498,9 +499,7 @@ public ClusterState execute(ClusterState currentState) {
// Remove all aliases - they shouldn't be restored
indexMdBuilder.removeAllAliases();
} else {
for (final String alias : snapshotIndexMetadata.getAliases().keySet()) {
aliases.add(alias);
}
applyAliasesWithRename(snapshotIndexMetadata, indexMdBuilder, aliases);
}
IndexMetadata updatedIndexMetadata = indexMdBuilder.build();
if (partial) {
Expand Down Expand Up @@ -545,9 +544,7 @@ public ClusterState execute(ClusterState currentState) {
indexMdBuilder.putAlias(alias);
}
} else {
for (final String alias : snapshotIndexMetadata.getAliases().keySet()) {
aliases.add(alias);
}
applyAliasesWithRename(snapshotIndexMetadata, indexMdBuilder, aliases);
}
final Settings.Builder indexSettingsBuilder = Settings.builder()
.put(snapshotIndexMetadata.getSettings())
Expand Down Expand Up @@ -682,6 +679,27 @@ private void checkAliasNameConflicts(Map<String, String> renamedIndices, Set<Str
}
}

private void applyAliasesWithRename(
IndexMetadata snapshotIndexMetadata,
IndexMetadata.Builder indexMdBuilder,
Set<String> aliases
) {
if (request.renameAliasPattern() == null || request.renameAliasReplacement() == null) {
aliases.addAll(snapshotIndexMetadata.getAliases().keySet());
} else {
Pattern renameAliasPattern = Pattern.compile(request.renameAliasPattern());
for (final Map.Entry<String, AliasMetadata> alias : snapshotIndexMetadata.getAliases().entrySet()) {
String currentAliasName = alias.getKey();
indexMdBuilder.removeAlias(currentAliasName);
String newAliasName = renameAliasPattern.matcher(currentAliasName)
.replaceAll(request.renameAliasReplacement());
AliasMetadata newAlias = AliasMetadata.newAliasMetadata(alias.getValue(), newAliasName);
indexMdBuilder.putAlias(newAlias);
aliases.add(newAliasName);
}
}
}

private String[] getIgnoreSettingsInternal() {
// for non-remote store enabled domain, we will remove all the remote store
// related index settings present in the snapshot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ private RestoreSnapshotRequest randomState(RestoreSnapshotRequest instance) {
if (randomBoolean()) {
instance.renameReplacement(randomUnicodeOfLengthBetween(1, 100));
}
if (randomBoolean()) {
instance.renameAliasPattern(randomUnicodeOfLengthBetween(1, 100));
}
if (randomBoolean()) {
instance.renameAliasReplacement(randomUnicodeOfLengthBetween(1, 100));
}
instance.partial(randomBoolean());
instance.includeAliases(randomBoolean());

Expand Down
Loading

0 comments on commit c34bee4

Please sign in to comment.