Search in sources :

Example 56 with ClusterStateResponse

use of org.opensearch.action.admin.cluster.state.ClusterStateResponse in project OpenSearch by opensearch-project.

the class RestClusterGetSettingsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest().routingTable(false).nodes(false);
    final boolean renderDefaults = request.paramAsBoolean("include_defaults", false);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("cluster_manager_timeout", clusterStateRequest.masterNodeTimeout()));
    parseDeprecatedMasterTimeoutParameter(clusterStateRequest, request, deprecationLogger, getName());
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
            return new BytesRestResponse(RestStatus.OK, renderResponse(response.getState(), renderDefaults, builder, request));
        }
    });
}
Also used : SettingsFilter(org.opensearch.common.settings.SettingsFilter) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) ClusterGetSettingsResponse(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsResponse) RestRequest(org.opensearch.rest.RestRequest) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ToXContent(org.opensearch.common.xcontent.ToXContent) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Collections.singletonList(java.util.Collections.singletonList) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) Requests(org.opensearch.client.Requests) ClusterState(org.opensearch.cluster.ClusterState) List(java.util.List) ClusterSettings(org.opensearch.common.settings.ClusterSettings) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 57 with ClusterStateResponse

use of org.opensearch.action.admin.cluster.state.ClusterStateResponse in project OpenSearch by opensearch-project.

the class GceDiscoverTests method testJoin.

public void testJoin() {
    // start cluster-manager node
    final String clusterManagerNode = internalCluster().startClusterManagerOnlyNode();
    registerGceNode(clusterManagerNode);
    ClusterStateResponse clusterStateResponse = client(clusterManagerNode).admin().cluster().prepareState().setMasterNodeTimeout("1s").clear().setNodes(true).get();
    assertNotNull(clusterStateResponse.getState().nodes().getMasterNodeId());
    // start another node
    final String secondNode = internalCluster().startNode();
    registerGceNode(secondNode);
    clusterStateResponse = client(secondNode).admin().cluster().prepareState().setMasterNodeTimeout("1s").clear().setNodes(true).setLocal(true).get();
    assertNotNull(clusterStateResponse.getState().nodes().getMasterNodeId());
    // wait for the cluster to form
    assertNoTimeout(client().admin().cluster().prepareHealth().setWaitForNodes(Integer.toString(2)).get());
    assertNumberOfNodes(2);
    // add one more node and wait for it to join
    final String thirdNode = internalCluster().startDataOnlyNode();
    registerGceNode(thirdNode);
    assertNoTimeout(client().admin().cluster().prepareHealth().setWaitForNodes(Integer.toString(3)).get());
    assertNumberOfNodes(3);
}
Also used : ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse)

Example 58 with ClusterStateResponse

use of org.opensearch.action.admin.cluster.state.ClusterStateResponse in project OpenSearch by opensearch-project.

the class GatewayIndexStateIT method testJustMasterNode.

public void testJustMasterNode() throws Exception {
    logger.info("--> cleaning nodes");
    logger.info("--> starting 1 master node non data");
    internalCluster().startNode(nonDataNode());
    logger.info("--> create an index");
    client().admin().indices().prepareCreate("test").setWaitForActiveShards(ActiveShardCount.NONE).execute().actionGet();
    logger.info("--> restarting master node");
    internalCluster().fullRestart(new RestartCallback() {

        @Override
        public Settings onNodeStopped(String nodeName) {
            return nonDataNode();
        }
    });
    logger.info("--> waiting for test index to be created");
    ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setIndices("test").execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    logger.info("--> verify we have an index");
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().setIndices("test").execute().actionGet();
    assertThat(clusterStateResponse.getState().metadata().hasIndex("test"), equalTo(true));
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) RestartCallback(org.opensearch.test.InternalTestCluster.RestartCallback) Settings(org.opensearch.common.settings.Settings)

Example 59 with ClusterStateResponse

use of org.opensearch.action.admin.cluster.state.ClusterStateResponse in project OpenSearch by opensearch-project.

the class NoMasterNodeIT method testNoMasterActionsMetadataWriteMasterBlock.

