Skip to content

Commit

Permalink
Fix Rollover handing of hidden aliases (#53146)
Browse files Browse the repository at this point in the history
Prior to this commit, rollover did not propagate the `is_hidden` alias
property when rollover over an index. This commit ensures that an alias
that's rollover over will remain hidden.
  • Loading branch information
gwbrown authored Mar 10, 2020
1 parent 5ce6de2 commit 20bbe5b
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.AliasOrIndex;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
Expand Down Expand Up @@ -122,7 +123,8 @@ protected void masterOperation(Task task, final RolloverRequest rolloverRequest,
validate(metaData, rolloverRequest);
final AliasOrIndex.Alias alias = (AliasOrIndex.Alias) metaData.getAliasAndIndexLookup().get(rolloverRequest.getAlias());
final IndexMetaData indexMetaData = alias.getWriteIndex();
final boolean explicitWriteIndex = Boolean.TRUE.equals(indexMetaData.getAliases().get(alias.getAliasName()).writeIndex());
final AliasMetaData aliasMetaData = indexMetaData.getAliases().get(alias.getAliasName());
final boolean explicitWriteIndex = Boolean.TRUE.equals(aliasMetaData.writeIndex());
final String sourceProvidedName = indexMetaData.getSettings().get(IndexMetaData.SETTING_INDEX_PROVIDED_NAME,
indexMetaData.getIndex().getName());
final String sourceIndexName = indexMetaData.getIndex().getName();
Expand Down Expand Up @@ -162,7 +164,8 @@ public void onResponse(IndicesStatsResponse statsResponse) {
public ClusterState execute(ClusterState currentState) throws Exception {
ClusterState newState = createIndexService.applyCreateIndexRequest(currentState, createIndexRequest);
newState = indexAliasesService.applyAliasActions(newState,
rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, rolloverRequest, explicitWriteIndex));
rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, rolloverRequest, explicitWriteIndex,
aliasMetaData.isHidden()));
RolloverInfo rolloverInfo = new RolloverInfo(rolloverRequest.getAlias(), metConditions,
threadPool.absoluteTimeInMillis());
return ClusterState.builder(newState)
Expand Down Expand Up @@ -210,15 +213,15 @@ public void onFailure(Exception e) {
* alias pointing to multiple indices will have to be an explicit write index (ie. the old index alias has is_write_index set to true)
* in which case, after the rollover, the new index will need to be the explicit write index.
*/
static List<AliasAction> rolloverAliasToNewIndex(String oldIndex, String newIndex, RolloverRequest request,
boolean explicitWriteIndex) {
static List<AliasAction> rolloverAliasToNewIndex(String oldIndex, String newIndex, RolloverRequest request, boolean explicitWriteIndex,
@Nullable Boolean isHidden) {
if (explicitWriteIndex) {
return unmodifiableList(Arrays.asList(
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, true, null),
new AliasAction.Add(oldIndex, request.getAlias(), null, null, null, false, null)));
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, true, isHidden),
new AliasAction.Add(oldIndex, request.getAlias(), null, null, null, false, isHidden)));
} else {
return unmodifiableList(Arrays.asList(
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, null, null),
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, null, isHidden),
new AliasAction.Remove(oldIndex, request.getAlias())));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.core.CombinableMatcher.both;
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
Expand Down Expand Up @@ -432,4 +433,66 @@ public void testRolloverWithClosedWriteIndex() throws Exception {
assertEquals(writeIndexPrefix + "000001", rolloverResponse.getOldIndex());
assertEquals(writeIndexPrefix + "000002", rolloverResponse.getNewIndex());
}

public void testRolloverWithHiddenAliasesAndExplicitWriteIndex() {
long beforeTime = client().threadPool().absoluteTimeInMillis() - 1000L;
final String indexNamePrefix = "test_index_hidden-";
final String firstIndexName = indexNamePrefix + "000001";
final String secondIndexName = indexNamePrefix + "000002";

final String aliasName = "test_alias";
assertAcked(prepareCreate(firstIndexName).addAlias(new Alias(aliasName).writeIndex(true).isHidden(true)).get());
index(aliasName, SINGLE_MAPPING_NAME, "1", "field", "value");
refresh();
final RolloverResponse response = client().admin().indices().prepareRolloverIndex(aliasName).get();
assertThat(response.getOldIndex(), equalTo(firstIndexName));
assertThat(response.getNewIndex(), equalTo(secondIndexName));
assertThat(response.isDryRun(), equalTo(false));
assertThat(response.isRolledOver(), equalTo(true));
assertThat(response.getConditionStatus().size(), equalTo(0));
final ClusterState state = client().admin().cluster().prepareState().get().getState();
final IndexMetaData oldIndex = state.metaData().index(firstIndexName);
assertTrue(oldIndex.getAliases().containsKey(aliasName));
assertTrue(oldIndex.getAliases().get(aliasName).isHidden());
assertFalse(oldIndex.getAliases().get(aliasName).writeIndex());
final IndexMetaData newIndex = state.metaData().index(secondIndexName);
assertTrue(newIndex.getAliases().containsKey(aliasName));
assertTrue(newIndex.getAliases().get(aliasName).isHidden());
assertTrue(newIndex.getAliases().get(aliasName).writeIndex());
assertThat(oldIndex.getRolloverInfos().size(), equalTo(1));
assertThat(oldIndex.getRolloverInfos().get(aliasName).getAlias(), equalTo(aliasName));
assertThat(oldIndex.getRolloverInfos().get(aliasName).getMetConditions(), is(empty()));
assertThat(oldIndex.getRolloverInfos().get(aliasName).getTime(),
is(both(greaterThanOrEqualTo(beforeTime)).and(lessThanOrEqualTo(client().threadPool().absoluteTimeInMillis() + 1000L))));
}

