Search in sources :

Example 1 with TransportResponse

use of org.elasticsearch.transport.TransportResponse in project elasticsearch by elastic.

the class ZenDiscoveryUnitTests method testValidateOnUnsupportedIndexVersionCreated.

public void testValidateOnUnsupportedIndexVersionCreated() throws Exception {
    final int iters = randomIntBetween(3, 10);
    for (int i = 0; i < iters; i++) {
        ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.DEFAULT);
        final DiscoveryNode otherNode = new DiscoveryNode("other_node", buildNewFakeTransportAddress(), emptyMap(), EnumSet.allOf(DiscoveryNode.Role.class), Version.CURRENT);
        MembershipAction.ValidateJoinRequestRequestHandler request = new MembershipAction.ValidateJoinRequestRequestHandler();
        final boolean incompatible = randomBoolean();
        IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(Settings.builder().put(SETTING_VERSION_CREATED, incompatible ? VersionUtils.getPreviousVersion(Version.CURRENT.minimumIndexCompatibilityVersion()) : VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)).put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())).state(IndexMetaData.State.OPEN).build();
        IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetaData.getIndex());
        RoutingTable.Builder routing = new RoutingTable.Builder();
        routing.addAsNew(indexMetaData);
        final ShardId shardId = new ShardId("test", "_na_", 0);
        IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
        final DiscoveryNode primaryNode = otherNode;
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, primaryNode.getId(), null, true, ShardRoutingState.INITIALIZING, new UnassignedInfo(UnassignedInfo.Reason.INDEX_REOPENED, "getting there")));
        indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build());
        IndexRoutingTable indexRoutingTable = indexRoutingTableBuilder.build();
        IndexMetaData updatedIndexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData);
        stateBuilder.metaData(MetaData.builder().put(updatedIndexMetaData, false).generateClusterUuidIfNeeded()).routingTable(RoutingTable.builder().add(indexRoutingTable).build());
        if (incompatible) {
            IllegalStateException ex = expectThrows(IllegalStateException.class, () -> request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), null));
            assertEquals("index [test] version not supported: " + VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion()) + " minimum compatible index version is: " + Version.CURRENT.minimumCompatibilityVersion(), ex.getMessage());
        } else {
            AtomicBoolean sendResponse = new AtomicBoolean(false);
            request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), new TransportChannel() {

                @Override
                public String action() {
                    return null;
                }

                @Override
                public String getProfileName() {
                    return null;
                }

                @Override
                public long getRequestId() {
                    return 0;
                }

                @Override
                public String getChannelType() {
                    return null;
                }

                @Override
                public void sendResponse(TransportResponse response) throws IOException {
                    sendResponse.set(true);
                }

                @Override
                public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
                }

                @Override
                public void sendResponse(Exception exception) throws IOException {
                }
            });
            assertTrue(sendResponse.get());
        }
    }
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportResponse(org.elasticsearch.transport.TransportResponse) ShardId(org.elasticsearch.index.shard.ShardId) TransportChannel(org.elasticsearch.transport.TransportChannel) ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) IOException(java.io.IOException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) TransportResponseOptions(org.elasticsearch.transport.TransportResponseOptions)

Example 2 with TransportResponse

use of org.elasticsearch.transport.TransportResponse in project elasticsearch by elastic.

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.asUnordered().iterator().next().currentNodeId();
    for (ShardRouting shard : shardIt.asUnordered()) {
        if (nodeId.equals(shard.currentNodeId())) {
            shards.add(shard);
        }
    }
    final TransportBroadcastByNodeAction.BroadcastByNodeTransportRequestHandler handler = action.new BroadcastByNodeTransportRequestHandler();
    TestTransportChannel channel = new TestTransportChannel();
    handler.messageReceived(action.new NodeRequest(nodeId, new Request(), new ArrayList<>(shards)), channel);
    // check the operation was executed only on the expected shards
    assertEquals(shards, action.getResults().keySet());
    TransportResponse response = channel.getCapturedResponse();
    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 ElasticsearchException)) {
            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 : IndicesRequest(org.elasticsearch.action.IndicesRequest) BroadcastRequest(org.elasticsearch.action.support.broadcast.BroadcastRequest) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) TransportResponse(org.elasticsearch.transport.TransportResponse) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet)

Example 3 with TransportResponse

use of org.elasticsearch.transport.TransportResponse in project elasticsearch by elastic.

the class PublishClusterStateActionTests method testOutOfOrderCommitMessages.

public void testOutOfOrderCommitMessages() throws Throwable {
    MockNode node = createMockNode("node").setAsMaster();
    final CapturingTransportChannel channel = new CapturingTransportChannel();
    List<ClusterState> states = new ArrayList<>();
    final int numOfStates = scaledRandomIntBetween(3, 25);
    for (int i = 1; i <= numOfStates; i++) {
        states.add(ClusterState.builder(node.clusterState).version(i).stateUUID(ClusterState.UNKNOWN_UUID).build());
    }
    final ClusterState finalState = states.get(numOfStates - 1);
    logger.info("--> publishing states");
    for (ClusterState state : states) {
        node.action.handleIncomingClusterStateRequest(new BytesTransportRequest(PublishClusterStateAction.serializeFullClusterState(state, Version.CURRENT), Version.CURRENT), channel);
        assertThat(channel.response.get(), equalTo((TransportResponse) TransportResponse.Empty.INSTANCE));
        assertThat(channel.error.get(), nullValue());
        channel.clear();
    }
    logger.info("--> committing states");
    long largestVersionSeen = Long.MIN_VALUE;
    Randomness.shuffle(states);
    for (ClusterState state : states) {
        node.action.handleCommitRequest(new PublishClusterStateAction.CommitClusterStateRequest(state.stateUUID()), channel);
        if (largestVersionSeen < state.getVersion()) {
            assertThat(channel.response.get(), equalTo((TransportResponse) TransportResponse.Empty.INSTANCE));
            if (channel.error.get() != null) {
                throw channel.error.get();
            }
            largestVersionSeen = state.getVersion();
        } else {
            // older cluster states will be rejected
            assertNotNull(channel.error.get());
            assertThat(channel.error.get(), instanceOf(IllegalStateException.class));
        }
        channel.clear();
    }
    //now check the last state held
    assertSameState(node.clusterState, finalState);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BytesTransportRequest(org.elasticsearch.transport.BytesTransportRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TransportResponse(org.elasticsearch.transport.TransportResponse)

Aggregations

TransportResponse (org.elasticsearch.transport.TransportResponse)3 ArrayList (java.util.ArrayList)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 IndicesRequest (org.elasticsearch.action.IndicesRequest)1 BroadcastRequest (org.elasticsearch.action.support.broadcast.BroadcastRequest)1 BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 Role (org.elasticsearch.cluster.node.DiscoveryNode.Role)1 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)1 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1 ShardsIterator (org.elasticsearch.cluster.routing.ShardsIterator)1 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)1