public SearchException(StreamInput in) throws IOException { super(in); if (in.readBoolean()) { shardTarget = new SearchShardTarget(in); } else { shardTarget = null; } }
/** * Returns the fully qualified index name, including the cluster alias. */ public String getFullyQualifiedIndexName() { return RemoteClusterAware.buildRemoteIndexName(getClusterAlias(), getIndex()); }
/** * Releases shard targets that are not used in the docsIdsToLoad. */ private void releaseIrrelevantSearchContext(QuerySearchResult queryResult) { // we only release search context that we did not fetch from if we are not scrolling // and if it has at lease one hit that didn't make it to the global topDocs if (context.getRequest().scroll() == null && queryResult.hasSearchContext()) { try { SearchShardTarget searchShardTarget = queryResult.getSearchShardTarget(); Transport.Connection connection = context.getConnection(searchShardTarget.getClusterAlias(), searchShardTarget.getNodeId()); context.sendReleaseSearchContext(queryResult.getRequestId(), connection, searchShardTarget.getOriginalIndices()); } catch (Exception e) { context.getLogger().trace("failed to release context", e); } } }
@Override public void readFrom(StreamInput in) throws IOException { if (in.readBoolean()) { shardTarget = new SearchShardTarget(in); index = shardTarget.getFullyQualifiedIndexName(); shardId = shardTarget.getShardId().getId(); } reason = in.readString(); status = RestStatus.readFrom(in); cause = in.readException(); }
@Override protected void setSearchShardTarget(T response) { // don't do this - it's part of the response... assert response.getSearchShardTarget() != null : "search shard target must not be null"; if (target.getClusterAlias() != null) { // re-create the search target and add the cluster alias if there is any, // we need this down the road for subseq. phases SearchShardTarget searchShardTarget = response.getSearchShardTarget(); response.setSearchShardTarget(new SearchShardTarget(searchShardTarget.getNodeId(), searchShardTarget.getShardId(), target.getClusterAlias(), null)); } }
@Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); getSearchShardTarget().writeTo(out); result.writeTo(out); } }
public static SearchShardTarget readSearchShardTarget(StreamInput in) throws IOException { SearchShardTarget result = new SearchShardTarget(); result.readFrom(in); return result; }
static String buildScrollId(AtomicArray<? extends SearchPhaseResult> searchPhaseResults) throws IOException { try (RAMOutputStream out = new RAMOutputStream()) { out.writeString(searchPhaseResults.length() == 1 ? ParsedScrollId.QUERY_AND_FETCH_TYPE : ParsedScrollId.QUERY_THEN_FETCH_TYPE); out.writeVInt(searchPhaseResults.asList().size()); for (SearchPhaseResult searchPhaseResult : searchPhaseResults.asList()) { out.writeLong(searchPhaseResult.getRequestId()); SearchShardTarget searchShardTarget = searchPhaseResult.getSearchShardTarget(); if (searchShardTarget.getClusterAlias() != null) { out.writeString(RemoteClusterAware.buildRemoteIndexName(searchShardTarget.getClusterAlias(), searchShardTarget.getNodeId())); } else { out.writeString(searchShardTarget.getNodeId()); } } byte[] bytes = new byte[(int) out.getFilePointer()]; out.writeTo(bytes, 0); return Base64.getUrlEncoder().encodeToString(bytes); } }
builder.field(Fields._SHARD, shard.getShardId()); builder.field(Fields._NODE, shard.getNodeIdText());
/** * This method should be called if a search phase failed to ensure all relevant search contexts and resources are released. * this method will also notify the listener and sends back a failure to the user. * * @param exception the exception explaining or causing the phase failure */ private void raisePhaseFailure(SearchPhaseExecutionException exception) { results.getSuccessfulResults().forEach((entry) -> { try { SearchShardTarget searchShardTarget = entry.getSearchShardTarget(); Transport.Connection connection = getConnection(null, searchShardTarget.getNodeId()); sendReleaseSearchContext(entry.getRequestId(), connection, searchShardTarget.getOriginalIndices()); } catch (Exception inner) { inner.addSuppressed(exception); logger.trace("failed to release context", inner); } }); listener.onFailure(exception); }
String key = result.getSearchShardTarget().toString(); profileResults.put(key, result.consumeProfileResult());
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field(SHARD_FIELD, shardId()); builder.field(INDEX_FIELD, index()); if (shardTarget != null) { builder.field(NODE_FIELD, shardTarget.getNodeId()); } builder.field(REASON_FIELD); builder.startObject(); ElasticsearchException.generateThrowableXContent(builder, params, cause); builder.endObject(); return builder; }
@Override public int compareTo(SearchShardTarget o) { int i = shardId.getIndexName().compareTo(o.getIndex()); if (i == 0) { i = shardId.getId() - o.shardId.id(); } return i; }
public WriterResult execute(SearchIntoContext context) throws WriterException { logger.info("writing {}/{}", context.shardTarget().index(), context.shardTarget().getShardId()); Query query = context.query(); context.version(true); WriterCollector wc = collectors.get(context.targetType()).create( context); wc.open(); try { context.searcher().search(query, wc); } catch (IOException e) { throw new WriterException(context, "Failed to write docs", e); } wc.close(); WriterResult res = wc.getResult(); logger.info("exported {} docs from {}/{}", res.getTotalWrites(), context.shardTarget().index(), context.shardTarget().getShardId()); return res; }
public ShardSearchFailure(Exception e, @Nullable SearchShardTarget shardTarget) { super(shardTarget == null ? null : shardTarget.getFullyQualifiedIndexName(), shardTarget == null ? -1 : shardTarget.getShardId().getId(), ExceptionsHelper.detailedMessage(e), ExceptionsHelper.status(ExceptionsHelper.unwrapCause(e)), ExceptionsHelper.unwrapCause(e)); final Throwable actual = ExceptionsHelper.unwrapCause(e); if (actual instanceof SearchException) { this.shardTarget = ((SearchException) actual).shard(); } else if (shardTarget != null) { this.shardTarget = shardTarget; } }
/** * The shard id the search failed on. */ @Override public int shardId() { if (shardTarget != null) { return shardTarget.getShardId().id(); } return -1; }
@Override protected void setSearchShardTarget(T response) { // don't do this - it's part of the response... assert response.getSearchShardTarget() != null : "search shard target must not be null"; if (target.getClusterAlias() != null) { // re-create the search target and add the cluster alias if there is any, // we need this down the road for subseq. phases SearchShardTarget searchShardTarget = response.getSearchShardTarget(); response.setSearchShardTarget(new SearchShardTarget(searchShardTarget.getNodeId(), searchShardTarget.getShardId(), target.getClusterAlias(), null)); } }
@Override public void readFrom(StreamInput in) throws IOException { if (in.readBoolean()) { shardTarget = new SearchShardTarget(in); index = shardTarget.getFullyQualifiedIndexName(); shardId = shardTarget.getShardId().getId(); } reason = in.readString(); status = RestStatus.readFrom(in); cause = in.readException(); }
@Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); getSearchShardTarget().writeTo(out); result.writeTo(out); } }
lastEmittedDoc); SearchShardTarget searchShardTarget = querySearchResult.getSearchShardTarget(); DiscoveryNode node = clusterNodeLookup.apply(searchShardTarget.getClusterAlias(), searchShardTarget.getNodeId()); assert node != null : "target node is null in secondary phase"; Transport.Connection connection = getConnection(searchShardTarget.getClusterAlias(), node); searchTransportService.sendExecuteFetchScroll(connection, shardFetchRequest, task, new SearchActionListener<FetchSearchResult>(querySearchResult.getSearchShardTarget(), index) {