Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new parameters to snapshot restore to rename the restored aliases… #16292

Merged
merged 13 commits into from
Oct 23, 2024
Merged
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ 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))
- Latency and Memory allocation improvements to Multi Term Aggregation queries ([#14993](https://github.com/opensearch-project/OpenSearch/pull/14993))
- Flat object field use IndexOrDocValuesQuery to optimize query ([#14383](https://github.com/opensearch-project/OpenSearch/issues/14383))
- Add support for renaming aliases during snapshot restore ([#16292](https://github.com/opensearch-project/OpenSearch/pull/16292))
- Add method to return dynamic SecureTransportParameters from SecureTransportSettingsProvider interface ([#16387](https://github.com/opensearch-project/OpenSearch/pull/16387))
- URI path filtering support in cluster stats API ([#15938](https://github.com/opensearch-project/OpenSearch/pull/15938))
- [Star Tree - Search] Add support for metric aggregations with/without term query ([15289](https://github.com/opensearch-project/OpenSearch/pull/15289))

### Dependencies
- Bump `com.azure:azure-identity` from 1.13.0 to 1.13.2 ([#15578](https://github.com/opensearch-project/OpenSearch/pull/15578))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
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 @@ -164,6 +166,13 @@
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 @@ -191,6 +200,13 @@
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 @@ -361,6 +377,51 @@
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 @@ -625,6 +686,18 @@
} 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");

Check warning on line 693 in server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L693 was not covered by tests
}
} else if (name.equals("rename_alias_replacement")) {
if (entry.getValue() instanceof String) {
renameAliasReplacement((String) entry.getValue());
} else {
throw new IllegalArgumentException("malformed rename_alias_replacement");

Check warning on line 699 in server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L699 was not covered by tests
}
} else if (name.equals("index_settings")) {
if (!(entry.getValue() instanceof Map)) {
throw new IllegalArgumentException("malformed index_settings section");
Expand Down Expand Up @@ -685,6 +758,12 @@
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 @@ -733,6 +812,8 @@
&& 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 @@ -751,6 +832,8 @@
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 @@
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;

Check warning on line 159 in server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestBuilder.java#L158-L159

Added lines #L158 - L159 were not covered by tests
}

/**
* 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;

Check warning on line 172 in server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestBuilder.java#L171-L172

Added lines #L171 - L172 were not covered by tests
}

/**
* If this parameter is set to true the operation will wait for completion of restore process before returning.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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 @@ -486,9 +487,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 @@ -533,9 +532,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 @@ -665,6 +662,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
Loading