Search in sources :

Example 1 with ClusterIndexHealth

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

the class ClusterHealthResponsesTests method testVersionCompatibleSerialization.

public void testVersionCompatibleSerialization() throws IOException {
    boolean hasDiscoveredMaster = false;
    int indicesSize = randomInt(20);
    Map<String, ClusterIndexHealth> indices = new HashMap<>(indicesSize);
    if ("indices".equals(level) || "shards".equals(level)) {
        for (int i = 0; i < indicesSize; i++) {
            String indexName = randomAlphaOfLengthBetween(1, 5) + i;
            indices.put(indexName, ClusterIndexHealthTests.randomIndexHealth(indexName, level));
        }
    }
    ClusterStateHealth stateHealth = new ClusterStateHealth(randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), hasDiscoveredMaster, randomDoubleBetween(0d, 100d, true), randomFrom(ClusterHealthStatus.values()), indices);
    // Create the Cluster Health Response object with discovered master as false,
    // to verify serialization puts default value for the field
    ClusterHealthResponse clusterHealth = new ClusterHealthResponse("test-cluster", randomInt(100), randomInt(100), randomInt(100), TimeValue.timeValueMillis(randomInt(10000)), randomBoolean(), stateHealth);
    BytesStreamOutput out_lt_1_0 = new BytesStreamOutput();
    Version old_version = VersionUtils.randomVersionBetween(random(), LegacyESVersion.V_7_0_0, LegacyESVersion.V_7_8_0);
    out_lt_1_0.setVersion(old_version);
    clusterHealth.writeTo(out_lt_1_0);
    BytesStreamOutput out_gt_1_0 = new BytesStreamOutput();
    Version new_version = VersionUtils.randomVersionBetween(random(), Version.V_1_0_0, Version.CURRENT);
    out_gt_1_0.setVersion(new_version);
    clusterHealth.writeTo(out_gt_1_0);
    // The serialized output byte stream will not be same; and different by a boolean field "discovered_master"
    assertNotEquals(out_lt_1_0.size(), out_gt_1_0.size());
    assertThat(out_gt_1_0.size() - out_lt_1_0.size(), Matchers.equalTo(1));
    // Input stream constructed from Version 6_8 or less will not have field "discovered_master";
    // hence fallback to default as no value retained
    StreamInput in_lt_6_8 = out_lt_1_0.bytes().streamInput();
    in_lt_6_8.setVersion(old_version);
    clusterHealth = ClusterHealthResponse.readResponseFrom(in_lt_6_8);
    assertThat(clusterHealth.hasDiscoveredMaster(), Matchers.equalTo(true));
    // Input stream constructed from Version 7_0 and above will have field "discovered_master"; hence value will be retained
    StreamInput in_gt_7_0 = out_gt_1_0.bytes().streamInput();
    in_gt_7_0.setVersion(new_version);
    clusterHealth = ClusterHealthResponse.readResponseFrom(in_gt_7_0);
    assertThat(clusterHealth.hasDiscoveredMaster(), Matchers.equalTo(hasDiscoveredMaster));
}
Also used : ClusterStateHealth(org.opensearch.cluster.health.ClusterStateHealth) HashMap(java.util.HashMap) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) ClusterIndexHealth(org.opensearch.cluster.health.ClusterIndexHealth) StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 2 with ClusterIndexHealth

use of org.opensearch.cluster.health.ClusterIndexHealth 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 3 with ClusterIndexHealth

use of org.opensearch.cluster.health.ClusterIndexHealth 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 4 with ClusterIndexHealth

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

the class RestIndicesActionTests method testBuildTable.

