Skip to content

Commit

Permalink
Fix index not found reported as server error bug (opensearch-project#…
Browse files Browse the repository at this point in the history
…1353)

* Capture IndexNotFoundException and re-throw directly

Signed-off-by: Chen Dai <[email protected]>

* Add IT

Signed-off-by: Chen Dai <[email protected]>

---------

Signed-off-by: Chen Dai <[email protected]>
  • Loading branch information
dai-chen authored Feb 17, 2023
1 parent 114e02a commit 76a8d29
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public void testQueryEndpointShouldFail() throws IOException {
client().performRequest(makePPLRequest("search invalid"));
}

@Test
public void testQueryEndpointShouldFailWithNonExistIndex() throws IOException {
exceptionRule.expect(ResponseException.class);
exceptionRule.expect(hasProperty("response", statusCode(400)));

client().performRequest(makePPLRequest("search source=non_exist_index"));
}

@Test
public void sqlEnableSettingsTest() throws IOException {
String query =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.opensearch.client.node.NodeClient;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.common.settings.Settings;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.IndexSettings;
import org.opensearch.sql.opensearch.mapping.IndexMapping;
import org.opensearch.sql.opensearch.request.OpenSearchRequest;
Expand All @@ -42,15 +42,11 @@ public class OpenSearchNodeClient implements OpenSearchClient {
/** Node client provided by OpenSearch container. */
private final NodeClient client;

/** Index name expression resolver to get concrete index name. */
private final IndexNameExpressionResolver resolver;

/**
* Constructor of ElasticsearchNodeClient.
*/
public OpenSearchNodeClient(NodeClient client) {
this.client = client;
this.resolver = new IndexNameExpressionResolver(client.threadPool().getThreadContext());
}

@Override
Expand Down Expand Up @@ -96,6 +92,9 @@ public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
return Streams.stream(mappingsResponse.mappings().iterator())
.collect(Collectors.toMap(cursor -> cursor.key,
cursor -> new IndexMapping(cursor.value)));
} catch (IndexNotFoundException e) {
// Re-throw directly to be treated as client error finally
throw e;
} catch (Exception e) {
throw new IllegalStateException(
"Failed to read mapping for index pattern [" + indexExpression + "]", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.sql.data.model.ExprIntegerValue;
Expand Down Expand Up @@ -200,8 +201,13 @@ void getIndexMappingsWithIOException() {

@Test
void getIndexMappingsWithNonExistIndex() {
mockNodeClient("test");
assertTrue(client.getIndexMappings("non_exist_index").isEmpty());
when(nodeClient.admin().indices()
.prepareGetMappings(any())
.setLocal(anyBoolean())
.get()
).thenThrow(IndexNotFoundException.class);

assertThrows(IndexNotFoundException.class, () -> client.getIndexMappings("non_exist_index"));
}

@Test
Expand Down Expand Up @@ -375,15 +381,6 @@ public void mockNodeClientIndicesMappings(String indexName, String mappings) {
}
}

public void mockNodeClient(String indexName) {
GetMappingsResponse mockResponse = mock(GetMappingsResponse.class);
when(nodeClient.admin().indices()
.prepareGetMappings(any())
.setLocal(anyBoolean())
.get()).thenReturn(mockResponse);
when(mockResponse.mappings()).thenReturn(ImmutableOpenMap.of());
}

private void mockNodeClientSettings(String indexName, String indexMetadata)
throws IOException {
GetSettingsResponse mockResponse = mock(GetSettingsResponse.class);
Expand Down

0 comments on commit 76a8d29

Please sign in to comment.