Search in sources :

Example 6 with CommitLogPosition

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;
}
Also used : MemtableAllocator(org.apache.cassandra.utils.memory.MemtableAllocator) CommitLogPosition(org.apache.cassandra.db.commitlog.CommitLogPosition) AsyncPromise(org.apache.cassandra.utils.concurrent.AsyncPromise)

Example 7 with CommitLogPosition

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;
    }
}
Also used : OpOrder(org.apache.cassandra.utils.concurrent.OpOrder) CommitLogPosition(org.apache.cassandra.db.commitlog.CommitLogPosition)

Aggregations

CommitLogPosition (org.apache.cassandra.db.commitlog.CommitLogPosition)7 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)2 StartupException (org.apache.cassandra.exceptions.StartupException)2 OpOrder (org.apache.cassandra.utils.concurrent.OpOrder)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Lock (java.util.concurrent.locks.Lock)1 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)1 WriteTimeoutException (org.apache.cassandra.exceptions.WriteTimeoutException)1 UpdateTransaction (org.apache.cassandra.index.transactions.UpdateTransaction)1 TableId (org.apache.cassandra.schema.TableId)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1 AsyncPromise (org.apache.cassandra.utils.concurrent.AsyncPromise)1 UncheckedInterruptedException (org.apache.cassandra.utils.concurrent.UncheckedInterruptedException)1 MemtableAllocator (org.apache.cassandra.utils.memory.MemtableAllocator)1