Search in sources :

Example 1 with TestTransportChannel

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

the class NodeJoinTests method handleFollowerCheckFrom.

private void handleFollowerCheckFrom(DiscoveryNode node, long term) throws Exception {
    final RequestHandlerRegistry<FollowersChecker.FollowerCheckRequest> followerCheckHandler = transport.getRequestHandlers().getHandler(FollowersChecker.FOLLOWER_CHECK_ACTION_NAME);
    final TestTransportChannel channel = new TestTransportChannel(new ActionListener<TransportResponse>() {

        @Override
        public void onResponse(TransportResponse transportResponse) {
        }

        @Override
        public void onFailure(Exception e) {
            fail();
        }
    });
    followerCheckHandler.processMessageReceived(new FollowersChecker.FollowerCheckRequest(term, node), channel);
    // Will throw exception if failed
    deterministicTaskQueue.runAllRunnableTasks();
    assertFalse(isLocalNodeElectedMaster());
    assertThat(coordinator.getMode(), equalTo(Coordinator.Mode.FOLLOWER));
}
Also used : TestTransportChannel(org.opensearch.transport.TestTransportChannel) TransportResponse(org.opensearch.transport.TransportResponse) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 2 with TestTransportChannel

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

the class TransportBroadcastByNodeActionTests method testOperationExecution.

public void testOperationExecution() throws Exception {
    ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX });
    Set<ShardRouting> shards = new HashSet<>();
    String nodeId = shardIt.iterator().next().currentNodeId();
    for (ShardRouting shard : shardIt) {
        if (nodeId.equals(shard.currentNodeId())) {
            shards.add(shard);
        }
    }
    final TransportBroadcastByNodeAction.BroadcastByNodeTransportRequestHandler handler = action.new BroadcastByNodeTransportRequestHandler();
    final PlainActionFuture<TransportResponse> future = PlainActionFuture.newFuture();
    TestTransportChannel channel = new TestTransportChannel(future);
    handler.messageReceived(action.new NodeRequest(nodeId, new Request(), new ArrayList<>(shards)), channel, null);
    // check the operation was executed only on the expected shards
    assertEquals(shards, action.getResults().keySet());
    TransportResponse response = future.actionGet();
    assertTrue(response instanceof TransportBroadcastByNodeAction.NodeResponse);
    TransportBroadcastByNodeAction.NodeResponse nodeResponse = (TransportBroadcastByNodeAction.NodeResponse) response;
    // check the operation was executed on the correct node
    assertEquals("node id", nodeId, nodeResponse.getNodeId());
    int successfulShards = 0;
    int failedShards = 0;
    for (Object result : action.getResults().values()) {
        if (!(result instanceof OpenSearchException)) {
            successfulShards++;
        } else {
            failedShards++;
        }
    }
    // check the operation results
    assertEquals("successful shards", successfulShards, nodeResponse.getSuccessfulShards());
    assertEquals("total shards", action.getResults().size(), nodeResponse.getTotalShards());
    assertEquals("failed shards", failedShards, nodeResponse.getExceptions().size());
    List<BroadcastShardOperationFailedException> exceptions = nodeResponse.getExceptions();
    for (BroadcastShardOperationFailedException exception : exceptions) {
        assertThat(exception.getMessage(), is("operation indices:admin/test failed"));
        assertThat(exception, hasToString(containsString("operation failed")));
    }
}
Also used : BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) IndicesRequest(org.opensearch.action.IndicesRequest) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) TransportResponse(org.opensearch.transport.TransportResponse) TestTransportChannel(org.opensearch.transport.TestTransportChannel) OpenSearchException(org.opensearch.OpenSearchException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) HashSet(java.util.HashSet)

Example 3 with TestTransportChannel

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

the class NodeJoinTests method joinNodeAsync.

private SimpleFuture joinNodeAsync(final JoinRequest joinRequest) {
    final SimpleFuture future = new SimpleFuture("join of " + joinRequest + "]");
    logger.debug("starting {}", future);
    // disco node object serialized off the network
    try {
        final RequestHandlerRegistry<JoinRequest> joinHandler = transport.getRequestHandlers().getHandler(JoinHelper.JOIN_ACTION_NAME);
        final ActionListener<TransportResponse> listener = new ActionListener<TransportResponse>() {

            @Override
            public void onResponse(TransportResponse transportResponse) {
                logger.debug("{} completed", future);
                future.markAsDone();
            }

            @Override
            public void onFailure(Exception e) {
                logger.error(() -> new ParameterizedMessage("unexpected error for {}", future), e);
                future.markAsFailed(e);
            }
        };
        joinHandler.processMessageReceived(joinRequest, new TestTransportChannel(listener));
    } catch (Exception e) {
        logger.error(() -> new ParameterizedMessage("unexpected error for {}", future), e);
        future.markAsFailed(e);
    }
    return future;
}
Also used : ActionListener(org.opensearch.action.ActionListener) TestTransportChannel(org.opensearch.transport.TestTransportChannel) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) TransportResponse(org.opensearch.transport.TransportResponse) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Example 4 with TestTransportChannel

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

the class NodeJoinTests method handleStartJoinFrom.

private void handleStartJoinFrom(DiscoveryNode node, long term) throws Exception {
    final RequestHandlerRegistry<StartJoinRequest> startJoinHandler = transport.getRequestHandlers().getHandler(JoinHelper.START_JOIN_ACTION_NAME);
    startJoinHandler.processMessageReceived(new StartJoinRequest(node, term), new TestTransportChannel(new ActionListener<TransportResponse>() {

        @Override
        public void onResponse(TransportResponse transportResponse) {
        }

        @Override
        public void onFailure(Exception e) {
            fail();
        }
    }));
    deterministicTaskQueue.runAllRunnableTasks();
    assertFalse(isLocalNodeElectedMaster());
    assertThat(coordinator.getMode(), equalTo(Coordinator.Mode.CANDIDATE));
}
Also used : ActionListener(org.opensearch.action.ActionListener) TestTransportChannel(org.opensearch.transport.TestTransportChannel) TransportResponse(org.opensearch.transport.TransportResponse) BrokenBarrierException(java.util.concurrent.BrokenBarrierException)

Aggregations

TestTransportChannel (org.opensearch.transport.TestTransportChannel)4 TransportResponse (org.opensearch.transport.TransportResponse)4 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)3 ActionListener (org.opensearch.action.ActionListener)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)1 OpenSearchException (org.opensearch.OpenSearchException)1 IndicesRequest (org.opensearch.action.IndicesRequest)1 BroadcastRequest (org.opensearch.action.support.broadcast.BroadcastRequest)1 BroadcastShardOperationFailedException (org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException)1 ShardRouting (org.opensearch.cluster.routing.ShardRouting)1 ShardsIterator (org.opensearch.cluster.routing.ShardsIterator)1 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)1