Skip to content

Commit 54fc3a9

Browse files
Prakash BhardwajPrakash Bhardwaj
authored andcommitted
Return 404 for missing indices in cluster health API instead of RED with timeout
1 parent a033cd1 commit 54fc3a9

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

server/src/internalClusterTest/java/org/opensearch/cluster/ClusterHealthIT.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.opensearch.common.action.ActionFuture;
4747
import org.opensearch.common.settings.Settings;
4848
import org.opensearch.common.unit.TimeValue;
49+
import org.opensearch.index.IndexNotFoundException;
4950
import org.opensearch.test.InternalTestCluster;
5051
import org.opensearch.test.OpenSearchIntegTestCase;
5152

@@ -83,19 +84,17 @@ public void testSimpleLocalHealth() {
8384

8485
public void testHealth() {
8586
logger.info("--> running cluster health on an index that does not exists");
86-
ClusterHealthResponse healthResponse = client().admin()
87-
.cluster()
88-
.prepareHealth("test1")
89-
.setWaitForYellowStatus()
90-
.setTimeout("1s")
91-
.execute()
92-
.actionGet();
93-
assertThat(healthResponse.isTimedOut(), equalTo(true));
94-
assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.RED));
95-
assertThat(healthResponse.getIndices().isEmpty(), equalTo(true));
96-
87+
expectThrows(IndexNotFoundException.class,()->{
88+
client().admin()
89+
.cluster()
90+
.prepareHealth("test1")
91+
.setWaitForYellowStatus()
92+
.setTimeout("1s")
93+
.execute()
94+
.actionGet();
95+
});
9796
logger.info("--> running cluster wide health");
98-
healthResponse = client().admin().cluster().prepareHealth().setWaitForGreenStatus().setTimeout("10s").execute().actionGet();
97+
ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForGreenStatus().setTimeout("10s").execute().actionGet();
9998
assertThat(healthResponse.isTimedOut(), equalTo(false));
10099
assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.GREEN));
101100
assertThat(healthResponse.getIndices().isEmpty(), equalTo(true));
@@ -302,6 +301,7 @@ public void run() {
302301
}
303302

304303
public void testWaitForEventsRetriesIfOtherConditionsNotMet() {
304+
createIndex("index");
305305
final ActionFuture<ClusterHealthResponse> healthResponseFuture = client().admin()
306306
.cluster()
307307
.prepareHealth("index")
@@ -338,7 +338,6 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
338338
});
339339

340340
try {
341-
createIndex("index");
342341
assertFalse(client().admin().cluster().prepareHealth("index").setWaitForGreenStatus().get().isTimedOut());
343342

344343
// at this point the original health response should not have returned: there was never a point where the index was green AND

server/src/main/java/org/opensearch/action/admin/cluster/health/TransportClusterHealthAction.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,9 @@ static int prepareResponse(
419419
}
420420
}
421421
if (CollectionUtils.isEmpty(request.indices()) == false) {
422-
try {
423-
indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.strictExpand(), request);
424-
waitForCounter++;
425-
} catch (IndexNotFoundException e) {
426-
response.setStatus(ClusterHealthStatus.RED); // no indices, make sure its RED
427-
// missing indices, wait a bit more...
428-
}
422+
indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.strictExpand(), request);
423+
waitForCounter++;
424+
429425
}
430426
if (!request.waitForNodes().isEmpty()) {
431427
if (request.waitForNodes().startsWith(">=")) {

0 commit comments

Comments
 (0)