Search in sources :

Example 56 with Response

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

the class FullClusterRestartIT method testSystemIndexMetadataIsUpgraded.

@SuppressWarnings("unchecked")
public void testSystemIndexMetadataIsUpgraded() throws Exception {
    final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + "access to system indices will be prevented by default";
    if (isRunningAgainstOldCluster()) {
        // create index
        Request createTestIndex = new Request("PUT", "/test_index_old");
        createTestIndex.setJsonEntity("{\"settings\": {\"index.number_of_replicas\": 0, \"index.number_of_shards\": 1}}");
        client().performRequest(createTestIndex);
        Request bulk = new Request("POST", "/_bulk");
        bulk.addParameter("refresh", "true");
        bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n");
        client().performRequest(bulk);
        // start a async reindex job
        Request reindex = new Request("POST", "/_reindex");
        reindex.setJsonEntity("{\n" + "  \"source\":{\n" + "    \"index\":\"test_index_old\"\n" + "  },\n" + "  \"dest\":{\n" + "    \"index\":\"test_index_reindex\"\n" + "  }\n" + "}");
        reindex.addParameter("wait_for_completion", "false");
        Map<String, Object> response = entityAsMap(client().performRequest(reindex));
        String taskId = (String) response.get("task");
        // wait for task
        Request getTask = new Request("GET", "/_tasks/" + taskId);
        getTask.addParameter("wait_for_completion", "true");
        client().performRequest(getTask);
        // make sure .tasks index exists
        Request getTasksIndex = new Request("GET", "/.tasks");
        getTasksIndex.addParameter("allow_no_indices", "false");
        if (getOldClusterVersion().before(LegacyESVersion.V_7_0_0)) {
            getTasksIndex.addParameter("include_type_name", "false");
        }
        getTasksIndex.setOptions(expectVersionSpecificWarnings(v -> {
            v.current(systemIndexWarning);
            v.compatible(systemIndexWarning);
        }));
        assertBusy(() -> {
            try {
                assertThat(client().performRequest(getTasksIndex).getStatusLine().getStatusCode(), is(200));
            } catch (ResponseException e) {
                throw new AssertionError(".tasks index does not exist yet");
            }
        });
        // upgraded properly. If we're already on 8.x, skip this part of the test.
        if (minimumNodeVersion().before(SYSTEM_INDEX_ENFORCEMENT_VERSION)) {
            // Create an alias to make sure it gets upgraded properly
            Request putAliasRequest = new Request("POST", "/_aliases");
            putAliasRequest.setJsonEntity("{\n" + "  \"actions\": [\n" + "    {\"add\":  {\"index\":  \".tasks\", \"alias\": \"test-system-alias\"}},\n" + "    {\"add\":  {\"index\":  \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" + "  ]\n" + "}");
            assertThat(client().performRequest(putAliasRequest).getStatusLine().getStatusCode(), is(200));
        }
    } else {
        assertBusy(() -> {
            Request clusterStateRequest = new Request("GET", "/_cluster/state/metadata");
            Map<String, Object> indices = new XContentTestUtils.JsonMapView(entityAsMap(client().performRequest(clusterStateRequest))).get("metadata.indices");
            // Make sure our non-system index is still non-system
            assertThat(new XContentTestUtils.JsonMapView(indices).get("test_index_old.system"), is(false));
            // Can't get the .tasks index via JsonMapView because it splits on `.`
            assertThat(indices, hasKey(".tasks"));
            XContentTestUtils.JsonMapView tasksIndex = new XContentTestUtils.JsonMapView((Map<String, Object>) indices.get(".tasks"));
            assertThat(tasksIndex.get("system"), is(true));
            // If .tasks was created in a 7.x version, it should have an alias on it that we need to make sure got upgraded properly.
            final String tasksCreatedVersionString = tasksIndex.get("settings.index.version.created");
            assertThat(tasksCreatedVersionString, notNullValue());
            final Version tasksCreatedVersion = Version.fromId(Integer.parseInt(tasksCreatedVersionString));
            if (tasksCreatedVersion.before(SYSTEM_INDEX_ENFORCEMENT_VERSION)) {
                // Verify that the alias survived the upgrade
                Request getAliasRequest = new Request("GET", "/_alias/test-system-alias");
                getAliasRequest.setOptions(expectVersionSpecificWarnings(v -> {
                    v.current(systemIndexWarning);
                    v.compatible(systemIndexWarning);
                }));
                Map<String, Object> aliasResponse = entityAsMap(client().performRequest(getAliasRequest));
                assertThat(aliasResponse, hasKey(".tasks"));
                assertThat(aliasResponse, hasKey("test_index_reindex"));
            }
        });
    }
}
Also used : CheckedFunction(org.opensearch.common.CheckedFunction) SETTING_ALLOCATION_MAX_RETRY(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY) Version(org.opensearch.Version) Request(org.opensearch.client.Request) Matchers.hasKey(org.hamcrest.Matchers.hasKey) EntityUtils(org.apache.http.util.EntityUtils) Strings(org.opensearch.common.Strings) Collections.singletonList(java.util.Collections.singletonList) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) RestClient(org.opensearch.client.RestClient) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) NotEqualMessageBuilder(org.opensearch.test.NotEqualMessageBuilder) Collection(java.util.Collection) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING(org.opensearch.cluster.routing.UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING) OpenSearchRestTestCase(org.opensearch.test.rest.OpenSearchRestTestCase) Base64(java.util.Base64) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Pattern(java.util.regex.Pattern) Response(org.opensearch.client.Response) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) Booleans(org.opensearch.common.Booleans) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections.singletonMap(java.util.Collections.singletonMap) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IOException(java.io.IOException) XContentTestUtils(org.opensearch.test.XContentTestUtils) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ResponseException(org.opensearch.client.ResponseException) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) SYSTEM_INDEX_ENFORCEMENT_VERSION(org.opensearch.cluster.metadata.IndexNameExpressionResolver.SYSTEM_INDEX_ENFORCEMENT_VERSION) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) ResponseException(org.opensearch.client.ResponseException) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) Request(org.opensearch.client.Request) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentTestUtils(org.opensearch.test.XContentTestUtils)

