Search in sources :

Example 1 with AsyncPromise

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

the class LocalSessionTest method prepareCancellation.

/**
 * If the session is cancelled mid-prepare, the isCancelled boolean supplier should start returning true
 */
@Test
public void prepareCancellation() {
    UUID sessionID = registerSession();
    AtomicReference<BooleanSupplier> isCancelledRef = new AtomicReference<>();
    Promise<List<Void>> future = new AsyncPromise<>();
    InstrumentedLocalSessions sessions = new InstrumentedLocalSessions() {

        Future<List<Void>> prepareSession(KeyspaceRepairManager repairManager, UUID sessionID, Collection<ColumnFamilyStore> tables, RangesAtEndpoint ranges, ExecutorService executor, BooleanSupplier isCancelled) {
            isCancelledRef.set(isCancelled);
            return future;
        }
    };
    sessions.start();
    sessions.handlePrepareMessage(PARTICIPANT1, new PrepareConsistentRequest(sessionID, COORDINATOR, PARTICIPANTS));
    BooleanSupplier isCancelled = isCancelledRef.get();
    Assert.assertNotNull(isCancelled);
    Assert.assertFalse(isCancelled.getAsBoolean());
    Assert.assertTrue(sessions.sentMessages.isEmpty());
    sessions.failSession(sessionID, false);
    Assert.assertTrue(isCancelled.getAsBoolean());
    // now that the session has failed, it send a negative response to the coordinator (even if the anti-compaction completed successfully)
    future.trySuccess(null);
    assertMessagesSent(sessions, COORDINATOR, new PrepareConsistentResponse(sessionID, PARTICIPANT1, false));
}
Also used : RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) PrepareConsistentResponse(org.apache.cassandra.repair.messages.PrepareConsistentResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncPromise(org.apache.cassandra.utils.concurrent.AsyncPromise) KeyspaceRepairManager(org.apache.cassandra.repair.KeyspaceRepairManager) PrepareConsistentRequest(org.apache.cassandra.repair.messages.PrepareConsistentRequest) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) BooleanSupplier(java.util.function.BooleanSupplier) AbstractRepairTest(org.apache.cassandra.repair.AbstractRepairTest) Test(org.junit.Test)

Example 2 with AsyncPromise

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

the class MemtableCleanerThreadTest method testCleanerError.

