Skip to content

Commit

Permalink
496: Added filters, indexRouting and searchRouting when doing rollove…
Browse files Browse the repository at this point in the history
…r on alias

Signed-off-by: Stevan Buzejic <buzejic.stevan@gmail.com>
  • Loading branch information
stevanbuzejic committed Sep 13, 2022
1 parent 54364a5 commit 3b5f890
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private RolloverResult rolloverAlias(
ClusterState newState = createIndexService.applyCreateIndexRequest(currentState, createIndexClusterStateRequest, silent);
newState = indexAliasesService.applyAliasActions(
newState,
rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, explicitWriteIndex, aliasMetadata.isHidden(), aliasName)
rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, explicitWriteIndex, aliasMetadata, aliasName)
);

RolloverInfo rolloverInfo = new RolloverInfo(aliasName, metConditions, threadPool.absoluteTimeInMillis());
Expand Down Expand Up @@ -309,20 +309,20 @@ static List<AliasAction> rolloverAliasToNewIndex(
String oldIndex,
String newIndex,
boolean explicitWriteIndex,
@Nullable Boolean isHidden,
AliasMetadata aliasMetada,
String alias
) {
if (explicitWriteIndex) {
return Collections.unmodifiableList(
Arrays.asList(
new AliasAction.Add(newIndex, alias, null, null, null, true, isHidden),
new AliasAction.Add(oldIndex, alias, null, null, null, false, isHidden)
new AliasAction.Add(newIndex, alias, aliasMetada.getFilterAsString(), aliasMetada.getIndexRouting(), aliasMetada.getSearchRouting(), true, aliasMetada.isHidden()),
new AliasAction.Add(oldIndex, alias, aliasMetada.getFilterAsString(), aliasMetada.getIndexRouting(), aliasMetada.getSearchRouting(), false, aliasMetada.isHidden())
)
);
} else {
return Collections.unmodifiableList(
Arrays.asList(
new AliasAction.Add(newIndex, alias, null, null, null, null, isHidden),
new AliasAction.Add(newIndex, alias, aliasMetada.getFilterAsString(), aliasMetada.getIndexRouting(), aliasMetada.getSearchRouting(), null, aliasMetada.isHidden()),
new AliasAction.Remove(oldIndex, alias, null)
)
);
Expand Down Expand Up @@ -385,8 +385,8 @@ static void validate(Metadata metadata, String rolloverTarget, String newIndexNa
+ indexAbstraction.getType().getDisplayName()
+ "] but one of ["
+ Strings.collectionToCommaDelimitedString(
VALID_ROLLOVER_TARGETS.stream().map(IndexAbstraction.Type::getDisplayName).collect(Collectors.toList())
)
VALID_ROLLOVER_TARGETS.stream().map(IndexAbstraction.Type::getDisplayName).collect(Collectors.toList())
)
+ "] was expected"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public String getAlias() {
return alias;
}

public String getFilter(){ return filter; }

public String getSearchRouting() { return searchRouting; }

public String getIndexRouting() { return indexRouting; }

public Boolean writeIndex() {
return writeIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public CompressedXContent getFilter() {
return filter();
}

public String getFilterAsString() {
if(filter == null){
return null;
}
return filter.string();
}

public boolean filteringRequired() {
return filter != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.opensearch.cluster.DataStreamTestHelper;
import org.opensearch.cluster.metadata.AliasAction;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.AliasMetadata.Builder;
import org.opensearch.cluster.metadata.AliasValidator;
import org.opensearch.cluster.metadata.ComponentTemplate;
import org.opensearch.cluster.metadata.ComposableIndexTemplate;
Expand Down Expand Up @@ -100,6 +101,7 @@
import java.util.Map;

import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.in;
import static org.opensearch.cluster.DataStreamTestHelper.generateMapping;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -126,7 +128,7 @@ public void testRolloverAliasActions() {
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);

List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, false, null, sourceAlias);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, false, createDefaultAliasMetadata(sourceAlias, null), sourceAlias);
assertThat(actions, hasSize(2));
boolean foundAdd = false;
boolean foundRemove = false;
Expand All @@ -149,7 +151,7 @@ public void testRolloverAliasActionsWithExplicitWriteIndex() {
String sourceAlias = randomAlphaOfLength(10);
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, true, null, sourceAlias);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, true, createDefaultAliasMetadata(sourceAlias, null), sourceAlias);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
Expand All @@ -172,11 +174,47 @@ public void testRolloverAliasActionsWithExplicitWriteIndex() {
assertTrue(foundRemoveWrite);
}

public void testRolloverAliasActionsWithFilterAndExplicitWriteIndex() {
String sourceAlias = randomAlphaOfLength(10);
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
Map filter = Collections.singletonMap(randomAlphaOfLength(2), randomAlphaOfLength(2));
String indexRouting = randomAlphaOfLength(10);
String sourceRouting = randomAlphaOfLength(10);
AliasMetadata aliasMetadata = createAliasMetadata(sourceAlias, filter, indexRouting, sourceRouting, true);

List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, true, aliasMetadata, sourceAlias);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
boolean foundRemoveWrite = false;
for (AliasAction action : actions) {
AliasAction.Add addAction = (AliasAction.Add) action;
if (action.getIndex().equals(targetIndex)) {
assertEquals(sourceAlias, addAction.getAlias());
assertEquals(aliasMetadata.getFilterAsString(), addAction.getFilter());
assertEquals(indexRouting, addAction.getIndexRouting());
assertEquals(sourceRouting, addAction.getSearchRouting());

assertTrue(addAction.writeIndex());
foundAddWrite = true;
} else if (action.getIndex().equals(sourceIndex)) {
assertEquals(sourceAlias, addAction.getAlias());
assertFalse(addAction.writeIndex());
foundRemoveWrite = true;
} else {
throw new AssertionError("Unknown index [" + action.getIndex() + "]");
}
}
assertTrue(foundAddWrite);
assertTrue(foundRemoveWrite);
}

