|
14 | 14 | import org.apache.lucene.search.ScoreDoc;
|
15 | 15 | import org.apache.lucene.search.TopFieldDocs;
|
16 | 16 | import org.elasticsearch.ExceptionsHelper;
|
| 17 | +import org.elasticsearch.TransportVersion; |
17 | 18 | import org.elasticsearch.TransportVersions;
|
18 | 19 | import org.elasticsearch.Version;
|
19 | 20 | import org.elasticsearch.action.ActionListener;
|
|
23 | 24 | import org.elasticsearch.action.support.IndicesOptions;
|
24 | 25 | import org.elasticsearch.client.internal.Client;
|
25 | 26 | import org.elasticsearch.cluster.ClusterState;
|
| 27 | +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; |
26 | 28 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
27 | 29 | import org.elasticsearch.common.io.stream.StreamInput;
|
28 | 30 | import org.elasticsearch.common.io.stream.StreamOutput;
|
|
50 | 52 | import org.elasticsearch.tasks.TaskCancelledException;
|
51 | 53 | import org.elasticsearch.tasks.TaskId;
|
52 | 54 | import org.elasticsearch.threadpool.ThreadPool;
|
| 55 | +import org.elasticsearch.transport.BytesTransportResponse; |
53 | 56 | import org.elasticsearch.transport.LeakTracker;
|
54 | 57 | import org.elasticsearch.transport.SendRequestTransportException;
|
55 | 58 | import org.elasticsearch.transport.Transport;
|
@@ -232,11 +235,6 @@ public static final class NodeQueryResponse extends TransportResponse {
|
232 | 235 | assert Arrays.stream(results).noneMatch(Objects::isNull) : Arrays.toString(results);
|
233 | 236 | }
|
234 | 237 |
|
235 |
| - // public for tests |
236 |
| - public Object[] getResults() { |
237 |
| - return results; |
238 |
| - } |
239 |
| - |
240 | 238 | @Override
|
241 | 239 | public void writeTo(StreamOutput out) throws IOException {
|
242 | 240 | out.writeArray((o, v) -> {
|
@@ -465,60 +463,83 @@ protected void doRun(Map<SearchShardIterator, Integer> shardIndexMap) {
|
465 | 463 | return;
|
466 | 464 | }
|
467 | 465 | searchTransportService.transportService()
|
468 |
| - .sendChildRequest(connection, NODE_SEARCH_ACTION_NAME, request, task, new TransportResponseHandler<NodeQueryResponse>() { |
469 |
| - @Override |
470 |
| - public NodeQueryResponse read(StreamInput in) throws IOException { |
471 |
| - return new NodeQueryResponse(in); |
472 |
| - } |
473 |
| - |
474 |
| - @Override |
475 |
| - public Executor executor() { |
476 |
| - return EsExecutors.DIRECT_EXECUTOR_SERVICE; |
477 |
| - } |
| 466 | + .sendChildRequest( |
| 467 | + connection, |
| 468 | + NODE_SEARCH_ACTION_NAME, |
| 469 | + request, |
| 470 | + task, |
| 471 | + new TransportResponseHandler<BytesTransportResponse>() { |
| 472 | + @Override |
| 473 | + public BytesTransportResponse read(StreamInput in) throws IOException { |
| 474 | + return new BytesTransportResponse(in); |
| 475 | + } |
478 | 476 |
|
479 |
| - @Override |
480 |
| - public void handleResponse(NodeQueryResponse response) { |
481 |
| - if (results instanceof QueryPhaseResultConsumer queryPhaseResultConsumer) { |
482 |
| - queryPhaseResultConsumer.addBatchedPartialResult(response.topDocsStats, response.mergeResult); |
| 477 | + @Override |
| 478 | + public Executor executor() { |
| 479 | + return EsExecutors.DIRECT_EXECUTOR_SERVICE; |
483 | 480 | }
|
484 |
| - for (int i = 0; i < response.results.length; i++) { |
485 |
| - var s = request.shards.get(i); |
486 |
| - int shardIdx = s.shardIndex; |
487 |
| - final SearchShardTarget target = new SearchShardTarget(routing.nodeId(), s.shardId, routing.clusterAlias()); |
488 |
| - switch (response.results[i]) { |
489 |
| - case Exception e -> onShardFailure(shardIdx, target, shardIterators[shardIdx], e); |
490 |
| - case SearchPhaseResult q -> { |
491 |
| - q.setShardIndex(shardIdx); |
492 |
| - q.setSearchShardTarget(target); |
493 |
| - onShardResult(q); |
| 481 | + |
| 482 | + @Override |
| 483 | + public void handleResponse(BytesTransportResponse bytesTransportResponse) { |
| 484 | + outstandingShards.incrementAndGet(); |
| 485 | + try { |
| 486 | + var in = bytesTransportResponse.bytes().streamInput(); |
| 487 | + in.setTransportVersion(TransportVersion.current()); |
| 488 | + in = new NamedWriteableAwareStreamInput(in, namedWriteableRegistry); |
| 489 | + in.setTransportVersion(TransportVersion.current()); |
| 490 | + final int count = in.readVInt(); |
| 491 | + for (int i = 0; i < count; i++) { |
| 492 | + var s = request.shards.get(i); |
| 493 | + int shardIdx = s.shardIndex; |
| 494 | + final SearchShardTarget target = new SearchShardTarget( |
| 495 | + routing.nodeId(), |
| 496 | + s.shardId, |
| 497 | + routing.clusterAlias() |
| 498 | + ); |
| 499 | + if (in.readBoolean()) { |
| 500 | + var q = new QuerySearchResult(in); |
| 501 | + q.setShardIndex(shardIdx); |
| 502 | + q.setSearchShardTarget(target); |
| 503 | + onShardResult(q); |
| 504 | + } else { |
| 505 | + onShardFailure(shardIdx, target, shardIterators[shardIdx], in.readException()); |
| 506 | + } |
494 | 507 | }
|
495 |
| - case null, default -> { |
496 |
| - assert false : "impossible [" + response.results[i] + "]"; |
| 508 | + var mergeResult = QueryPhaseResultConsumer.MergeResult.readFrom(in); |
| 509 | + var topDocsStats = SearchPhaseController.TopDocsStats.readFrom(in); |
| 510 | + if (results instanceof QueryPhaseResultConsumer queryPhaseResultConsumer) { |
| 511 | + queryPhaseResultConsumer.addBatchedPartialResult(topDocsStats, mergeResult); |
497 | 512 | }
|
| 513 | + } catch (IOException e) { |
| 514 | + assert false : e; |
| 515 | + handleException(new TransportException(e)); |
| 516 | + } finally { |
| 517 | + onShardDone(); |
498 | 518 | }
|
499 | 519 | }
|
500 |
| - } |
501 | 520 |
|
502 |
| - @Override |
503 |
| - public void handleException(TransportException e) { |
504 |
| - Exception cause = (Exception) ExceptionsHelper.unwrapCause(e); |
505 |
| - if (e instanceof SendRequestTransportException || cause instanceof TaskCancelledException) { |
506 |
| - // two possible special cases here where we do not want to fail the phase: |
507 |
| - // failure to send out the request -> handle things the same way a shard would fail with unbatched execution |
508 |
| - // as this could be a transient failure and partial results we may have are still valid |
509 |
| - // cancellation of the whole batched request on the remote -> maybe we timed out or so, partial results may |
510 |
| - // still be valid |
511 |
| - onNodeQueryFailure(e, request, routing); |
512 |
| - } else { |
513 |
| - // Remote failure that wasn't due to networking or cancellation means that the data node was unable to reduce |
514 |
| - // its local results. Failure to reduce always fails the phase without exception so we fail the phase here. |
515 |
| - if (results instanceof QueryPhaseResultConsumer queryPhaseResultConsumer) { |
516 |
| - queryPhaseResultConsumer.failure.compareAndSet(null, cause); |
| 521 | + @Override |
| 522 | + public void handleException(TransportException e) { |
| 523 | + Exception cause = (Exception) ExceptionsHelper.unwrapCause(e); |
| 524 | + if (e instanceof SendRequestTransportException || cause instanceof TaskCancelledException) { |
| 525 | + // two possible special cases here where we do not want to fail the phase: |
| 526 | + // failure to send out the request -> handle things the same way a shard would fail with unbatched execution |
| 527 | + // as this could be a transient failure and partial results we may have are still valid |
| 528 | + // cancellation of the whole batched request on the remote -> maybe we timed out or so, partial results may |
| 529 | + // still be valid |
| 530 | + onNodeQueryFailure(e, request, routing); |
| 531 | + } else { |
| 532 | + // Remote failure that wasn't due to networking or cancellation means that the data node was unable to |
| 533 | + // reduce |
| 534 | + // its local results. Failure to reduce always fails the phase without exception so we fail the phase here. |
| 535 | + if (results instanceof QueryPhaseResultConsumer queryPhaseResultConsumer) { |
| 536 | + queryPhaseResultConsumer.failure.compareAndSet(null, cause); |
| 537 | + } |
| 538 | + onPhaseFailure(getName(), "", cause); |
517 | 539 | }
|
518 |
| - onPhaseFailure(getName(), "", cause); |
519 | 540 | }
|
520 | 541 | }
|
521 |
| - }); |
| 542 | + ); |
522 | 543 | });
|
523 | 544 | }
|
524 | 545 |
|
|
0 commit comments