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()]));
}
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;
}
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());
}
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();
}
}
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));
}
Aggregations