Search in sources :

Example 86 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class Zen2RestApiIT method testRollingRestartOfTwoNodeCluster.

public void testRollingRestartOfTwoNodeCluster() throws Exception {
    internalCluster().setBootstrapMasterNodeIndex(1);
    final List<String> nodes = internalCluster().startNodes(2);
    createIndex("test", Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), // assign shards
    TimeValue.ZERO).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, // causes rebalancing
    2).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).build());
    ensureGreen("test");
    RestClient restClient = getRestClient();
    internalCluster().rollingRestart(new InternalTestCluster.RestartCallback() {

        @Override
        public void doAfterNodes(int n, Client client) throws IOException {
            ensureGreen("test");
            Response response = restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/" + internalCluster().getNodeNames()[n]));
            assertThat(response.getStatusLine().getStatusCode(), is(200));
        }

        @Override
        public Settings onNodeStopped(String nodeName) throws IOException {
            String viaNode = randomValueOtherThan(nodeName, () -> randomFrom(nodes));
            List<Node> allNodes = restClient.getNodes();
            try {
                restClient.setNodes(Collections.singletonList(new Node(HttpHost.create(internalCluster().getInstance(HttpServerTransport.class, viaNode).boundAddress().publishAddress().toString()))));
                Response deleteResponse = restClient.performRequest(new Request("DELETE", "/_cluster/voting_config_exclusions"));
                assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200));
                ClusterHealthResponse clusterHealthResponse = client(viaNode).admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes(Integer.toString(1)).setTimeout(TimeValue.timeValueSeconds(30L)).setWaitForYellowStatus().get();
                assertFalse(nodeName, clusterHealthResponse.isTimedOut());
                return Settings.EMPTY;
            } finally {
                restClient.setNodes(allNodes);
            }
        }
    });
    ensureStableCluster(2);
    ensureGreen("test");
    assertThat(internalCluster().size(), is(2));
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) Node(org.opensearch.client.Node) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) InternalTestCluster(org.opensearch.test.InternalTestCluster) IOException(java.io.IOException) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) Response(org.opensearch.client.Response) List(java.util.List) Client(org.opensearch.client.Client) RestClient(org.opensearch.client.RestClient) Settings(org.opensearch.common.settings.Settings)

Example 87 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class Netty4BadRequestIT method testInvalidHeaderValue.

public void testInvalidHeaderValue() throws IOException {
    final Request request = new Request("GET", "/_cluster/settings");
    final RequestOptions.Builder options = request.getOptions().toBuilder();
    options.addHeader("Content-Type", "\t");
    request.setOptions(options);
    final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
    final Response response = e.getResponse();
    assertThat(response.getStatusLine().getStatusCode(), equalTo(400));
    final ObjectPath objectPath = ObjectPath.createFromResponse(response);
    final Map<String, Object> map = objectPath.evaluate("error");
    assertThat(map.get("type"), equalTo("content_type_header_exception"));
    assertThat(map.get("reason"), equalTo("java.lang.IllegalArgumentException: invalid Content-Type header []"));
}
Also used : Response(org.opensearch.client.Response) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) RequestOptions(org.opensearch.client.RequestOptions) ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 88 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class MappingIT method testAllFieldDisable6x.

/**
 * Create a mapping that explicitly disables the _all field (possible in 6x, see #37429)
 * and check that it can be upgraded to 7x.
 */
public void testAllFieldDisable6x() throws Exception {
    assumeTrue("_all", UPGRADE_FROM_VERSION.before(LegacyESVersion.V_7_0_0));
    switch(CLUSTER_TYPE) {
        case OLD:
            Request createTestIndex = new Request("PUT", "all-index");
            createTestIndex.addParameter("include_type_name", "false");
            createTestIndex.setJsonEntity("{ \"settings\": { \"index.number_of_shards\": 1 }, " + "\"mappings\": {\"_all\": { \"enabled\": false }, \"properties\": { \"field\": { \"type\": \"text\" }}}}");
            createTestIndex.setOptions(expectWarnings("[_all] is deprecated in 6.0+ and will be removed in 7.0. As a replacement," + " " + "you can use [copy_to] on mapping fields to create your own catch all field."));
            Response resp = client().performRequest(createTestIndex);
            assertEquals(200, resp.getStatusLine().getStatusCode());
            break;
        default:
            final Request request = new Request("GET", "all-index");
            Response response = client().performRequest(request);
            assertEquals(200, response.getStatusLine().getStatusCode());
            Object enabled = XContentMapValues.extractValue("all-index.mappings._all.enabled", entityAsMap(response));
            assertNotNull(enabled);
            assertEquals(false, enabled);
            break;
    }
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request)

Example 89 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class RecoveryIT method indexVersionCreated.

/**
 * Returns the version in which the given index has been created
 */
private static Version indexVersionCreated(final String indexName) throws IOException {
    final Request request = new Request("GET", "/" + indexName + "/_settings");
    final String versionCreatedSetting = indexName + ".settings.index.version.created";
    request.addParameter("filter_path", versionCreatedSetting);
    final Response response = client().performRequest(request);
    return Version.fromId(Integer.parseInt(ObjectPath.createFromResponse(response).evaluate(versionCreatedSetting)));
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request)

Example 90 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class RecoveryIT method testCloseIndexDuringRollingUpgrade.

/**
 * This test creates and closes a new index at every stage of the rolling upgrade. It then checks that the index
 * is effectively closed and potentially replicated if the cluster supports replication of closed indices at the
 * time the index was closed.
 */
public void testCloseIndexDuringRollingUpgrade() throws Exception {
    final Version minimumNodeVersion = minimumNodeVersion();
    final String indexName = String.join("_", "index", CLUSTER_TYPE.toString(), Integer.toString(minimumNodeVersion.id)).toLowerCase(Locale.ROOT);
    final Request indexExistsRequest = new Request("HEAD", "/" + indexName);
    indexExistsRequest.setOptions(allowTypesRemovalWarnings());
    final Response indexExistsResponse = client().performRequest(indexExistsRequest);
    if (RestStatus.OK.getStatus() != indexExistsResponse.getStatusLine().getStatusCode()) {
        createIndex(indexName, Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0).build());
        ensureGreen(indexName);
        closeIndex(indexName);
    }
    if (minimumNodeVersion.onOrAfter(LegacyESVersion.V_7_2_0)) {
        // index is created on a version that supports the replication of closed indices,
        // so we expect the index to be closed and replicated
        ensureGreen(indexName);
        assertClosedIndex(indexName, true);
    } else {
        assertClosedIndex(indexName, false);
    }
}
Also used : Response(org.opensearch.client.Response) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) Request(org.opensearch.client.Request)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7