use of org.apache.cassandra.db.commitlog.CommitLogPosition 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.db.commitlog.CommitLogPosition in project cassandra by apache.
the class CassandraKeyspaceWriteHandler method beginWrite.
@Override
// group is closed when CassandraWriteContext is closed
@SuppressWarnings("resource")
public WriteContext beginWrite(Mutation mutation, boolean makeDurable) throws RequestExecutionException {
OpOrder.Group group = null;
try {
group = Keyspace.writeOrder.start();
// write the mutation to the commitlog and memtables
CommitLogPosition position = null;
if (makeDurable) {
Tracing.trace("Appending to commitlog");
position = CommitLog.instance.add(mutation);
}
return new CassandraWriteContext(group, position);
} catch (Throwable t) {
if (group != null) {
group.close();
}
throw t;
}
}
Aggregations