Search in sources :

Example 1 with ClusterHealthResponse

use of org.opensearch.action.admin.cluster.health.ClusterHealthResponse in project fess-crawler by codelibs.

the class FesenClient method connect.

public void connect() {
    destroy();
    client = createClient();
    final ClusterHealthResponse healthResponse = get(c -> c.admin().cluster().prepareHealth(targetIndices).setWaitForYellowStatus().execute());
    if (!healthResponse.isTimedOut()) {
        onConnectListenerList.forEach(l -> {
            try {
                l.onConnect();
            } catch (final Exception e) {
                logger.warn("Failed to invoke " + l, e);
            }
        });
        connected = true;
    } else {
        logger.warn("Could not connect to {}", address);
    }
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) OpenSearchException(org.opensearch.OpenSearchException) EsAccessException(org.codelibs.fess.crawler.exception.EsAccessException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 2 with ClusterHealthResponse

use of org.opensearch.action.admin.cluster.health.ClusterHealthResponse in project OpenSearch by opensearch-project.

the class ClusterClientIT method testClusterHealthNotFoundIndex.

public void testClusterHealthNotFoundIndex() throws IOException {
    createIndex("index", Settings.EMPTY);
    ClusterHealthRequest request = new ClusterHealthRequest("notexisted-index");
    request.timeout("5s");
    ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync);
    assertThat(response, notNullValue());
    assertThat(response.isTimedOut(), equalTo(true));
    assertThat(response.status(), equalTo(RestStatus.REQUEST_TIMEOUT));
    assertThat(response.getStatus(), equalTo(ClusterHealthStatus.RED));
    assertNoIndices(response);
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest)

Example 3 with ClusterHealthResponse

use of org.opensearch.action.admin.cluster.health.ClusterHealthResponse in project OpenSearch by opensearch-project.

the class ClusterClientIT method testClusterHealthYellowClusterLevel.

public void testClusterHealthYellowClusterLevel() throws IOException {
    createIndex("index", Settings.EMPTY);
    createIndex("index2", Settings.EMPTY);
    ClusterHealthRequest request = new ClusterHealthRequest();
    request.timeout("5s");
    ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync);
    logger.info("Shard stats\n{}", EntityUtils.toString(client().performRequest(new Request("GET", "/_cat/shards")).getEntity()));
    assertThat(response.getIndices().size(), equalTo(0));
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) ClusterGetSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsRequest) ComponentTemplatesExistRequest(org.opensearch.client.indices.ComponentTemplatesExistRequest) DeleteComponentTemplateRequest(org.opensearch.client.indices.DeleteComponentTemplateRequest) RemoteInfoRequest(org.opensearch.client.cluster.RemoteInfoRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest)

Example 4 with ClusterHealthResponse

use of org.opensearch.action.admin.cluster.health.ClusterHealthResponse in project OpenSearch by opensearch-project.

the class ClusterClientIT method testClusterHealthYellowIndicesLevel.

