Search in sources :

Example 26 with ClusterName

use of org.opensearch.cluster.ClusterName in project OpenSearch by opensearch-project.

the class RestMainActionTests method testGetResponse.

public void testGetResponse() throws Exception {
    final String nodeName = "node1";
    final ClusterName clusterName = new ClusterName("cluster1");
    final String clusterUUID = randomAlphaOfLengthBetween(10, 20);
    final Version version = Version.CURRENT;
    final Build build = Build.CURRENT;
    final boolean prettyPrint = randomBoolean();
    final MainResponse mainResponse = new MainResponse(nodeName, version, clusterName, clusterUUID, build);
    XContentBuilder builder = JsonXContent.contentBuilder();
    Map<String, String> params = new HashMap<>();
    if (prettyPrint == false) {
        params.put("pretty", String.valueOf(prettyPrint));
    }
    RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    BytesRestResponse response = RestMainAction.convertMainResponse(mainResponse, restRequest, builder);
    assertNotNull(response);
    assertThat(response.status(), equalTo(RestStatus.OK));
    assertThat(response.content().length(), greaterThan(0));
    XContentBuilder responseBuilder = JsonXContent.contentBuilder();
    if (prettyPrint) {
        // do this to mimic what the rest layer does
        responseBuilder.prettyPrint().lfAtEnd();
    }
    mainResponse.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
    BytesReference xcontentBytes = BytesReference.bytes(responseBuilder);
    assertEquals(xcontentBytes, response.content());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) Version(org.opensearch.Version) MainResponse(org.opensearch.action.main.MainResponse) Build(org.opensearch.Build) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ClusterName(org.opensearch.cluster.ClusterName) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 27 with ClusterName

use of org.opensearch.cluster.ClusterName in project OpenSearch by opensearch-project.

the class SniffConnectionStrategyTests method startTransport.