@Test
public void testCleanerError() throws Exception {
    AtomicReference<CountDownLatch> cleanerLatch = new AtomicReference<>(new CountDownLatch(1));
    AtomicReference<AsyncPromise<Boolean>> fut = new AtomicReference<>(new AsyncPromise<>());
    AtomicBoolean needsCleaning = new AtomicBoolean(false);
    AtomicInteger numTimeCleanerInvoked = new AtomicInteger(0);
    when(pool.needsCleaning()).thenAnswer(invocation -> needsCleaning.get());
    cleaner = () -> {
        needsCleaning.set(false);
        numTimeCleanerInvoked.incrementAndGet();
        cleanerLatch.get().countDown();
        return fut.get();
    };
    // start the thread with needsCleaning returning true, the cleaner should be invoked
    needsCleaning.set(true);
    startThread(cleaner);
    assertTrue(cleanerLatch.get().await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(0, cleanerLatch.get().getCount());
    assertEquals(1, cleanerThread.numPendingTasks());
    assertEquals(1, numTimeCleanerInvoked.get());
    // complete the cleaning task with an error, no other cleaning task should be invoked
    cleanerLatch.set(new CountDownLatch(1));
    AsyncPromise<Boolean> oldFut = fut.get();
    fut.set(new AsyncPromise<>());
    needsCleaning.set(false);
    oldFut.setFailure(new RuntimeException("Test"));
    assertFalse(cleanerLatch.get().await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(1, cleanerLatch.get().getCount());
    assertEquals(1, numTimeCleanerInvoked.get());
    // now trigger cleaning again and verify that a new task is invoked
    cleanerLatch.set(new CountDownLatch(1));
    fut.set(new AsyncPromise<>());
    needsCleaning.set(true);
    cleanerThread.trigger();
    assertTrue(cleanerLatch.get().await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(0, cleanerLatch.get().getCount());
    assertEquals(2, numTimeCleanerInvoked.get());
    // complete the cleaning task with false (nothing should be scheduled)
    cleanerLatch.set(new CountDownLatch(1));
    oldFut = fut.get();
    fut.set(new AsyncPromise<>());
    needsCleaning.set(false);
    oldFut.setSuccess(false);
    assertFalse(cleanerLatch.get().await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(1, cleanerLatch.get().getCount());
    assertEquals(2, numTimeCleanerInvoked.get());
    // now trigger cleaning again and verify that a new task is invoked
    cleanerLatch.set(new CountDownLatch(1));
    fut.set(new AsyncPromise<>());
    needsCleaning.set(true);
    cleanerThread.trigger();
    assertTrue(cleanerLatch.get().await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(0, cleanerLatch.get().getCount());
    assertEquals(3, numTimeCleanerInvoked.get());
    stopThread();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncPromise(org.apache.cassandra.utils.concurrent.AsyncPromise) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 3 with AsyncPromise

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

the class ColumnFamilyStore method flushLargestMemtable.

/**
 * Finds the largest memtable, as a percentage of *either* on- or off-heap memory limits, and immediately
 * queues it for flushing. If the memtable selected is flushed before this completes, no work is done.
 */
public static Future<Boolean> flushLargestMemtable() {
    float largestRatio = 0f;
    Memtable largest = null;
    float liveOnHeap = 0, liveOffHeap = 0;
    for (ColumnFamilyStore cfs : ColumnFamilyStore.all()) {
        // we take a reference to the current main memtable for the CF prior to snapping its ownership ratios
        // to ensure we have some ordering guarantee for performing the switchMemtableIf(), i.e. we will only
        // swap if the memtables we are measuring here haven't already been swapped by the time we try to swap them
        Memtable current = cfs.getTracker().getView().getCurrentMemtable();
        // find the total ownership ratio for the memtable and all SecondaryIndexes owned by this CF,
        // both on- and off-heap, and select the largest of the two ratios to weight this CF
        float onHeap = 0f, offHeap = 0f;
        onHeap += current.getAllocator().onHeap().ownershipRatio();
        offHeap += current.getAllocator().offHeap().ownershipRatio();
        for (ColumnFamilyStore indexCfs : cfs.indexManager.getAllIndexColumnFamilyStores()) {
            MemtableAllocator allocator = indexCfs.getTracker().getView().getCurrentMemtable().getAllocator();
            onHeap += allocator.onHeap().ownershipRatio();
            offHeap += allocator.offHeap().ownershipRatio();
        }
        float ratio = Math.max(onHeap, offHeap);
        if (ratio > largestRatio) {
            largest = current;
            largestRatio = ratio;
        }
        liveOnHeap += onHeap;
        liveOffHeap += offHeap;
    }
    Promise<Boolean> returnFuture = new AsyncPromise<>();
    if (largest != null) {
        float usedOnHeap = Memtable.MEMORY_POOL.onHeap.usedRatio();
        float usedOffHeap = Memtable.MEMORY_POOL.offHeap.usedRatio();
        float flushingOnHeap = Memtable.MEMORY_POOL.onHeap.reclaimingRatio();
        float flushingOffHeap = Memtable.MEMORY_POOL.offHeap.reclaimingRatio();
        float thisOnHeap = largest.getAllocator().onHeap().ownershipRatio();
        float thisOffHeap = largest.getAllocator().offHeap().ownershipRatio();
        logger.debug("Flushing largest {} to free up room. Used total: {}, live: {}, flushing: {}, this: {}", largest.cfs, ratio(usedOnHeap, usedOffHeap), ratio(liveOnHeap, liveOffHeap), ratio(flushingOnHeap, flushingOffHeap), ratio(thisOnHeap, thisOffHeap));
        Future<CommitLogPosition> flushFuture = largest.cfs.switchMemtableIfCurrent(largest);
        flushFuture.addListener(() -> {
            try {
                flushFuture.get();
                returnFuture.trySuccess(true);
            } catch (Throwable t) {
                returnFuture.tryFailure(t);
            }
        });
    } else {
        logger.debug("Flushing of largest memtable, not done, no memtable found");
        returnFuture.trySuccess(false);
    }
    return returnFuture;
}
Also used : MemtableAllocator(org.apache.cassandra.utils.memory.MemtableAllocator) CommitLogPosition(org.apache.cassandra.db.commitlog.CommitLogPosition) AsyncPromise(org.apache.cassandra.utils.concurrent.AsyncPromise)

Example 4 with AsyncPromise

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

the class MemtableCleanerThreadTest method testCleanerInvoked.

@Test
public void testCleanerInvoked() throws Exception {
    CountDownLatch cleanerExecutedLatch = new CountDownLatch(1);
    AsyncPromise<Boolean> fut = new AsyncPromise<>();
    AtomicBoolean needsCleaning = new AtomicBoolean(false);
    when(pool.needsCleaning()).thenAnswer(invocation -> needsCleaning.get());
    cleaner = () -> {
        needsCleaning.set(false);
        cleanerExecutedLatch.countDown();
        return fut;
    };
    // start the thread with needsCleaning returning false, the cleaner should not be invoked
    needsCleaning.set(false);
    startThread(cleaner);
    assertFalse(cleanerExecutedLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(1, cleanerExecutedLatch.getCount());
    assertEquals(0, cleanerThread.numPendingTasks());
    // now invoke the cleaner
    needsCleaning.set(true);
    cleanerThread.trigger();
    assertTrue(cleanerExecutedLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals(0, cleanerExecutedLatch.getCount());
    assertEquals(1, cleanerThread.numPendingTasks());
    // now complete the cleaning task
    needsCleaning.set(false);
    fut.setSuccess(true);
    waitForPendingTasks();
    stopThread();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncPromise(org.apache.cassandra.utils.concurrent.AsyncPromise) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

AsyncPromise (org.apache.cassandra.utils.concurrent.AsyncPromise)4 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 UUID (java.util.UUID)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BooleanSupplier (java.util.function.BooleanSupplier)1 CommitLogPosition (org.apache.cassandra.db.commitlog.CommitLogPosition)1 RangesAtEndpoint (org.apache.cassandra.locator.RangesAtEndpoint)1 AbstractRepairTest (org.apache.cassandra.repair.AbstractRepairTest)1 KeyspaceRepairManager (org.apache.cassandra.repair.KeyspaceRepairManager)1 PrepareConsistentRequest (org.apache.cassandra.repair.messages.PrepareConsistentRequest)1 PrepareConsistentResponse (org.apache.cassandra.repair.messages.PrepareConsistentResponse)1 MemtableAllocator (org.apache.cassandra.utils.memory.MemtableAllocator)1