Search in sources :

Example 1 with BroadcastShardOperationFailedException

use of org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException in project OpenSearch by opensearch-project.

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.opensearch.action.FailedNodeException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardRouting(org.opensearch.cluster.routing.ShardRouting) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException)

Example 2 with BroadcastShardOperationFailedException

use of org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException in project OpenSearch by opensearch-project.

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<DefaultShardOperationFailedException> 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.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException)

Example 3 with BroadcastShardOperationFailedException

use of org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException in project OpenSearch by opensearch-project.

the class TransportBroadcastByNodeActionTests method testResultAggregation.

public void testResultAggregation() throws ExecutionException, InterruptedException {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    // simulate removing the master
    final boolean simulateFailedMasterNode = rarely();
    DiscoveryNode failedMasterNode = null;
    if (simulateFailedMasterNode) {
        failedMasterNode = clusterService.state().nodes().getMasterNode();
        DiscoveryNodes.Builder builder = DiscoveryNodes.builder(clusterService.state().getNodes());
        builder.remove(failedMasterNode.getId());
        builder.masterNodeId(null);
        setState(clusterService, ClusterState.builder(clusterService.state()).nodes(builder));
    }
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { TEST_INDEX });
    Map<String, List<ShardRouting>> map = new HashMap<>();
    for (ShardRouting shard : shardIt) {
        if (!map.containsKey(shard.currentNodeId())) {
            map.put(shard.currentNodeId(), new ArrayList<>());
        }
        map.get(shard.currentNodeId()).add(shard);
    }
    int totalShards = 0;
    int totalSuccessfulShards = 0;
    int totalFailedShards = 0;
    for (Map.Entry<String, List<CapturingTransport.CapturedRequest>> entry : capturedRequests.entrySet()) {
        List<BroadcastShardOperationFailedException> exceptions = new ArrayList<>();
        long requestId = entry.getValue().get(0).requestId;
        if (rarely()) {
            // simulate node failure
            totalShards += map.get(entry.getKey()).size();
            totalFailedShards += map.get(entry.getKey()).size();
            transport.handleRemoteError(requestId, new Exception());
        } else {
            List<ShardRouting> shards = map.get(entry.getKey());
            List<TransportBroadcastByNodeAction.EmptyResult> shardResults = new ArrayList<>();
            for (ShardRouting shard : shards) {
                totalShards++;
                if (rarely()) {
                    // simulate operation failure
                    totalFailedShards++;
                    exceptions.add(new BroadcastShardOperationFailedException(shard.shardId(), "operation indices:admin/test failed"));
                } else {
                    shardResults.add(TransportBroadcastByNodeAction.EmptyResult.INSTANCE);
                }
            }
            totalSuccessfulShards += shardResults.size();
            TransportBroadcastByNodeAction.NodeResponse nodeResponse = action.new NodeResponse(entry.getKey(), shards.size(), shardResults, exceptions);
            transport.handleResponse(requestId, nodeResponse);
        }
    }
    if (simulateFailedMasterNode) {
        totalShards += map.get(failedMasterNode.getId()).size();
    }
    Response response = listener.get();
    assertEquals("total shards", totalShards, response.getTotalShards());
    assertEquals("successful shards", totalSuccessfulShards, response.getSuccessfulShards());
    assertEquals("failed shards", totalFailedShards, response.getFailedShards());
    assertEquals("accumulated exceptions", totalFailedShards, response.getShardFailures().length);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) List(java.util.List) ArrayList(java.util.ArrayList) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CapturingTransport(org.opensearch.test.transport.CapturingTransport) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) IndicesRequest(org.opensearch.action.IndicesRequest) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TransportResponse(org.opensearch.transport.TransportResponse) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 4 with BroadcastShardOperationFailedException

use of org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException 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);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) Index(org.opensearch.index.Index) ShardId(org.opensearch.index.shard.ShardId) ScriptException(org.opensearch.script.ScriptException) ToXContent(org.opensearch.common.xcontent.ToXContent) XContent(org.opensearch.common.xcontent.XContent) ParsingException(org.opensearch.common.ParsingException) NodeClosedException(org.opensearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) RoutingMissingException(org.opensearch.action.RoutingMissingException) BytesReference(org.opensearch.common.bytes.BytesReference) SearchContextMissingException(org.opensearch.search.SearchContextMissingException) RepositoryException(org.opensearch.repositories.RepositoryException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) IndexShardRecoveringException(org.opensearch.index.shard.IndexShardRecoveringException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) ScriptException(org.opensearch.script.ScriptException) NodeClosedException(org.opensearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) ParsingException(org.opensearch.common.ParsingException) RepositoryException(org.opensearch.repositories.RepositoryException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) QueryShardException(org.opensearch.index.query.QueryShardException) SearchParseException(org.opensearch.search.SearchParseException) SearchContextMissingException(org.opensearch.search.SearchContextMissingException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) RoutingMissingException(org.opensearch.action.RoutingMissingException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) XContentParseException(org.opensearch.common.xcontent.XContentParseException) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) SearchShardTarget(org.opensearch.search.SearchShardTarget) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with BroadcastShardOperationFailedException