public void testNoMasterActionsMetadataWriteMasterBlock() throws Exception {
    Settings settings = Settings.builder().put(NoMasterBlockService.NO_MASTER_BLOCK_SETTING.getKey(), "metadata_write").put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(), "100ms").build();
    final List<String> nodes = internalCluster().startNodes(3, settings);
    prepareCreate("test1").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)).get();
    client().admin().cluster().prepareHealth("_all").setWaitForGreenStatus().get();
    client().prepareIndex("test1").setId("1").setSource("field", "value1").get();
    refresh();
    ensureGreen("test1");
    ClusterStateResponse clusterState = client().admin().cluster().prepareState().get();
    logger.info("Cluster state:\n{}", clusterState.getState());
    final List<String> nodesWithShards = clusterState.getState().routingTable().index("test1").shard(0).activeShards().stream().map(shardRouting -> shardRouting.currentNodeId()).map(nodeId -> clusterState.getState().nodes().resolveNode(nodeId)).map(DiscoveryNode::getName).collect(Collectors.toList());
    client().execute(AddVotingConfigExclusionsAction.INSTANCE, new AddVotingConfigExclusionsRequest(nodesWithShards.toArray(new String[0]))).get();
    ensureGreen("test1");
    String partitionedNode = nodes.stream().filter(n -> nodesWithShards.contains(n) == false).findFirst().get();
    final NetworkDisruption disruptionScheme = new NetworkDisruption(new NetworkDisruption.TwoPartitions(Collections.singleton(partitionedNode), new HashSet<>(nodesWithShards)), NetworkDisruption.DISCONNECT);
    internalCluster().setDisruptionScheme(disruptionScheme);
    disruptionScheme.startDisrupting();
    assertBusy(() -> {
        for (String node : nodesWithShards) {
            ClusterState state = client(node).admin().cluster().prepareState().setLocal(true).get().getState();
            assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
        }
    });
    GetResponse getResponse = client(randomFrom(nodesWithShards)).prepareGet("test1", "1").get();
    assertExists(getResponse);
    expectThrows(Exception.class, () -> client(partitionedNode).prepareGet("test1", "1").get());
    SearchResponse countResponse = client(randomFrom(nodesWithShards)).prepareSearch("test1").setAllowPartialSearchResults(true).setSize(0).get();
    assertHitCount(countResponse, 1L);
    expectThrows(Exception.class, () -> client(partitionedNode).prepareSearch("test1").setAllowPartialSearchResults(true).setSize(0).get());
    TimeValue timeout = TimeValue.timeValueMillis(200);
    client(randomFrom(nodesWithShards)).prepareUpdate("test1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").setTimeout(timeout).get();
    expectThrows(Exception.class, () -> client(partitionedNode).prepareUpdate("test1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").setTimeout(timeout).get());
    client(randomFrom(nodesWithShards)).prepareIndex("test1").setId("1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get();
    // dynamic mapping updates fail
    expectThrows(MasterNotDiscoveredException.class, () -> client(randomFrom(nodesWithShards)).prepareIndex("test1").setId("1").setSource(XContentFactory.jsonBuilder().startObject().field("new_field", "value").endObject()).setTimeout(timeout).get());
    // dynamic index creation fails
    expectThrows(MasterNotDiscoveredException.class, () -> client(randomFrom(nodesWithShards)).prepareIndex("test2").setId("1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get());
    expectThrows(Exception.class, () -> client(partitionedNode).prepareIndex("test1").setId("1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get());
    internalCluster().clearDisruptionScheme(true);
}
Also used : ActionRequestBuilder(org.opensearch.action.ActionRequestBuilder) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterScope(org.opensearch.test.OpenSearchIntegTestCase.ClusterScope) MasterNotDiscoveredException(org.opensearch.discovery.MasterNotDiscoveredException) MockTransportService(org.opensearch.test.transport.MockTransportService) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) AutoCreateIndex(org.opensearch.action.support.AutoCreateIndex) MappingUpdatedAction(org.opensearch.cluster.action.index.MappingUpdatedAction) ScriptType(org.opensearch.script.ScriptType) HashSet(java.util.HashSet) Requests(org.opensearch.client.Requests) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) AddVotingConfigExclusionsAction(org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsAction) XContentFactory(org.opensearch.common.xcontent.XContentFactory) IsolateAllNodes(org.opensearch.test.disruption.NetworkDisruption.IsolateAllNodes) SearchResponse(org.opensearch.action.search.SearchResponse) NoMasterBlockService(org.opensearch.cluster.coordination.NoMasterBlockService) GetResponse(org.opensearch.action.get.GetResponse) OpenSearchAssertions.assertRequestBuilderThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) Script(org.opensearch.script.Script) Collection(java.util.Collection) AddVotingConfigExclusionsRequest(org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) Scope(org.opensearch.test.OpenSearchIntegTestCase.Scope) Collectors(java.util.stream.Collectors) Plugin(org.opensearch.plugins.Plugin) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) NetworkDisruption(org.opensearch.test.disruption.NetworkDisruption) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) OpenSearchAssertions.assertExists(org.opensearch.test.hamcrest.OpenSearchAssertions.assertExists) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Collections(java.util.Collections) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.opensearch.action.get.GetResponse) SearchResponse(org.opensearch.action.search.SearchResponse) AddVotingConfigExclusionsRequest(org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest) NetworkDisruption(org.opensearch.test.disruption.NetworkDisruption) Settings(org.opensearch.common.settings.Settings) TimeValue(org.opensearch.common.unit.TimeValue) HashSet(java.util.HashSet)

