Search in sources :

Example 11 with ShardInfo

use of org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo in project crate by crate.

the class ReplicationOperationTests method testPrimaryFailureHandlingReplicaResponse.

@Test
public void testPrimaryFailureHandlingReplicaResponse() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    final Request request = new Request(shardId);
    final ClusterState state = stateWithActivePrimary(index, true, 1, 0);
    final IndexMetadata indexMetadata = state.getMetadata().index(index);
    final long primaryTerm = indexMetadata.primaryTerm(0);
    final ShardRouting primaryRouting = state.getRoutingTable().shardRoutingTable(shardId).primaryShard();
    final Set<String> inSyncAllocationIds = indexMetadata.inSyncAllocationIds(0);
    final IndexShardRoutingTable shardRoutingTable = state.routingTable().index(index).shard(shardId.id());
    final Set<String> trackedShards = shardRoutingTable.getAllAllocationIds();
    final ReplicationGroup initialReplicationGroup = new ReplicationGroup(shardRoutingTable, inSyncAllocationIds, trackedShards);
    final boolean fatal = randomBoolean();
    final AtomicBoolean primaryFailed = new AtomicBoolean();
    final ReplicationOperation.Primary<Request, Request, TestPrimary.Result> primary = new TestPrimary(primaryRouting, () -> initialReplicationGroup) {

        @Override
        public void failShard(String message, Exception exception) {
            primaryFailed.set(true);
        }

        @Override
        public void updateLocalCheckpointForShard(String allocationId, long checkpoint) {
            if (primaryRouting.allocationId().getId().equals(allocationId)) {
                super.updateLocalCheckpointForShard(allocationId, checkpoint);
            } else {
                if (fatal) {
                    throw new NullPointerException();
                } else {
                    throw new AlreadyClosedException("already closed");
                }
            }
        }
    };
    final PlainActionFuture<TestPrimary.Result> listener = new PlainActionFuture<>();
    final ReplicationOperation.Replicas<Request> replicas = new TestReplicaProxy(Collections.emptyMap());
    TestReplicationOperation operation = new TestReplicationOperation(request, primary, listener, replicas, primaryTerm);
    operation.execute();
    assertThat(primaryFailed.get(), equalTo(fatal));
    final ShardInfo shardInfo = listener.actionGet().getShardInfo();
    assertThat(shardInfo.getFailed(), equalTo(0));
    assertThat(shardInfo.getFailures(), arrayWithSize(0));
    assertThat(shardInfo.getSuccessful(), equalTo(1 + getExpectedReplicas(shardId, state, trackedShards).size()));
}
Also used : IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ReplicationGroup(org.elasticsearch.index.shard.ReplicationGroup) ShardId(org.elasticsearch.index.shard.ShardId) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo) ClusterState(org.elasticsearch.cluster.ClusterState) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NodeClosedException(org.elasticsearch.node.NodeClosedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IndexShardNotStartedException(org.elasticsearch.index.shard.IndexShardNotStartedException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ExecutionException(java.util.concurrent.ExecutionException) SendRequestTransportException(org.elasticsearch.transport.SendRequestTransportException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Test(org.junit.Test)

Aggregations

ShardInfo (org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)11 ShardId (org.elasticsearch.index.shard.ShardId)5 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 ExecutionException (java.util.concurrent.ExecutionException)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 UnavailableShardsException (org.elasticsearch.action.UnavailableShardsException)3 IndexShardNotStartedException (org.elasticsearch.index.shard.IndexShardNotStartedException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 ReplicationGroup (org.elasticsearch.index.shard.ReplicationGroup)2 NodeClosedException (org.elasticsearch.node.NodeClosedException)2 SendRequestTransportException (org.elasticsearch.transport.SendRequestTransportException)2