Search in sources :

Example 1 with Builder

use of org.opensearch.client.RequestOptions.Builder in project OpenSearch by opensearch-project.

the class OpenSearchRestTestCase method refreshAllIndices.

protected void refreshAllIndices() throws IOException {
    boolean includeHidden = minimumNodeVersion().onOrAfter(LegacyESVersion.V_7_7_0);
    Request refreshRequest = new Request("POST", "/_refresh");
    refreshRequest.addParameter("expand_wildcards", "open" + (includeHidden ? ",hidden" : ""));
    // Allow system index deprecation warnings
    final Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
    requestOptions.setWarningsHandler(warnings -> {
        if (warnings.isEmpty()) {
            return false;
        } else if (warnings.size() > 1) {
            return true;
        } else {
            return warnings.get(0).startsWith("this request accesses system indices:") == false;
        }
    });
    refreshRequest.setOptions(requestOptions);
    client().performRequest(refreshRequest);
}
Also used : RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Request(org.opensearch.client.Request) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Example 2 with Builder

use of org.opensearch.client.RequestOptions.Builder in project OpenSearch by opensearch-project.

the class OpenSearchRestTestCase method expectSoftDeletesWarning.

protected static void expectSoftDeletesWarning(Request request, String indexName) {
    final List<String> esExpectedWarnings = Collections.singletonList("Creating indices with soft-deletes disabled is deprecated and will be removed in future Elasticsearch versions. " + "Please do not specify value for setting [index.soft_deletes.enabled] of index [" + indexName + "].");
    final List<String> opensearchExpectedWarnings = Collections.singletonList("Creating indices with soft-deletes disabled is deprecated and will be removed in future OpenSearch versions. " + "Please do not specify value for setting [index.soft_deletes.enabled] of index [" + indexName + "].");
    final Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
    if (nodeVersions.stream().allMatch(version -> version.onOrAfter(LegacyESVersion.V_7_6_0) && version.before(Version.V_1_0_0))) {
        requestOptions.setWarningsHandler(warnings -> warnings.equals(esExpectedWarnings) == false);
        request.setOptions(requestOptions);
    } else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(LegacyESVersion.V_7_6_0) && version.before(Version.V_1_0_0))) {
        requestOptions.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(esExpectedWarnings) == false);
        request.setOptions(requestOptions);
    }
    if (nodeVersions.stream().allMatch(version -> version.onOrAfter(Version.V_1_0_0))) {
        requestOptions.setWarningsHandler(warnings -> warnings.equals(opensearchExpectedWarnings) == false);
        request.setOptions(requestOptions);
    } else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(Version.V_1_0_0))) {
        requestOptions.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(opensearchExpectedWarnings) == false);
        request.setOptions(requestOptions);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) WarningsHandler(org.opensearch.client.WarningsHandler) Collections.unmodifiableList(java.util.Collections.unmodifiableList) HttpStatus(org.apache.http.HttpStatus) SnapshotState(org.opensearch.snapshots.SnapshotState) Version(org.opensearch.Version) KeyStoreException(java.security.KeyStoreException) Request(org.opensearch.client.Request) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Header(org.apache.http.Header) EntityUtils(org.apache.http.util.EntityUtils) Strings(org.opensearch.common.Strings) Collections.sort(java.util.Collections.sort) XContentParser(org.opensearch.common.xcontent.XContentParser) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) Matchers.everyItem(org.hamcrest.Matchers.everyItem) SSLContexts(org.apache.http.ssl.SSLContexts) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) RestClientBuilder(org.opensearch.client.RestClientBuilder) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ReplicationTracker(org.opensearch.index.seqno.ReplicationTracker) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CheckedRunnable(org.opensearch.common.CheckedRunnable) IndexSettings(org.opensearch.index.IndexSettings) XContentType(org.opensearch.common.xcontent.XContentType) Pattern(java.util.regex.Pattern) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Response(org.opensearch.client.Response) Matchers.in(org.hamcrest.Matchers.in) PathUtils(org.opensearch.common.io.PathUtils) Builder(org.opensearch.client.RequestOptions.Builder) RequestOptions(org.opensearch.client.RequestOptions) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) Before(org.junit.Before) SetOnce(org.apache.lucene.util.SetOnce) ListTasksAction(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksAction) Files(java.nio.file.Files) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) InputStreamReader(java.io.InputStreamReader) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) HttpPut(org.apache.http.client.methods.HttpPut) ResponseException(org.opensearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) BufferedReader(java.io.BufferedReader) HttpHost(org.apache.http.HttpHost) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) Collections(java.util.Collections) InputStream(java.io.InputStream) RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with Builder