Example 60 with ClusterStateResponse

use of org.opensearch.action.admin.cluster.state.ClusterStateResponse in project OpenSearch by opensearch-project.

the class NoMasterNodeIT method testNoMasterActionsWriteMasterBlock.

public void testNoMasterActionsWriteMasterBlock() throws Exception {
    Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).put(NoMasterBlockService.NO_MASTER_BLOCK_SETTING.getKey(), "write").build();
    final List<String> nodes = internalCluster().startNodes(3, settings);
    prepareCreate("test1").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 2)).get();
    prepareCreate("test2").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)).get();
    client().admin().cluster().prepareHealth("_all").setWaitForGreenStatus().get();
    client().prepareIndex("test1").setId("1").setSource("field", "value1").get();
    client().prepareIndex("test2").setId("1").setSource("field", "value1").get();
    refresh();
    ensureSearchable("test1", "test2");
    ClusterStateResponse clusterState = client().admin().cluster().prepareState().get();
    logger.info("Cluster state:\n{}", clusterState.getState());
    final NetworkDisruption disruptionScheme = new NetworkDisruption(new IsolateAllNodes(new HashSet<>(nodes)), NetworkDisruption.DISCONNECT);
    internalCluster().setDisruptionScheme(disruptionScheme);
    disruptionScheme.startDisrupting();
    final Client clientToMasterlessNode = client();
    assertBusy(() -> {
        ClusterState state = clientToMasterlessNode.admin().cluster().prepareState().setLocal(true).get().getState();
        assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
    });
    GetResponse getResponse = clientToMasterlessNode.prepareGet("test1", "1").get();
    assertExists(getResponse);
    SearchResponse countResponse = clientToMasterlessNode.prepareSearch("test1").setAllowPartialSearchResults(true).setSize(0).get();
    assertHitCount(countResponse, 1L);
    logger.info("--> here 3");
    SearchResponse searchResponse = clientToMasterlessNode.prepareSearch("test1").setAllowPartialSearchResults(true).get();
    assertHitCount(searchResponse, 1L);
    countResponse = clientToMasterlessNode.prepareSearch("test2").setAllowPartialSearchResults(true).setSize(0).get();
    assertThat(countResponse.getTotalShards(), equalTo(3));
    assertThat(countResponse.getSuccessfulShards(), equalTo(1));
    TimeValue timeout = TimeValue.timeValueMillis(200);
    long now = System.currentTimeMillis();
    try {
        clientToMasterlessNode.prepareUpdate("test1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").setTimeout(timeout).get();
        fail("Expected ClusterBlockException");
    } catch (ClusterBlockException e) {
        assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
    } catch (Exception e) {
        logger.info("unexpected", e);
        throw e;
    }
    try {
        clientToMasterlessNode.prepareIndex("test1").setId("1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get();
        fail("Expected ClusterBlockException");
    } catch (ClusterBlockException e) {
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
    }
    internalCluster().clearDisruptionScheme(true);
}
Also used : IsolateAllNodes(org.opensearch.test.disruption.NetworkDisruption.IsolateAllNodes) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.opensearch.action.get.GetResponse) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) MasterNotDiscoveredException(org.opensearch.discovery.MasterNotDiscoveredException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) SearchResponse(org.opensearch.action.search.SearchResponse) NetworkDisruption(org.opensearch.test.disruption.NetworkDisruption) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) TimeValue(org.opensearch.common.unit.TimeValue) HashSet(java.util.HashSet)

Aggregations

ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)60 Settings (org.opensearch.common.settings.Settings)23 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)20 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)19 List (java.util.List)17 NodeClient (org.opensearch.client.node.NodeClient)13 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)13 DeprecationLogger (org.opensearch.common.logging.DeprecationLogger)12 RestRequest (org.opensearch.rest.RestRequest)12 GET (org.opensearch.rest.RestRequest.Method.GET)12 RestResponse (org.opensearch.rest.RestResponse)12 Table (org.opensearch.common.Table)11 SearchResponse (org.opensearch.action.search.SearchResponse)10 Client (org.opensearch.client.Client)10 ClusterState (org.opensearch.cluster.ClusterState)10 RestResponseListener (org.opensearch.rest.action.RestResponseListener)10 MockTransportService (org.opensearch.test.transport.MockTransportService)10 HashSet (java.util.HashSet)9 ShardRouting (org.opensearch.cluster.routing.ShardRouting)8 Strings (org.opensearch.common.Strings)8