use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.
the class StatsIT method assertShardExecutionState.
private void assertShardExecutionState(SearchResponse response, int expectedFailures) throws Exception {
ShardSearchFailure[] failures = response.getShardFailures();
if (failures.length != expectedFailures) {
for (ShardSearchFailure failure : failures) {
logger.error(new ParameterizedMessage("Shard Failure: {}", failure), failure.getCause());
}
fail("Unexpected shard failures!");
}
assertThat("Not all shards are initialized", response.getSuccessfulShards(), equalTo(response.getTotalShards()));
}
use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.
the class OpenSearchExceptionTests method testFailureToAndFromXContentWithDetails.
public void testFailureToAndFromXContentWithDetails() throws IOException {
final XContent xContent = randomFrom(XContentType.values()).xContent();
Exception failure;
Throwable failureCause;
OpenSearchException expected;
OpenSearchException expectedCause;
OpenSearchException suppressed;
switch(randomIntBetween(0, 6)) {
case // Simple opensearch exception without cause
0:
failure = new NoNodeAvailableException("A");
expected = new OpenSearchException("OpenSearch exception [type=no_node_available_exception, reason=A]");
expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=no_node_available_exception, reason=A]"));
break;
case // Simple opensearch exception with headers (other metadata of type number are not parsed)
1:
failure = new ParsingException(3, 2, "B", null);
((OpenSearchException) failure).addHeader("header_name", "0", "1");
expected = new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=B]");
expected.addHeader("header_name", "0", "1");
suppressed = new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=B]");
suppressed.addHeader("header_name", "0", "1");
expected.addSuppressed(suppressed);
break;
case // OpenSearch exception with a cause, headers and parsable metadata
2:
failureCause = new NullPointerException("var is null");
failure = new ScriptException("C", failureCause, singletonList("stack"), "test", "painless");
((OpenSearchException) failure).addHeader("script_name", "my_script");
expectedCause = new OpenSearchException("OpenSearch exception [type=null_pointer_exception, reason=var is null]");
expected = new OpenSearchException("OpenSearch exception [type=script_exception, reason=C]", expectedCause);
expected.addHeader("script_name", "my_script");
expected.addMetadata("opensearch.lang", "painless");
expected.addMetadata("opensearch.script", "test");
expected.addMetadata("opensearch.script_stack", "stack");
suppressed = new OpenSearchException("OpenSearch exception [type=script_exception, reason=C]");
suppressed.addHeader("script_name", "my_script");
suppressed.addMetadata("opensearch.lang", "painless");
suppressed.addMetadata("opensearch.script", "test");
suppressed.addMetadata("opensearch.script_stack", "stack");
expected.addSuppressed(suppressed);
break;
case // JDK exception without cause
3:
failure = new IllegalStateException("D");
expected = new OpenSearchException("OpenSearch exception [type=illegal_state_exception, reason=D]");
suppressed = new OpenSearchException("OpenSearch exception [type=illegal_state_exception, reason=D]");
expected.addSuppressed(suppressed);
break;
case // JDK exception with cause
4:
failureCause = new RoutingMissingException("idx", "id");
failure = new RuntimeException("E", failureCause);
expectedCause = new OpenSearchException("OpenSearch exception [type=routing_missing_exception, " + "reason=routing is required for [idx]/[id]]");
expectedCause.addMetadata("opensearch.index", "idx");
expectedCause.addMetadata("opensearch.index_uuid", "_na_");
expected = new OpenSearchException("OpenSearch exception [type=runtime_exception, reason=E]", expectedCause);
suppressed = new OpenSearchException("OpenSearch exception [type=runtime_exception, reason=E]");
expected.addSuppressed(suppressed);
break;
case // Wrapped exception with cause
5:
failureCause = new FileAlreadyExistsException("File exists");
failure = new BroadcastShardOperationFailedException(new ShardId("_index", "_uuid", 5), "F", failureCause);
expected = new OpenSearchException("OpenSearch exception [type=file_already_exists_exception, reason=File exists]");
suppressed = new OpenSearchException("OpenSearch exception [type=file_already_exists_exception, reason=File exists]");
expected.addSuppressed(suppressed);
break;
case // SearchPhaseExecutionException with cause and multiple failures
6:
DiscoveryNode node = new DiscoveryNode("node_g", buildNewFakeTransportAddress(), Version.CURRENT);
failureCause = new NodeClosedException(node);
failureCause = new NoShardAvailableActionException(new ShardId("_index_g", "_uuid_g", 6), "node_g", failureCause);
ShardSearchFailure[] shardFailures = new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(0, 0, "Parsing g", null), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 61), null, OriginalIndices.NONE)), new ShardSearchFailure(new RepositoryException("repository_g", "Repo"), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 62), null, OriginalIndices.NONE)), new ShardSearchFailure(new SearchContextMissingException(new ShardSearchContextId(UUIDs.randomBase64UUID(), 0L)), null) };
failure = new SearchPhaseExecutionException("phase_g", "G", failureCause, shardFailures);
expectedCause = new OpenSearchException("OpenSearch exception [type=node_closed_exception, " + "reason=node closed " + node + "]");
expectedCause = new OpenSearchException("OpenSearch exception [type=no_shard_available_action_exception, " + "reason=node_g]", expectedCause);
expectedCause.addMetadata("opensearch.index", "_index_g");
expectedCause.addMetadata("opensearch.index_uuid", "_uuid_g");
expectedCause.addMetadata("opensearch.shard", "6");
expected = new OpenSearchException("OpenSearch exception [type=search_phase_execution_exception, " + "reason=G]", expectedCause);
expected.addMetadata("opensearch.phase", "phase_g");
expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=Parsing g]"));
expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=repository_exception, " + "reason=[repository_g] Repo]"));
expected.addSuppressed(new OpenSearchException("OpenSearch exception [type=search_context_missing_exception, " + "reason=No search context found for id [0]]"));
break;
default:
throw new UnsupportedOperationException("Failed to generate randomized failure");
}
Exception finalFailure = failure;
BytesReference failureBytes = toShuffledXContent((builder, params) -> {
OpenSearchException.generateFailureXContent(builder, params, finalFailure, true);
return builder;
}, xContent.type(), ToXContent.EMPTY_PARAMS, randomBoolean());
try (XContentParser parser = createParser(xContent, failureBytes)) {
failureBytes = BytesReference.bytes(shuffleXContent(parser, randomBoolean()));
}
OpenSearchException parsedFailure;
try (XContentParser parser = createParser(xContent, failureBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
parsedFailure = OpenSearchException.failureFromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertDeepEquals(expected, parsedFailure);
}
use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.
the class OpenSearchAssertions method assertFailures.
public static void assertFailures(SearchRequestBuilder searchRequestBuilder, RestStatus restStatus, Matcher<String> reasonMatcher) {
// we can either run into partial or total failures depending on the current number of shards
try {
SearchResponse searchResponse = searchRequestBuilder.get();
assertThat("Expected shard failures, got none", searchResponse.getShardFailures().length, greaterThan(0));
for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
assertThat(shardSearchFailure.status(), equalTo(restStatus));
assertThat(shardSearchFailure.reason(), reasonMatcher);
}
} catch (SearchPhaseExecutionException e) {
assertThat(e.status(), equalTo(restStatus));
assertThat(e.toString(), reasonMatcher);
for (ShardSearchFailure shardSearchFailure : e.shardFailures()) {
assertThat(shardSearchFailure.status(), equalTo(restStatus));
assertThat(shardSearchFailure.reason(), reasonMatcher);
}
} catch (Exception e) {
fail("SearchPhaseExecutionException expected but got " + e.getClass());
}
}
use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.
the class OpenSearchAssertions method formatShardStatus.
public static String formatShardStatus(SearchResponse response) {
StringBuilder msg = new StringBuilder();
msg.append(" Total shards: ").append(response.getTotalShards()).append(" Successful shards: ").append(response.getSuccessfulShards()).append(" & ").append(response.getFailedShards()).append(" shard failures:");
for (ShardSearchFailure failure : response.getShardFailures()) {
msg.append("\n ").append(failure);
}
return msg.toString();
}
use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testCount.
@SuppressWarnings({ "unused", "unchecked" })
public void testCount() throws Exception {
indexCountTestData();
RestHighLevelClient client = highLevelClient();
{
// tag::count-request-basic
// <1>
CountRequest countRequest = new CountRequest();
// <2>
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// <3>
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
// <4>
countRequest.source(searchSourceBuilder);
// end::count-request-basic
}
{
// tag::count-request-args
CountRequest countRequest = // <1>
new CountRequest("blog").routing(// <2>
"routing").indicesOptions(// <3>
IndicesOptions.lenientExpandOpen()).preference(// <4>
"_local");
// end::count-request-args
assertNotNull(client.count(countRequest, RequestOptions.DEFAULT));
}
{
// tag::count-source-basics
// <1>
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// <2>
sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
// end::count-source-basics
// tag::count-source-setter
CountRequest countRequest = new CountRequest();
countRequest.indices("blog", "author");
countRequest.source(sourceBuilder);
// end::count-source-setter
// tag::count-execute
CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
// end::count-execute
// tag::count-execute-listener
ActionListener<CountResponse> listener = new ActionListener<CountResponse>() {
@Override
public void onResponse(CountResponse countResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::count-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::count-execute-async
// <1>
client.countAsync(countRequest, RequestOptions.DEFAULT, listener);
// end::count-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
// tag::count-response-1
long count = countResponse.getCount();
RestStatus status = countResponse.status();
Boolean terminatedEarly = countResponse.isTerminatedEarly();
// end::count-response-1
// tag::count-response-2
int totalShards = countResponse.getTotalShards();
int skippedShards = countResponse.getSkippedShards();
int successfulShards = countResponse.getSuccessfulShards();
int failedShards = countResponse.getFailedShards();
for (ShardSearchFailure failure : countResponse.getShardFailures()) {
// failures should be handled here
}
// end::count-response-2
assertNotNull(countResponse);
assertEquals(4, countResponse.getCount());
}
}
Aggregations