public void testClusterHealthYellowIndicesLevel() throws IOException {
    String firstIndex = "index";
    String secondIndex = "index2";
    // including another index that we do not assert on, to ensure that we are not
    // accidentally asserting on entire cluster state
    String ignoredIndex = "tasks";
    createIndex(firstIndex, Settings.EMPTY);
    createIndex(secondIndex, Settings.EMPTY);
    if (randomBoolean()) {
        createIndex(ignoredIndex, Settings.EMPTY);
    }
    ClusterHealthRequest request = new ClusterHealthRequest(firstIndex, secondIndex);
    request.timeout("5s");
    request.level(ClusterHealthRequest.Level.INDICES);
    ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync);
    logger.info("Shard stats\n{}", EntityUtils.toString(client().performRequest(new Request("GET", "/_cat/shards")).getEntity()));
    assertYellowShards(response);
    assertThat(response.getIndices().size(), equalTo(2));
    for (Map.Entry<String, ClusterIndexHealth> entry : response.getIndices().entrySet()) {
        assertYellowIndex(entry.getKey(), entry.getValue(), true);
    }
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) ClusterGetSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsRequest) ComponentTemplatesExistRequest(org.opensearch.client.indices.ComponentTemplatesExistRequest) DeleteComponentTemplateRequest(org.opensearch.client.indices.DeleteComponentTemplateRequest) RemoteInfoRequest(org.opensearch.client.cluster.RemoteInfoRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ClusterIndexHealth(org.opensearch.cluster.health.ClusterIndexHealth) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 5 with ClusterHealthResponse

use of org.opensearch.action.admin.cluster.health.ClusterHealthResponse in project OpenSearch by opensearch-project.

the class ClusterClientDocumentationIT method testClusterHealth.

@SuppressWarnings("unused")
public void testClusterHealth() throws IOException {
    RestHighLevelClient client = highLevelClient();
    client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT);
    {
        // tag::health-request
        ClusterHealthRequest request = new ClusterHealthRequest();
    // end::health-request
    }
    {
        // tag::health-request-indices-ctr
        ClusterHealthRequest request = new ClusterHealthRequest("index1", "index2");
    // end::health-request-indices-ctr
    }
    {
        // tag::health-request-indices-setter
        ClusterHealthRequest request = new ClusterHealthRequest();
        request.indices("index1", "index2");
    // end::health-request-indices-setter
    }
    ClusterHealthRequest request = new ClusterHealthRequest();
    // tag::health-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueSeconds(50));
    // <2>
    request.timeout("50s");
    // end::health-request-timeout
    // tag::health-request-master-timeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueSeconds(20));
    // <2>
    request.masterNodeTimeout("20s");
    // end::health-request-master-timeout
    // tag::health-request-wait-status
    // <1>
    request.waitForStatus(ClusterHealthStatus.YELLOW);
    // <2>
    request.waitForYellowStatus();
    // end::health-request-wait-status
    // tag::health-request-wait-events
    // <1>
    request.waitForEvents(Priority.NORMAL);
    // end::health-request-wait-events
    // tag::health-request-level
    // <1>
    request.level(ClusterHealthRequest.Level.SHARDS);
    // end::health-request-level
    // tag::health-request-wait-relocation
    // <1>
    request.waitForNoRelocatingShards(true);
    // end::health-request-wait-relocation
    // tag::health-request-wait-initializing
    // <1>
    request.waitForNoInitializingShards(true);
    // end::health-request-wait-initializing
    // tag::health-request-wait-nodes
    // <1>
    request.waitForNodes("2");
    // <2>
    request.waitForNodes(">=2");
    // <3>
    request.waitForNodes("le(2)");
    // end::health-request-wait-nodes
    // tag::health-request-wait-active
    // <1>
    request.waitForActiveShards(ActiveShardCount.ALL);
    // <2>
    request.waitForActiveShards(1);
    // end::health-request-wait-active
    // tag::health-request-local
    // <1>
    request.local(true);
    // end::health-request-local
    // tag::health-execute
    ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
    // end::health-execute
    assertThat(response.isTimedOut(), equalTo(false));
    assertThat(response.status(), equalTo(RestStatus.OK));
    assertThat(response.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
    assertThat(response, notNullValue());
    // tag::health-response-general
    // <1>
    String clusterName = response.getClusterName();
    // <2>
    ClusterHealthStatus status = response.getStatus();
    // end::health-response-general
    // tag::health-response-request-status
    // <1>
    boolean timedOut = response.isTimedOut();
    // <2>
    RestStatus restStatus = response.status();
    // end::health-response-request-status
    // tag::health-response-nodes
    // <1>
    int numberOfNodes = response.getNumberOfNodes();
    // <2>
    int numberOfDataNodes = response.getNumberOfDataNodes();
    // end::health-response-nodes
    {
        // tag::health-response-shards
        // <1>
        int activeShards = response.getActiveShards();
        // <2>
        int activePrimaryShards = response.getActivePrimaryShards();
        // <3>
        int relocatingShards = response.getRelocatingShards();
        // <4>
        int initializingShards = response.getInitializingShards();
        // <5>
        int unassignedShards = response.getUnassignedShards();
        // <6>
        int delayedUnassignedShards = response.getDelayedUnassignedShards();
        // <7>
        double activeShardsPercent = response.getActiveShardsPercent();
    // end::health-response-shards
    }
    // tag::health-response-task
    // <1>
    TimeValue taskMaxWaitingTime = response.getTaskMaxWaitingTime();
    // <2>
    int numberOfPendingTasks = response.getNumberOfPendingTasks();
    // <3>
    int numberOfInFlightFetch = response.getNumberOfInFlightFetch();
    // end::health-response-task
    // tag::health-response-indices
    // <1>
    Map<String, ClusterIndexHealth> indices = response.getIndices();
    // end::health-response-indices
    {
        // tag::health-response-index
        // <1>
        ClusterIndexHealth index = indices.get("index");
        ClusterHealthStatus indexStatus = index.getStatus();
        int numberOfShards = index.getNumberOfShards();
        int numberOfReplicas = index.getNumberOfReplicas();
        int activeShards = index.getActiveShards();
        int activePrimaryShards = index.getActivePrimaryShards();
        int initializingShards = index.getInitializingShards();
        int relocatingShards = index.getRelocatingShards();
        int unassignedShards = index.getUnassignedShards();
        // end::health-response-index
        // tag::health-response-shard-details
        // <1>
        Map<Integer, ClusterShardHealth> shards = index.getShards();
        ClusterShardHealth shardHealth = shards.get(0);
        int shardId = shardHealth.getShardId();
        ClusterHealthStatus shardStatus = shardHealth.getStatus();
        int active = shardHealth.getActiveShards();
        int initializing = shardHealth.getInitializingShards();
        int unassigned = shardHealth.getUnassignedShards();
        int relocating = shardHealth.getRelocatingShards();
        boolean primaryActive = shardHealth.isPrimaryActive();
    // end::health-response-shard-details
    }
}
Also used : ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ClusterIndexHealth(org.opensearch.cluster.health.ClusterIndexHealth) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) RestStatus(org.opensearch.rest.RestStatus) ClusterShardHealth(org.opensearch.cluster.health.ClusterShardHealth) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) HashMap(java.util.HashMap) Map(java.util.Map) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

ClusterHealthResponse (org.opensearch.action.admin.cluster.health.ClusterHealthResponse)86 Settings (org.opensearch.common.settings.Settings)20 Client (org.opensearch.client.Client)17 ClusterState (org.opensearch.cluster.ClusterState)15 MoveAllocationCommand (org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand)12 SearchResponse (org.opensearch.action.search.SearchResponse)11 ClusterHealthRequest (org.opensearch.action.admin.cluster.health.ClusterHealthRequest)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9 OpenIndexResponse (org.opensearch.action.admin.indices.open.OpenIndexResponse)9 ArrayList (java.util.ArrayList)8 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)8 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)7 ClusterService (org.opensearch.cluster.service.ClusterService)6 IndexSettings (org.opensearch.index.IndexSettings)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 ShardRouting (org.opensearch.cluster.routing.ShardRouting)5 MockTransportService (org.opensearch.test.transport.MockTransportService)5 Map (java.util.Map)4