diff --git a/server/src/main/java/org/opensearch/action/admin/indices/searchonly/SearchOnlyRequestBuilder.java b/server/src/main/java/org/opensearch/action/admin/indices/searchonly/SearchOnlyRequestBuilder.java index 72152da8dde0c..1ee4552390c72 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/searchonly/SearchOnlyRequestBuilder.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/searchonly/SearchOnlyRequestBuilder.java @@ -2,8 +2,8 @@ import org.opensearch.action.ActionRequestBuilder; import org.opensearch.action.support.clustermanager.AcknowledgedResponse; -import org.opensearch.client.OpenSearchClient; import org.opensearch.common.annotation.PublicApi; +import org.opensearch.transport.client.OpenSearchClient; @PublicApi(since = "1.0.0") public class SearchOnlyRequestBuilder extends ActionRequestBuilder { diff --git a/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java b/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java index 1a3c657f5b1b8..fad504a476511 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java @@ -146,7 +146,7 @@ protected void clusterManagerOperation( final String customDataPath = IndexMetadata.INDEX_DATA_PATH_SETTING.get(state.metadata().index(index).getSettings()); for (IndexShardRoutingTable routing : indexShardRoutingTables) { final int shardId = routing.shardId().id(); - ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing); + ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing, state.metadata().index(index)); if (request.shardStatuses().contains(shardHealth.getStatus())) { shardsToFetch.add(Tuple.tuple(routing.shardId(), customDataPath)); } diff --git a/server/src/main/java/org/opensearch/cluster/health/ClusterIndexHealth.java b/server/src/main/java/org/opensearch/cluster/health/ClusterIndexHealth.java index e38f90fd45e00..6815ee92039be 100644 --- a/server/src/main/java/org/opensearch/cluster/health/ClusterIndexHealth.java +++ b/server/src/main/java/org/opensearch/cluster/health/ClusterIndexHealth.java @@ -156,7 +156,7 @@ public ClusterIndexHealth(final IndexMetadata indexMetadata, final IndexRoutingT shards = new HashMap<>(); for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) { int shardId = shardRoutingTable.shardId().id(); - shards.put(shardId, new ClusterShardHealth(shardId, shardRoutingTable)); + shards.put(shardId, new ClusterShardHealth(shardId, shardRoutingTable, indexMetadata)); } // update the index status @@ -218,7 +218,7 @@ public ClusterIndexHealth( if (isShardLevelHealthRequired) { for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { int shardId = indexShardRoutingTable.shardId().id(); - ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, indexShardRoutingTable); + ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, indexShardRoutingTable, indexMetadata); if (shardHealth.isPrimaryActive()) { computeActivePrimaryShards++; } @@ -268,7 +268,8 @@ public ClusterIndexHealth( ClusterHealthStatus shardHealth = ClusterShardHealth.getShardHealth( primaryShard, activeShardsPerShardId, - shardRoutingCountPerShardId + shardRoutingCountPerShardId, + indexMetadata ); computeStatus = getIndexHealthStatus(shardHealth, computeStatus); } diff --git a/server/src/main/java/org/opensearch/cluster/health/ClusterShardHealth.java b/server/src/main/java/org/opensearch/cluster/health/ClusterShardHealth.java index 1ba99cf7a98f0..1fb72d95b61e4 100644 --- a/server/src/main/java/org/opensearch/cluster/health/ClusterShardHealth.java +++ b/server/src/main/java/org/opensearch/cluster/health/ClusterShardHealth.java @@ -32,6 +32,7 @@ package org.opensearch.cluster.health; +import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.cluster.routing.IndexShardRoutingTable; import org.opensearch.cluster.routing.RecoverySource; import org.opensearch.cluster.routing.ShardRouting; @@ -113,7 +114,7 @@ public final class ClusterShardHealth implements Writeable, ToXContentFragment { private int delayedUnassignedShards; private final boolean primaryActive; - public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardRoutingTable) { + public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardRoutingTable, final IndexMetadata indexMetadata) { this.shardId = shardId; int computeActiveShards = 0; int computeRelocatingShards = 0; @@ -139,13 +140,13 @@ public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardR } } final ShardRouting primaryRouting = shardRoutingTable.primaryShard(); - this.status = getShardHealth(primaryRouting, computeActiveShards, shardRoutingTable.size()); + this.status = getShardHealth(primaryRouting, computeActiveShards, shardRoutingTable.size(), indexMetadata); this.activeShards = computeActiveShards; this.relocatingShards = computeRelocatingShards; this.initializingShards = computeInitializingShards; this.unassignedShards = computeUnassignedShards; this.delayedUnassignedShards = computeDelayedUnassignedShards; - this.primaryActive = primaryRouting.active(); + this.primaryActive = primaryRouting != null && primaryRouting.active(); } public ClusterShardHealth(final StreamInput in) throws IOException { @@ -230,16 +231,23 @@ public void writeTo(final StreamOutput out) throws IOException { * Shard health is RED when the primary is not active. *

*/ - public static ClusterHealthStatus getShardHealth(final ShardRouting primaryRouting, final int activeShards, final int totalShards) { - // TO DO - // assert primaryRouting != null : "Primary shard routing can't be null"; + public static ClusterHealthStatus getShardHealth(final ShardRouting primaryRouting, final int activeShards, final int totalShards, final IndexMetadata indexMetadata) { + if (primaryRouting == null) { + boolean isSearchOnlyEnabled = indexMetadata.getSettings() + .getAsBoolean(IndexMetadata.INDEX_BLOCKS_SEARCH_ONLY_SETTING.getKey(), false); + return isSearchOnlyEnabled ? ClusterHealthStatus.GREEN : ClusterHealthStatus.RED; + } + if (primaryRouting.active()) { + // If primary is active, check if all replicas are active if (activeShards == totalShards) { return ClusterHealthStatus.GREEN; } else { + // Primary is active but some replicas are not return ClusterHealthStatus.YELLOW; } } else { + // Primary is not active, determine health based on recovery state return getInactivePrimaryHealth(primaryRouting); } } diff --git a/server/src/main/java/org/opensearch/rest/action/admin/indices/RestSearchonlyAction.java b/server/src/main/java/org/opensearch/rest/action/admin/indices/RestSearchonlyAction.java index cb103197ce22c..55696ca6deb4d 100644 --- a/server/src/main/java/org/opensearch/rest/action/admin/indices/RestSearchonlyAction.java +++ b/server/src/main/java/org/opensearch/rest/action/admin/indices/RestSearchonlyAction.java @@ -1,10 +1,10 @@ package org.opensearch.rest.action.admin.indices; -import org.opensearch.client.node.NodeClient; import org.opensearch.core.common.Strings; import org.opensearch.rest.BaseRestHandler; import org.opensearch.rest.RestRequest; import org.opensearch.rest.action.RestToXContentListener; +import org.opensearch.transport.client.node.NodeClient; import java.util.List;