Skip to content

Commit 20bbe5b

Browse files
authored
Fix Rollover handing of hidden aliases (#53146)
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.
1 parent 5ce6de2 commit 20bbe5b

File tree

3 files changed

+139
-9
lines changed

3 files changed

+139
-9
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.elasticsearch.cluster.block.ClusterBlockException;
3737
import org.elasticsearch.cluster.block.ClusterBlockLevel;
3838
import org.elasticsearch.cluster.metadata.AliasAction;
39+
import org.elasticsearch.cluster.metadata.AliasMetaData;
3940
import org.elasticsearch.cluster.metadata.AliasOrIndex;
4041
import org.elasticsearch.cluster.metadata.IndexMetaData;
4142
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
@@ -122,7 +123,8 @@ protected void masterOperation(Task task, final RolloverRequest rolloverRequest,
122123
validate(metaData, rolloverRequest);
123124
final AliasOrIndex.Alias alias = (AliasOrIndex.Alias) metaData.getAliasAndIndexLookup().get(rolloverRequest.getAlias());
124125
final IndexMetaData indexMetaData = alias.getWriteIndex();
125-
final boolean explicitWriteIndex = Boolean.TRUE.equals(indexMetaData.getAliases().get(alias.getAliasName()).writeIndex());
126+
final AliasMetaData aliasMetaData = indexMetaData.getAliases().get(alias.getAliasName());
127+
final boolean explicitWriteIndex = Boolean.TRUE.equals(aliasMetaData.writeIndex());
126128
final String sourceProvidedName = indexMetaData.getSettings().get(IndexMetaData.SETTING_INDEX_PROVIDED_NAME,
127129
indexMetaData.getIndex().getName());
128130
final String sourceIndexName = indexMetaData.getIndex().getName();
@@ -162,7 +164,8 @@ public void onResponse(IndicesStatsResponse statsResponse) {
162164
public ClusterState execute(ClusterState currentState) throws Exception {
163165
ClusterState newState = createIndexService.applyCreateIndexRequest(currentState, createIndexRequest);
164166
newState = indexAliasesService.applyAliasActions(newState,
165-
rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, rolloverRequest, explicitWriteIndex));
167+
rolloverAliasToNewIndex(sourceIndexName, rolloverIndexName, rolloverRequest, explicitWriteIndex,
168+
aliasMetaData.isHidden()));
166169
RolloverInfo rolloverInfo = new RolloverInfo(rolloverRequest.getAlias(), metConditions,
167170
threadPool.absoluteTimeInMillis());
168171
return ClusterState.builder(newState)
@@ -210,15 +213,15 @@ public void onFailure(Exception e) {
210213
* 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)
211214
* in which case, after the rollover, the new index will need to be the explicit write index.
212215
*/
213-
static List<AliasAction> rolloverAliasToNewIndex(String oldIndex, String newIndex, RolloverRequest request,
214-
boolean explicitWriteIndex) {
216+
static List<AliasAction> rolloverAliasToNewIndex(String oldIndex, String newIndex, RolloverRequest request, boolean explicitWriteIndex,
217+
@Nullable Boolean isHidden) {
215218
if (explicitWriteIndex) {
216219
return unmodifiableList(Arrays.asList(
217-
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, true, null),
218-
new AliasAction.Add(oldIndex, request.getAlias(), null, null, null, false, null)));
220+
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, true, isHidden),
221+
new AliasAction.Add(oldIndex, request.getAlias(), null, null, null, false, isHidden)));
219222
} else {
220223
return unmodifiableList(Arrays.asList(
221-
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, null, null),
224+
new AliasAction.Add(newIndex, request.getAlias(), null, null, null, null, isHidden),
222225
new AliasAction.Remove(oldIndex, request.getAlias())));
223226
}
224227
}

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static org.hamcrest.Matchers.everyItem;
4848
import static org.hamcrest.Matchers.is;
4949
import static org.hamcrest.Matchers.lessThanOrEqualTo;
50+
import static org.hamcrest.Matchers.nullValue;
5051
import static org.hamcrest.collection.IsEmptyCollection.empty;
5152
import static org.hamcrest.core.CombinableMatcher.both;
5253
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
@@ -432,4 +433,66 @@ public void testRolloverWithClosedWriteIndex() throws Exception {
432433
assertEquals(writeIndexPrefix + "000001", rolloverResponse.getOldIndex());
433434
assertEquals(writeIndexPrefix + "000002", rolloverResponse.getNewIndex());
434435
}
436+
437+
public void testRolloverWithHiddenAliasesAndExplicitWriteIndex() {
438+
long beforeTime = client().threadPool().absoluteTimeInMillis() - 1000L;
439+
final String indexNamePrefix = "test_index_hidden-";
440+
final String firstIndexName = indexNamePrefix + "000001";
441+
final String secondIndexName = indexNamePrefix + "000002";
442+
443+
final String aliasName = "test_alias";
444+
assertAcked(prepareCreate(firstIndexName).addAlias(new Alias(aliasName).writeIndex(true).isHidden(true)).get());
445+
index(aliasName, SINGLE_MAPPING_NAME, "1", "field", "value");
446+
refresh();
447+
final RolloverResponse response = client().admin().indices().prepareRolloverIndex(aliasName).get();
448+
assertThat(response.getOldIndex(), equalTo(firstIndexName));
449+
assertThat(response.getNewIndex(), equalTo(secondIndexName));
450+
assertThat(response.isDryRun(), equalTo(false));
451+
assertThat(response.isRolledOver(), equalTo(true));
452+
assertThat(response.getConditionStatus().size(), equalTo(0));
453+
final ClusterState state = client().admin().cluster().prepareState().get().getState();
454+
final IndexMetaData oldIndex = state.metaData().index(firstIndexName);
455+
assertTrue(oldIndex.getAliases().containsKey(aliasName));
456+
assertTrue(oldIndex.getAliases().get(aliasName).isHidden());
457+
assertFalse(oldIndex.getAliases().get(aliasName).writeIndex());
458+
final IndexMetaData newIndex = state.metaData().index(secondIndexName);
459+
assertTrue(newIndex.getAliases().containsKey(aliasName));
460+
assertTrue(newIndex.getAliases().get(aliasName).isHidden());
461+
assertTrue(newIndex.getAliases().get(aliasName).writeIndex());
462+
assertThat(oldIndex.getRolloverInfos().size(), equalTo(1));
463+
assertThat(oldIndex.getRolloverInfos().get(aliasName).getAlias(), equalTo(aliasName));
464+
assertThat(oldIndex.getRolloverInfos().get(aliasName).getMetConditions(), is(empty()));
465+
assertThat(oldIndex.getRolloverInfos().get(aliasName).getTime(),
466+
is(both(greaterThanOrEqualTo(beforeTime)).and(lessThanOrEqualTo(client().threadPool().absoluteTimeInMillis() + 1000L))));
467+
}
468+
469+
public void testRolloverWithHiddenAliasesAndImplicitWriteIndex() {
470+
long beforeTime = client().threadPool().absoluteTimeInMillis() - 1000L;
471+
final String indexNamePrefix = "test_index_hidden-";
472+
final String firstIndexName = indexNamePrefix + "000001";
473+
final String secondIndexName = indexNamePrefix + "000002";
474+
475+
final String aliasName = "test_alias";
476+
assertAcked(prepareCreate(firstIndexName).addAlias(new Alias(aliasName).isHidden(true)).get());
477+
index(aliasName, SINGLE_MAPPING_NAME, "1", "field", "value");
478+
refresh();
479+
final RolloverResponse response = client().admin().indices().prepareRolloverIndex(aliasName).get();
480+
assertThat(response.getOldIndex(), equalTo(firstIndexName));
481+
assertThat(response.getNewIndex(), equalTo(secondIndexName));
482+
assertThat(response.isDryRun(), equalTo(false));
483+
assertThat(response.isRolledOver(), equalTo(true));
484+
assertThat(response.getConditionStatus().size(), equalTo(0));
485+
final ClusterState state = client().admin().cluster().prepareState().get().getState();
486+
final IndexMetaData oldIndex = state.metaData().index(firstIndexName);
487+
assertFalse(oldIndex.getAliases().containsKey(aliasName));
488+
final IndexMetaData newIndex = state.metaData().index(secondIndexName);
489+
assertTrue(newIndex.getAliases().containsKey(aliasName));
490+
assertTrue(newIndex.getAliases().get(aliasName).isHidden());
491+
assertThat(newIndex.getAliases().get(aliasName).writeIndex(), nullValue());
492+
assertThat(oldIndex.getRolloverInfos().size(), equalTo(1));
493+
assertThat(oldIndex.getRolloverInfos().get(aliasName).getAlias(), equalTo(aliasName));
494+
assertThat(oldIndex.getRolloverInfos().get(aliasName).getMetConditions(), is(empty()));
495+
assertThat(oldIndex.getRolloverInfos().get(aliasName).getTime(),
496+
is(both(greaterThanOrEqualTo(beforeTime)).and(lessThanOrEqualTo(client().threadPool().absoluteTimeInMillis() + 1000L))));
497+
}
435498
}

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@
9191
import static org.hamcrest.Matchers.containsString;
9292
import static org.hamcrest.Matchers.equalTo;
9393
import static org.hamcrest.Matchers.hasSize;
94+
import static org.hamcrest.Matchers.instanceOf;
9495
import static org.hamcrest.Matchers.is;
96+
import static org.hamcrest.Matchers.nullValue;
9597
import static org.mockito.Matchers.any;
9698
import static org.mockito.Matchers.eq;
9799
import static org.mockito.Mockito.doAnswer;
@@ -225,7 +227,7 @@ public void testRolloverAliasActions() {
225227
String targetIndex = randomAlphaOfLength(10);
226228
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
227229

228-
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, false);
230+
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, false, null);
229231
assertThat(actions, hasSize(2));
230232
boolean foundAdd = false;
231233
boolean foundRemove = false;
@@ -249,7 +251,7 @@ public void testRolloverAliasActionsWithExplicitWriteIndex() {
249251
String sourceIndex = randomAlphaOfLength(10);
250252
String targetIndex = randomAlphaOfLength(10);
251253
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
252-
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, true);
254+
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, true, null);
253255

