From 2d39807654f9183c06b0befd925494d5313dfc47 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Mon, 28 Jan 2019 17:50:59 +0100 Subject: [PATCH] Move IndexMetadata lookup and fix test when primary term == 1 --- .../action/shard/ShardStateAction.java | 10 ++-------- ...dStartedClusterStateTaskExecutorTests.java | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java b/server/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java index a7677bfb15f24..4419d921a3b4a 100644 --- a/server/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java +++ b/server/src/main/java/org/elasticsearch/cluster/action/shard/ShardStateAction.java @@ -552,14 +552,6 @@ public ClusterTasksResult execute(ClusterState currentState, List shardRoutingsToBeApplied = new ArrayList<>(tasks.size()); Set seenShardRoutings = new HashSet<>(); // to prevent duplicates for (StartedShardEntry task : tasks) { - final IndexMetaData indexMetaData = currentState.metaData().index(task.shardId.getIndex()); - if (indexMetaData == null) { - // tasks that correspond to non-existent indices are marked as successful - logger.debug("{} ignoring shard started task [{}] (unknown index {})", task.shardId, task, task.shardId.getIndex()); - builder.success(task); - continue; - } - final ShardRouting matched = currentState.getRoutingTable().getByAllocationId(task.shardId, task.allocationId); if (matched == null) { // tasks that correspond to non-existent shards are marked as successful. The reason is that we resend shard started @@ -570,6 +562,8 @@ public ClusterTasksResult execute(ClusterState currentState, builder.success(task); } else { if (matched.primary() && task.primaryTerm > 0) { + final IndexMetaData indexMetaData = currentState.metaData().index(task.shardId.getIndex()); + assert indexMetaData != null; final long currentPrimaryTerm = indexMetaData.primaryTerm(task.shardId.id()); if (currentPrimaryTerm != task.primaryTerm) { assert currentPrimaryTerm > task.primaryTerm : "received a primary term with a higher term than in the " + diff --git a/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java b/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java index fe36b1588b5cc..20b7548004f4a 100644 --- a/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.cluster.ESAllocationTestCase; import org.elasticsearch.cluster.action.shard.ShardStateAction.StartedShardEntry; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRoutingState; @@ -187,16 +188,22 @@ public void testDuplicateStartsAreOkay() throws Exception { public void testPrimaryTermsMismatch() throws Exception { final String indexName = "test"; - ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING, ShardRoutingState.INITIALIZING); - final IndexMetaData indexMetaData = clusterState.metaData().index(indexName); - final ShardId shardId = new ShardId(indexMetaData.getIndex(), 0); + final int shard = 0; + final int primaryTerm = 2 + randomInt(200); - final long primaryTerm = indexMetaData.primaryTerm(shardId.id()); + ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING, ShardRoutingState.INITIALIZING); + clusterState = ClusterState.builder(clusterState) + .metaData(MetaData.builder(clusterState.metaData()) + .put(IndexMetaData.builder(clusterState.metaData().index(indexName)) + .primaryTerm(shard, primaryTerm) + .build(), true) + .build()) + .build(); + final ShardId shardId = new ShardId(clusterState.metaData().index(indexName).getIndex(), shard); final String primaryAllocationId = clusterState.routingTable().shardRoutingTable(shardId).primaryShard().allocationId().getId(); { - final StartedShardEntry task = - new StartedShardEntry(shardId, primaryAllocationId, primaryTerm -1, "primary terms does not match on primary"); + new StartedShardEntry(shardId, primaryAllocationId, primaryTerm - 1, "primary terms does not match on primary"); final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, singletonList(task)); assertSame(clusterState, result.resultingState);