Search in sources :

Example 6 with ReplicationGroup

use of org.elasticsearch.index.shard.ReplicationGroup in project crate by crate.

the class ReplicationOperationTests method testAddedReplicaAfterPrimaryOperation.

@Test
public void testAddedReplicaAfterPrimaryOperation() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    final ClusterState initialState = stateWithActivePrimary(index, true, 0);
    Set<String> inSyncAllocationIds = initialState.metadata().index(index).inSyncAllocationIds(0);
    IndexShardRoutingTable shardRoutingTable = initialState.getRoutingTable().shardRoutingTable(shardId);
    Set<String> trackedShards = new HashSet<>();
    addTrackingInfo(shardRoutingTable, null, trackedShards, new HashSet<>());
    ReplicationGroup initialReplicationGroup = new ReplicationGroup(shardRoutingTable, inSyncAllocationIds, trackedShards);
    final ClusterState stateWithAddedReplicas;
    if (randomBoolean()) {
        stateWithAddedReplicas = state(index, true, ShardRoutingState.STARTED, randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.STARTED);
    } else {
        stateWithAddedReplicas = state(index, true, ShardRoutingState.RELOCATING);
    }
    inSyncAllocationIds = stateWithAddedReplicas.metadata().index(index).inSyncAllocationIds(0);
    shardRoutingTable = stateWithAddedReplicas.getRoutingTable().shardRoutingTable(shardId);
    trackedShards = new HashSet<>();
    addTrackingInfo(shardRoutingTable, null, trackedShards, new HashSet<>());
    ReplicationGroup updatedReplicationGroup = new ReplicationGroup(shardRoutingTable, inSyncAllocationIds, trackedShards);
    final AtomicReference<ReplicationGroup> replicationGroup = new AtomicReference<>(initialReplicationGroup);
    logger.debug("--> using initial replicationGroup:\n{}", replicationGroup.get());
    final long primaryTerm = initialState.getMetadata().index(shardId.getIndexName()).primaryTerm(shardId.id());
    final ShardRouting primaryShard = updatedReplicationGroup.getRoutingTable().primaryShard();
    final TestPrimary primary = new TestPrimary(primaryShard, replicationGroup::get) {

        @Override
        public void perform(Request request, ActionListener<Result> listener) {
            super.perform(request, ActionListener.map(listener, result -> {
                replicationGroup.set(updatedReplicationGroup);
                logger.debug("--> state after primary operation:\n{}", replicationGroup.get());
                return result;
            }));
        }
    };
    Request request = new Request(shardId);
    PlainActionFuture<TestPrimary.Result> listener = new PlainActionFuture<>();
    final TestReplicationOperation op = new TestReplicationOperation(request, primary, listener, new TestReplicaProxy(), primaryTerm);
    op.execute();
    assertThat("request was not processed on primary", request.processedOnPrimary.get(), equalTo(true));
    Set<ShardRouting> expectedReplicas = getExpectedReplicas(shardId, stateWithAddedReplicas, trackedShards);
    assertThat(request.processedOnReplicas, equalTo(expectedReplicas));
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) ClusterStateCreationUtils.state(org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) NodeClosedException(org.elasticsearch.node.NodeClosedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) ArrayList(java.util.ArrayList) Sets(io.crate.common.collections.Sets) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ClusterStateCreationUtils.stateWithActivePrimary(org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithActivePrimary) ESTestCase(org.elasticsearch.test.ESTestCase) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) IndexShardState(org.elasticsearch.index.shard.IndexShardState) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Set(java.util.Set) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) Test(org.junit.Test) IndexShardNotStartedException(org.elasticsearch.index.shard.IndexShardNotStartedException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) ExecutionException(java.util.concurrent.ExecutionException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) SendRequestTransportException(org.elasticsearch.transport.SendRequestTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ReplicationGroup(org.elasticsearch.index.shard.ReplicationGroup) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReplicationGroup(org.elasticsearch.index.shard.ReplicationGroup) ShardId(org.elasticsearch.index.shard.ShardId) ActionListener(org.elasticsearch.action.ActionListener) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ReplicationGroup

use of org.elasticsearch.index.shard.ReplicationGroup 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

ReplicationGroup (org.elasticsearch.index.shard.ReplicationGroup)7 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)6 ClusterState (org.elasticsearch.cluster.ClusterState)6 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)6 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)6 ShardId (org.elasticsearch.index.shard.ShardId)6 Test (org.junit.Test)6 ExecutionException (java.util.concurrent.ExecutionException)5 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)5 UnavailableShardsException (org.elasticsearch.action.UnavailableShardsException)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 NodeClosedException (org.elasticsearch.node.NodeClosedException)5 HashSet (java.util.HashSet)4 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)4 IndexShardNotStartedException (org.elasticsearch.index.shard.IndexShardNotStartedException)4 SendRequestTransportException (org.elasticsearch.transport.SendRequestTransportException)4 HashMap (java.util.HashMap)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ActionListener (org.elasticsearch.action.ActionListener)3 ShardInfo (org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)3