public MockTransportService startTransport(final String id, final List<DiscoveryNode> knownNodes, final Version version, final Settings settings) {
    boolean success = false;
    final Settings s = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), clusterAlias).put("node.name", id).put(settings).build();
    ClusterName clusterName = ClusterName.CLUSTER_NAME_SETTING.get(s);
    MockTransportService newService = MockTransportService.createNewService(s, version, threadPool);
    try {
        newService.registerRequestHandler(ClusterStateAction.NAME, ThreadPool.Names.SAME, ClusterStateRequest::new, (request, channel, task) -> {
            DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
            for (DiscoveryNode node : knownNodes) {
                builder.add(node);
            }
            ClusterState build = ClusterState.builder(clusterName).nodes(builder.build()).build();
            channel.sendResponse(new ClusterStateResponse(clusterName, build, false));
        });
        newService.start();
        newService.acceptIncomingRequests();
        success = true;
        return newService;
    } finally {
        if (success == false) {
            newService.close();
        }
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) ClusterName(org.opensearch.cluster.ClusterName) AbstractScopedSettings(org.opensearch.common.settings.AbstractScopedSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 28 with ClusterName

use of org.opensearch.cluster.ClusterName in project OpenSearch by opensearch-project.

the class TransportServiceDeserializationFailureTests method testDeserializationFailureLogIdentifiesListener.

public void testDeserializationFailureLogIdentifiesListener() {
    final DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
    final DiscoveryNode otherNode = new DiscoveryNode("other", buildNewFakeTransportAddress(), Version.CURRENT);
    final Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), "local").build();
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    final String testActionName = "internal:test-action";
    final MockTransport transport = new MockTransport() {

        @Override
        protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) {
            if (action.equals(TransportService.HANDSHAKE_ACTION_NAME)) {
                handleResponse(requestId, new TransportService.HandshakeResponse(otherNode, new ClusterName(""), Version.CURRENT));
            }
        }
    };
    final TransportService transportService = transport.createTransportService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), TransportService.NOOP_TRANSPORT_INTERCEPTOR, ignored -> localNode, null, Collections.emptySet());
    transportService.registerRequestHandler(testActionName, ThreadPool.Names.SAME, TransportRequest.Empty::new, (request, channel, task) -> channel.sendResponse(TransportResponse.Empty.INSTANCE));
    transportService.start();
    transportService.acceptIncomingRequests();
    final PlainActionFuture<Void> connectionFuture = new PlainActionFuture<>();
    transportService.connectToNode(otherNode, connectionFuture);
    assertTrue(connectionFuture.isDone());
    {
        // requests without a parent task are recorded directly in the response context
        transportService.sendRequest(otherNode, testActionName, TransportRequest.Empty.INSTANCE, TransportRequestOptions.EMPTY, new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                fail("should not be called");
            }

            @Override
            public void handleException(TransportException exp) {
                fail("should not be called");
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }

            @Override
            public TransportResponse.Empty read(StreamInput in) {
                throw new AssertionError("should not be called");
            }

            @Override
            public String toString() {
                return "test handler without parent";
            }
        });
        final List<Transport.ResponseContext<? extends TransportResponse>> responseContexts = transport.getResponseHandlers().prune(ignored -> true);
        assertThat(responseContexts, hasSize(1));
        final TransportResponseHandler<? extends TransportResponse> handler = responseContexts.get(0).handler();
        assertThat(handler, hasToString(containsString("test handler without parent")));
    }
    {
        // requests with a parent task get wrapped up by the transport service, including the action name
        final Task parentTask = transportService.getTaskManager().register("test", "test-action", new TaskAwareRequest() {

            @Override
            public void setParentTask(TaskId taskId) {
                fail("should not be called");
            }

            @Override
            public TaskId getParentTask() {
                return TaskId.EMPTY_TASK_ID;
            }
        });
        transportService.sendChildRequest(otherNode, testActionName, TransportRequest.Empty.INSTANCE, parentTask, TransportRequestOptions.EMPTY, new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                fail("should not be called");
            }

            @Override
            public void handleException(TransportException exp) {
                fail("should not be called");
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }

            @Override
            public TransportResponse.Empty read(StreamInput in) {
                throw new AssertionError("should not be called");
            }

            @Override
            public String toString() {
                return "test handler with parent";
            }
        });
        final List<Transport.ResponseContext<? extends TransportResponse>> responseContexts = transport.getResponseHandlers().prune(ignored -> true);
        assertThat(responseContexts, hasSize(1));
        final TransportResponseHandler<? extends TransportResponse> handler = responseContexts.get(0).handler();
        assertThat(handler, hasToString(allOf(containsString("test handler with parent"), containsString(testActionName))));
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.hasToString(org.hamcrest.Matchers.hasToString) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) Matchers.allOf(org.hamcrest.Matchers.allOf) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ThreadPool(org.opensearch.threadpool.ThreadPool) TaskId(org.opensearch.tasks.TaskId) Version(org.opensearch.Version) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) List(java.util.List) NODE_NAME_SETTING(org.opensearch.node.Node.NODE_NAME_SETTING) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockTransport(org.opensearch.test.transport.MockTransport) ClusterName(org.opensearch.cluster.ClusterName) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockTransport(org.opensearch.test.transport.MockTransport) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) List(java.util.List) MockTransport(org.opensearch.test.transport.MockTransport) Settings(org.opensearch.common.settings.Settings)

Example 29 with ClusterName

use of org.opensearch.cluster.ClusterName in project OpenSearch by opensearch-project.

the class ClusterStateCreationUtils method state.

/**
 * Creates cluster state with the given indices, each index containing #(numberOfPrimaries)
 * started primary shards and no replicas.  The cluster state contains #(numberOfNodes) nodes
 * and assigns primaries to those nodes.
 */
