Search in sources :

Example 11 with CountDown

use of org.elasticsearch.common.util.concurrent.CountDown in project crate by crate.

the class SyncedFlushService method sendPreSyncRequests.

/**
 * send presync requests to all started copies of the given shard
 */
void sendPreSyncRequests(final List<ShardRouting> shards, final ClusterState state, final ShardId shardId, final ActionListener<Map<String, PreSyncedFlushResponse>> listener) {
    final CountDown countDown = new CountDown(shards.size());
    final ConcurrentMap<String, PreSyncedFlushResponse> presyncResponses = ConcurrentCollections.newConcurrentMap();
    for (final ShardRouting shard : shards) {
        LOGGER.trace("{} sending pre-synced flush request to {}", shardId, shard);
        final DiscoveryNode node = state.nodes().get(shard.currentNodeId());
        if (node == null) {
            LOGGER.trace("{} shard routing {} refers to an unknown node. skipping.", shardId, shard);
            if (countDown.countDown()) {
                listener.onResponse(presyncResponses);
            }
            continue;
        }
        transportService.sendRequest(node, PRE_SYNCED_FLUSH_ACTION_NAME, new PreShardSyncedFlushRequest(shard.shardId()), new TransportResponseHandler<PreSyncedFlushResponse>() {

            @Override
            public PreSyncedFlushResponse read(StreamInput in) throws IOException {
                return new PreSyncedFlushResponse(in);
            }

            @Override
            public void handleResponse(PreSyncedFlushResponse response) {
                PreSyncedFlushResponse existing = presyncResponses.putIfAbsent(node.getId(), response);
                assert existing == null : "got two answers for node [" + node + "]";
                // count after the assert so we won't decrement twice in handleException
                if (countDown.countDown()) {
                    listener.onResponse(presyncResponses);
                }
            }

            @Override
            public void handleException(TransportException exp) {
                LOGGER.trace(() -> new ParameterizedMessage("{} error while performing pre synced flush on [{}], skipping", shardId, shard), exp);
                if (countDown.countDown()) {
                    listener.onResponse(presyncResponses);
                }
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }
        });
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IOException(java.io.IOException) CountDown(org.elasticsearch.common.util.concurrent.CountDown) TransportException(org.elasticsearch.transport.TransportException) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

CountDown (org.elasticsearch.common.util.concurrent.CountDown)11 TransportException (org.elasticsearch.transport.TransportException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)5 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)5 ShardId (org.elasticsearch.index.shard.ShardId)5 List (java.util.List)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 Supplier (org.apache.logging.log4j.util.Supplier)3 ActionListener (org.elasticsearch.action.ActionListener)3 ClusterState (org.elasticsearch.cluster.ClusterState)3 UnknownHostException (java.net.UnknownHostException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2