Search in sources :

Example 1 with BroadcastShardOperationFailedException

use of org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFailureToAndFromXContentWithDetails.

public void testFailureToAndFromXContentWithDetails() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    Exception failure;
    Throwable failureCause;
    ElasticsearchException expected;
    ElasticsearchException expectedCause;
    ElasticsearchException suppressed;
    switch(randomIntBetween(0, 6)) {
        case // Simple elasticsearch exception without cause
        0:
            failure = new NoNodeAvailableException("A");
            expected = new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]"));
            break;
        case // Simple elasticsearch exception with headers (other metadata of type number are not parsed)
        1:
            failure = new CircuitBreakingException("B", 5_000, 2_000);
            ((ElasticsearchException) failure).addHeader("header_name", "0", "1");
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            expected.addHeader("header_name", "0", "1");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            suppressed.addHeader("header_name", "0", "1");
            expected.addSuppressed(suppressed);
            break;
        case // Elasticsearch exception with a cause, headers and parsable metadata
        2:
            failureCause = new NullPointerException("var is null");
            failure = new ScriptException("C", failureCause, singletonList("stack"), "test", "painless");
            ((ElasticsearchException) failure).addHeader("script_name", "my_script");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, reason=var is null]");
            expected = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]", expectedCause);
            expected.addHeader("script_name", "my_script");
            expected.addMetadata("es.lang", "painless");
            expected.addMetadata("es.script", "test");
            expected.addMetadata("es.script_stack", "stack");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]");
            suppressed.addHeader("script_name", "my_script");
            suppressed.addMetadata("es.lang", "painless");
            suppressed.addMetadata("es.script", "test");
            suppressed.addMetadata("es.script_stack", "stack");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception without cause
        3:
            failure = new IllegalStateException("D");
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception with cause
        4:
            failureCause = new RoutingMissingException("idx", "type", "id");
            failure = new RuntimeException("E", failureCause);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=routing_missing_exception, " + "reason=routing is required for [idx]/[type]/[id]]");
            expectedCause.addMetadata("es.index", "idx");
            expectedCause.addMetadata("es.index_uuid", "_na_");
            expected = new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=E]", expectedCause);
            suppressed = new ElasticsearchException("Elasticsearch 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 ElasticsearchException("Elasticsearch exception [type=file_already_exists_exception, reason=File exists]");
            // strangely, the wrapped exception appears as the root cause...
            suppressed = new ElasticsearchException("Elasticsearch exception [type=broadcast_shard_operation_failed_exception, " + "reason=F]");
            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))), new ShardSearchFailure(new RepositoryException("repository_g", "Repo"), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 62))), new ShardSearchFailure(new SearchContextMissingException(0L), null) };
            failure = new SearchPhaseExecutionException("phase_g", "G", failureCause, shardFailures);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=node_closed_exception, " + "reason=node closed " + node + "]");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=no_shard_available_action_exception, " + "reason=node_g]", expectedCause);
            expectedCause.addMetadata("es.index", "_index_g");
            expectedCause.addMetadata("es.index_uuid", "_uuid_g");
            expectedCause.addMetadata("es.shard", "6");
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=G]", expectedCause);
            expected.addMetadata("es.phase", "phase_g");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=parsing_exception, reason=Parsing g]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=repository_exception, " + "reason=[repository_g] Repo]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch 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 = XContentHelper.toXContent((builder, params) -> {
        ElasticsearchException.generateFailureXContent(builder, params, finalFailure, true);
        return builder;
    }, xContent.type(), randomBoolean());
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        failureBytes = shuffleXContent(parser, randomBoolean()).bytes();
    }
    ElasticsearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = ElasticsearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertDeepEquals(expected, parsedFailure);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) ScriptException(org.elasticsearch.script.ScriptException) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ParsingException(org.elasticsearch.common.ParsingException) NodeClosedException(org.elasticsearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) RepositoryException(org.elasticsearch.repositories.RepositoryException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) SearchParseException(org.elasticsearch.search.SearchParseException) NodeClosedException(org.elasticsearch.node.NodeClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) RepositoryException(org.elasticsearch.repositories.RepositoryException) QueryShardException(org.elasticsearch.index.query.QueryShardException) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) ScriptException(org.elasticsearch.script.ScriptException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) ParsingException(org.elasticsearch.common.ParsingException) IndexShardRecoveringException(org.elasticsearch.index.shard.IndexShardRecoveringException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with BroadcastShardOperationFailedException

use of org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException in project elasticsearch by elastic.

the class TransportBroadcastByNodeAction method newResponse.

private Response newResponse(Request request, AtomicReferenceArray responses, List<NoShardAvailableActionException> unavailableShardExceptions, Map<String, List<ShardRouting>> nodes, ClusterState clusterState) {
    int totalShards = 0;
    int successfulShards = 0;
    List<ShardOperationResult> broadcastByNodeResponses = new ArrayList<>();
    List<ShardOperationFailedException> exceptions = new ArrayList<>();
    for (int i = 0; i < responses.length(); i++) {
        if (responses.get(i) instanceof FailedNodeException) {
            FailedNodeException exception = (FailedNodeException) responses.get(i);
            totalShards += nodes.get(exception.nodeId()).size();
            for (ShardRouting shard : nodes.get(exception.nodeId())) {
                exceptions.add(new DefaultShardOperationFailedException(shard.getIndexName(), shard.getId(), exception));
            }
        } else {
            NodeResponse response = (NodeResponse) responses.get(i);
            broadcastByNodeResponses.addAll(response.results);
            totalShards += response.getTotalShards();
            successfulShards += response.getSuccessfulShards();
            for (BroadcastShardOperationFailedException throwable : response.getExceptions()) {
                if (!TransportActions.isShardNotAvailableException(throwable)) {
                    exceptions.add(new DefaultShardOperationFailedException(throwable.getShardId().getIndexName(), throwable.getShardId().getId(), throwable));
                }
            }
        }
    }
    totalShards += unavailableShardExceptions.size();
    int failedShards = exceptions.size();
    return newResponse(request, totalShards, successfulShards, failedShards, broadcastByNodeResponses, exceptions, clusterState);
}
Also used : ArrayList(java.util.ArrayList) FailedNodeException(org.elasticsearch.action.FailedNodeException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException)

Example 3 with BroadcastShardOperationFailedException

use of org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException in project crate by crate.

the class TransportBroadcastByNodeAction method newResponse.

private Response newResponse(Request request, AtomicReferenceArray responses, List<NoShardAvailableActionException> unavailableShardExceptions, Map<String, List<ShardRouting>> nodes, ClusterState clusterState) {
    int totalShards = 0;
    int successfulShards = 0;
    List<ShardOperationResult> broadcastByNodeResponses = new ArrayList<>();
    List<DefaultShardOperationFailedException> exceptions = new ArrayList<>();
    for (int i = 0; i < responses.length(); i++) {
        if (responses.get(i) instanceof FailedNodeException) {
            FailedNodeException exception = (FailedNodeException) responses.get(i);
            totalShards += nodes.get(exception.nodeId()).size();
            for (ShardRouting shard : nodes.get(exception.nodeId())) {
                exceptions.add(new DefaultShardOperationFailedException(shard.getIndexName(), shard.getId(), exception));
            }
        } else {
            NodeResponse response = (NodeResponse) responses.get(i);
            broadcastByNodeResponses.addAll(response.results);
            totalShards += response.getTotalShards();
            successfulShards += response.getSuccessfulShards();
            for (BroadcastShardOperationFailedException throwable : response.getExceptions()) {
                if (!TransportActions.isShardNotAvailableException(throwable)) {
                    exceptions.add(new DefaultShardOperationFailedException(throwable.getShardId().getIndexName(), throwable.getShardId().getId(), throwable));
                }
            }
        }
    }
    totalShards += unavailableShardExceptions.size();
    int failedShards = exceptions.size();
    return newResponse(request, totalShards, successfulShards, failedShards, broadcastByNodeResponses, exceptions, clusterState);
}
Also used : ArrayList(java.util.ArrayList) FailedNodeException(org.elasticsearch.action.FailedNodeException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException)

Example 4 with BroadcastShardOperationFailedException

use of org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException in project elasticsearch by elastic.

the class TransportBroadcastReplicationAction method finishAndNotifyListener.

private void finishAndNotifyListener(ActionListener listener, CopyOnWriteArrayList<ShardResponse> shardsResponses) {
    logger.trace("{}: got all shard responses", actionName);
    int successfulShards = 0;
    int failedShards = 0;
    int totalNumCopies = 0;
    List<ShardOperationFailedException> shardFailures = null;
    for (int i = 0; i < shardsResponses.size(); i++) {
        ReplicationResponse shardResponse = shardsResponses.get(i);
        if (shardResponse == null) {
        // non active shard, ignore
        } else {
            failedShards += shardResponse.getShardInfo().getFailed();
            successfulShards += shardResponse.getShardInfo().getSuccessful();
            totalNumCopies += shardResponse.getShardInfo().getTotal();
            if (shardFailures == null) {
                shardFailures = new ArrayList<>();
            }
            for (ReplicationResponse.ShardInfo.Failure failure : shardResponse.getShardInfo().getFailures()) {
                shardFailures.add(new DefaultShardOperationFailedException(new BroadcastShardOperationFailedException(failure.fullShardId(), failure.getCause())));
            }
        }
    }
    listener.onResponse(newResponse(successfulShards, failedShards, totalNumCopies, shardFailures));
}
Also used : BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException)

