Search in sources :

Example 1 with FSReadError

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

the class CompressedChecksummedDataInput method readBuffer.

@Override
protected void readBuffer() {
    sourcePosition = filePosition;
    if (isEOF())
        return;
    metadataBuffer.clear();
    channel.read(metadataBuffer, filePosition);
    filePosition += CompressedHintsWriter.METADATA_SIZE;
    metadataBuffer.rewind();
    int uncompressedSize = metadataBuffer.getInt();
    int compressedSize = metadataBuffer.getInt();
    if (compressedBuffer == null || compressedSize > compressedBuffer.capacity()) {
        // allocate +5% to cover variability in compressed size
        int bufferSize = compressedSize + (compressedSize / 20);
        if (compressedBuffer != null) {
            BufferPool.put(compressedBuffer);
        }
        compressedBuffer = BufferPool.get(bufferSize, compressor.preferredBufferType());
    }
    compressedBuffer.clear();
    compressedBuffer.limit(compressedSize);
    channel.read(compressedBuffer, filePosition);
    compressedBuffer.rewind();
    filePosition += compressedSize;
    if (buffer.capacity() < uncompressedSize) {
        int bufferSize = uncompressedSize + (uncompressedSize / 20);
        BufferPool.put(buffer);
        buffer = BufferPool.get(bufferSize, compressor.preferredBufferType());
    }
    buffer.clear();
    buffer.limit(uncompressedSize);
    try {
        compressor.uncompress(compressedBuffer, buffer);
        buffer.flip();
    } catch (IOException e) {
        throw new FSReadError(e, getPath());
    }
}
Also used : FSReadError(org.apache.cassandra.io.FSReadError) IOException(java.io.IOException)

Example 2 with FSReadError

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

the class EncryptedChecksummedDataInput method readBuffer.

@Override
protected void readBuffer() {
    this.sourcePosition = readChannel.getCurrentPosition();
    if (isEOF())
        return;
    try {
        ByteBuffer byteBuffer = reusableBuffers.get();
        ByteBuffer decrypted = EncryptionUtils.decrypt(readChannel, byteBuffer, true, cipher);
        buffer = EncryptionUtils.uncompress(decrypted, buffer, true, compressor);
        if (decrypted.capacity() > byteBuffer.capacity())
            reusableBuffers.set(decrypted);
    } catch (IOException ioe) {
        throw new FSReadError(ioe, getPath());
    }
}
Also used : FSReadError(org.apache.cassandra.io.FSReadError) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with FSReadError

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

the class SASIIndexBuilder method build.

