Skip to content

[7.4] Fix Rollover error when alias has closed indices (#47148) #47540

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

Merged
merged 4 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.ActiveShardCount;
Expand All @@ -49,11 +51,12 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

import java.util.Arrays;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -108,7 +111,13 @@ protected ClusterBlockException checkBlock(RolloverRequest request, ClusterState
}

@Override
protected void masterOperation(final RolloverRequest rolloverRequest, final ClusterState state,
protected void masterOperation(RolloverRequest request, ClusterState state,
ActionListener<RolloverResponse> listener) throws Exception {
throw new UnsupportedOperationException("The task parameter is required");
}

@Override
protected void masterOperation(Task task, final RolloverRequest rolloverRequest, final ClusterState state,
final ActionListener<RolloverResponse> listener) {
final MetaData metaData = state.metaData();
validate(metaData, rolloverRequest);
Expand All @@ -124,7 +133,12 @@ protected void masterOperation(final RolloverRequest rolloverRequest, final Clus
final String rolloverIndexName = indexNameExpressionResolver.resolveDateMathExpression(unresolvedName);
MetaDataCreateIndexService.validateIndexName(rolloverIndexName, state); // will fail if the index already exists
checkNoDuplicatedAliasInIndexTemplate(metaData, rolloverIndexName, rolloverRequest.getAlias());
client.admin().indices().prepareStats(rolloverRequest.getAlias()).clear().setDocs(true).execute(
IndicesStatsRequest statsRequest = new IndicesStatsRequest().indices(rolloverRequest.getAlias())
.clear()
.indicesOptions(IndicesOptions.fromOptions(true, false, true, true))
.docs(true);
statsRequest.setParentTask(clusterService.localNode().getId(), task.getId());
client.execute(IndicesStatsAction.INSTANCE, statsRequest,
new ActionListener<IndicesStatsResponse>() {
@Override
public void onResponse(IndicesStatsResponse statsResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.List;
import java.util.Set;

import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -381,4 +382,54 @@ public void testRejectIfAliasFoundInTemplate() throws Exception {
assertThat(error.getMessage(), equalTo(
"Rollover alias [logs-write] can point to multiple indices, found duplicated alias [[logs-write]] in index template [logs]"));
}

public void testRolloverWithClosedIndexInAlias() throws Exception {
final String aliasName = "alias";
final String openNonwriteIndex = "open-index-nonwrite";
final String closedIndex = "closed-index-nonwrite";
final String writeIndexPrefix = "write-index-";
assertAcked(prepareCreate(openNonwriteIndex).addAlias(new Alias(aliasName)).get());
assertAcked(prepareCreate(closedIndex).addAlias(new Alias(aliasName)).get());
assertAcked(prepareCreate(writeIndexPrefix + "000001").addAlias(new Alias(aliasName).writeIndex(true)).get());

index(closedIndex, SINGLE_MAPPING_NAME, null, "{\"foo\": \"bar\"}");
index(aliasName, SINGLE_MAPPING_NAME, null, "{\"foo\": \"bar\"}");
index(aliasName, SINGLE_MAPPING_NAME, null, "{\"foo\": \"bar\"}");
refresh(aliasName);

assertAcked(client().admin().indices().prepareClose(closedIndex).get());

RolloverResponse rolloverResponse = client().admin().indices().prepareRolloverIndex(aliasName)
.addMaxIndexDocsCondition(1)
.get();
assertTrue(rolloverResponse.isRolledOver());
assertEquals(writeIndexPrefix + "000001", rolloverResponse.getOldIndex());
assertEquals(writeIndexPrefix + "000002", rolloverResponse.getNewIndex());
}

public void testRolloverWithClosedWriteIndex() throws Exception {
final String aliasName = "alias";
final String openNonwriteIndex = "open-index-nonwrite";
final String closedIndex = "closed-index-nonwrite";
final String writeIndexPrefix = "write-index-";
assertAcked(prepareCreate(openNonwriteIndex).addAlias(new Alias(aliasName)).get());
assertAcked(prepareCreate(closedIndex).addAlias(new Alias(aliasName)).get());
assertAcked(prepareCreate(writeIndexPrefix + "000001").addAlias(new Alias(aliasName).writeIndex(true)).get());

index(closedIndex, SINGLE_MAPPING_NAME, null, "{\"foo\": \"bar\"}");
index(aliasName, SINGLE_MAPPING_NAME, null, "{\"foo\": \"bar\"}");
index(aliasName, SINGLE_MAPPING_NAME, null, "{\"foo\": \"bar\"}");
refresh(aliasName);

assertAcked(client().admin().indices().prepareClose(closedIndex).get());
assertAcked(client().admin().indices().prepareClose(writeIndexPrefix + "000001").get());
ensureGreen(aliasName);

RolloverResponse rolloverResponse = client().admin().indices().prepareRolloverIndex(aliasName)
.addMaxIndexDocsCondition(1)
.get();
assertTrue(rolloverResponse.isRolledOver());
assertEquals(writeIndexPrefix + "000001", rolloverResponse.getOldIndex());
assertEquals(writeIndexPrefix + "000002", rolloverResponse.getNewIndex());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest;
import org.elasticsearch.action.admin.indices.stats.CommonStats;
import org.elasticsearch.action.admin.indices.stats.IndexStats;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsTests;
import org.elasticsearch.action.admin.indices.stats.ShardStats;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.AliasAction;
Expand All @@ -45,6 +44,7 @@
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService;
import org.elasticsearch.cluster.metadata.MetaDataIndexAliasesService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.RecoverySource;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.UnassignedInfo;
Expand All @@ -71,6 +71,7 @@
import org.elasticsearch.index.store.StoreStats;
import org.elasticsearch.index.warmer.WarmerStats;
import org.elasticsearch.search.suggest.completion.CompletionStats;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
Expand All @@ -92,6 +93,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -366,9 +368,12 @@ public void testRejectDuplicateAlias() {
assertThat(ex.getMessage(), containsString("index template [test-template]"));
}

public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPrimariesFromWriteIndex() {
public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPrimariesFromWriteIndex() throws Exception {
final TransportService mockTransportService = mock(TransportService.class);
final ClusterService mockClusterService = mock(ClusterService.class);
final DiscoveryNode mockNode = mock(DiscoveryNode.class);
when(mockNode.getId()).thenReturn("mocknode");
when(mockClusterService.localNode()).thenReturn(mockNode);
final ThreadPool mockThreadPool = mock(ThreadPool.class);
final MetaDataCreateIndexService mockCreateIndexService = mock(MetaDataCreateIndexService.class);
final IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
Expand All @@ -377,31 +382,23 @@ public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPr
final MetaDataIndexAliasesService mdIndexAliasesService = mock(MetaDataIndexAliasesService.class);

final Client mockClient = mock(Client.class);
final AdminClient mockAdminClient = mock(AdminClient.class);
final IndicesAdminClient mockIndicesAdminClient = mock(IndicesAdminClient.class);
when(mockClient.admin()).thenReturn(mockAdminClient);
when(mockAdminClient.indices()).thenReturn(mockIndicesAdminClient);

final IndicesStatsRequestBuilder mockIndicesStatsBuilder = mock(IndicesStatsRequestBuilder.class);
when(mockIndicesAdminClient.prepareStats(any())).thenReturn(mockIndicesStatsBuilder);
final Map<String, IndexStats> indexStats = new HashMap<>();
int total = randomIntBetween(500, 1000);
indexStats.put("logs-index-000001", createIndexStats(200L, total));
indexStats.put("logs-index-000002", createIndexStats(300L, total));
final IndicesStatsResponse statsResponse = createAliasToMultipleIndicesStatsResponse(indexStats);
when(mockIndicesStatsBuilder.clear()).thenReturn(mockIndicesStatsBuilder);
when(mockIndicesStatsBuilder.setDocs(true)).thenReturn(mockIndicesStatsBuilder);

assert statsResponse.getPrimaries().getDocs().getCount() == 500L;
assert statsResponse.getTotal().getDocs().getCount() == (total + total);

doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assert args.length == 1;
ActionListener<IndicesStatsResponse> listener = (ActionListener<IndicesStatsResponse>) args[0];
assert args.length == 3;
ActionListener<IndicesStatsResponse> listener = (ActionListener<IndicesStatsResponse>) args[2];
listener.onResponse(statsResponse);
return null;
}).when(mockIndicesStatsBuilder).execute(any(ActionListener.class));
}).when(mockClient).execute(eq(IndicesStatsAction.INSTANCE), any(ActionRequest.class), any(ActionListener.class));

assert statsResponse.getPrimaries().getDocs().getCount() == 500L;
assert statsResponse.getTotal().getDocs().getCount() == (total + total);

final IndexMetaData.Builder indexMetaData = IndexMetaData.builder("logs-index-000001")
.putAlias(AliasMetaData.builder("logs-alias").writeIndex(false).build()).settings(settings(Version.CURRENT))
Expand All @@ -422,7 +419,7 @@ public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPr
RolloverRequest rolloverRequest = new RolloverRequest("logs-alias", "logs-index-000003");
rolloverRequest.addMaxIndexDocsCondition(500L);
rolloverRequest.dryRun(true);
transportRolloverAction.masterOperation(rolloverRequest, stateBefore, future);
transportRolloverAction.masterOperation(mock(Task.class), rolloverRequest, stateBefore, future);

RolloverResponse response = future.actionGet();
assertThat(response.getOldIndex(), equalTo("logs-index-000002"));
Expand All @@ -438,7 +435,7 @@ public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPr
rolloverRequest = new RolloverRequest("logs-alias", "logs-index-000003");
rolloverRequest.addMaxIndexDocsCondition(300L);
rolloverRequest.dryRun(true);
transportRolloverAction.masterOperation(rolloverRequest, stateBefore, future);
transportRolloverAction.masterOperation(mock(Task.class), rolloverRequest, stateBefore, future);

response = future.actionGet();
assertThat(response.getOldIndex(), equalTo("logs-index-000002"));
Expand Down