public void testBuildTable() {
    final int numIndices = randomIntBetween(3, 20);
    final Map<String, Settings> indicesSettings = new LinkedHashMap<>();
    final Map<String, IndexMetadata> indicesMetadatas = new LinkedHashMap<>();
    final Map<String, ClusterIndexHealth> indicesHealths = new LinkedHashMap<>();
    final Map<String, IndexStats> indicesStats = new LinkedHashMap<>();
    for (int i = 0; i < numIndices; i++) {
        String indexName = "index-" + i;
        Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexSettings.INDEX_SEARCH_THROTTLED.getKey(), randomBoolean()).build();
        indicesSettings.put(indexName, indexSettings);
        IndexMetadata.State indexState = randomBoolean() ? IndexMetadata.State.OPEN : IndexMetadata.State.CLOSE;
        if (frequently()) {
            ClusterHealthStatus healthStatus = randomFrom(ClusterHealthStatus.values());
            int numberOfShards = randomIntBetween(1, 3);
            int numberOfReplicas = healthStatus == ClusterHealthStatus.YELLOW ? 1 : randomInt(1);
            IndexMetadata indexMetadata = IndexMetadata.builder(indexName).settings(indexSettings).creationDate(System.currentTimeMillis()).numberOfShards(numberOfShards).numberOfReplicas(numberOfReplicas).state(indexState).build();
            indicesMetadatas.put(indexName, indexMetadata);
            if (frequently()) {
                Index index = indexMetadata.getIndex();
                IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index);
                switch(randomFrom(ClusterHealthStatus.values())) {
                    case GREEN:
                        IntStream.range(0, numberOfShards).mapToObj(n -> new ShardId(index, n)).map(shardId -> TestShardRouting.newShardRouting(shardId, "nodeA", true, ShardRoutingState.STARTED)).forEach(indexRoutingTable::addShard);
                        if (numberOfReplicas > 0) {
                            IntStream.range(0, numberOfShards).mapToObj(n -> new ShardId(index, n)).map(shardId -> TestShardRouting.newShardRouting(shardId, "nodeB", false, ShardRoutingState.STARTED)).forEach(indexRoutingTable::addShard);
                        }
                        break;
                    case YELLOW:
                        IntStream.range(0, numberOfShards).mapToObj(n -> new ShardId(index, n)).map(shardId -> TestShardRouting.newShardRouting(shardId, "nodeA", true, ShardRoutingState.STARTED)).forEach(indexRoutingTable::addShard);
                        if (numberOfReplicas > 0) {
                            IntStream.range(0, numberOfShards).mapToObj(n -> new ShardId(index, n)).map(shardId -> TestShardRouting.newShardRouting(shardId, null, false, ShardRoutingState.UNASSIGNED)).forEach(indexRoutingTable::addShard);
                        }
                        break;
                    case RED:
                        break;
                }
                indicesHealths.put(indexName, new ClusterIndexHealth(indexMetadata, indexRoutingTable.build()));
                if (frequently()) {
                    IndexStats indexStats = mock(IndexStats.class);
                    when(indexStats.getPrimaries()).thenReturn(new CommonStats());
                    when(indexStats.getTotal()).thenReturn(new CommonStats());
                    indicesStats.put(indexName, indexStats);
                }
            }
        }
    }
    final RestIndicesAction action = new RestIndicesAction();
    final Table table = action.buildTable(new FakeRestRequest(), indicesSettings, indicesHealths, indicesStats, indicesMetadatas);
    // now, verify the table is correct
    List<Table.Cell> headers = table.getHeaders();
    assertThat(headers.get(0).value, equalTo("health"));
    assertThat(headers.get(1).value, equalTo("status"));
    assertThat(headers.get(2).value, equalTo("index"));
    assertThat(headers.get(3).value, equalTo("uuid"));
    assertThat(headers.get(4).value, equalTo("pri"));
    assertThat(headers.get(5).value, equalTo("rep"));
    final List<List<Table.Cell>> rows = table.getRows();
    assertThat(rows.size(), equalTo(indicesMetadatas.size()));
    for (final List<Table.Cell> row : rows) {
        final String indexName = (String) row.get(2).value;
        ClusterIndexHealth indexHealth = indicesHealths.get(indexName);
        IndexStats indexStats = indicesStats.get(indexName);
        IndexMetadata indexMetadata = indicesMetadatas.get(indexName);
        if (indexHealth != null) {
            assertThat(row.get(0).value, equalTo(indexHealth.getStatus().toString().toLowerCase(Locale.ROOT)));
        } else if (indexStats != null) {
            assertThat(row.get(0).value, equalTo("red*"));
        } else {
            assertThat(row.get(0).value, equalTo(""));
        }
        assertThat(row.get(1).value, equalTo(indexMetadata.getState().toString().toLowerCase(Locale.ROOT)));
        assertThat(row.get(2).value, equalTo(indexName));
        assertThat(row.get(3).value, equalTo(indexMetadata.getIndexUUID()));
        if (indexHealth != null) {
            assertThat(row.get(4).value, equalTo(indexMetadata.getNumberOfShards()));
            assertThat(row.get(5).value, equalTo(indexMetadata.getNumberOfReplicas()));
        } else {
            assertThat(row.get(4).value, nullValue());
            assertThat(row.get(5).value, nullValue());
        }
    }
}
Also used : CommonStats(org.opensearch.action.admin.indices.stats.CommonStats) IntStream(java.util.stream.IntStream) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Table(org.opensearch.common.Table) Version(org.opensearch.Version) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) LinkedHashMap(java.util.LinkedHashMap) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) UUIDs(org.opensearch.common.UUIDs) IndexStats(org.opensearch.action.admin.indices.stats.IndexStats) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) ClusterIndexHealth(org.opensearch.cluster.health.ClusterIndexHealth) Index(org.opensearch.index.Index) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) ShardId(org.opensearch.index.shard.ShardId) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) Mockito.mock(org.mockito.Mockito.mock) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) Index(org.opensearch.index.Index) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) LinkedHashMap(java.util.LinkedHashMap) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) ShardId(org.opensearch.index.shard.ShardId) CommonStats(org.opensearch.action.admin.indices.stats.CommonStats) List(java.util.List) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) Table(org.opensearch.common.Table) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ClusterIndexHealth(org.opensearch.cluster.health.ClusterIndexHealth) IndexStats(org.opensearch.action.admin.indices.stats.IndexStats)

