Java Code Examples for org.elasticsearch.action.support.TransportActions#isShardNotAvailableException()

The following examples show how to use org.elasticsearch.action.support.TransportActions#isShardNotAvailableException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransportShardMultiPercolateAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected Response shardOperation(Request request, ShardId shardId) {
    // TODO: Look into combining the shard req's docs into one in memory index.
    Response response = new Response();
    response.items = new ArrayList<>(request.items.size());
    for (Request.Item item : request.items) {
        Response.Item responseItem;
        int slot = item.slot;
        try {
            responseItem = new Response.Item(slot, percolatorService.percolate(item.request));
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to multi percolate", t, request.shardId());
                responseItem = new Response.Item(slot, t);
            }
        }
        response.items.add(responseItem);
    }
    return response;
}
 
Example 2
Source File: AbstractSearchAsyncAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
protected final void addShardFailure(final int shardIndex, @Nullable SearchShardTarget shardTarget, Throwable t) {
    // we don't aggregate shard failures on non active shards (but do keep the header counts right)
    if (TransportActions.isShardNotAvailableException(t)) {
        return;
    }

    // lazily create shard failures, so we can early build the empty shard failure list in most cases (no failures)
    if (shardFailures == null) {
        synchronized (shardFailuresMutex) {
            if (shardFailures == null) {
                shardFailures = new AtomicArray<>(shardsIts.size());
            }
        }
    }
    ShardSearchFailure failure = shardFailures.get(shardIndex);
    if (failure == null) {
        shardFailures.set(shardIndex, new ShardSearchFailure(t, shardTarget));
    } else {
        // the failure is already present, try and not override it with an exception that is less meaningless
        // for example, getting illegal shard state
        if (TransportActions.isReadOverrideException(t)) {
            shardFailures.set(shardIndex, new ShardSearchFailure(t, shardTarget));
        }
    }
}
 
Example 3
Source File: TransportShardMultiTermsVectorAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected MultiTermVectorsShardResponse shardOperation(MultiTermVectorsShardRequest request, ShardId shardId) {
    MultiTermVectorsShardResponse response = new MultiTermVectorsShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
        TermVectorsRequest termVectorsRequest = request.requests.get(i);
        try {
            IndexService indexService = indicesService.indexServiceSafe(request.index());
            IndexShard indexShard = indexService.shardSafe(shardId.id());
            TermVectorsResponse termVectorsResponse = indexShard.termVectorsService().getTermVectors(termVectorsRequest, shardId.getIndex());
            termVectorsResponse.updateTookInMillis(termVectorsRequest.startTime());
            response.add(request.locations.get(i), termVectorsResponse);
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to execute multi term vectors for [{}]/[{}]", t, shardId, termVectorsRequest.type(), termVectorsRequest.id());
                response.add(request.locations.get(i),
                        new MultiTermVectorsResponse.Failure(request.index(), termVectorsRequest.type(), termVectorsRequest.id(), t));
            }
        }
    }

    return response;
}
 
Example 4
Source File: TransportBroadcastByNodeAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void onShardOperation(final NodeRequest request, final Object[] shardResults, final int shardIndex, final ShardRouting shardRouting) {
    try {
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  executing operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
        ShardOperationResult result = shardOperation(request.indicesLevelRequest, shardRouting);
        shardResults[shardIndex] = result;
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  completed operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
    } catch (Throwable t) {
        BroadcastShardOperationFailedException e = new BroadcastShardOperationFailedException(shardRouting.shardId(), "operation " + actionName + " failed", t);
        e.setIndex(shardRouting.getIndex());
        e.setShard(shardRouting.shardId());
        shardResults[shardIndex] = e;
        if (TransportActions.isShardNotAvailableException(t)) {
            if (logger.isTraceEnabled()) {
                logger.trace("[{}] failed to execute operation for shard [{}]", t, actionName, shardRouting.shortSummary());
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("[{}] failed to execute operation for shard [{}]", t, actionName, shardRouting.shortSummary());
            }
        }
    }
}
 
Example 5
Source File: TransportShardMultiGetAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected MultiGetShardResponse shardOperation(MultiGetShardRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

    if (request.refresh() && !request.realtime()) {
        indexShard.refresh("refresh_flag_mget");
    }

    MultiGetShardResponse response = new MultiGetShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
        MultiGetRequest.Item item = request.items.get(i);
        try {
            GetResult getResult = indexShard.getService().get(item.type(), item.id(), item.fields(), request.realtime(), item.version(), item.versionType(), item.fetchSourceContext(), request.ignoreErrorsOnGeneratedFields());
            response.add(request.locations.get(i), new GetResponse(getResult));
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to execute multi_get for [{}]/[{}]", t, shardId, item.type(), item.id());
                response.add(request.locations.get(i), new MultiGetResponse.Failure(request.index(), item.type(), item.id(), t));
            }
        }
    }

    return response;
}
 
