Search in sources :

Example 1 with ClusterHealthStatus

use of org.opensearch.cluster.health.ClusterHealthStatus 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)

Example 2 with ClusterHealthStatus

use of org.opensearch.cluster.health.ClusterHealthStatus in project OpenSearch by opensearch-project.

the class TransportClusterStatsAction method nodeOperation.

@Override
protected ClusterStatsNodeResponse nodeOperation(ClusterStatsNodeRequest nodeRequest) {
    NodeInfo nodeInfo = nodeService.info(true, true, false, true, false, true, false, true, false, false, false);
    NodeStats nodeStats = nodeService.stats(CommonStatsFlags.NONE, true, true, true, false, true, false, false, false, false, false, true, false, false, false, false);
    List<ShardStats> shardsStats = new ArrayList<>();
    for (IndexService indexService : indicesService) {
        for (IndexShard indexShard : indexService) {
            if (indexShard.routingEntry() != null && indexShard.routingEntry().active()) {
                // only report on fully started shards
                CommitStats commitStats;
                SeqNoStats seqNoStats;
                RetentionLeaseStats retentionLeaseStats;
                try {
                    commitStats = indexShard.commitStats();
                    seqNoStats = indexShard.seqNoStats();
                    retentionLeaseStats = indexShard.getRetentionLeaseStats();
                } catch (final AlreadyClosedException e) {
                    // shard is closed - no stats is fine
                    commitStats = null;
                    seqNoStats = null;
                    retentionLeaseStats = null;
                }
                shardsStats.add(new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indicesService.getIndicesQueryCache(), indexShard, SHARD_STATS_FLAGS), commitStats, seqNoStats, retentionLeaseStats));
            }
        }
    }
    ClusterHealthStatus clusterStatus = null;
    if (clusterService.state().nodes().isLocalNodeElectedMaster()) {
        clusterStatus = new ClusterStateHealth(clusterService.state()).getStatus();
    }
    return new ClusterStatsNodeResponse(nodeInfo.getNode(), clusterStatus, nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()]));
}
Also used : ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) ClusterStateHealth(org.opensearch.cluster.health.ClusterStateHealth) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) RetentionLeaseStats(org.opensearch.index.seqno.RetentionLeaseStats) ArrayList(java.util.ArrayList) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) NodeStats(org.opensearch.action.admin.cluster.node.stats.NodeStats) CommonStats(org.opensearch.action.admin.indices.stats.CommonStats) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) NodeInfo(org.opensearch.action.admin.cluster.node.info.NodeInfo) CommitStats(org.opensearch.index.engine.CommitStats)

Example 3 with ClusterHealthStatus

use of org.opensearch.cluster.health.ClusterHealthStatus in project OpenSearch by opensearch-project.

the class CorruptedFileIT method testCorruptPrimaryNoReplica.

/**
 * Tests corruption that happens on a single shard when no replicas are present. We make sure that the primary stays unassigned
 * and all other replicas for the healthy shards happens
 */