254256
assertThat(actions, hasSize(2));
255257
boolean foundAddWrite = false;
@@ -272,6 +274,68 @@ public void testRolloverAliasActionsWithExplicitWriteIndex() {
272274
assertTrue(foundRemoveWrite);
273275
}
274276

277+
public void testRolloverAliasActionsWithHiddenAliasAndExplicitWriteIndex() {
278+
String sourceAlias = randomAlphaOfLength(10);
279+
String sourceIndex = randomAlphaOfLength(10);
280+
String targetIndex = randomAlphaOfLength(10);
281+
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
282+
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, true, true);
283+
284+
assertThat(actions, hasSize(2));
285+
boolean foundAddWrite = false;
286+
boolean foundRemoveWrite = false;
287+
for (AliasAction action : actions) {
288+
assertThat(action, instanceOf(AliasAction.Add.class));
289+
AliasAction.Add addAction = (AliasAction.Add) action;
290+
if (action.getIndex().equals(targetIndex)) {
291+
assertEquals(sourceAlias, addAction.getAlias());
292+
assertTrue(addAction.writeIndex());
293+
assertTrue(addAction.isHidden());
294+
foundAddWrite = true;
295+
} else if (action.getIndex().equals(sourceIndex)) {
296+
assertEquals(sourceAlias, addAction.getAlias());
297+
assertFalse(addAction.writeIndex());
298+
assertTrue(addAction.isHidden());
299+
foundRemoveWrite = true;
300+
} else {
301+
throw new AssertionError("Unknown index [" + action.getIndex() + "]");
302+
}
303+
}
304+
assertTrue(foundAddWrite);
305+
assertTrue(foundRemoveWrite);
306+
}
307+
308+
public void testRolloverAliasActionsWithHiddenAliasAndImplicitWriteIndex() {
309+
String sourceAlias = randomAlphaOfLength(10);
310+
String sourceIndex = randomAlphaOfLength(10);
311+
String targetIndex = randomAlphaOfLength(10);
312+
final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
313+
List<AliasAction> actions = TransportRolloverAction.rolloverAliasToNewIndex(sourceIndex, targetIndex, rolloverRequest, false, true);
314+
315+
assertThat(actions, hasSize(2));
316+
boolean foundAddWrite = false;
317+
boolean foundRemoveWrite = false;
318+
for (AliasAction action : actions) {
319+
if (action.getIndex().equals(targetIndex)) {
320+
assertThat(action, instanceOf(AliasAction.Add.class));
321+
AliasAction.Add addAction = (AliasAction.Add) action;
322+
assertEquals(sourceAlias, addAction.getAlias());
323+
assertThat(addAction.writeIndex(), nullValue());
324+
assertTrue(addAction.isHidden());
325+
foundAddWrite = true;
326+
} else if (action.getIndex().equals(sourceIndex)) {
327+
assertThat(action, instanceOf(AliasAction.Remove.class));
328+
AliasAction.Remove removeAction = (AliasAction.Remove) action;
329+
assertEquals(sourceAlias, removeAction.getAlias());
330+
foundRemoveWrite = true;
331+
} else {
332+
throw new AssertionError("Unknown index [" + action.getIndex() + "]");
333+
}
334+
}
335+
assertTrue(foundAddWrite);
336+
assertTrue(foundRemoveWrite);
337+
}
338+
275339
public void testValidation() {
276340
String index1 = randomAlphaOfLength(10);
277341
String aliasWithWriteIndex = randomAlphaOfLength(10);

0 commit comments

Comments
 (0)