Example 6
Source File: TransportWriteAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void failShardIfNeeded(ShardRouting replica, String message, Exception exception, ActionListener<Void> listener) {
    if (TransportActions.isShardNotAvailableException(exception) == false) {
        logger.warn(new ParameterizedMessage("[{}] {}", replica.shardId(), message), exception);
    }
    shardStateAction.remoteShardFailed(
        replica.shardId(), replica.allocationId().getId(), primaryTerm, true, message, exception, listener);
}
 
Example 7
Source File: TransportBroadcastByNodeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
private void onShardOperation(final NodeRequest request, final Object[] shardResults, final int shardIndex, final ShardRouting shardRouting) {
    try {
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  executing operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
        ShardOperationResult result = shardOperation(request.indicesLevelRequest, shardRouting);
        shardResults[shardIndex] = result;
        if (logger.isTraceEnabled()) {
            logger.trace("[{}]  completed operation for shard [{}]", actionName, shardRouting.shortSummary());
        }
    } catch (Exception e) {
        BroadcastShardOperationFailedException failure =
            new BroadcastShardOperationFailedException(shardRouting.shardId(), "operation " + actionName + " failed", e);
        failure.setShard(shardRouting.shardId());
        shardResults[shardIndex] = failure;
        if (TransportActions.isShardNotAvailableException(e)) {
            if (logger.isTraceEnabled()) {
                logger.trace(new ParameterizedMessage(
                    "[{}] failed to execute operation for shard [{}]", actionName, shardRouting.shortSummary()), e);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(new ParameterizedMessage(
                    "[{}] failed to execute operation for shard [{}]", actionName, shardRouting.shortSummary()), e);
            }
        }
    }
}
 
Example 8
Source File: TransportBroadcastByNodeAction.java    From crate with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: TransportReplicaShardIngestAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
boolean ignoreReplicaException(Throwable e) {
    if (TransportActions.isShardNotAvailableException(e)) {
        return true;
    }
    Throwable cause = ExceptionsHelper.unwrapCause(e);
    return cause instanceof VersionConflictEngineException;
}
 
Example 10
Source File: TransportReplicationAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Should an exception be ignored when the operation is performed on the replica.
 */
protected boolean ignoreReplicaException(Throwable e) {
    if (TransportActions.isShardNotAvailableException(e)) {
        return true;
    }
    // on version conflict or document missing, it means
    // that a new change has crept into the replica, and it's fine
    if (isConflictException(e)) {
        return true;
    }
    return false;
}
 
Example 11
Source File: TransportBroadcastByNodeAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private final 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.getIndex(), 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.getIndex(), throwable.getShardId().getId(), throwable));
                }
            }
        }
    }
    totalShards += unavailableShardExceptions.size();
    int failedShards = exceptions.size();
    return newResponse(request, totalShards, successfulShards, failedShards, broadcastByNodeResponses, exceptions, clusterState);
}
 
Example 12
Source File: TransportBroadcastAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
void setFailure(ShardIterator shardIt, int shardIndex, Throwable t) {
    // we don't aggregate shard failures on non active shards (but do keep the header counts right)
    if (TransportActions.isShardNotAvailableException(t)) {
        return;
    }

    if (!(t instanceof BroadcastShardOperationFailedException)) {
        t = new BroadcastShardOperationFailedException(shardIt.shardId(), t);
    }

    Object response = shardsResponses.get(shardIndex);
    if (response == null) {
        // just override it and return
        shardsResponses.set(shardIndex, t);
    }

    if (!(response instanceof Throwable)) {
        // we should never really get here...
        return;
    }

    // the failure is already present, try and not override it with an exception that is less meaningless
    // for example, getting illegal shard state
    if (TransportActions.isReadOverrideException(t)) {
        shardsResponses.set(shardIndex, t);
    }
}
 
Example 13
Source File: TransportBroadcastAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
void onOperation(@Nullable ShardRouting shard, final ShardIterator shardIt, int shardIndex, Throwable t) {
    // we set the shard failure always, even if its the first in the replication group, and the next one
    // will work (it will just override it...)
    setFailure(shardIt, shardIndex, t);
    ShardRouting nextShard = shardIt.nextOrNull();
    if (nextShard != null) {
        if (t != null) {
            if (logger.isTraceEnabled()) {
                if (!TransportActions.isShardNotAvailableException(t)) {
                    logger.trace("{}: failed to execute [{}]", t, shard != null ? shard.shortSummary() : shardIt.shardId(), request);
                }
            }
        }
        performOperation(shardIt, nextShard, shardIndex);
    } else {
        if (logger.isDebugEnabled()) {
            if (t != null) {
                if (!TransportActions.isShardNotAvailableException(t)) {
                    logger.debug("{}: failed to execute [{}]", t, shard != null ? shard.shortSummary() : shardIt.shardId(), request);
                }
            }
        }
        if (expectedOps == counterOps.incrementAndGet()) {
            finishHim();
        }
    }
}
 
Example 14
Source File: TransportReplicationAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected boolean retryPrimaryException(Throwable e) {
    return e.getClass() == RetryOnPrimaryException.class
        || TransportActions.isShardNotAvailableException(e);
}
 
