Search in sources :

Example 1 with ClusterStateHealth

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

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

the class DecisionsImpactOnClusterHealthTests method runAllocationTest.

private ClusterState runAllocationTest(final Settings settings, final String indexName, final Set<AllocationDecider> allocationDeciders, final ClusterHealthStatus expectedStatus) throws IOException {
    final String clusterName = "test-cluster";
    final AllocationService allocationService = newAllocationService(settings, allocationDeciders);
    logger.info("Building initial routing table");
    final int numShards = randomIntBetween(1, 5);
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder(indexName).settings(settings(Version.CURRENT)).numberOfShards(numShards).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metadata.index(indexName)).build();
    ClusterState clusterState = ClusterState.builder(new ClusterName(clusterName)).metadata(metadata).routingTable(routingTable).build();
    logger.info("--> adding nodes");
    // we need at least as many nodes as shards for the THROTTLE case, because
    // once a shard has been throttled on a node, that node no longer accepts
    // any allocations on it
    final DiscoveryNodes.Builder discoveryNodes = DiscoveryNodes.builder();
    for (int i = 0; i < numShards; i++) {
        discoveryNodes.add(newNode("node" + i));
    }
    clusterState = ClusterState.builder(clusterState).nodes(discoveryNodes).build();
    logger.info("--> do the reroute");
    routingTable = allocationService.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    logger.info("--> assert cluster health");
    ClusterStateHealth health = new ClusterStateHealth(clusterState);
    assertThat(health.getStatus(), equalTo(expectedStatus));
    return clusterState;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterStateHealth(org.opensearch.cluster.health.ClusterStateHealth) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ClusterName(org.opensearch.cluster.ClusterName) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 3 with ClusterStateHealth

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

the class PrimaryTermsTests method applyRerouteResult.

private void applyRerouteResult(ClusterState newClusterState) {
    ClusterState previousClusterState = this.clusterState;
    ClusterState.Builder builder = ClusterState.builder(newClusterState).incrementVersion();
    if (previousClusterState.routingTable() != newClusterState.routingTable()) {
        builder.routingTable(RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1).build());
    }
    if (previousClusterState.metadata() != newClusterState.metadata()) {
        builder.metadata(Metadata.builder(newClusterState.metadata()).version(newClusterState.metadata().version() + 1));
    }
    this.clusterState = builder.build();
    final ClusterStateHealth clusterHealth = new ClusterStateHealth(clusterState);
    logger.info("applied reroute. active shards: p [{}], t [{}], init shards: [{}], relocating: [{}]", clusterHealth.getActivePrimaryShards(), clusterHealth.getActiveShards(), clusterHealth.getInitializingShards(), clusterHealth.getRelocatingShards());
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterStateHealth(org.opensearch.cluster.health.ClusterStateHealth)

Example 4 with ClusterStateHealth

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

the class ClusterHealthResponsesTests method mutateInstance.

@Override
protected ClusterHealthResponse mutateInstance(ClusterHealthResponse instance) {
    String mutate = randomFrom("clusterName", "numberOfPendingTasks", "numberOfInFlightFetch", "delayedUnassignedShards", "taskMaxWaitingTime", "timedOut", "clusterStateHealth");
    switch(mutate) {
        case "clusterName":
            return new ClusterHealthResponse(instance.getClusterName() + randomAlphaOfLengthBetween(2, 5), instance.getNumberOfPendingTasks(), instance.getNumberOfInFlightFetch(), instance.getDelayedUnassignedShards(), instance.getTaskMaxWaitingTime(), instance.isTimedOut(), instance.getClusterStateHealth());
        case "numberOfPendingTasks":
            return new ClusterHealthResponse(instance.getClusterName(), instance.getNumberOfPendingTasks() + between(1, 10), instance.getNumberOfInFlightFetch(), instance.getDelayedUnassignedShards(), instance.getTaskMaxWaitingTime(), instance.isTimedOut(), instance.getClusterStateHealth());
        case "numberOfInFlightFetch":
            return new ClusterHealthResponse(instance.getClusterName(), instance.getNumberOfPendingTasks(), instance.getNumberOfInFlightFetch() + between(1, 10), instance.getDelayedUnassignedShards(), instance.getTaskMaxWaitingTime(), instance.isTimedOut(), instance.getClusterStateHealth());
        case "delayedUnassignedShards":
            return new ClusterHealthResponse(instance.getClusterName(), instance.getNumberOfPendingTasks(), instance.getNumberOfInFlightFetch(), instance.getDelayedUnassignedShards() + between(1, 10), instance.getTaskMaxWaitingTime(), instance.isTimedOut(), instance.getClusterStateHealth());
        case "taskMaxWaitingTime":
            return new ClusterHealthResponse(instance.getClusterName(), instance.getNumberOfPendingTasks(), instance.getNumberOfInFlightFetch(), instance.getDelayedUnassignedShards(), new TimeValue(instance.getTaskMaxWaitingTime().millis() + between(1, 10)), instance.isTimedOut(), instance.getClusterStateHealth());
        case "timedOut":
            return new ClusterHealthResponse(instance.getClusterName(), instance.getNumberOfPendingTasks(), instance.getNumberOfInFlightFetch(), instance.getDelayedUnassignedShards(), instance.getTaskMaxWaitingTime(), instance.isTimedOut() == false, instance.getClusterStateHealth());
        case "clusterStateHealth":
            ClusterStateHealth state = instance.getClusterStateHealth();
            ClusterStateHealth newState = new ClusterStateHealth(state.getActivePrimaryShards() + between(1, 10), state.getActiveShards(), state.getRelocatingShards(), state.getInitializingShards(), state.getUnassignedShards(), state.getNumberOfNodes(), state.getNumberOfDataNodes(), state.hasDiscoveredMaster(), state.getActiveShardsPercent(), state.getStatus(), state.getIndices());
            return new ClusterHealthResponse(instance.getClusterName(), instance.getNumberOfPendingTasks(), instance.getNumberOfInFlightFetch(), instance.getDelayedUnassignedShards(), instance.getTaskMaxWaitingTime(), instance.isTimedOut(), newState);
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : ClusterStateHealth(org.opensearch.cluster.health.ClusterStateHealth) TimeValue(org.opensearch.common.unit.TimeValue)

Example 5 with ClusterStateHealth

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

Aggregations

ClusterStateHealth (org.opensearch.cluster.health.ClusterStateHealth)9 ClusterState (org.opensearch.cluster.ClusterState)4 HashMap (java.util.HashMap)2 ClusterName (org.opensearch.cluster.ClusterName)2 ClusterIndexHealth (org.opensearch.cluster.health.ClusterIndexHealth)2 RoutingTable (org.opensearch.cluster.routing.RoutingTable)2 ArrayList (java.util.ArrayList)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 LegacyESVersion (org.opensearch.LegacyESVersion)1 Version (org.opensearch.Version)1 NodeInfo (org.opensearch.action.admin.cluster.node.info.NodeInfo)1 NodeStats (org.opensearch.action.admin.cluster.node.stats.NodeStats)1 CommonStats (org.opensearch.action.admin.indices.stats.CommonStats)1 ShardStats (org.opensearch.action.admin.indices.stats.ShardStats)1 ClusterHealthStatus (org.opensearch.cluster.health.ClusterHealthStatus)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 Metadata (org.opensearch.cluster.metadata.Metadata)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)1 RoutingNodes (org.opensearch.cluster.routing.RoutingNodes)1