Example 5 with BroadcastShardOperationFailedException

use of org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException in project elasticsearch by elastic.

the class TransportBroadcastByNodeActionTests method testOperationExecution.

public void testOperationExecution() throws Exception {
    ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX });
    Set<ShardRouting> shards = new HashSet<>();
    String nodeId = shardIt.asUnordered().iterator().next().currentNodeId();
    for (ShardRouting shard : shardIt.asUnordered()) {
        if (nodeId.equals(shard.currentNodeId())) {
            shards.add(shard);
        }
    }
    final TransportBroadcastByNodeAction.BroadcastByNodeTransportRequestHandler handler = action.new BroadcastByNodeTransportRequestHandler();
    TestTransportChannel channel = new TestTransportChannel();
    handler.messageReceived(action.new NodeRequest(nodeId, new Request(), new ArrayList<>(shards)), channel);
    // check the operation was executed only on the expected shards
    assertEquals(shards, action.getResults().keySet());
    TransportResponse response = channel.getCapturedResponse();
    assertTrue(response instanceof TransportBroadcastByNodeAction.NodeResponse);
    TransportBroadcastByNodeAction.NodeResponse nodeResponse = (TransportBroadcastByNodeAction.NodeResponse) response;
    // check the operation was executed on the correct node
    assertEquals("node id", nodeId, nodeResponse.getNodeId());
    int successfulShards = 0;
    int failedShards = 0;
    for (Object result : action.getResults().values()) {
        if (!(result instanceof ElasticsearchException)) {
            successfulShards++;
        } else {
            failedShards++;
        }
    }
    // check the operation results
    assertEquals("successful shards", successfulShards, nodeResponse.getSuccessfulShards());
    assertEquals("total shards", action.getResults().size(), nodeResponse.getTotalShards());
    assertEquals("failed shards", failedShards, nodeResponse.getExceptions().size());
    List<BroadcastShardOperationFailedException> exceptions = nodeResponse.getExceptions();
    for (BroadcastShardOperationFailedException exception : exceptions) {
        assertThat(exception.getMessage(), is("operation indices:admin/test failed"));
        assertThat(exception, hasToString(containsString("operation failed")));
    }
}
Also used : IndicesRequest(org.elasticsearch.action.IndicesRequest) BroadcastRequest(org.elasticsearch.action.support.broadcast.BroadcastRequest) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) TransportResponse(org.elasticsearch.transport.TransportResponse) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet)

Aggregations

BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)7 ArrayList (java.util.ArrayList)4 DefaultShardOperationFailedException (org.elasticsearch.action.support.DefaultShardOperationFailedException)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)3 IOException (java.io.IOException)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 FailedNodeException (org.elasticsearch.action.FailedNodeException)2 IndicesRequest (org.elasticsearch.action.IndicesRequest)2 BroadcastRequest (org.elasticsearch.action.support.broadcast.BroadcastRequest)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 ShardsIterator (org.elasticsearch.cluster.routing.ShardsIterator)2 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)2 TransportResponse (org.elasticsearch.transport.TransportResponse)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1