static ParsedScrollId parseScrollId(String scrollId) { try { byte[] bytes = Base64.getUrlDecoder().decode(scrollId); ByteArrayDataInput in = new ByteArrayDataInput(bytes); String type = in.readString(); ScrollIdForNode[] context = new ScrollIdForNode[in.readVInt()]; for (int i = 0; i < context.length; ++i) { long id = in.readLong(); String target = in.readString(); String clusterAlias; final int index = target.indexOf(RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR); if (index == -1) { clusterAlias = null; } else { clusterAlias = target.substring(0, index); target = target.substring(index+1); } context[i] = new ScrollIdForNode(clusterAlias, target, id); } if (in.getPosition() != bytes.length) { throw new IllegalArgumentException("Not all bytes were read"); } return new ParsedScrollId(scrollId, type, context); } catch (Exception e) { throw new IllegalArgumentException("Cannot parse scroll id", e); } }
protected SearchScrollAsyncAction(ParsedScrollId scrollId, Logger logger, DiscoveryNodes nodes, ActionListener<SearchResponse> listener, SearchPhaseController searchPhaseController, SearchScrollRequest request, SearchTransportService searchTransportService) { this.startTime = System.currentTimeMillis(); this.scrollId = scrollId; this.successfulOps = new AtomicInteger(scrollId.getContext().length); this.logger = logger; this.listener = listener; this.nodes = nodes; this.searchPhaseController = searchPhaseController; this.request = request; this.searchTransportService = searchTransportService; }
@Override protected void doExecute(Task task, SearchScrollRequest request, ActionListener<SearchResponse> listener) { try { ParsedScrollId scrollId = parseScrollId(request.scrollId()); Runnable action; switch (scrollId.getType()) { case QUERY_THEN_FETCH_TYPE: action = new SearchScrollQueryThenFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; case QUERY_AND_FETCH_TYPE: // TODO can we get rid of this? action = new SearchScrollQueryAndFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; default: throw new IllegalArgumentException("Scroll id type [" + scrollId.getType() + "] unrecognized"); } action.run(); } catch (Exception e) { listener.onFailure(e); } } }
((InternalSearchHits) internalResponse.hits()).totalHits = Long.parseLong(this.scrollId.getAttributes().get("total_hits")); if (request.scroll() != null) { scrollId = TransportSearchHelper.buildScrollId(this.scrollId.getType(), queryFetchResults, this.scrollId.getAttributes()); // continue moving the total_hits listener.onResponse(new SearchResponse(internalResponse, scrollId, this.scrollId.getContext().length, successfulOps.get(), buildTookInMillis(), buildShardFailures()));
public void start() { if (scrollId.getContext().length == 0) { final InternalSearchResponse internalResponse = new InternalSearchResponse(new InternalSearchHits(InternalSearchHits.EMPTY, Long.parseLong(this.scrollId.getAttributes().get("total_hits")), 0.0f), null, null, null, false, null); listener.onResponse(new SearchResponse(internalResponse, request.scrollId(), 0, 0, 0l, buildShardFailures())); return; ScrollIdForNode[] context = scrollId.getContext(); for (int i = 0; i < context.length; i++) { ScrollIdForNode target = context[i]; } else { if (logger.isDebugEnabled()) { logger.debug("Node [" + target.getScrollId() + "] not available for scroll request [" + scrollId.getSource() + "]"); for (ScrollIdForNode target : scrollId.getContext()) { DiscoveryNode node = nodes.get(target.getNode()); if (node == null) { if (logger.isDebugEnabled()) { logger.debug("Node [" + target.getNode() + "] not available for scroll request [" + scrollId.getSource() + "]");
public void start() { if (scrollId.getContext().length == 0) { listener.onFailure(new SearchPhaseExecutionException("query", "no nodes to search on", ShardSearchFailure.EMPTY_ARRAY)); return; } final AtomicInteger counter = new AtomicInteger(scrollId.getContext().length); ScrollIdForNode[] context = scrollId.getContext(); for (int i = 0; i < context.length; i++) { ScrollIdForNode target = context[i]; DiscoveryNode node = nodes.get(target.getNode()); if (node != null) { executeQueryPhase(i, counter, node, target.getScrollId()); } else { if (logger.isDebugEnabled()) { logger.debug("Node [" + target.getNode() + "] not available for scroll request [" + scrollId.getSource() + "]"); } successfulOps.decrementAndGet(); if (counter.decrementAndGet() == 0) { try { executeFetchPhase(); } catch (Throwable e) { listener.onFailure(new SearchPhaseExecutionException("query", "Fetch failed", e, ShardSearchFailure.EMPTY_ARRAY)); return; } } } } }
public void start() { if (scrollId.getContext().length == 0) { listener.onFailure(new SearchPhaseExecutionException("query", "no nodes to search on", ShardSearchFailure.EMPTY_ARRAY)); return; ScrollIdForNode[] context = scrollId.getContext(); for (int i = 0; i < context.length; i++) { ScrollIdForNode target = context[i]; } else { if (logger.isDebugEnabled()) { logger.debug("Node [" + target.getNode() + "] not available for scroll request [" + scrollId.getSource() + "]"); for (ScrollIdForNode target : scrollId.getContext()) { DiscoveryNode node = nodes.get(target.getNode()); if (node == null) { if (logger.isDebugEnabled()) { logger.debug("Node [" + target.getNode() + "] not available for scroll request [" + scrollId.getSource() + "]");
SearchScrollQueryThenFetchAsyncAction(Logger logger, ClusterService clusterService, SearchTransportService searchTransportService, SearchPhaseController searchPhaseController, SearchScrollRequest request, SearchTask task, ParsedScrollId scrollId, ActionListener<SearchResponse> listener) { super(scrollId, logger, clusterService.state().nodes(), listener, searchPhaseController, request, searchTransportService); this.task = task; this.fetchResults = new AtomicArray<>(scrollId.getContext().length); this.queryResults = new AtomicArray<>(scrollId.getContext().length); }
@Override protected void doExecute(Task task, SearchScrollRequest request, ActionListener<SearchResponse> listener) { try { ParsedScrollId scrollId = parseScrollId(request.scrollId()); Runnable action; switch (scrollId.getType()) { case QUERY_THEN_FETCH_TYPE: action = new SearchScrollQueryThenFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; case QUERY_AND_FETCH_TYPE: // TODO can we get rid of this? action = new SearchScrollQueryAndFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; default: throw new IllegalArgumentException("Scroll id type [" + scrollId.getType() + "] unrecognized"); } action.run(); } catch (Exception e) { listener.onFailure(e); } } }
return new ParsedScrollId(scrollId, type, context, attributes);
SearchScrollQueryAndFetchAsyncAction(Logger logger, ClusterService clusterService, SearchTransportService searchTransportService, SearchPhaseController searchPhaseController, SearchScrollRequest request, SearchTask task, ParsedScrollId scrollId, ActionListener<SearchResponse> listener) { super(scrollId, logger, clusterService.state().nodes(), listener, searchPhaseController, request, searchTransportService); this.task = task; this.queryFetchResults = new AtomicArray<>(scrollId.getContext().length); }
@Override protected void doExecute(Task task, SearchScrollRequest request, ActionListener<SearchResponse> listener) { try { ParsedScrollId scrollId = parseScrollId(request.scrollId()); Runnable action; switch (scrollId.getType()) { case QUERY_THEN_FETCH_TYPE: action = new SearchScrollQueryThenFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; case QUERY_AND_FETCH_TYPE: // TODO can we get rid of this? action = new SearchScrollQueryAndFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; default: throw new IllegalArgumentException("Scroll id type [" + scrollId.getType() + "] unrecognized"); } action.run(); } catch (Exception e) { listener.onFailure(e); } } }
static ParsedScrollId parseScrollId(String scrollId) { try { byte[] bytes = Base64.getUrlDecoder().decode(scrollId); ByteArrayDataInput in = new ByteArrayDataInput(bytes); String type = in.readString(); ScrollIdForNode[] context = new ScrollIdForNode[in.readVInt()]; for (int i = 0; i < context.length; ++i) { long id = in.readLong(); String target = in.readString(); String clusterAlias; final int index = target.indexOf(RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR); if (index == -1) { clusterAlias = null; } else { clusterAlias = target.substring(0, index); target = target.substring(index+1); } context[i] = new ScrollIdForNode(clusterAlias, target, id); } if (in.getPosition() != bytes.length) { throw new IllegalArgumentException("Not all bytes were read"); } return new ParsedScrollId(scrollId, type, context); } catch (Exception e) { throw new IllegalArgumentException("Cannot parse scroll id", e); } }
public final void run() { final ScrollIdForNode[] context = scrollId.getContext(); if (context.length == 0) { listener.onFailure(new SearchPhaseExecutionException("query", "no nodes to search on", ShardSearchFailure.EMPTY_ARRAY)); } else { collectNodesAndRun(Arrays.asList(context), nodes, searchTransportService, ActionListener.wrap(lookup -> run(lookup, context), listener::onFailure)); } }
@Override protected void doExecute(Task task, SearchScrollRequest request, ActionListener<SearchResponse> listener) { try { ParsedScrollId scrollId = parseScrollId(request.scrollId()); Runnable action; switch (scrollId.getType()) { case QUERY_THEN_FETCH_TYPE: action = new SearchScrollQueryThenFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; case QUERY_AND_FETCH_TYPE: // TODO can we get rid of this? action = new SearchScrollQueryAndFetchAsyncAction(logger, clusterService, searchTransportService, searchPhaseController, request, (SearchTask)task, scrollId, listener); break; default: throw new IllegalArgumentException("Scroll id type [" + scrollId.getType() + "] unrecognized"); } action.run(); } catch (Exception e) { listener.onFailure(e); } } }
static ParsedScrollId parseScrollId(String scrollId) { try { byte[] bytes = Base64.getUrlDecoder().decode(scrollId); ByteArrayDataInput in = new ByteArrayDataInput(bytes); String type = in.readString(); ScrollIdForNode[] context = new ScrollIdForNode[in.readVInt()]; for (int i = 0; i < context.length; ++i) { long id = in.readLong(); String target = in.readString(); String clusterAlias; final int index = target.indexOf(RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR); if (index == -1) { clusterAlias = null; } else { clusterAlias = target.substring(0, index); target = target.substring(index+1); } context[i] = new ScrollIdForNode(clusterAlias, target, id); } if (in.getPosition() != bytes.length) { throw new IllegalArgumentException("Not all bytes were read"); } return new ParsedScrollId(scrollId, type, context); } catch (Exception e) { throw new IllegalArgumentException("Cannot parse scroll id", e); } }
ClearScrollController(ClearScrollRequest request, ActionListener<ClearScrollResponse> listener, DiscoveryNodes nodes, Logger logger, SearchTransportService searchTransportService) { this.nodes = nodes; this.logger = logger; this.searchTransportService = searchTransportService; this.listener = listener; List<String> scrollIds = request.getScrollIds(); final int expectedOps; if (scrollIds.size() == 1 && "_all".equals(scrollIds.get(0))) { expectedOps = nodes.getSize(); runner = this::cleanAllScrolls; } else { List<ScrollIdForNode> parsedScrollIds = new ArrayList<>(); for (String parsedScrollId : request.getScrollIds()) { ScrollIdForNode[] context = parseScrollId(parsedScrollId).getContext(); for (ScrollIdForNode id : context) { parsedScrollIds.add(id); } } if (parsedScrollIds.isEmpty()) { expectedOps = 0; runner = () -> listener.onResponse(new ClearScrollResponse(true, 0)); } else { expectedOps = parsedScrollIds.size(); runner = () -> cleanScrollIds(parsedScrollIds); } } this.expectedOps = new CountDown(expectedOps); }
@Override protected void doExecute(SearchScrollRequest request, ActionListener<SearchResponse> listener) { try { ParsedScrollId scrollId = parseScrollId(request.scrollId()); AbstractAsyncAction action; switch (scrollId.getType()) { case QUERY_THEN_FETCH_TYPE: action = new SearchScrollQueryThenFetchAsyncAction(logger, clusterService, searchService, searchPhaseController, request, scrollId, listener); break; case QUERY_AND_FETCH_TYPE: action = new SearchScrollQueryAndFetchAsyncAction(logger, clusterService, searchService, searchPhaseController, request, scrollId, listener); break; case SCAN: action = new SearchScrollScanAsyncAction(logger, clusterService, searchService, searchPhaseController, request, scrollId, listener); break; default: throw new IllegalArgumentException("Scroll id type [" + scrollId.getType() + "] unrecognized"); } action.start(); } catch (Throwable e) { listener.onFailure(e); } } }
static ParsedScrollId parseScrollId(String scrollId) { try { byte[] bytes = Base64.getUrlDecoder().decode(scrollId); ByteArrayDataInput in = new ByteArrayDataInput(bytes); String type = in.readString(); ScrollIdForNode[] context = new ScrollIdForNode[in.readVInt()]; for (int i = 0; i < context.length; ++i) { long id = in.readLong(); String target = in.readString(); context[i] = new ScrollIdForNode(target, id); } if (in.getPosition() != bytes.length) { throw new IllegalArgumentException("Not all bytes were read"); } return new ParsedScrollId(scrollId, type, context); } catch (Exception e) { throw new IllegalArgumentException("Cannot parse scroll id", e); } }
protected final void sendResponse(SearchPhaseController.ReducedQueryPhase queryPhase, final AtomicArray<? extends SearchPhaseResult> fetchResults) { try { final InternalSearchResponse internalResponse = searchPhaseController.merge(true, queryPhase, fetchResults.asList(), fetchResults::get); // the scroll ID never changes we always return the same ID. This ID contains all the shards and their context ids // such that we can talk to them abgain in the next roundtrip. String scrollId = null; if (request.scroll() != null) { scrollId = request.scrollId(); } listener.onResponse(new SearchResponse(internalResponse, scrollId, this.scrollId.getContext().length, successfulOps.get(), 0, buildTookInMillis(), buildShardFailures(), SearchResponse.Clusters.EMPTY)); } catch (Exception e) { listener.onFailure(new ReduceSearchPhaseException("fetch", "inner finish failed", e, buildShardFailures())); } }