public void build() {
    AbstractType<?> keyValidator = cfs.metadata().partitionKeyType;
    for (Map.Entry<SSTableReader, Map<ColumnMetadata, ColumnIndex>> e : sstables.entrySet()) {
        SSTableReader sstable = e.getKey();
        Map<ColumnMetadata, ColumnIndex> indexes = e.getValue();
        try (RandomAccessReader dataFile = sstable.openDataReader()) {
            PerSSTableIndexWriter indexWriter = SASIIndex.newWriter(keyValidator, sstable.descriptor, indexes, OperationType.COMPACTION);
            long previousKeyPosition = 0;
            try (KeyIterator keys = new KeyIterator(sstable.descriptor, cfs.metadata())) {
                while (keys.hasNext()) {
                    if (isStopRequested())
                        throw new CompactionInterruptedException(getCompactionInfo());
                    final DecoratedKey key = keys.next();
                    final long keyPosition = keys.getKeyPosition();
                    indexWriter.startPartition(key, keyPosition);
                    try {
                        RowIndexEntry indexEntry = sstable.getPosition(key, SSTableReader.Operator.EQ);
                        dataFile.seek(indexEntry.position);
                        // key
                        ByteBufferUtil.readWithShortLength(dataFile);
                        try (SSTableIdentityIterator partition = SSTableIdentityIterator.create(sstable, dataFile, key)) {
                            // if the row has statics attached, it has to be indexed separately
                            if (cfs.metadata().hasStaticColumns())
                                indexWriter.nextUnfilteredCluster(partition.staticRow());
                            while (partition.hasNext()) indexWriter.nextUnfilteredCluster(partition.next());
                        }
                    } catch (IOException ex) {
                        throw new FSReadError(ex, sstable.getFilename());
                    }
                    bytesProcessed += keyPosition - previousKeyPosition;
                    previousKeyPosition = keyPosition;
                }
                completeSSTable(indexWriter, sstable, indexes.values());
            }
        }
    }
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SSTableIdentityIterator(org.apache.cassandra.io.sstable.SSTableIdentityIterator) KeyIterator(org.apache.cassandra.io.sstable.KeyIterator) CompactionInterruptedException(org.apache.cassandra.db.compaction.CompactionInterruptedException) DecoratedKey(org.apache.cassandra.db.DecoratedKey) IOException(java.io.IOException) RowIndexEntry(org.apache.cassandra.db.RowIndexEntry) PerSSTableIndexWriter(org.apache.cassandra.index.sasi.disk.PerSSTableIndexWriter) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnIndex(org.apache.cassandra.index.sasi.conf.ColumnIndex) RandomAccessReader(org.apache.cassandra.io.util.RandomAccessReader) FSReadError(org.apache.cassandra.io.FSReadError)

Example 4 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 5 with FSReadError

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

the class SSTableFlushObserverTest method testFlushObserver.

@Test
public void testFlushObserver() {
    TableMetadata cfm = TableMetadata.builder(KS_NAME, CF_NAME).addPartitionKeyColumn("id", UTF8Type.instance).addRegularColumn("first_name", UTF8Type.instance).addRegularColumn("age", Int32Type.instance).addRegularColumn("height", LongType.instance).build();
    LifecycleTransaction transaction = LifecycleTransaction.offline(OperationType.COMPACTION);
    FlushObserver observer = new FlushObserver();
    String sstableDirectory = DatabaseDescriptor.getAllDataFileLocations()[0];
    File directory = new File(sstableDirectory + File.pathSeparator + KS_NAME + File.pathSeparator + CF_NAME);
    directory.deleteOnExit();
    if (!directory.exists() && !directory.mkdirs())
        throw new FSWriteError(new IOException("failed to create tmp directory"), directory.getAbsolutePath());
    SSTableFormat.Type sstableFormat = SSTableFormat.Type.current();
    BigTableWriter writer = new BigTableWriter(new Descriptor(sstableFormat.info.getLatestVersion(), directory, KS_NAME, CF_NAME, 0, sstableFormat), 10L, 0L, null, TableMetadataRef.forOfflineTools(cfm), new MetadataCollector(cfm.comparator).sstableLevel(0), new SerializationHeader(true, cfm, cfm.regularAndStaticColumns(), EncodingStats.NO_STATS), Collections.singletonList(observer), transaction);
    SSTableReader reader = null;
    Multimap<ByteBuffer, Cell> expected = ArrayListMultimap.create();
    try {
        final long now = System.currentTimeMillis();
        ByteBuffer key = UTF8Type.instance.fromString("key1");
        expected.putAll(key, Arrays.asList(BufferCell.live(getColumn(cfm, "age"), now, Int32Type.instance.decompose(27)), BufferCell.live(getColumn(cfm, "first_name"), now, UTF8Type.instance.fromString("jack")), BufferCell.live(getColumn(cfm, "height"), now, LongType.instance.decompose(183L))));
        writer.append(new RowIterator(cfm, key.duplicate(), Collections.singletonList(buildRow(expected.get(key)))));
        key = UTF8Type.instance.fromString("key2");
        expected.putAll(key, Arrays.asList(BufferCell.live(getColumn(cfm, "age"), now, Int32Type.instance.decompose(30)), BufferCell.live(getColumn(cfm, "first_name"), now, UTF8Type.instance.fromString("jim")), BufferCell.live(getColumn(cfm, "height"), now, LongType.instance.decompose(180L))));
        writer.append(new RowIterator(cfm, key, Collections.singletonList(buildRow(expected.get(key)))));
        key = UTF8Type.instance.fromString("key3");
        expected.putAll(key, Arrays.asList(BufferCell.live(getColumn(cfm, "age"), now, Int32Type.instance.decompose(30)), BufferCell.live(getColumn(cfm, "first_name"), now, UTF8Type.instance.fromString("ken")), BufferCell.live(getColumn(cfm, "height"), now, LongType.instance.decompose(178L))));
        writer.append(new RowIterator(cfm, key, Collections.singletonList(buildRow(expected.get(key)))));
        reader = writer.finish(true);
    } finally {
        FileUtils.closeQuietly(writer);
    }
    Assert.assertTrue(observer.isComplete);
    Assert.assertEquals(expected.size(), observer.rows.size());
    for (Pair<ByteBuffer, Long> e : observer.rows.keySet()) {
        ByteBuffer key = e.left;
        Long indexPosition = e.right;
        try (FileDataInput index = reader.ifile.createReader(indexPosition)) {
            ByteBuffer indexKey = ByteBufferUtil.readWithShortLength(index);
            Assert.assertEquals(0, UTF8Type.instance.compare(key, indexKey));
        } catch (IOException ex) {
            throw new FSReadError(ex, reader.getIndexFilename());
        }
        Assert.assertEquals(expected.get(key), observer.rows.get(e));
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) FSWriteError(org.apache.cassandra.io.FSWriteError) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileDataInput(org.apache.cassandra.io.util.FileDataInput) SerializationHeader(org.apache.cassandra.db.SerializationHeader) FSReadError(org.apache.cassandra.io.FSReadError) BigTableWriter(org.apache.cassandra.io.sstable.format.big.BigTableWriter) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) MetadataCollector(org.apache.cassandra.io.sstable.metadata.MetadataCollector) File(java.io.File) Test(org.junit.Test)

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