Search in sources :

Example 6 with CountDownLatch

use of org.apache.cassandra.utils.concurrent.CountDownLatch in project cassandra by apache.

the class ActiveRepairService method prepareForRepair.

public UUID prepareForRepair(UUID parentRepairSession, InetAddressAndPort coordinator, Set<InetAddressAndPort> endpoints, RepairOption options, boolean isForcedRepair, List<ColumnFamilyStore> columnFamilyStores) {
    if (!verifyCompactionsPendingThreshold(parentRepairSession, options.getPreviewKind()))
        // failRepair throws exception
        failRepair(parentRepairSession, "Rejecting incoming repair, pending compactions above threshold");
    long repairedAt = getRepairedAt(options, isForcedRepair);
    registerParentRepairSession(parentRepairSession, coordinator, columnFamilyStores, options.getRanges(), options.isIncremental(), repairedAt, options.isGlobal(), options.getPreviewKind());
    final CountDownLatch prepareLatch = newCountDownLatch(endpoints.size());
    final AtomicBoolean status = new AtomicBoolean(true);
    final Set<String> failedNodes = synchronizedSet(new HashSet<String>());
    final AtomicInteger timeouts = new AtomicInteger(0);
    RequestCallback callback = new RequestCallback() {

        @Override
        public void onResponse(Message msg) {
            prepareLatch.decrement();
        }

        @Override
        public void onFailure(InetAddressAndPort from, RequestFailureReason failureReason) {
            status.set(false);
            failedNodes.add(from.toString());
            if (failureReason == RequestFailureReason.TIMEOUT)
                timeouts.incrementAndGet();
            prepareLatch.decrement();
        }

        @Override
        public boolean invokeOnFailure() {
            return true;
        }
    };
    List<TableId> tableIds = new ArrayList<>(columnFamilyStores.size());
    for (ColumnFamilyStore cfs : columnFamilyStores) tableIds.add(cfs.metadata.id);
    for (InetAddressAndPort neighbour : endpoints) {
        if (FailureDetector.instance.isAlive(neighbour)) {
            PrepareMessage message = new PrepareMessage(parentRepairSession, tableIds, options.getRanges(), options.isIncremental(), repairedAt, options.isGlobal(), options.getPreviewKind());
            Message<RepairMessage> msg = out(PREPARE_MSG, message);
            MessagingService.instance().sendWithCallback(msg, neighbour, callback);
        } else {
            // remaining ones go down, we still want to fail so we don't create repair sessions that can't complete
            if (isForcedRepair && !options.isIncremental()) {
                prepareLatch.decrement();
            } else {
                // bailout early to avoid potentially waiting for a long time.
                failRepair(parentRepairSession, "Endpoint not alive: " + neighbour);
            }
        }
    }
    try {
        if (!prepareLatch.await(getRpcTimeout(MILLISECONDS), MILLISECONDS) || timeouts.get() > 0)
            failRepair(parentRepairSession, "Did not get replies from all endpoints.");
    } catch (InterruptedException e) {
        failRepair(parentRepairSession, "Interrupted while waiting for prepare repair response.");
    }
    if (!status.get()) {
        failRepair(parentRepairSession, "Got negative replies from endpoints " + failedNodes);
    }
    return parentRepairSession;
}
Also used : TableId(org.apache.cassandra.schema.TableId) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) PrepareMessage(org.apache.cassandra.repair.messages.PrepareMessage) Message(org.apache.cassandra.net.Message) CleanupMessage(org.apache.cassandra.repair.messages.CleanupMessage) RepairMessage(org.apache.cassandra.repair.messages.RepairMessage) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RepairMessage(org.apache.cassandra.repair.messages.RepairMessage) RequestFailureReason(org.apache.cassandra.exceptions.RequestFailureReason) PrepareMessage(org.apache.cassandra.repair.messages.PrepareMessage) RequestCallback(org.apache.cassandra.net.RequestCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Aggregations

CountDownLatch (org.apache.cassandra.utils.concurrent.CountDownLatch)6 CountDownLatch.newCountDownLatch (org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 TimeUnit (java.util.concurrent.TimeUnit)3 Gossiper (org.apache.cassandra.gms.Gossiper)3 Global.nanoTime (org.apache.cassandra.utils.Clock.Global.nanoTime)3 FBUtilities (org.apache.cassandra.utils.FBUtilities)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 HashMultimap (com.google.common.collect.HashMultimap)2 SetMultimap (com.google.common.collect.SetMultimap)2