Search in sources :

Example 1 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project OpenSearch by opensearch-project.

the class ZenDiscoveryIT method testHandleNodeJoin_incompatibleClusterState.

public void testHandleNodeJoin_incompatibleClusterState() throws InterruptedException, ExecutionException, TimeoutException {
    String masterNode = internalCluster().startMasterOnlyNode();
    String node1 = internalCluster().startNode();
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class, node1);
    Coordinator coordinator = (Coordinator) internalCluster().getInstance(Discovery.class, masterNode);
    final ClusterState state = clusterService.state();
    Metadata.Builder mdBuilder = Metadata.builder(state.metadata());
    mdBuilder.putCustom(CustomMetadata.TYPE, new CustomMetadata("data"));
    ClusterState stateWithCustomMetadata = ClusterState.builder(state).metadata(mdBuilder).build();
    final CompletableFuture<Throwable> future = new CompletableFuture<>();
    DiscoveryNode node = state.nodes().getLocalNode();
    coordinator.sendValidateJoinRequest(stateWithCustomMetadata, new JoinRequest(node, 0L, Optional.empty()), new JoinHelper.JoinCallback() {

        @Override
        public void onSuccess() {
            future.completeExceptionally(new AssertionError("onSuccess should not be called"));
        }

        @Override
        public void onFailure(Exception e) {
            future.complete(e);
        }
    });
    Throwable t = future.get(10, TimeUnit.SECONDS);
    assertTrue(t instanceof IllegalStateException);
    assertTrue(t.getCause() instanceof RemoteTransportException);
    assertTrue(t.getCause().getCause() instanceof IllegalArgumentException);
    assertThat(t.getCause().getCause().getMessage(), containsString("Unknown NamedWriteable"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) RemoteTransportException(org.opensearch.transport.RemoteTransportException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Discovery(org.opensearch.discovery.Discovery) Metadata(org.opensearch.cluster.metadata.Metadata) TestCustomMetadata(org.opensearch.test.TestCustomMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeoutException(java.util.concurrent.TimeoutException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterService(org.opensearch.cluster.service.ClusterService) TestCustomMetadata(org.opensearch.test.TestCustomMetadata)

Example 2 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project OpenSearch by opensearch-project.

the class ClusterRerouteIT method testClusterRerouteWithBlocks.

public void testClusterRerouteWithBlocks() {
    List<String> nodesIds = internalCluster().startNodes(2);
    logger.info("--> create an index with 1 shard and 0 replicas");
    createIndex("test-blocks", Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build());
    if (randomBoolean()) {
        assertAcked(client().admin().indices().prepareClose("test-blocks"));
    }
    ensureGreen("test-blocks");
    logger.info("--> check that the index has 1 shard");
    ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
    List<ShardRouting> shards = state.routingTable().allShards("test-blocks");
    assertThat(shards, hasSize(1));
    logger.info("--> check that the shard is allocated");
    ShardRouting shard = shards.get(0);
    assertThat(shard.assignedToNode(), equalTo(true));
    logger.info("--> retrieve the node where the shard is allocated");
    DiscoveryNode node = state.nodes().resolveNode(shard.currentNodeId());
    assertNotNull(node);
    // toggle is used to mve the shard from one node to another
    int toggle = nodesIds.indexOf(node.getName());
    // Rerouting shards is not blocked
    for (String blockSetting : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY, SETTING_BLOCKS_METADATA, SETTING_READ_ONLY_ALLOW_DELETE)) {
        try {
            enableIndexBlock("test-blocks", blockSetting);
            assertAcked(client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test-blocks", 0, nodesIds.get(toggle % 2), nodesIds.get(++toggle % 2))));
            ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setIndices("test-blocks").setWaitForYellowStatus().setWaitForNoRelocatingShards(true).execute().actionGet();
            assertThat(healthResponse.isTimedOut(), equalTo(false));
        } finally {
            disableIndexBlock("test-blocks", blockSetting);
        }
    }
    // Rerouting shards is blocked when the cluster is read only
    try {
        setClusterReadOnly(true);
        assertBlocked(client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test-blocks", 1, nodesIds.get(toggle % 2), nodesIds.get(++toggle % 2))));
    } finally {
        setClusterReadOnly(false);
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) MoveAllocationCommand(org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 3 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project OpenSearch by opensearch-project.

the class VotingConfigurationIT method testElectsNodeNotInVotingConfiguration.

public void testElectsNodeNotInVotingConfiguration() throws Exception {
    internalCluster().setBootstrapMasterNodeIndex(0);
    final List<String> nodeNames = internalCluster().startNodes(4);
    // a 4-node cluster settles on a 3-node configuration; we then prevent the nodes in the configuration from winning an election
    // by failing at the pre-voting stage, so that the extra node must be elected instead when the master shuts down. This extra node
    // should then add itself into the voting configuration.
    assertFalse(internalCluster().client().admin().cluster().prepareHealth().setWaitForNodes("4").setWaitForEvents(Priority.LANGUID).get().isTimedOut());
    String excludedNodeName = null;
    final ClusterState clusterState = internalCluster().client().admin().cluster().prepareState().clear().setNodes(true).setMetadata(true).get().getState();
    final Set<String> votingConfiguration = clusterState.getLastCommittedConfiguration().getNodeIds();
    assertThat(votingConfiguration, hasSize(3));
    assertThat(clusterState.nodes().getSize(), equalTo(4));
    assertThat(votingConfiguration, hasItem(clusterState.nodes().getMasterNodeId()));
    for (DiscoveryNode discoveryNode : clusterState.nodes()) {
        if (votingConfiguration.contains(discoveryNode.getId()) == false) {
            assertThat(excludedNodeName, nullValue());
            excludedNodeName = discoveryNode.getName();
        }
    }
    for (final String sender : nodeNames) {
        if (sender.equals(excludedNodeName)) {
            continue;
        }
        final MockTransportService senderTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, sender);
        for (final String receiver : nodeNames) {
            senderTransportService.addSendBehavior(internalCluster().getInstance(TransportService.class, receiver), (connection, requestId, action, request, options) -> {
                if (action.equals(PreVoteCollector.REQUEST_PRE_VOTE_ACTION_NAME)) {
                    throw new OpenSearchException("rejected");
                }
                connection.sendRequest(requestId, action, request, options);
            });
        }
    }
    internalCluster().stopCurrentMasterNode();
    assertFalse(internalCluster().client().admin().cluster().prepareHealth().setWaitForNodes("3").setWaitForEvents(Priority.LANGUID).get().isTimedOut());
    final ClusterState newClusterState = internalCluster().client().admin().cluster().prepareState().clear().setNodes(true).setMetadata(true).get().getState();
    assertThat(newClusterState.nodes().getMasterNode().getName(), equalTo(excludedNodeName));
    assertThat(newClusterState.getLastCommittedConfiguration().getNodeIds(), hasItem(newClusterState.nodes().getMasterNodeId()));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) MockTransportService(org.opensearch.test.transport.MockTransportService) TransportService(org.opensearch.transport.TransportService) OpenSearchException(org.opensearch.OpenSearchException)

Example 4 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project OpenSearch by opensearch-project.

the class SearchScrollAsyncAction method run.

private void run(BiFunction<String, String, DiscoveryNode> clusterNodeLookup, final SearchContextIdForNode[] context) {
    final CountDown counter = new CountDown(scrollId.getContext().length);
    for (int i = 0; i < context.length; i++) {
        SearchContextIdForNode target = context[i];
        final int shardIndex = i;
        final Transport.Connection connection;
        try {
            DiscoveryNode node = clusterNodeLookup.apply(target.getClusterAlias(), target.getNode());
            if (node == null) {
                throw new IllegalStateException("node [" + target.getNode() + "] is not available");
            }
            connection = getConnection(target.getClusterAlias(), node);
        } catch (Exception ex) {
            onShardFailure("query", counter, target.getSearchContextId(), ex, null, () -> SearchScrollAsyncAction.this.moveToNextPhase(clusterNodeLookup));
            continue;
        }
        final InternalScrollSearchRequest internalRequest = TransportSearchHelper.internalScrollSearchRequest(target.getSearchContextId(), request);
        // we can't create a SearchShardTarget here since we don't know the index and shard ID we are talking to
        // we only know the node and the search context ID. Yet, the response will contain the SearchShardTarget
        // from the target node instead...that's why we pass null here
        SearchActionListener<T> searchActionListener = new SearchActionListener<T>(null, shardIndex) {

            @Override
            protected void setSearchShardTarget(T response) {
                // don't do this - it's part of the response...
                assert response.getSearchShardTarget() != null : "search shard target must not be null";
                if (target.getClusterAlias() != null) {
                    // re-create the search target and add the cluster alias if there is any,
                    // we need this down the road for subseq. phases
                    SearchShardTarget searchShardTarget = response.getSearchShardTarget();
                    response.setSearchShardTarget(new SearchShardTarget(searchShardTarget.getNodeId(), searchShardTarget.getShardId(), target.getClusterAlias(), null));
                }
            }

            @Override
            protected void innerOnResponse(T result) {
                assert shardIndex == result.getShardIndex() : "shard index mismatch: " + shardIndex + " but got: " + result.getShardIndex();
                onFirstPhaseResult(shardIndex, result);
                if (counter.countDown()) {
                    SearchPhase phase = moveToNextPhase(clusterNodeLookup);
                    try {
                        phase.run();
                    } catch (Exception e) {
                        // we need to fail the entire request here - the entire phase just blew up
                        // don't call onShardFailure or onFailure here since otherwise we'd countDown the counter
                        // again which would result in an exception
                        listener.onFailure(new SearchPhaseExecutionException(phase.getName(), "Phase failed", e, ShardSearchFailure.EMPTY_ARRAY));
                    }
                }
            }

            @Override
            public void onFailure(Exception t) {
                onShardFailure("query", counter, target.getSearchContextId(), t, null, () -> SearchScrollAsyncAction.this.moveToNextPhase(clusterNodeLookup));
            }
        };
        executeInitialPhase(connection, internalRequest, searchActionListener);
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) InternalScrollSearchRequest(org.opensearch.search.internal.InternalScrollSearchRequest) CountDown(org.opensearch.common.util.concurrent.CountDown) IOException(java.io.IOException) SearchShardTarget(org.opensearch.search.SearchShardTarget) Transport(org.opensearch.transport.Transport)

Example 5 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project OpenSearch by opensearch-project.

the class LocalAllocateDangledIndices method allocateDangled.

public void allocateDangled(Collection<IndexMetadata> indices, ActionListener<AllocateDangledResponse> listener) {
    ClusterState clusterState = clusterService.state();
    DiscoveryNode masterNode = clusterState.nodes().getMasterNode();
    if (masterNode == null) {
        listener.onFailure(new MasterNotDiscoveredException("no master to send allocate dangled request"));
        return;
    }
    AllocateDangledRequest request = new AllocateDangledRequest(clusterService.localNode(), indices.toArray(new IndexMetadata[indices.size()]));
    transportService.sendRequest(masterNode, ACTION_NAME, request, new ActionListenerResponseHandler<>(listener, AllocateDangledResponse::new, ThreadPool.Names.SAME));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MasterNotDiscoveredException(org.opensearch.discovery.MasterNotDiscoveredException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Aggregations

DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)549 ClusterState (org.opensearch.cluster.ClusterState)158 Settings (org.opensearch.common.settings.Settings)137 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)114 ArrayList (java.util.ArrayList)105 HashSet (java.util.HashSet)81 IOException (java.io.IOException)79 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)78 Matchers.containsString (org.hamcrest.Matchers.containsString)74 MockTransportService (org.opensearch.test.transport.MockTransportService)73 ShardRouting (org.opensearch.cluster.routing.ShardRouting)72 ShardId (org.opensearch.index.shard.ShardId)72 CountDownLatch (java.util.concurrent.CountDownLatch)71 TransportService (org.opensearch.transport.TransportService)69 Version (org.opensearch.Version)68 List (java.util.List)67 HashMap (java.util.HashMap)66 ActionListener (org.opensearch.action.ActionListener)66 ClusterName (org.opensearch.cluster.ClusterName)66 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)59