public static ClusterState state(final int numberOfNodes, final String[] indices, final int numberOfPrimaries) {
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
    Set<String> nodes = new HashSet<>();
    for (int i = 0; i < numberOfNodes; i++) {
        final DiscoveryNode node = newNode(i);
        discoBuilder = discoBuilder.add(node);
        nodes.add(node.getId());
    }
    discoBuilder.localNodeId(newNode(0).getId());
    discoBuilder.masterNodeId(newNode(0).getId());
    Metadata.Builder metadata = Metadata.builder();
    RoutingTable.Builder routingTable = RoutingTable.builder();
    List<String> nodesList = new ArrayList<>(nodes);
    int currentNodeToAssign = 0;
    for (String index : indices) {
        IndexMetadata indexMetadata = IndexMetadata.builder(index).settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).put(SETTING_NUMBER_OF_SHARDS, numberOfPrimaries).put(SETTING_NUMBER_OF_REPLICAS, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())).build();
        IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex());
        for (int i = 0; i < numberOfPrimaries; i++) {
            ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
            IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
            indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(shardId, nodesList.get(currentNodeToAssign++), true, ShardRoutingState.STARTED));
            if (currentNodeToAssign == nodesList.size()) {
                currentNodeToAssign = 0;
            }
            indexRoutingTable.addIndexShard(indexShardRoutingBuilder.build());
        }
        metadata.put(indexMetadata, false);
        routingTable.add(indexRoutingTable);
    }
    ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
    state.nodes(discoBuilder);
    state.metadata(metadata.generateClusterUuidIfNeeded().build());
    state.routingTable(routingTable.build());
    return state.build();
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ArrayList(java.util.ArrayList) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) ShardId(org.opensearch.index.shard.ShardId) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 30 with ClusterName

use of org.opensearch.cluster.ClusterName in project OpenSearch by opensearch-project.

the class ClusterStateCreationUtils method stateWithAssignedPrimariesAndOneReplica.

/**
 * Creates cluster state with several shards and one replica and all shards STARTED.
 */
public static ClusterState stateWithAssignedPrimariesAndOneReplica(String index, int numberOfShards) {
    // we need a non-local master to test shard failures
    int numberOfNodes = 2;
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
    for (int i = 0; i < numberOfNodes + 1; i++) {
        final DiscoveryNode node = newNode(i);
        discoBuilder = discoBuilder.add(node);
    }
    discoBuilder.localNodeId(newNode(0).getId());
    // we need a non-local master to test shard failures
    discoBuilder.masterNodeId(newNode(1).getId());
    IndexMetadata indexMetadata = IndexMetadata.builder(index).settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).put(SETTING_NUMBER_OF_SHARDS, numberOfShards).put(SETTING_NUMBER_OF_REPLICAS, 1).put(SETTING_CREATION_DATE, System.currentTimeMillis())).build();
    ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
    state.nodes(discoBuilder);
    state.metadata(Metadata.builder().put(indexMetadata, false).generateClusterUuidIfNeeded());
    IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetadata.getIndex());
    for (int i = 0; i < numberOfShards; i++) {
        final ShardId shardId = new ShardId(index, "_na_", i);
        IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(index, i, newNode(0).getId(), null, true, ShardRoutingState.STARTED));
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(index, i, newNode(1).getId(), null, false, ShardRoutingState.STARTED));
        indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build());
    }
    state.routingTable(RoutingTable.builder().add(indexRoutingTableBuilder.build()).build());
    return state.build();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) ShardId(org.opensearch.index.shard.ShardId) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Aggregations

ClusterName (org.opensearch.cluster.ClusterName)224 ClusterState (org.opensearch.cluster.ClusterState)176 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)73 Matchers.containsString (org.hamcrest.Matchers.containsString)57 Settings (org.opensearch.common.settings.Settings)53 Metadata (org.opensearch.cluster.metadata.Metadata)45 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)43 ClusterService (org.opensearch.cluster.service.ClusterService)42 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)36 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)33 ClusterSettings (org.opensearch.common.settings.ClusterSettings)32 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)32 Version (org.opensearch.Version)31 HashMap (java.util.HashMap)30 ThreadPool (org.opensearch.threadpool.ThreadPool)29 Index (org.opensearch.index.Index)27 Before (org.junit.Before)26 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)25 BytesArray (org.opensearch.common.bytes.BytesArray)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)23