use of org.opensearch.client.RequestOptions.Builder in project OpenSearch by opensearch-project.

the class OpenSearchRestTestCase method allowTypesRemovalWarnings.

/**
 * Creates RequestOptions designed to ignore [types removal] warnings but nothing else
 * @deprecated this method is only required while we deprecate types and can be removed in 8.0
 */
@Deprecated
public static RequestOptions allowTypesRemovalWarnings() {
    Builder builder = RequestOptions.DEFAULT.toBuilder();
    builder.setWarningsHandler(new WarningsHandler() {

        @Override
        public boolean warningsShouldFailRequest(List<String> warnings) {
            for (String warning : warnings) {
                if (warning.startsWith("[types removal]") == false) {
                    // Something other than a types removal message - return true
                    return true;
                }
            }
            return false;
        }
    });
    return builder.build();
}
Also used : RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) WarningsHandler(org.opensearch.client.WarningsHandler)

Example 4 with Builder

use of org.opensearch.client.RequestOptions.Builder in project OpenSearch by opensearch-project.

the class OpenSearchRestTestCase method expectTranslogRetentionWarning.

protected static void expectTranslogRetentionWarning(Request request) {
    final List<String> expectedWarnings = Collections.singletonList("Translog retention settings [index.translog.retention.age] " + "and [index.translog.retention.size] are deprecated and effectively ignored. They will be removed in a future version.");
    final Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
    if (nodeVersions.stream().allMatch(version -> version.onOrAfter(LegacyESVersion.V_7_7_0))) {
        requestOptions.setWarningsHandler(warnings -> warnings.equals(expectedWarnings) == false);
        request.setOptions(requestOptions);
    } else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(LegacyESVersion.V_7_7_0))) {
        requestOptions.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(expectedWarnings) == false);
        request.setOptions(requestOptions);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) WarningsHandler(org.opensearch.client.WarningsHandler) Collections.unmodifiableList(java.util.Collections.unmodifiableList) HttpStatus(org.apache.http.HttpStatus) SnapshotState(org.opensearch.snapshots.SnapshotState) Version(org.opensearch.Version) KeyStoreException(java.security.KeyStoreException) Request(org.opensearch.client.Request) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Header(org.apache.http.Header) EntityUtils(org.apache.http.util.EntityUtils) Strings(org.opensearch.common.Strings) Collections.sort(java.util.Collections.sort) XContentParser(org.opensearch.common.xcontent.XContentParser) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) Matchers.everyItem(org.hamcrest.Matchers.everyItem) SSLContexts(org.apache.http.ssl.SSLContexts) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) RestClientBuilder(org.opensearch.client.RestClientBuilder) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ReplicationTracker(org.opensearch.index.seqno.ReplicationTracker) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CheckedRunnable(org.opensearch.common.CheckedRunnable) IndexSettings(org.opensearch.index.IndexSettings) XContentType(org.opensearch.common.xcontent.XContentType) Pattern(java.util.regex.Pattern) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Response(org.opensearch.client.Response) Matchers.in(org.hamcrest.Matchers.in) PathUtils(org.opensearch.common.io.PathUtils) Builder(org.opensearch.client.RequestOptions.Builder) RequestOptions(org.opensearch.client.RequestOptions) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) Before(org.junit.Before) SetOnce(org.apache.lucene.util.SetOnce) ListTasksAction(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksAction) Files(java.nio.file.Files) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) InputStreamReader(java.io.InputStreamReader) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) HttpPut(org.apache.http.client.methods.HttpPut) ResponseException(org.opensearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) BufferedReader(java.io.BufferedReader) HttpHost(org.apache.http.HttpHost) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) Collections(java.util.Collections) InputStream(java.io.InputStream) RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 5 with Builder

use of org.opensearch.client.RequestOptions.Builder in project OpenSearch by opensearch-project.

