org.elasticsearch.action.LatchedActionListener Java Examples

The following examples show how to use org.elasticsearch.action.LatchedActionListener. 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: ClientUtil.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
/**
 * Send a nonblocking request with a timeout and return response. Blocking is not allowed in a
 * transport call context. See BaseFuture.blockingAllowed
 * @param request request like index/search/get
 * @param LOG log
 * @param consumer functional interface to operate as a client request like client::get
 * @param <Request> ActionRequest
 * @param <Response> ActionResponse
 * @return the response
 * @throws ElasticsearchTimeoutException when we cannot get response within time.
 * @throws IllegalStateException when the waiting thread is interrupted
 */
public <Request extends ActionRequest, Response extends ActionResponse> Optional<Response> timedRequest(
    Request request,
    Logger LOG,
    BiConsumer<Request, ActionListener<Response>> consumer
) {
    try {
        AtomicReference<Response> respReference = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);

        consumer
            .accept(
                request,
                new LatchedActionListener<Response>(
                    ActionListener
                        .wrap(
                            response -> { respReference.set(response); },
                            exception -> { LOG.error("Cannot get response for request {}, error: {}", request, exception); }
                        ),
                    latch
                )
            );

        if (!latch.await(requestTimeout.getSeconds(), TimeUnit.SECONDS)) {
            throw new ElasticsearchTimeoutException("Cannot get response within time limit: " + request.toString());
        }
        return Optional.ofNullable(respReference.get());
    } catch (InterruptedException e1) {
        LOG.error(CommonErrorMessages.WAIT_ERR_MSG);
        throw new IllegalStateException(e1);
    }
}
 
Example #2
Source File: InternalClusterInfoService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the latest nodes stats, calling the listener when complete
 * @return a latch that can be used to wait for the nodes stats to complete if desired
 */
protected CountDownLatch updateNodeStats(final ActionListener<NodesStatsResponse> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    final NodesStatsRequest nodesStatsRequest = new NodesStatsRequest("data:true");
    nodesStatsRequest.clear();
    nodesStatsRequest.fs(true);
    nodesStatsRequest.timeout(fetchTimeout);

    transportNodesStatsAction.execute(nodesStatsRequest, new LatchedActionListener<>(listener, latch));
    return latch;
}
 
Example #3
Source File: InternalClusterInfoService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the latest indices stats, calling the listener when complete
 * @return a latch that can be used to wait for the indices stats to complete if desired
 */
protected CountDownLatch updateIndicesStats(final ActionListener<IndicesStatsResponse> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    final IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
    indicesStatsRequest.clear();
    indicesStatsRequest.store(true);

    transportIndicesStatsAction.execute(indicesStatsRequest, new LatchedActionListener<>(listener, latch));
    return latch;
}
 
Example #4
Source File: InternalClusterInfoService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the latest nodes stats, calling the listener when complete
 * @return a latch that can be used to wait for the nodes stats to complete if desired
 */
protected CountDownLatch updateNodeStats(final ActionListener<NodesStatsResponse> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    ObjectContainer<DiscoveryNode> allDataNodes = this.clusterService.state().getNodes().getDataNodes().values();
    final NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(allDataNodes.toArray(DiscoveryNode.class));
    nodesStatsRequest.timeout(fetchTimeout);
    client.admin().cluster().nodesStats(nodesStatsRequest, new LatchedActionListener<>(listener, latch));
    return latch;
}
 
Example #5
Source File: InternalClusterInfoService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the latest indices stats, calling the listener when complete
 * @return a latch that can be used to wait for the indices stats to complete if desired
 */
private CountDownLatch updateIndicesStats(final ActionListener<IndicesStatsResponse> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    final IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
    indicesStatsRequest.clear();
    indicesStatsRequest.store(true);

    client.admin().indices().stats(indicesStatsRequest, new LatchedActionListener<>(listener, latch));
    return latch;
}