public void testRolloverAliasActionsWithHiddenAliasAndExplicitWriteIndex() {
String sourceAlias = randomAlphaOfLength(10);
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, true, true, sourceAlias);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, true, createDefaultAliasMetadata(sourceAlias,true), sourceAlias);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
Expand All @@ -202,11 +240,49 @@ public void testRolloverAliasActionsWithHiddenAliasAndExplicitWriteIndex() {
assertTrue(foundRemoveWrite);
}

public void testRolloverAliasActionsWithFilterAndHiddenAliasAndImplicitWriteIndex() {
String sourceAlias = randomAlphaOfLength(10);
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
Map filter = Collections.singletonMap(randomAlphaOfLength(2), randomAlphaOfLength(2));
String indexRouting = randomAlphaOfLength(10);
String sourceRouting = randomAlphaOfLength(10);
AliasMetadata aliasMetadata = createAliasMetadata(sourceAlias, filter, indexRouting, sourceRouting, true);

List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, false, aliasMetadata, sourceAlias);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
boolean foundRemoveWrite = false;
for (AliasAction action : actions) {
if (action.getIndex().equals(targetIndex)) {
assertThat(action, instanceOf(AliasAction.Add.class));
AliasAction.Add addAction = (AliasAction.Add) action;
assertEquals(sourceAlias, addAction.getAlias());
assertThat(addAction.writeIndex(), nullValue());
assertTrue(addAction.isHidden());
assertEquals(aliasMetadata.getFilterAsString(), addAction.getFilter());
assertEquals(indexRouting, addAction.getIndexRouting());
assertEquals(sourceRouting, addAction.getSearchRouting());
foundAddWrite = true;
} else if (action.getIndex().equals(sourceIndex)) {
assertThat(action, instanceOf(AliasAction.Remove.class));
AliasAction.Remove removeAction = (AliasAction.Remove) action;
assertEquals(sourceAlias, removeAction.getAlias());
foundRemoveWrite = true;
} else {
throw new AssertionError("Unknown index [" + action.getIndex() + "]");
}
}
assertTrue(foundAddWrite);
assertTrue(foundRemoveWrite);
}

public void testRolloverAliasActionsWithHiddenAliasAndImplicitWriteIndex() {
String sourceAlias = randomAlphaOfLength(10);
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, false, true, sourceAlias);
List<AliasAction> actions = MetadataRolloverService.rolloverAliasToNewIndex(sourceIndex, targetIndex, false, createDefaultAliasMetadata(sourceAlias,true), sourceAlias);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
Expand Down Expand Up @@ -1010,4 +1086,18 @@ private static IndexMetadata createMetadata(String indexName) {
.settings(settings)
.build();
}

private static AliasMetadata createDefaultAliasMetadata(String alias, Boolean isHidden) {
return AliasMetadata.builder(alias).isHidden(isHidden).build();
}

private static AliasMetadata createAliasMetadata(String alias, Map filter, String indexRouting, String searchRouting, Boolean isHidden) {
return AliasMetadata
.builder(alias)
.isHidden(isHidden)
.filter(filter)
.indexRouting(indexRouting)
.searchRouting(searchRouting)
.build();
}
}

0 comments on commit 3b5f890

Please sign in to comment.