Search in sources :

Example 6 with FSReadError

use of org.apache.cassandra.io.FSReadError in project cassandra by apache.

the class CompressionMetadata method readChunkOffsets.

/**
     * Read offsets of the individual chunks from the given input.
     *
     * @param input Source of the data.
     *
     * @return collection of the chunk offsets.
     */
private Memory readChunkOffsets(DataInput input) {
    final int chunkCount;
    try {
        chunkCount = input.readInt();
        if (chunkCount <= 0)
            throw new IOException("Compressed file with 0 chunks encountered: " + input);
    } catch (IOException e) {
        throw new FSReadError(e, indexFilePath);
    }
    @SuppressWarnings("resource") Memory offsets = Memory.allocate(chunkCount * 8L);
    int i = 0;
    try {
        for (i = 0; i < chunkCount; i++) {
            offsets.setLong(i * 8L, input.readLong());
        }
        return offsets;
    } catch (IOException e) {
        if (offsets != null)
            offsets.close();
        if (e instanceof EOFException) {
            String msg = String.format("Corrupted Index File %s: read %d but expected %d chunks.", indexFilePath, i, chunkCount);
            throw new CorruptSSTableException(new IOException(msg, e), indexFilePath);
        }
        throw new FSReadError(e, indexFilePath);
    }
}
Also used : Memory(org.apache.cassandra.io.util.Memory) SafeMemory(org.apache.cassandra.io.util.SafeMemory) FSReadError(org.apache.cassandra.io.FSReadError) EOFException(java.io.EOFException) IOException(java.io.IOException) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException)

Example 7 with FSReadError

use of org.apache.cassandra.io.FSReadError in project cassandra by apache.

the class CompressedSequentialWriter method resetAndTruncate.

@Override
public synchronized void resetAndTruncate(DataPosition mark) {
    assert mark instanceof CompressedFileWriterMark;
    CompressedFileWriterMark realMark = (CompressedFileWriterMark) mark;
    // reset position
    long truncateTarget = realMark.uncDataOffset;
    if (realMark.chunkOffset == chunkOffset) {
        // simply drop bytes to the right of our mark
        buffer.position(realMark.validBufferBytes);
        return;
    }
    // synchronize current buffer with disk - we don't want any data loss
    syncInternal();
    chunkOffset = realMark.chunkOffset;
    // compressed chunk size (- 4 bytes reserved for checksum)
    int chunkSize = (int) (metadataWriter.chunkOffsetBy(realMark.nextChunkIndex) - chunkOffset - 4);
    if (compressed.capacity() < chunkSize)
        compressed = compressor.preferredBufferType().allocate(chunkSize);
    try {
        compressed.clear();
        compressed.limit(chunkSize);
        fchannel.position(chunkOffset);
        fchannel.read(compressed);
        try {
            // Repopulate buffer from compressed data
            buffer.clear();
            compressed.flip();
            if (chunkSize <= maxCompressedLength)
                compressor.uncompress(compressed, buffer);
            else
                buffer.put(compressed);
        } catch (IOException e) {
            throw new CorruptBlockException(getPath(), chunkOffset, chunkSize, e);
        }
        CRC32 checksum = new CRC32();
        compressed.rewind();
        checksum.update(compressed);
        crcCheckBuffer.clear();
        fchannel.read(crcCheckBuffer);
        crcCheckBuffer.flip();
        if (crcCheckBuffer.getInt() != (int) checksum.getValue())
            throw new CorruptBlockException(getPath(), chunkOffset, chunkSize);
    } catch (CorruptBlockException e) {
        throw new CorruptSSTableException(e, getPath());
    } catch (EOFException e) {
        throw new CorruptSSTableException(new CorruptBlockException(getPath(), chunkOffset, chunkSize), getPath());
    } catch (IOException e) {
        throw new FSReadError(e, getPath());
    }
    // Mark as dirty so we can guarantee the newly buffered bytes won't be lost on a rebuffer
    buffer.position(realMark.validBufferBytes);
    bufferOffset = truncateTarget - buffer.position();
    chunkCount = realMark.nextChunkIndex - 1;
    // truncate data and index file
    truncate(chunkOffset);
    metadataWriter.resetAndTruncate(realMark.nextChunkIndex - 1);
}
Also used : CRC32(java.util.zip.CRC32) EOFException(java.io.EOFException) FSReadError(org.apache.cassandra.io.FSReadError) IOException(java.io.IOException) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException)

Example 8 with FSReadError

use of org.apache.cassandra.io.FSReadError in project cassandra by apache.

the class SequentialWriter method resetAndTruncate.

/**
     * Drops all buffered data that's past the limits of our new file mark + buffer capacity, or syncs and truncates
     * the underlying file to the marked position
     */
public void resetAndTruncate(DataPosition mark) {
    assert mark instanceof BufferedFileWriterMark;
    long previous = current();
    long truncateTarget = ((BufferedFileWriterMark) mark).pointer;
    // the right of the desired mark.
    if (previous - truncateTarget <= buffer.position()) {
        buffer.position(buffer.position() - ((int) (previous - truncateTarget)));
        return;
    }
    // synchronize current buffer with disk - we don't want any data loss
    syncInternal();
    // truncate file to given position
    truncate(truncateTarget);
    try {
        fchannel.position(truncateTarget);
    } catch (IOException e) {
        throw new FSReadError(e, getPath());
    }
    bufferOffset = truncateTarget;
    resetBuffer();
}
Also used : FSReadError(org.apache.cassandra.io.FSReadError) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)8 FSReadError (org.apache.cassandra.io.FSReadError)8 EOFException (java.io.EOFException)2 ByteBuffer (java.nio.ByteBuffer)2 CorruptSSTableException (org.apache.cassandra.io.sstable.CorruptSSTableException)2 Test (org.junit.Test)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 SocketException (java.net.SocketException)1 CRC32 (java.util.zip.CRC32)1 Config (org.apache.cassandra.config.Config)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 DecoratedKey (org.apache.cassandra.db.DecoratedKey)1 RowIndexEntry (org.apache.cassandra.db.RowIndexEntry)1 SerializationHeader (org.apache.cassandra.db.SerializationHeader)1 CompactionInterruptedException (org.apache.cassandra.db.compaction.CompactionInterruptedException)1 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)1 ColumnIndex (org.apache.cassandra.index.sasi.conf.ColumnIndex)1 PerSSTableIndexWriter (org.apache.cassandra.index.sasi.disk.PerSSTableIndexWriter)1 FSWriteError (org.apache.cassandra.io.FSWriteError)1