Skip to content

Commit 64bb954

Browse files
Upstream fetch
Signed-off-by: Prudhvi Godithi <[email protected]>
1 parent db5212b commit 64bb954

File tree

7 files changed

+69
-16
lines changed

7 files changed

+69
-16
lines changed

server/src/main/java/org/opensearch/action/admin/indices/searchonly/SearchOnlyRequestBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.opensearch.action.ActionRequestBuilder;
44
import org.opensearch.action.support.clustermanager.AcknowledgedResponse;
5-
import org.opensearch.client.OpenSearchClient;
65
import org.opensearch.common.annotation.PublicApi;
6+
import org.opensearch.transport.client.OpenSearchClient;
77

88
@PublicApi(since = "1.0.0")
99
public class SearchOnlyRequestBuilder extends ActionRequestBuilder<SearchOnlyRequest, AcknowledgedResponse> {

server/src/main/java/org/opensearch/action/admin/indices/searchonly/TransportSearchOnlyAction.java

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.EnumSet;
5454
import java.util.HashMap;
5555
import java.util.List;
56+
import java.util.Locale;
5657
import java.util.Map;
5758
import java.util.UUID;
5859
import java.util.stream.Collectors;
@@ -229,6 +230,7 @@ private void handleShardSyncRequest(NodeSearchOnlyRequest request, TransportChan
229230
);
230231
throw new IllegalStateException(
231232
String.format(
233+
Locale.ROOT,
232234
"Shard [%s] still has %d uncommitted operations after flush. Please wait and retry the scale down operation.",
233235
shard.shardId(),
234236
shard.translogStats().getUncommittedOperations()

server/src/main/java/org/opensearch/action/admin/indices/shards/TransportIndicesShardStoresAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected void clusterManagerOperation(
146146
final String customDataPath = IndexMetadata.INDEX_DATA_PATH_SETTING.get(state.metadata().index(index).getSettings());
147147
for (IndexShardRoutingTable routing : indexShardRoutingTables) {
148148
final int shardId = routing.shardId().id();
149-
ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing);
149+
ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing, state.metadata().index(index));
150150
if (request.shardStatuses().contains(shardHealth.getStatus())) {
151151
shardsToFetch.add(Tuple.tuple(routing.shardId(), customDataPath));
152152
}

server/src/main/java/org/opensearch/cluster/health/ClusterIndexHealth.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public ClusterIndexHealth(final IndexMetadata indexMetadata, final IndexRoutingT
156156
shards = new HashMap<>();
157157
for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) {
158158
int shardId = shardRoutingTable.shardId().id();
159-
shards.put(shardId, new ClusterShardHealth(shardId, shardRoutingTable));
159+
shards.put(shardId, new ClusterShardHealth(shardId, shardRoutingTable, indexMetadata));
160160
}
161161

162162
// update the index status
@@ -218,7 +218,7 @@ public ClusterIndexHealth(
218218
if (isShardLevelHealthRequired) {
219219
for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
220220
int shardId = indexShardRoutingTable.shardId().id();
221-
ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, indexShardRoutingTable);
221+
ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, indexShardRoutingTable, indexMetadata);
222222
if (shardHealth.isPrimaryActive()) {
223223
computeActivePrimaryShards++;
224224
}
@@ -268,7 +268,8 @@ public ClusterIndexHealth(
268268
ClusterHealthStatus shardHealth = ClusterShardHealth.getShardHealth(
269269
primaryShard,
270270
activeShardsPerShardId,
271-
shardRoutingCountPerShardId
271+
shardRoutingCountPerShardId,
272+
indexMetadata
272273
);
273274
computeStatus = getIndexHealthStatus(shardHealth, computeStatus);
274275
}

server/src/main/java/org/opensearch/cluster/health/ClusterShardHealth.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package org.opensearch.cluster.health;
3434

35+
import org.opensearch.cluster.metadata.IndexMetadata;
3536
import org.opensearch.cluster.routing.IndexShardRoutingTable;
3637
import org.opensearch.cluster.routing.RecoverySource;
3738
import org.opensearch.cluster.routing.ShardRouting;
@@ -113,7 +114,7 @@ public final class ClusterShardHealth implements Writeable, ToXContentFragment {
113114
private int delayedUnassignedShards;
114115
private final boolean primaryActive;
115116

116-
public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardRoutingTable) {
117+
public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardRoutingTable, final IndexMetadata indexMetadata) {
117118
this.shardId = shardId;
118119
int computeActiveShards = 0;
119120
int computeRelocatingShards = 0;
@@ -139,13 +140,13 @@ public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardR
139140
}
140141
}
141142
final ShardRouting primaryRouting = shardRoutingTable.primaryShard();
142-
this.status = getShardHealth(primaryRouting, computeActiveShards, shardRoutingTable.size());
143+
this.status = getShardHealth(primaryRouting, computeActiveShards, shardRoutingTable.size(), indexMetadata);
143144
this.activeShards = computeActiveShards;
144145
this.relocatingShards = computeRelocatingShards;
145146
this.initializingShards = computeInitializingShards;
146147
this.unassignedShards = computeUnassignedShards;
147148
this.delayedUnassignedShards = computeDelayedUnassignedShards;
148-
this.primaryActive = primaryRouting.active();
149+
this.primaryActive = primaryRouting != null && primaryRouting.active();
149150
}
150151

151152
public ClusterShardHealth(final StreamInput in) throws IOException {
@@ -230,9 +231,17 @@ public void writeTo(final StreamOutput out) throws IOException {
230231
* Shard health is RED when the primary is not active.
231232
* </p>
232233
*/
233-
public static ClusterHealthStatus getShardHealth(final ShardRouting primaryRouting, final int activeShards, final int totalShards) {
234-
// TO DO
235-
// assert primaryRouting != null : "Primary shard routing can't be null";
234+
public static ClusterHealthStatus getShardHealth(
235+
final ShardRouting primaryRouting,
236+
final int activeShards,
237+
final int totalShards,
238+
final IndexMetadata indexMetadata
239+
) {
240+
if (primaryRouting == null) {
241+
boolean isSearchOnlyEnabled = indexMetadata.getSettings()
242+
.getAsBoolean(IndexMetadata.INDEX_BLOCKS_SEARCH_ONLY_SETTING.getKey(), false);
243+
return isSearchOnlyEnabled ? ClusterHealthStatus.GREEN : ClusterHealthStatus.RED;
244+
}
236245
if (primaryRouting.active()) {
237246
if (activeShards == totalShards) {
238247
return ClusterHealthStatus.GREEN;

server/src/main/java/org/opensearch/rest/action/admin/indices/RestSearchonlyAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.opensearch.rest.action.admin.indices;
22

3-
import org.opensearch.client.node.NodeClient;
43
import org.opensearch.core.common.Strings;
54
import org.opensearch.rest.BaseRestHandler;
65
import org.opensearch.rest.RestRequest;
76
import org.opensearch.rest.action.RestToXContentListener;
7+
import org.opensearch.transport.client.node.NodeClient;
88

99
import java.util.List;
1010

server/src/test/java/org/opensearch/cluster/health/ClusterShardHealthTests.java

+45-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131

3232
package org.opensearch.cluster.health;
3333

34+
import org.opensearch.Version;
35+
import org.opensearch.cluster.metadata.IndexMetadata;
3436
import org.opensearch.cluster.routing.IndexShardRoutingTable;
3537
import org.opensearch.cluster.routing.RecoverySource;
3638
import org.opensearch.cluster.routing.ShardRouting;
3739
import org.opensearch.cluster.routing.ShardRoutingState;
3840
import org.opensearch.cluster.routing.TestShardRouting;
3941
import org.opensearch.cluster.routing.UnassignedInfo;
42+
import org.opensearch.common.settings.Settings;
4043
import org.opensearch.core.common.io.stream.Writeable;
4144
import org.opensearch.core.index.Index;
4245
import org.opensearch.core.index.shard.ShardId;
@@ -64,8 +67,18 @@ public void testClusterShardGreenHealth() {
6467
indexShardRoutingBuilder.addShard(
6568
TestShardRouting.newShardRouting(indexName, shardID, "node_1", null, false, ShardRoutingState.STARTED)
6669
);
70+
71+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName)
72+
.settings(Settings.builder()
73+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
74+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
75+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
76+
)
77+
.creationDate(System.currentTimeMillis());
78+
IndexMetadata indexMetadata = indexMetadataBuilder.build();
6779
IndexShardRoutingTable indexShardRoutingTable = indexShardRoutingBuilder.build();
68-
ClusterShardHealth clusterShardHealth = new ClusterShardHealth(shardID, indexShardRoutingTable);
80+
81+
ClusterShardHealth clusterShardHealth = new ClusterShardHealth(shardID, indexShardRoutingTable, indexMetadata);
6982
assertEquals(2, clusterShardHealth.getActiveShards());
7083
assertEquals(0, clusterShardHealth.getInitializingShards());
7184
assertEquals(0, clusterShardHealth.getRelocatingShards());
@@ -112,7 +125,17 @@ public void testClusterShardYellowHealth() {
112125
)
113126
);
114127
IndexShardRoutingTable indexShardRoutingTable = indexShardRoutingBuilder.build();
115-
ClusterShardHealth clusterShardHealth = new ClusterShardHealth(shardID, indexShardRoutingTable);
128+
129+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName)
130+
.settings(Settings.builder()
131+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
132+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
133+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
134+
)
135+
.creationDate(System.currentTimeMillis());
136+
IndexMetadata indexMetadata = indexMetadataBuilder.build();
137+
138+
ClusterShardHealth clusterShardHealth = new ClusterShardHealth(shardID, indexShardRoutingTable, indexMetadata);
116139
assertEquals(2, clusterShardHealth.getActiveShards());
117140
assertEquals(1, clusterShardHealth.getInitializingShards());
118141
assertEquals(1, clusterShardHealth.getRelocatingShards());
@@ -150,7 +173,17 @@ public void testClusterShardRedHealth() {
150173
TestShardRouting.newShardRouting(indexName, shardID, null, null, false, ShardRoutingState.UNASSIGNED)
151174
);
152175
IndexShardRoutingTable indexShardRoutingTable = indexShardRoutingBuilder.build();
153-
ClusterShardHealth clusterShardHealth = new ClusterShardHealth(shardID, indexShardRoutingTable);
176+
177+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName)
178+
.settings(Settings.builder()
179+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
180+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
181+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
182+
)
183+
.creationDate(System.currentTimeMillis());
184+
IndexMetadata indexMetadata = indexMetadataBuilder.build();
185+
186+
ClusterShardHealth clusterShardHealth = new ClusterShardHealth(shardID, indexShardRoutingTable, indexMetadata);
154187
assertEquals(0, clusterShardHealth.getActiveShards());
155188
assertEquals(0, clusterShardHealth.getInitializingShards());
156189
assertEquals(0, clusterShardHealth.getRelocatingShards());
@@ -161,7 +194,15 @@ public void testClusterShardRedHealth() {
161194
}
162195

163196
public void testShardRoutingNullCheck() {
164-
assertThrows(AssertionError.class, () -> ClusterShardHealth.getShardHealth(null, 0, 0));
197+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder("test")
198+
.settings(Settings.builder()
199+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
200+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
201+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
202+
);
203+
IndexMetadata indexMetadata = indexMetadataBuilder.build();
204+
205+
assertThrows(AssertionError.class, () -> ClusterShardHealth.getShardHealth(null, 0, 0, indexMetadata));
165206
}
166207

167208
@Override

0 commit comments

Comments
 (0)