Skip to content

Commit

Permalink
Move IndexMetadata lookup and fix test when primary term == 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tlrx committed Jan 28, 2019
1 parent 8c19dea commit 2d39807
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,6 @@ public ClusterTasksResult<StartedShardEntry> execute(ClusterState currentState,
List<ShardRouting> shardRoutingsToBeApplied = new ArrayList<>(tasks.size());
Set<ShardRouting> 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
Expand All @@ -570,6 +562,8 @@ public ClusterTasksResult<StartedShardEntry> 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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2d39807

Please sign in to comment.