the class OpenSearchRestTestCase method syncedFlush.

protected void syncedFlush(String indexName, boolean retryOnConflict) throws Exception {
    final Request request = new Request("POST", indexName + "/_flush/synced");
    final Builder options = RequestOptions.DEFAULT.toBuilder();
    // 8.0 kept in warning message for legacy purposes TODO: changge to 3.0
    final List<String> warningMessage = Arrays.asList("Synced flush is deprecated and will be removed in 8.0. Use flush at _/flush or /{index}/_flush instead.");
    final List<String> expectedWarnings = Arrays.asList("Synced flush was removed and a normal flush was performed instead. This transition will be removed in a future version.");
    if (nodeVersions.stream().allMatch(version -> version.onOrAfter(Version.V_2_0_0))) {
        options.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(expectedWarnings) == false);
    } else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(LegacyESVersion.V_7_6_0))) {
        options.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(expectedWarnings) == false && warnings.equals(warningMessage) == false);
    }
    request.setOptions(options);
    // We have to spin synced-flush requests here because we fire the global checkpoint sync for the last write operation.
    // A synced-flush request considers the global checkpoint sync as an going operation because it acquires a shard permit.
    assertBusy(() -> {
        try {
            Response resp = client().performRequest(request);
            if (retryOnConflict) {
                Map<String, Object> result = ObjectPath.createFromResponse(resp).evaluate("_shards");
                assertThat(result.get("failed"), equalTo(0));
            }
        } catch (ResponseException ex) {
            assertThat(ex.getResponse().getStatusLine(), equalTo(HttpStatus.SC_CONFLICT));
            if (retryOnConflict) {
                // cause assert busy to retry
                throw new AssertionError(ex);
            }
        }
    });
    // ensure the global checkpoint is synced; otherwise we might trim the commit with syncId
    ensureGlobalCheckpointSynced(indexName);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) WarningsHandler(org.opensearch.client.WarningsHandler) Collections.unmodifiableList(java.util.Collections.unmodifiableList) HttpStatus(org.apache.http.HttpStatus) SnapshotState(org.opensearch.snapshots.SnapshotState) Version(org.opensearch.Version) KeyStoreException(java.security.KeyStoreException) Request(org.opensearch.client.Request) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Header(org.apache.http.Header) EntityUtils(org.apache.http.util.EntityUtils) Strings(org.opensearch.common.Strings) Collections.sort(java.util.Collections.sort) XContentParser(org.opensearch.common.xcontent.XContentParser) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) Matchers.everyItem(org.hamcrest.Matchers.everyItem) SSLContexts(org.apache.http.ssl.SSLContexts) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) RestClientBuilder(org.opensearch.client.RestClientBuilder) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ReplicationTracker(org.opensearch.index.seqno.ReplicationTracker) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CheckedRunnable(org.opensearch.common.CheckedRunnable) IndexSettings(org.opensearch.index.IndexSettings) XContentType(org.opensearch.common.xcontent.XContentType) Pattern(java.util.regex.Pattern) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Response(org.opensearch.client.Response) Matchers.in(org.hamcrest.Matchers.in) PathUtils(org.opensearch.common.io.PathUtils) Builder(org.opensearch.client.RequestOptions.Builder) RequestOptions(org.opensearch.client.RequestOptions) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) Before(org.junit.Before) SetOnce(org.apache.lucene.util.SetOnce) ListTasksAction(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksAction) Files(java.nio.file.Files) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) InputStreamReader(java.io.InputStreamReader) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) HttpPut(org.apache.http.client.methods.HttpPut) ResponseException(org.opensearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) BufferedReader(java.io.BufferedReader) HttpHost(org.apache.http.HttpHost) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) Collections(java.util.Collections) InputStream(java.io.InputStream) Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Request(org.opensearch.client.Request) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Aggregations

Builder (org.opensearch.client.RequestOptions.Builder)6 RestClientBuilder (org.opensearch.client.RestClientBuilder)6 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)6 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Files (java.nio.file.Files)3 Path (java.nio.file.Path)3 KeyManagementException (java.security.KeyManagementException)3 KeyStore (java.security.KeyStore)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Collections.sort (java.util.Collections.sort)3 Collections.unmodifiableList (java.util.Collections.unmodifiableList)3