Search in sources :

Example 1 with NodeDisconnectedException

use of org.opensearch.transport.NodeDisconnectedException in project OpenSearch by opensearch-project.

the class IndicesShardStoreResponseTests method testBasicSerialization.

public void testBasicSerialization() throws Exception {
    ImmutableOpenMap.Builder<String, ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>>> indexStoreStatuses = ImmutableOpenMap.builder();
    List<IndicesShardStoresResponse.Failure> failures = new ArrayList<>();
    ImmutableOpenIntMap.Builder<List<IndicesShardStoresResponse.StoreStatus>> storeStatuses = ImmutableOpenIntMap.builder();
    DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    List<IndicesShardStoresResponse.StoreStatus> storeStatusList = new ArrayList<>();
    storeStatusList.add(new IndicesShardStoresResponse.StoreStatus(node1, null, IndicesShardStoresResponse.StoreStatus.AllocationStatus.PRIMARY, null));
    storeStatusList.add(new IndicesShardStoresResponse.StoreStatus(node2, UUIDs.randomBase64UUID(), IndicesShardStoresResponse.StoreStatus.AllocationStatus.REPLICA, null));
    storeStatusList.add(new IndicesShardStoresResponse.StoreStatus(node1, UUIDs.randomBase64UUID(), IndicesShardStoresResponse.StoreStatus.AllocationStatus.UNUSED, new IOException("corrupted")));
    storeStatuses.put(0, storeStatusList);
    storeStatuses.put(1, storeStatusList);
    ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>> storesMap = storeStatuses.build();
    indexStoreStatuses.put("test", storesMap);
    indexStoreStatuses.put("test2", storesMap);
    failures.add(new IndicesShardStoresResponse.Failure("node1", "test", 3, new NodeDisconnectedException(node1, "")));
    IndicesShardStoresResponse storesResponse = new IndicesShardStoresResponse(indexStoreStatuses.build(), Collections.unmodifiableList(failures));
    XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
    contentBuilder.startObject();
    storesResponse.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
    contentBuilder.endObject();
    BytesReference bytes = BytesReference.bytes(contentBuilder);
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) {
        Map<String, Object> map = parser.map();
        List<?> failureList = (List<?>) map.get("failures");
        assertThat(failureList.size(), equalTo(1));
        @SuppressWarnings("unchecked") Map<String, ?> failureMap = (Map<String, ?>) failureList.get(0);
        assertThat(failureMap.containsKey("index"), equalTo(true));
        assertThat(((String) failureMap.get("index")), equalTo("test"));
        assertThat(failureMap.containsKey("shard"), equalTo(true));
        assertThat(((int) failureMap.get("shard")), equalTo(3));
        assertThat(failureMap.containsKey("node"), equalTo(true));
        assertThat(((String) failureMap.get("node")), equalTo("node1"));
        @SuppressWarnings("unchecked") Map<String, Object> indices = (Map<String, Object>) map.get("indices");
        for (String index : new String[] { "test", "test2" }) {
            assertThat(indices.containsKey(index), equalTo(true));
            @SuppressWarnings("unchecked") Map<String, Object> shards = ((Map<String, Object>) ((Map<String, Object>) indices.get(index)).get("shards"));
            assertThat(shards.size(), equalTo(2));
            for (String shardId : shards.keySet()) {
                @SuppressWarnings("unchecked") Map<String, ?> shardStoresStatus = (Map<String, ?>) shards.get(shardId);
                assertThat(shardStoresStatus.containsKey("stores"), equalTo(true));
                List<?> stores = (List<?>) shardStoresStatus.get("stores");
                assertThat(stores.size(), equalTo(storeStatusList.size()));
                for (int i = 0; i < stores.size(); i++) {
                    @SuppressWarnings("unchecked") Map<String, ?> storeInfo = ((Map<String, ?>) stores.get(i));
                    IndicesShardStoresResponse.StoreStatus storeStatus = storeStatusList.get(i);
                    assertThat(((String) storeInfo.get("allocation_id")), equalTo((storeStatus.getAllocationId())));
                    assertThat(storeInfo.containsKey("allocation"), equalTo(true));
                    assertThat(((String) storeInfo.get("allocation")), equalTo(storeStatus.getAllocationStatus().value()));
                    assertThat(storeInfo.containsKey(storeStatus.getNode().getId()), equalTo(true));
                    if (storeStatus.getStoreException() != null) {
                        assertThat(storeInfo.containsKey("store_exception"), equalTo(true));
                    }
                }
            }
        }
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ArrayList(java.util.ArrayList) List(java.util.List) BytesReference(org.opensearch.common.bytes.BytesReference) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) IOException(java.io.IOException) ImmutableOpenIntMap(org.opensearch.common.collect.ImmutableOpenIntMap) Collections.emptyMap(java.util.Collections.emptyMap) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ImmutableOpenIntMap(org.opensearch.common.collect.ImmutableOpenIntMap) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with NodeDisconnectedException

