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));
}
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();
}
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;
}
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();
}
Aggregations