Example 5 with ClusterIndexHealth

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

the class ClusterHealthResponsesTests method createTestInstance.

@Override
protected ClusterHealthResponse createTestInstance() {
    int indicesSize = randomInt(20);
    Map<String, ClusterIndexHealth> indices = new HashMap<>(indicesSize);
    if (ClusterHealthRequest.Level.INDICES.equals(level) || ClusterHealthRequest.Level.SHARDS.equals(level)) {
        for (int i = 0; i < indicesSize; i++) {
            String indexName = randomAlphaOfLengthBetween(1, 5) + i;
            indices.put(indexName, ClusterIndexHealthTests.randomIndexHealth(indexName, level));
        }
    }
    ClusterStateHealth stateHealth = new ClusterStateHealth(randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomInt(100), randomBoolean(), randomDoubleBetween(0d, 100d, true), randomFrom(ClusterHealthStatus.values()), indices);
    return new ClusterHealthResponse(randomAlphaOfLengthBetween(1, 10), randomInt(100), randomInt(100), randomInt(100), TimeValue.timeValueMillis(randomInt(10000)), randomBoolean(), stateHealth);
}
Also used : ClusterStateHealth(org.opensearch.cluster.health.ClusterStateHealth) HashMap(java.util.HashMap) ClusterIndexHealth(org.opensearch.cluster.health.ClusterIndexHealth)

Aggregations

ClusterIndexHealth (org.opensearch.cluster.health.ClusterIndexHealth)8 HashMap (java.util.HashMap)5 Map (java.util.Map)4 ClusterHealthRequest (org.opensearch.action.admin.cluster.health.ClusterHealthRequest)3 ClusterHealthResponse (org.opensearch.action.admin.cluster.health.ClusterHealthResponse)3 ClusterHealthStatus (org.opensearch.cluster.health.ClusterHealthStatus)3 Collections.emptyMap (java.util.Collections.emptyMap)2 Version (org.opensearch.Version)2 CommonStats (org.opensearch.action.admin.indices.stats.CommonStats)2 IndexStats (org.opensearch.action.admin.indices.stats.IndexStats)2 ClusterStateHealth (org.opensearch.cluster.health.ClusterStateHealth)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 Table (org.opensearch.common.Table)2 ZonedDateTime (java.time.ZonedDateTime)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 IntStream (java.util.stream.IntStream)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.nullValue (org.hamcrest.Matchers.nullValue)1