From 1430a0aad87852687c78d1dc77e65701fc8af4f8 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Thu, 11 May 2023 14:16:46 -0500 Subject: [PATCH] fix test failures Signed-off-by: Nicholas Walter Knize --- .../java/org/opensearch/BaseOpenSearchException.java | 9 +++++++++ .../search/aggregations/bucket/DateHistogramIT.java | 6 +++--- .../search/aggregations/bucket/HistogramIT.java | 6 +++--- .../search/aggregations/bucket/terms/StringTermsIT.java | 5 +++-- .../main/java/org/opensearch/OpenSearchException.java | 1 - .../action/search/SearchPhaseExecutionException.java | 6 +++--- .../java/org/opensearch/OpenSearchExceptionTests.java | 4 ++-- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/libs/core/src/main/java/org/opensearch/BaseOpenSearchException.java b/libs/core/src/main/java/org/opensearch/BaseOpenSearchException.java index f760e2a602e94..6af113657882a 100644 --- a/libs/core/src/main/java/org/opensearch/BaseOpenSearchException.java +++ b/libs/core/src/main/java/org/opensearch/BaseOpenSearchException.java @@ -80,6 +80,7 @@ public abstract class BaseOpenSearchException extends RuntimeException { protected static final String RESOURCE_METADATA_TYPE_KEY = "opensearch.resource.type"; protected static final String RESOURCE_METADATA_ID_KEY = "opensearch.resource.id"; protected static final Version UNKNOWN_VERSION_ADDED = Version.fromId(0); + protected static final String INDEX_METADATA_KEY = "opensearch.index"; private static final boolean REST_EXCEPTION_SKIP_CAUSE_DEFAULT = false; protected final Map> metadata = new HashMap<>(); protected final Map> headers = new HashMap<>(); @@ -524,4 +525,12 @@ public String getResourceType() { } return null; } + + public String getIndexName() { + List index = getMetadata(INDEX_METADATA_KEY); + if (index != null && index.isEmpty() == false) { + return index.get(0); + } + return null; + } } diff --git a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/DateHistogramIT.java b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/DateHistogramIT.java index 617c5745c9bba..6f97b8ef40ffe 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/DateHistogramIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/DateHistogramIT.java @@ -31,7 +31,7 @@ package org.opensearch.search.aggregations.bucket; -import org.opensearch.OpenSearchException; +import org.opensearch.BaseOpenSearchException; import org.opensearch.action.index.IndexRequestBuilder; import org.opensearch.action.search.SearchPhaseExecutionException; import org.opensearch.action.search.SearchResponse; @@ -650,9 +650,9 @@ public void testSingleValuedFieldOrderedByIllegalAgg() throws Exception { .get(); fail("Expected an exception"); } catch (SearchPhaseExecutionException e) { - OpenSearchException[] rootCauses = e.guessRootCauses(); + BaseOpenSearchException[] rootCauses = e.guessRootCauses(); if (rootCauses.length == 1) { - OpenSearchException rootCause = rootCauses[0]; + BaseOpenSearchException rootCause = rootCauses[0]; if (rootCause instanceof AggregationExecutionException) { AggregationExecutionException aggException = (AggregationExecutionException) rootCause; assertThat(aggException.getMessage(), Matchers.startsWith("Invalid aggregation order path")); diff --git a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/HistogramIT.java b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/HistogramIT.java index dae788abe0d10..e56c2b3c713bd 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/HistogramIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/HistogramIT.java @@ -32,7 +32,7 @@ package org.opensearch.search.aggregations.bucket; import com.carrotsearch.hppc.LongHashSet; -import org.opensearch.OpenSearchException; +import org.opensearch.BaseOpenSearchException; import org.opensearch.action.index.IndexRequestBuilder; import org.opensearch.action.search.SearchPhaseExecutionException; import org.opensearch.action.search.SearchResponse; @@ -689,9 +689,9 @@ public void testSingleValuedFieldOrderedByIllegalAgg() throws Exception { .get(); fail("Expected an exception"); } catch (SearchPhaseExecutionException e) { - OpenSearchException[] rootCauses = e.guessRootCauses(); + BaseOpenSearchException[] rootCauses = e.guessRootCauses(); if (rootCauses.length == 1) { - OpenSearchException rootCause = rootCauses[0]; + BaseOpenSearchException rootCause = rootCauses[0]; if (rootCause instanceof AggregationExecutionException) { AggregationExecutionException aggException = (AggregationExecutionException) rootCause; assertThat(aggException.getMessage(), Matchers.startsWith("Invalid aggregation order path")); diff --git a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/terms/StringTermsIT.java b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/terms/StringTermsIT.java index fa8e823545b36..96b256dc81ce1 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/terms/StringTermsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/bucket/terms/StringTermsIT.java @@ -31,6 +31,7 @@ package org.opensearch.search.aggregations.bucket.terms; +import org.opensearch.BaseOpenSearchException; import org.opensearch.OpenSearchException; import org.opensearch.action.search.SearchPhaseExecutionException; import org.opensearch.action.search.SearchResponse; @@ -420,9 +421,9 @@ public void testSingleValuedFieldOrderedByIllegalAgg() throws Exception { .get(); fail("Expected an exception"); } catch (SearchPhaseExecutionException e) { - OpenSearchException[] rootCauses = e.guessRootCauses(); + BaseOpenSearchException[] rootCauses = e.guessRootCauses(); if (rootCauses.length == 1) { - OpenSearchException rootCause = rootCauses[0]; + BaseOpenSearchException rootCause = rootCauses[0]; if (rootCause instanceof AggregationExecutionException) { AggregationExecutionException aggException = (AggregationExecutionException) rootCause; assertThat(aggException.getMessage(), startsWith("Invalid aggregation order path")); diff --git a/server/src/main/java/org/opensearch/OpenSearchException.java b/server/src/main/java/org/opensearch/OpenSearchException.java index a11a76bb3c904..705a82574ca5e 100644 --- a/server/src/main/java/org/opensearch/OpenSearchException.java +++ b/server/src/main/java/org/opensearch/OpenSearchException.java @@ -88,7 +88,6 @@ public class OpenSearchException extends BaseOpenSearchException implements ToXC */ private static final int CUSTOM_ELASTICSEARCH_EXCEPTIONS_BASE_ID = 10000; - private static final String INDEX_METADATA_KEY = "opensearch.index"; private static final String INDEX_METADATA_KEY_UUID = "opensearch.index_uuid"; private static final String SHARD_METADATA_KEY = "opensearch.shard"; diff --git a/server/src/main/java/org/opensearch/action/search/SearchPhaseExecutionException.java b/server/src/main/java/org/opensearch/action/search/SearchPhaseExecutionException.java index 6b3decba09c45..b129536930c56 100644 --- a/server/src/main/java/org/opensearch/action/search/SearchPhaseExecutionException.java +++ b/server/src/main/java/org/opensearch/action/search/SearchPhaseExecutionException.java @@ -123,7 +123,7 @@ public Throwable getCause() { Throwable cause = super.getCause(); if (cause == null) { // fall back to guessed root cause - for (OpenSearchException rootCause : guessRootCauses()) { + for (BaseOpenSearchException rootCause : guessRootCauses()) { return rootCause; } } @@ -174,14 +174,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } @Override - public OpenSearchException[] guessRootCauses() { + public BaseOpenSearchException[] guessRootCauses() { ShardOperationFailedException[] failures = ExceptionsHelper.groupBy(shardFailures); List rootCauses = new ArrayList<>(failures.length); for (ShardOperationFailedException failure : failures) { BaseOpenSearchException[] guessRootCauses = BaseOpenSearchException.guessRootCauses(failure.getCause()); rootCauses.addAll(Arrays.asList(guessRootCauses)); } - return rootCauses.toArray(new OpenSearchException[0]); + return rootCauses.toArray(new BaseOpenSearchException[0]); } @Override diff --git a/server/src/test/java/org/opensearch/OpenSearchExceptionTests.java b/server/src/test/java/org/opensearch/OpenSearchExceptionTests.java index 203903f56b280..adc75f917c751 100644 --- a/server/src/test/java/org/opensearch/OpenSearchExceptionTests.java +++ b/server/src/test/java/org/opensearch/OpenSearchExceptionTests.java @@ -175,14 +175,14 @@ public void testGuessRootCause() { "all shards failed", new ShardSearchFailure[] { failure, failure1, failure2 } ); - final OpenSearchException[] rootCauses = ex.guessRootCauses(); + final BaseOpenSearchException[] rootCauses = ex.guessRootCauses(); assertEquals(rootCauses.length, 2); assertEquals(BaseOpenSearchException.getExceptionName(rootCauses[0]), "parsing_exception"); assertEquals(rootCauses[0].getMessage(), "foobar"); assertEquals(1, ((ParsingException) rootCauses[0]).getLineNumber()); assertEquals(2, ((ParsingException) rootCauses[0]).getColumnNumber()); assertEquals("query_shard_exception", BaseOpenSearchException.getExceptionName(rootCauses[1])); - assertEquals("foo1", rootCauses[1].getIndex().getName()); + assertEquals("foo1", rootCauses[1].getIndexName()); assertEquals("foobar", rootCauses[1].getMessage()); }