public void testCorruptPrimaryNoReplica() throws ExecutionException, InterruptedException, IOException {
    int numDocs = scaledRandomIntBetween(100, 1000);
    internalCluster().ensureAtLeastNumDataNodes(2);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, "0").put(MergePolicyConfig.INDEX_MERGE_ENABLED, false).put(MockFSIndexStore.INDEX_CHECK_INDEX_ON_CLOSE_SETTING.getKey(), // no checkindex - we corrupt shards on
    false).put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.PB))));
    ensureGreen();
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test").setSource("field", "value");
    }
    indexRandom(true, builders);
    ensureGreen();
    // double flush to create safe commit in case of async durability
    assertAllSuccessful(client().admin().indices().prepareFlush().setForce(true).get());
    assertAllSuccessful(client().admin().indices().prepareFlush().setForce(true).get());
    // we have to flush at least once here since we don't corrupt the translog
    SearchResponse countResponse = client().prepareSearch().setSize(0).get();
    assertHitCount(countResponse, numDocs);
    ShardRouting shardRouting = corruptRandomPrimaryFile();
    /*
         * we corrupted the primary shard - now lets make sure we never recover from it successfully
         */
    Settings build = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, "1").build();
    client().admin().indices().prepareUpdateSettings("test").setSettings(build).get();
    client().admin().cluster().prepareReroute().get();
    boolean didClusterTurnRed = waitUntil(() -> {
        ClusterHealthStatus test = client().admin().cluster().health(Requests.clusterHealthRequest("test")).actionGet().getStatus();
        return test == ClusterHealthStatus.RED;
    }, 5, // sometimes on slow nodes the replication / recovery is just dead slow
    TimeUnit.MINUTES);
    final ClusterHealthResponse response = client().admin().cluster().health(Requests.clusterHealthRequest("test")).get();
    if (response.getStatus() != ClusterHealthStatus.RED) {
        logger.info("Cluster turned red in busy loop: {}", didClusterTurnRed);
        logger.info("cluster state:\n{}\n{}", client().admin().cluster().prepareState().get().getState(), client().admin().cluster().preparePendingClusterTasks().get());
    }
    assertThat(response.getStatus(), is(ClusterHealthStatus.RED));
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    GroupShardsIterator<ShardIterator> shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[] { "test" }, false);
    for (ShardIterator iterator : shardIterators) {
        ShardRouting routing;
        while ((routing = iterator.nextOrNull()) != null) {
            if (routing.getId() == shardRouting.getId()) {
                assertThat(routing.state(), equalTo(ShardRoutingState.UNASSIGNED));
            } else {
                assertThat(routing.state(), anyOf(equalTo(ShardRoutingState.RELOCATING), equalTo(ShardRoutingState.STARTED)));
            }
        }
    }
    final List<Path> files = listShardFiles(shardRouting);
    Path corruptedFile = null;
    for (Path file : files) {
        if (file.getFileName().toString().startsWith("corrupted_")) {
            corruptedFile = file;
            break;
        }
    }
    assertThat(corruptedFile, notNullValue());
}
Also used : Path(java.nio.file.Path) ClusterState(org.opensearch.cluster.ClusterState) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) SearchResponse(org.opensearch.action.search.SearchResponse) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) ShardIterator(org.opensearch.cluster.routing.ShardIterator) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 4 with ClusterHealthStatus

use of org.opensearch.cluster.health.ClusterHealthStatus in project OpenSearch by opensearch-project.

the class IndicesShardStoresRequest method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeStringArrayNullable(indices);
    out.writeVInt(statuses.size());
    for (ClusterHealthStatus status : statuses) {
        out.writeByte(status.value());
    }
    indicesOptions.writeIndicesOptions(out);
}
Also used : ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus)

Example 5 with ClusterHealthStatus

use of org.opensearch.cluster.health.ClusterHealthStatus in project OpenSearch by opensearch-project.

the class AllocationService method logClusterHealthStateChange.

private void logClusterHealthStateChange(ClusterStateHealth previousStateHealth, ClusterStateHealth newStateHealth, String reason) {
    ClusterHealthStatus previousHealth = previousStateHealth.getStatus();
    ClusterHealthStatus currentHealth = newStateHealth.getStatus();
    if (!previousHealth.equals(currentHealth)) {
        logger.info("Cluster health status changed from [{}] to [{}] (reason: [{}]).", previousHealth, currentHealth, reason);
    }
}
Also used : ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus)

Aggregations

ClusterHealthStatus (org.opensearch.cluster.health.ClusterHealthStatus)11 HashMap (java.util.HashMap)3 ClusterHealthRequest (org.opensearch.action.admin.cluster.health.ClusterHealthRequest)3 CommonStats (org.opensearch.action.admin.indices.stats.CommonStats)3 ClusterIndexHealth (org.opensearch.cluster.health.ClusterIndexHealth)3 Map (java.util.Map)2 ClusterHealthResponse (org.opensearch.action.admin.cluster.health.ClusterHealthResponse)2 IndexStats (org.opensearch.action.admin.indices.stats.IndexStats)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 Priority (org.opensearch.common.Priority)2 Table (org.opensearch.common.Table)2 Settings (org.opensearch.common.settings.Settings)2 IndexSettings (org.opensearch.index.IndexSettings)2 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)2 Path (java.nio.file.Path)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1