Example 15
Source File: TransportLeaderShardIngestAction.java    From elasticsearch-helper with Apache License 2.0 4 votes vote down vote up
protected boolean retryLeaderException(Throwable e) {
    return TransportActions.isShardNotAvailableException(e);
}
 
Example 16
Source File: AbstractSearchAsyncAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
void onFirstPhaseResult(final int shardIndex, @Nullable ShardRouting shard, @Nullable String nodeId, final ShardIterator shardIt, Throwable t) {
    // we always add the shard failure for a specific shard instance
    // we do make sure to clean it on a successful response from a shard
    SearchShardTarget shardTarget = new SearchShardTarget(nodeId, shardIt.shardId().getIndex(), shardIt.shardId().getId());
    addShardFailure(shardIndex, shardTarget, t);

    if (totalOps.incrementAndGet() == expectedTotalOps) {
        if (logger.isDebugEnabled()) {
            if (t != null && !TransportActions.isShardNotAvailableException(t)) {
                if (shard != null) {
                    logger.debug(shard.shortSummary() + ": Failed to execute [" + request + "]", t);
                } else {
                    logger.debug(shardIt.shardId() + ": Failed to execute [" + request + "]", t);
                }
            } else if (logger.isTraceEnabled()) {
                logger.trace("{}: Failed to execute [{}]", t, shard, request);
            }
        }
        if (successfulOps.get() == 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("All shards failed for phase: [{}]", t, firstPhaseName());
            }
            // no successful ops, raise an exception
            raiseEarlyFailure(new SearchPhaseExecutionException(firstPhaseName(), "all shards failed", buildShardFailures()));
        } else {
            try {
                innerMoveToSecondPhase();
            } catch (Throwable e) {
                raiseEarlyFailure(new ReduceSearchPhaseException(firstPhaseName(), "", e, buildShardFailures()));
            }
        }
    } else {
        final ShardRouting nextShard = shardIt.nextOrNull();
        final boolean lastShard = nextShard == null;
        // trace log this exception
        if (logger.isTraceEnabled()) {
            logger.trace(executionFailureMsg(shard, shardIt, request, lastShard), t);
        }
        if (!lastShard) {
            try {
                performFirstPhase(shardIndex, shardIt, nextShard);
            } catch (Throwable t1) {
                onFirstPhaseResult(shardIndex, shard, shard.currentNodeId(), shardIt, t1);
            }
        } else {
            // no more shards active, add a failure
            if (logger.isDebugEnabled() && !logger.isTraceEnabled()) { // do not double log this exception
                if (t != null && !TransportActions.isShardNotAvailableException(t)) {
                    logger.debug(executionFailureMsg(shard, shardIt, request, lastShard), t);
                }
            }
        }
    }
}
 
Example 17
Source File: TransportUpdateAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean retryOnFailure(Throwable e) {
    return TransportActions.isShardNotAvailableException(e);
}
 
Example 18
Source File: TransportShardDeleteAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardResponse processRequestItems(ShardId shardId, ShardDeleteRequest request, AtomicBoolean killed) throws InterruptedException {
    ShardResponse shardResponse = new ShardResponse();
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    for (int i = 0; i < request.itemIndices().size(); i++) {
        int location = request.itemIndices().get(i);
        ShardDeleteRequest.Item item = request.items().get(i);
        if (killed.get()) {
            // set failure on response, mark current item and skip all next items.
            // this way replica operation will be executed, but only items already processed here
            // will be processed on the replica
            request.skipFromLocation(location);
            shardResponse.failure(new InterruptedException(JobKilledException.MESSAGE));
            break;
        }
        try {
            boolean found = shardDeleteOperationOnPrimary(request, item, indexShard);
            if (found) {
                logger.debug("{} successfully deleted [{}]/[{}]", request.shardId(), request.type(), item.id());
                shardResponse.add(location);
            } else {
                logger.debug("{} failed to execute delete for [{}]/[{}], doc not found",
                        request.shardId(), request.type(), item.id());
                shardResponse.add(location,
                        new ShardResponse.Failure(
                                item.id(),
                                "Document not found while deleting",
                                false));

            }
        } catch (Throwable t) {
            if (!TransportActions.isShardNotAvailableException(t)) {
                throw t;
            } else {
                logger.debug("{} failed to execute delete for [{}]/[{}]",
                        t, request.shardId(), request.type(), item.id());
                shardResponse.add(location,
                        new ShardResponse.Failure(
                                item.id(),
                                ExceptionsHelper.detailedMessage(t),
                                (t instanceof VersionConflictEngineException)));
            }
        }
    }

    return shardResponse;
}
 
Example 19
Source File: TransportReplicationAction.java    From crate with Apache License 2.0 4 votes vote down vote up
protected boolean retryPrimaryException(final Throwable e) {
    return e.getClass() == ReplicationOperation.RetryOnPrimaryException.class
            || TransportActions.isShardNotAvailableException(e);
}