Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fix index not found reported as server error bug #1355

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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