Example 57 with Response

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

the class CrossClusterSearchUnavailableClusterIT method updateRemoteClusterSettings.

private static void updateRemoteClusterSettings(Map<String, Object> settings) throws IOException {
    Request request = new Request("PUT", "/_cluster/settings");
    request.setEntity(buildUpdateSettingsRequestBody(settings));
    Response response = client().performRequest(request);
    assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) Response(org.opensearch.client.Response) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) Request(org.opensearch.client.Request) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) SearchRequest(org.opensearch.action.search.SearchRequest) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 58 with Response

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

the class CrossClusterSearchUnavailableClusterIT method testSearchSkipUnavailable.

public void testSearchSkipUnavailable() throws IOException {
    try (MockTransportService remoteTransport = startTransport("node0", new CopyOnWriteArrayList<>(), Version.CURRENT, threadPool)) {
        DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode();
        updateRemoteClusterSettings(Collections.singletonMap("seeds", remoteNode.getAddress().toString()));
        for (int i = 0; i < 10; i++) {
            restHighLevelClient.index(new IndexRequest("index").id(String.valueOf(i)).source("field", "value"), RequestOptions.DEFAULT);
        }
        Response refreshResponse = client().performRequest(new Request("POST", "/index/_refresh"));
        assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("index"), RequestOptions.DEFAULT);
            assertSame(SearchResponse.Clusters.EMPTY, response.getClusters());
            assertEquals(10, response.getHits().getTotalHits().value);
            assertEquals(10, response.getHits().getHits().length);
        }
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("index", "remote1:index"), RequestOptions.DEFAULT);
            assertEquals(2, response.getClusters().getTotal());
            assertEquals(2, response.getClusters().getSuccessful());
            assertEquals(0, response.getClusters().getSkipped());
            assertEquals(10, response.getHits().getTotalHits().value);
            assertEquals(10, response.getHits().getHits().length);
        }
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("remote1:index"), RequestOptions.DEFAULT);
            assertEquals(1, response.getClusters().getTotal());
            assertEquals(1, response.getClusters().getSuccessful());
            assertEquals(0, response.getClusters().getSkipped());
            assertEquals(0, response.getHits().getTotalHits().value);
        }
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("index", "remote1:index").scroll("1m"), RequestOptions.DEFAULT);
            assertEquals(2, response.getClusters().getTotal());
            assertEquals(2, response.getClusters().getSuccessful());
            assertEquals(0, response.getClusters().getSkipped());
            assertEquals(10, response.getHits().getTotalHits().value);
            assertEquals(10, response.getHits().getHits().length);
            String scrollId = response.getScrollId();
            SearchResponse scrollResponse = restHighLevelClient.scroll(new SearchScrollRequest(scrollId), RequestOptions.DEFAULT);
            assertSame(SearchResponse.Clusters.EMPTY, scrollResponse.getClusters());
            assertEquals(10, scrollResponse.getHits().getTotalHits().value);
            assertEquals(0, scrollResponse.getHits().getHits().length);
        }
        remoteTransport.close();
        updateRemoteClusterSettings(Collections.singletonMap("skip_unavailable", true));
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("index", "remote1:index"), RequestOptions.DEFAULT);
            assertEquals(2, response.getClusters().getTotal());
            assertEquals(1, response.getClusters().getSuccessful());
            assertEquals(1, response.getClusters().getSkipped());
            assertEquals(10, response.getHits().getTotalHits().value);
            assertEquals(10, response.getHits().getHits().length);
        }
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("remote1:index"), RequestOptions.DEFAULT);
            assertEquals(1, response.getClusters().getTotal());
            assertEquals(0, response.getClusters().getSuccessful());
            assertEquals(1, response.getClusters().getSkipped());
            assertEquals(0, response.getHits().getTotalHits().value);
        }
        {
            SearchResponse response = restHighLevelClient.search(new SearchRequest("index", "remote1:index").scroll("1m"), RequestOptions.DEFAULT);
            assertEquals(2, response.getClusters().getTotal());
            assertEquals(1, response.getClusters().getSuccessful());
            assertEquals(1, response.getClusters().getSkipped());
            assertEquals(10, response.getHits().getTotalHits().value);
            assertEquals(10, response.getHits().getHits().length);
            String scrollId = response.getScrollId();
            SearchResponse scrollResponse = restHighLevelClient.scroll(new SearchScrollRequest(scrollId), RequestOptions.DEFAULT);
            assertSame(SearchResponse.Clusters.EMPTY, scrollResponse.getClusters());
            assertEquals(10, scrollResponse.getHits().getTotalHits().value);
            assertEquals(0, scrollResponse.getHits().getHits().length);
        }
        updateRemoteClusterSettings(Collections.singletonMap("skip_unavailable", false));
        assertSearchConnectFailure();
        Map<String, Object> map = new HashMap<>();
        map.put("seeds", null);
        map.put("skip_unavailable", null);
        updateRemoteClusterSettings(map);
    }
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) HashMap(java.util.HashMap) Request(org.opensearch.client.Request) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) SearchRequest(org.opensearch.action.search.SearchRequest) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) Response(org.opensearch.client.Response) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 59 with Response

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

the class IndexingIT method assertVersion.

private void assertVersion(final String index, final int docId, final String preference, final int expectedVersion) throws IOException {
    Request request = new Request("GET", index + "/_doc/" + docId);
    request.addParameter("preference", preference);
    final Response response = client().performRequest(request);
    assertOK(response);
    final int actualVersion = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("_version").toString());
    assertThat("version mismatch for doc [" + docId + "] preference [" + preference + "]", actualVersion, equalTo(expectedVersion));
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request)

Example 60 with Response

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

the class IndexingIT method assertCount.

private void assertCount(final String index, final String preference, final int expectedCount) throws IOException {
    Request request = new Request("GET", index + "/_count");
    request.addParameter("preference", preference);
    final Response response = client().performRequest(request);
    assertOK(response);
    final int actualCount = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("count").toString());
    assertThat(actualCount, equalTo(expectedCount));
}
Also used : Response(org.opensearch.client.Response) 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