use of org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException in project OpenSearch by opensearch-project.

the class TransportBroadcastByNodeActionTests method testResultWithTimeouts.

public void testResultWithTimeouts() throws ExecutionException, InterruptedException {
    Request request = new Request(new String[] { TEST_INDEX });
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    action.new AsyncAction(null, request, listener).start();
    Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear();
    ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { TEST_INDEX });
    Map<String, List<ShardRouting>> map = new HashMap<>();
    for (ShardRouting shard : shardIt) {
        if (!map.containsKey(shard.currentNodeId())) {
            map.put(shard.currentNodeId(), new ArrayList<>());
        }
        map.get(shard.currentNodeId()).add(shard);
    }
    int totalShards = 0;
    int totalSuccessfulShards = 0;
    int totalFailedShards = 0;
    String failedNodeId = "node_" + randomIntBetween(0, capturedRequests.size());
    for (Map.Entry<String, List<CapturingTransport.CapturedRequest>> entry : capturedRequests.entrySet()) {
        List<BroadcastShardOperationFailedException> exceptions = new ArrayList<>();
        long requestId = entry.getValue().get(0).requestId;
        if (entry.getKey().equals(failedNodeId)) {
            // simulate node timeout
            totalShards += map.get(entry.getKey()).size();
            totalFailedShards += map.get(entry.getKey()).size();
            transport.handleError(requestId, new ReceiveTimeoutTransportException(clusterService.state().getRoutingNodes().node(entry.getKey()).node(), "indices:admin/test", "time_out_simulated"));
        } else {
            List<ShardRouting> shards = map.get(entry.getKey());
            List<TransportBroadcastByNodeAction.EmptyResult> shardResults = new ArrayList<>();
            for (ShardRouting shard : shards) {
                totalShards++;
                if (rarely()) {
                    // simulate operation failure
                    totalFailedShards++;
                    exceptions.add(new BroadcastShardOperationFailedException(shard.shardId(), "operation indices:admin/test failed"));
                } else {
                    shardResults.add(TransportBroadcastByNodeAction.EmptyResult.INSTANCE);
                }
            }
            totalSuccessfulShards += shardResults.size();
            TransportBroadcastByNodeAction.NodeResponse nodeResponse = action.new NodeResponse(entry.getKey(), shards.size(), shardResults, exceptions);
            transport.handleResponse(requestId, nodeResponse);
        }
    }
    Response response = listener.get();
    assertEquals("total shards", totalShards, response.getTotalShards());
    assertEquals("successful shards", totalSuccessfulShards, response.getSuccessfulShards());
    assertEquals("failed shards", totalFailedShards, response.getFailedShards());
    assertEquals("accumulated exceptions", totalFailedShards, response.getShardFailures().length);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) ReceiveTimeoutTransportException(org.opensearch.transport.ReceiveTimeoutTransportException) List(java.util.List) ArrayList(java.util.ArrayList) BroadcastShardOperationFailedException(org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException) CapturingTransport(org.opensearch.test.transport.CapturingTransport) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) IndicesRequest(org.opensearch.action.IndicesRequest) TransportResponse(org.opensearch.transport.TransportResponse) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap)

Aggregations

BroadcastShardOperationFailedException (org.opensearch.action.support.broadcast.BroadcastShardOperationFailedException)6 ArrayList (java.util.ArrayList)4 ShardRouting (org.opensearch.cluster.routing.ShardRouting)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 HasToString.hasToString (org.hamcrest.object.HasToString.hasToString)3 IndicesRequest (org.opensearch.action.IndicesRequest)3 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)3 BroadcastRequest (org.opensearch.action.support.broadcast.BroadcastRequest)3 ShardsIterator (org.opensearch.cluster.routing.ShardsIterator)3 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)3 IOException (java.io.IOException)2 Collections.emptyMap (java.util.Collections.emptyMap)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 OpenSearchException (org.opensearch.OpenSearchException)2 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)2 BroadcastResponse (org.opensearch.action.support.broadcast.BroadcastResponse)2 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2