Search in sources :

Example 1 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class ShardSearchFailure method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    if (in.readBoolean()) {
        shardTarget = new SearchShardTarget(in);
    }
    reason = in.readString();
    status = RestStatus.readFrom(in);
    cause = in.readException();
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 2 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class ShardSearchFailure method fromXContent.

public static ShardSearchFailure fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    String currentFieldName = null;
    int shardId = -1;
    String indexName = null;
    String nodeId = null;
    ElasticsearchException exception = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SHARD_FIELD.equals(currentFieldName)) {
                shardId = parser.intValue();
            } else if (INDEX_FIELD.equals(currentFieldName)) {
                indexName = parser.text();
            } else if (NODE_FIELD.equals(currentFieldName)) {
                nodeId = parser.text();
            } else {
                throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (REASON_FIELD.equals(currentFieldName)) {
                exception = ElasticsearchException.fromXContent(parser);
            } else {
                throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else {
            throwUnknownToken(token, parser.getTokenLocation());
        }
    }
    return new ShardSearchFailure(exception, new SearchShardTarget(nodeId, new ShardId(new Index(indexName, IndexMetaData.INDEX_UUID_NA_VALUE), shardId)));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class DfsQueryPhase method run.

@Override
public void run() throws IOException {
    // TODO we can potentially also consume the actual per shard results from the initial phase here in the aggregateDfs
    // to free up memory early
    final AggregatedDfs dfs = searchPhaseController.aggregateDfs(dfsSearchResults);
    final CountedCollector<QuerySearchResultProvider> counter = new CountedCollector<>(queryResult::consumeResult, dfsSearchResults.asList().size(), () -> {
        context.executeNextPhase(this, nextPhaseFactory.apply(queryResult));
    }, context);
    for (final AtomicArray.Entry<DfsSearchResult> entry : dfsSearchResults.asList()) {
        DfsSearchResult dfsResult = entry.value;
        final int shardIndex = entry.index;
        final SearchShardTarget searchShardTarget = dfsResult.shardTarget();
        Transport.Connection connection = context.getConnection(searchShardTarget.getNodeId());
        QuerySearchRequest querySearchRequest = new QuerySearchRequest(context.getRequest(), dfsResult.id(), dfs);
        searchTransportService.sendExecuteQuery(connection, querySearchRequest, context.getTask(), ActionListener.wrap(result -> counter.onResult(shardIndex, result, searchShardTarget), exception -> {
            try {
                if (context.getLogger().isDebugEnabled()) {
                    context.getLogger().debug((Supplier<?>) () -> new ParameterizedMessage("[{}] Failed to execute query phase", querySearchRequest.id()), exception);
                }
                counter.onFailure(shardIndex, searchShardTarget, exception);
            } finally {
                context.sendReleaseSearchContext(querySearchRequest.id(), connection);
            }
        }));
    }
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Transport(org.elasticsearch.transport.Transport) Supplier(org.apache.logging.log4j.util.Supplier) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) AggregatedDfs(org.elasticsearch.search.dfs.AggregatedDfs) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) QuerySearchRequest(org.elasticsearch.search.query.QuerySearchRequest) Function(java.util.function.Function) ActionListener(org.elasticsearch.action.ActionListener) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) AggregatedDfs(org.elasticsearch.search.dfs.AggregatedDfs) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) QuerySearchRequest(org.elasticsearch.search.query.QuerySearchRequest) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Transport(org.elasticsearch.transport.Transport)

Example 4 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class InitialSearchPhase method onShardFailure.

private void onShardFailure(final int shardIndex, @Nullable ShardRouting shard, @Nullable String nodeId, final ShardIterator shardIt, Exception e) {
    // 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());
    onShardFailure(shardIndex, shardTarget, e);
    if (totalOps.incrementAndGet() == expectedTotalOps) {
        if (logger.isDebugEnabled()) {
            if (e != null && !TransportActions.isShardNotAvailableException(e)) {
                logger.debug((Supplier<?>) () -> new ParameterizedMessage("{}: Failed to execute [{}]", shard != null ? shard.shortSummary() : shardIt.shardId(), request), e);
            } else if (logger.isTraceEnabled()) {
                logger.trace((Supplier<?>) () -> new ParameterizedMessage("{}: Failed to execute [{}]", shard, request), e);
            }
        }
        onPhaseDone();
    } else {
        final ShardRouting nextShard = shardIt.nextOrNull();
        final boolean lastShard = nextShard == null;
        // trace log this exception
        logger.trace((Supplier<?>) () -> new ParameterizedMessage("{}: Failed to execute [{}] lastShard [{}]", shard != null ? shard.shortSummary() : shardIt.shardId(), request, lastShard), e);
        if (!lastShard) {
            try {
                performPhaseOnShard(shardIndex, shardIt, nextShard);
            } catch (Exception inner) {
                inner.addSuppressed(e);
                onShardFailure(shardIndex, shard, shard.currentNodeId(), shardIt, inner);
            }
        } else {
            // no more shards active, add a failure
            if (logger.isDebugEnabled() && !logger.isTraceEnabled()) {
                // do not double log this exception
                if (e != null && !TransportActions.isShardNotAvailableException(e)) {
                    logger.debug((Supplier<?>) () -> new ParameterizedMessage("{}: Failed to execute [{}] lastShard [{}]", shard != null ? shard.shortSummary() : shardIt.shardId(), request, lastShard), e);
                }
            }
        }
    }
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Supplier(org.apache.logging.log4j.util.Supplier) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) IOException(java.io.IOException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException)

Example 5 with SearchShardTarget

use of org.elasticsearch.search.SearchShardTarget in project elasticsearch by elastic.

the class InitialSearchPhase method onShardResult.

private void onShardResult(int shardIndex, String nodeId, FirstResult result, ShardIterator shardIt) {
    result.shardTarget(new SearchShardTarget(nodeId, shardIt.shardId()));
    onShardSuccess(shardIndex, result);
    // we need to increment successful ops first before we compare the exit condition otherwise if we
    // are fast we could concurrently update totalOps but then preempt one of the threads which can
    // cause the successor to read a wrong value from successfulOps if second phase is very fast ie. count etc.
    // increment all the "future" shards to update the total ops since we some may work and some may not...
    // and when that happens, we break on total ops, so we must maintain them
    final int xTotalOps = totalOps.addAndGet(shardIt.remaining() + 1);
    if (xTotalOps == expectedTotalOps) {
        onPhaseDone();
    } else if (xTotalOps > expectedTotalOps) {
        throw new AssertionError("unexpected higher total ops [" + xTotalOps + "] compared to expected [" + expectedTotalOps + "]");
    }
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Aggregations

SearchShardTarget (org.elasticsearch.search.SearchShardTarget)29 Index (org.elasticsearch.index.Index)22 IOException (java.io.IOException)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)12 TopDocs (org.apache.lucene.search.TopDocs)11 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)11 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)10 ActionListener (org.elasticsearch.action.ActionListener)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ParsingException (org.elasticsearch.common.ParsingException)7 AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)7 ShardId (org.elasticsearch.index.shard.ShardId)6 ArrayList (java.util.ArrayList)5 SearchHit (org.elasticsearch.search.SearchHit)5 SearchHits (org.elasticsearch.search.SearchHits)5 DfsSearchResult (org.elasticsearch.search.dfs.DfsSearchResult)5 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)5 QueryFetchSearchResult (org.elasticsearch.search.fetch.QueryFetchSearchResult)5 ShardFetchSearchRequest (org.elasticsearch.search.fetch.ShardFetchSearchRequest)5 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)4