Search in sources :

Example 1 with Allocation

use of org.apache.cassandra.db.commitlog.CommitLogSegment.Allocation in project cassandra by apache.

the class CommitLog method add.

/**
 * Add a Mutation to the commit log. If CDC is enabled, this can fail.
 *
 * @param mutation the Mutation to add to the log
 * @throws CDCWriteException
 */
public CommitLogPosition add(Mutation mutation) throws CDCWriteException {
    assert mutation != null;
    mutation.validateSize(MessagingService.current_version, ENTRY_OVERHEAD_SIZE);
    try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
        Mutation.serializer.serialize(mutation, dob, MessagingService.current_version);
        int size = dob.getLength();
        int totalSize = size + ENTRY_OVERHEAD_SIZE;
        Allocation alloc = segmentManager.allocate(mutation, totalSize);
        CRC32 checksum = new CRC32();
        final ByteBuffer buffer = alloc.getBuffer();
        try (BufferedDataOutputStreamPlus dos = new DataOutputBufferFixed(buffer)) {
            // checksummed length
            dos.writeInt(size);
            updateChecksumInt(checksum, size);
            buffer.putInt((int) checksum.getValue());
            // checksummed mutation
            dos.write(dob.getData(), 0, size);
            updateChecksum(checksum, buffer, buffer.position() - size, size);
            buffer.putInt((int) checksum.getValue());
        } catch (IOException e) {
            throw new FSWriteError(e, alloc.getSegment().getPath());
        } finally {
            alloc.markWritten();
        }
        executor.finishWriteFor(alloc);
        return alloc.getCommitLogPosition();
    } catch (IOException e) {
        throw new FSWriteError(e, segmentManager.allocatingFrom().getPath());
    }
}
Also used : Allocation(org.apache.cassandra.db.commitlog.CommitLogSegment.Allocation) CRC32(java.util.zip.CRC32) FSWriteError(org.apache.cassandra.io.FSWriteError) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) IOException(java.io.IOException) BufferedDataOutputStreamPlus(org.apache.cassandra.io.util.BufferedDataOutputStreamPlus) ByteBuffer(java.nio.ByteBuffer) DataOutputBufferFixed(org.apache.cassandra.io.util.DataOutputBufferFixed)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CRC32 (java.util.zip.CRC32)1 Allocation (org.apache.cassandra.db.commitlog.CommitLogSegment.Allocation)1 FSWriteError (org.apache.cassandra.io.FSWriteError)1 BufferedDataOutputStreamPlus (org.apache.cassandra.io.util.BufferedDataOutputStreamPlus)1 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)1 DataOutputBufferFixed (org.apache.cassandra.io.util.DataOutputBufferFixed)1