public void testRolloverWithHiddenAliasesAndImplicitWriteIndex() {
long beforeTime = client().threadPool().absoluteTimeInMillis() - 1000L;
final String indexNamePrefix = "test_index_hidden-";
final String firstIndexName = indexNamePrefix + "000001";
final String secondIndexName = indexNamePrefix + "000002";

final String aliasName = "test_alias";
assertAcked(prepareCreate(firstIndexName).addAlias(new Alias(aliasName).isHidden(true)).get());
index(aliasName, SINGLE_MAPPING_NAME, "1", "field", "value");
refresh();
final RolloverResponse response = client().admin().indices().prepareRolloverIndex(aliasName).get();
assertThat(response.getOldIndex(), equalTo(firstIndexName));
assertThat(response.getNewIndex(), equalTo(secondIndexName));
assertThat(response.isDryRun(), equalTo(false));
assertThat(response.isRolledOver(), equalTo(true));
assertThat(response.getConditionStatus().size(), equalTo(0));
final ClusterState state = client().admin().cluster().prepareState().get().getState();
final IndexMetaData oldIndex = state.metaData().index(firstIndexName);
assertFalse(oldIndex.getAliases().containsKey(aliasName));
final IndexMetaData newIndex = state.metaData().index(secondIndexName);
assertTrue(newIndex.getAliases().containsKey(aliasName));
assertTrue(newIndex.getAliases().get(aliasName).isHidden());
assertThat(newIndex.getAliases().get(aliasName).writeIndex(), nullValue());
assertThat(oldIndex.getRolloverInfos().size(), equalTo(1));
assertThat(oldIndex.getRolloverInfos().get(aliasName).getAlias(), equalTo(aliasName));
assertThat(oldIndex.getRolloverInfos().get(aliasName).getMetConditions(), is(empty()));
assertThat(oldIndex.getRolloverInfos().get(aliasName).getTime(),
is(both(greaterThanOrEqualTo(beforeTime)).and(lessThanOrEqualTo(client().threadPool().absoluteTimeInMillis() + 1000L))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -225,7 +227,7 @@ public void testRolloverAliasActions() {
String targetIndex = randomAlphaOfLength(10);
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);

List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, false);
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, false, null);
assertThat(actions, hasSize(2));
boolean foundAdd = false;
boolean foundRemove = false;
Expand All @@ -249,7 +251,7 @@ public void testRolloverAliasActionsWithExplicitWriteIndex() {
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, true);
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, true, null);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
Expand All @@ -272,6 +274,68 @@ public void testRolloverAliasActionsWithExplicitWriteIndex() {
assertTrue(foundRemoveWrite);
}

public void testRolloverAliasActionsWithHiddenAliasAndExplicitWriteIndex() {
String sourceAlias = randomAlphaOfLength(10);
String sourceIndex = randomAlphaOfLength(10);
String targetIndex = randomAlphaOfLength(10);
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, true, true);

assertThat(actions, hasSize(2));
boolean foundAddWrite = false;
boolean foundRemoveWrite = false;
for (AliasAction action : actions) {
assertThat(action, instanceOf(AliasAction.Add.class));
AliasAction.Add addAction = (AliasAction.Add) action;
if (action.getIndex().equals(targetIndex)) {
assertEquals(sourceAlias, addAction.getAlias());
assertTrue(addAction.writeIndex());
assertTrue(addAction.isHidden());
foundAddWrite = true;
} else if (action.getIndex().equals(sourceIndex)) {
assertEquals(sourceAlias, addAction.getAlias());
assertFalse(addAction.writeIndex());
assertTrue(addAction.isHidden());
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);
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, false, true);

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());
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 testValidation() {
String index1 = randomAlphaOfLength(10);
String aliasWithWriteIndex = randomAlphaOfLength(10);
Expand Down

0 comments on commit 20bbe5b

Please sign in to comment.