use of org.opensearch.transport.NodeDisconnectedException in project OpenSearch by opensearch-project.

the class ShardStateActionTests method testMasterChannelException.

public void testMasterChannelException() throws InterruptedException {
    final String index = "test";
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger retries = new AtomicInteger();
    AtomicBoolean success = new AtomicBoolean();
    AtomicReference<Throwable> throwable = new AtomicReference<>();
    LongConsumer retryLoop = requestId -> {
        if (randomBoolean()) {
            transport.handleRemoteError(requestId, randomFrom(new NotMasterException("simulated"), new FailedToCommitClusterStateException("simulated")));
        } else {
            if (randomBoolean()) {
                transport.handleLocalError(requestId, new NodeNotConnectedException(null, "simulated"));
            } else {
                transport.handleError(requestId, new NodeDisconnectedException(null, ShardStateAction.SHARD_FAILED_ACTION_NAME));
            }
        }
    };
    final int numberOfRetries = randomIntBetween(1, 256);
    setUpMasterRetryVerification(numberOfRetries, retries, latch, retryLoop);
    ShardRouting failedShard = getRandomShardRouting(index);
    shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener<Void>() {

        @Override
        public void onResponse(Void aVoid) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            success.set(false);
            throwable.set(e);
            latch.countDown();
            assert false;
        }
    });
    final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    assertThat(capturedRequests.length, equalTo(1));
    assertFalse(success.get());
    assertThat(retries.get(), equalTo(0));
    retryLoop.accept(capturedRequests[0].requestId);
    latch.await();
    assertNull(throwable.get());
    assertThat(retries.get(), equalTo(numberOfRetries));
    assertTrue(success.get());
}
Also used : FailedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.FailedShardEntry) VersionUtils.randomCompatibleVersion(org.opensearch.test.VersionUtils.randomCompatibleVersion) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) ClusterStateCreationUtils(org.opensearch.action.support.replication.ClusterStateCreationUtils) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Matchers.nullValue(org.hamcrest.Matchers.nullValue) NotMasterException(org.opensearch.cluster.NotMasterException) ActionListener(org.opensearch.action.ActionListener) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) AfterClass(org.junit.AfterClass) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TransportResponse(org.opensearch.transport.TransportResponse) UUID(java.util.UUID) TransportService(org.opensearch.transport.TransportService) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) CountDownLatch(java.util.concurrent.CountDownLatch) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) Matchers.is(org.hamcrest.Matchers.is) TransportException(org.opensearch.transport.TransportException) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) BytesReference(org.opensearch.common.bytes.BytesReference) BeforeClass(org.junit.BeforeClass) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Writeable(org.opensearch.common.io.stream.Writeable) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) RerouteService(org.opensearch.cluster.routing.RerouteService) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) SetOnce(org.apache.lucene.util.SetOnce) TransportRequest(org.opensearch.transport.TransportRequest) IOException(java.io.IOException) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) LongConsumer(java.util.function.LongConsumer) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) Phaser(java.util.concurrent.Phaser) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NotMasterException(org.opensearch.cluster.NotMasterException) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(java.util.function.LongConsumer) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotMasterException(org.opensearch.cluster.NotMasterException) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Aggregations

IOException (java.io.IOException)2 BytesReference (org.opensearch.common.bytes.BytesReference)2 NodeDisconnectedException (org.opensearch.transport.NodeDisconnectedException)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Collections.emptyMap (java.util.Collections.emptyMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Phaser (java.util.concurrent.Phaser)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LongConsumer (java.util.function.LongConsumer)1 Predicate (java.util.function.Predicate)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 SetOnce (org.apache.lucene.util.SetOnce)1 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)1