Search in sources :

Example 1 with FileInput

use of org.apache.ignite.internal.processors.cache.persistence.wal.FileInput in project ignite by apache.

the class RecordV1Serializer method readWithCrc.

/**
 * Reads record from file {@code in0} and validates CRC of record.
 *
 * @param in0 File input.
 * @param expPtr Expected WAL pointer for record. Used to validate actual position against expected from the file.
 * @param reader Record reader I/O interface.
 * @return WAL record.
 * @throws EOFException In case of end of file.
 * @throws IgniteCheckedException If it's unable to read record.
 */
static WALRecord readWithCrc(FileInput in0, WALPointer expPtr, RecordIO reader) throws EOFException, IgniteCheckedException {
    long startPos = -1;
    try (FileInput.Crc32CheckingFileInput in = in0.startRead(skipCrc)) {
        startPos = in0.position();
        WALRecord res = reader.readWithHeaders(in, expPtr);
        assert res != null;
        // Account for CRC which will be read afterwards.
        res.size((int) (in0.position() - startPos + CRC_SIZE));
        return res;
    } catch (EOFException | SegmentEofException | WalSegmentTailReachedException e) {
        throw e;
    } catch (Exception e) {
        throw new IgniteCheckedException("Failed to read WAL record at position: " + startPos, e);
    }
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) WalSegmentTailReachedException(org.apache.ignite.internal.processors.cache.persistence.wal.WalSegmentTailReachedException) EOFException(java.io.EOFException) FileInput(org.apache.ignite.internal.processors.cache.persistence.wal.FileInput) SegmentEofException(org.apache.ignite.internal.processors.cache.persistence.wal.SegmentEofException) SegmentEofException(org.apache.ignite.internal.processors.cache.persistence.wal.SegmentEofException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) WalSegmentTailReachedException(org.apache.ignite.internal.processors.cache.persistence.wal.WalSegmentTailReachedException) EOFException(java.io.EOFException)

Example 2 with FileInput

use of org.apache.ignite.internal.processors.cache.persistence.wal.FileInput in project ignite by apache.

the class StandaloneWalRecordsIterator method scanIndexesFromFileHeaders.

/**
 * This methods checks all provided files to be correct WAL segment.
 * Header record and its position is checked. WAL position is used to determine real index.
 * File index from file name is ignored.
 *
 * @param allFiles files to scan.
 * @return list of file descriptors with checked header records, having correct file index is set
 */
private List<FileWriteAheadLogManager.FileDescriptor> scanIndexesFromFileHeaders(@Nullable final File[] allFiles) {
    if (allFiles == null || allFiles.length == 0)
        return Collections.emptyList();
    final List<FileWriteAheadLogManager.FileDescriptor> resultingDescs = new ArrayList<>();
    for (File file : allFiles) {
        if (file.length() < HEADER_RECORD_SIZE)
            // filter out this segment as it is too short
            continue;
        FileWALPointer ptr;
        try (FileIO fileIO = ioFactory.create(file);
            ByteBufferExpander buf = new ByteBufferExpander(HEADER_RECORD_SIZE, ByteOrder.nativeOrder())) {
            final DataInput in = new FileInput(fileIO, buf);
            // Header record must be agnostic to the serializer version.
            final int type = in.readUnsignedByte();
            if (type == WALRecord.RecordType.STOP_ITERATION_RECORD_TYPE) {
                if (log.isInfoEnabled())
                    log.info("Reached logical end of the segment for file " + file);
                // filter out this segment
                continue;
            }
            ptr = RecordV1Serializer.readPosition(in);
        } catch (IOException e) {
            U.warn(log, "Failed to scan index from file [" + file + "]. Skipping this file during iteration", e);
            // filter out this segment
            continue;
        }
        resultingDescs.add(new FileWriteAheadLogManager.FileDescriptor(file, ptr.index()));
    }
    Collections.sort(resultingDescs);
    return resultingDescs;
}
Also used : FileWALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.FileWALPointer) ArrayList(java.util.ArrayList) FileInput(org.apache.ignite.internal.processors.cache.persistence.wal.FileInput) IOException(java.io.IOException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) ByteBufferExpander(org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferExpander) DataInput(java.io.DataInput) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) File(java.io.File)

Example 3 with FileInput

use of org.apache.ignite.internal.processors.cache.persistence.wal.FileInput in project ignite by apache.

the class IgniteDataIntegrityTests method setUp.

/**
 * {@inheritDoc}
 */
@Override
protected void setUp() throws Exception {
    super.setUp();
    File file = File.createTempFile("integrity", "dat");
    file.deleteOnExit();
    expBuf = new ByteBufferExpander(1024, ByteOrder.BIG_ENDIAN);
    FileIOFactory factory = new RandomAccessFileIOFactory();
    fileInput = new FileInput(factory.create(file), expBuf);
    ByteBuffer buf = ByteBuffer.allocate(1024);
    ThreadLocalRandom curr = ThreadLocalRandom.current();
    for (int i = 0; i < 1024; i += 16) {
        buf.putInt(curr.nextInt());
        buf.putInt(curr.nextInt());
        buf.putInt(curr.nextInt());
        buf.position(i);
        buf.putInt(PureJavaCrc32.calcCrc32(buf, 12));
    }
    buf.rewind();
    fileInput.io().write(buf);
    fileInput.io().force();
}
Also used : ByteBufferExpander(org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferExpander) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) FileInput(org.apache.ignite.internal.processors.cache.persistence.wal.FileInput) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)

Aggregations

FileInput (org.apache.ignite.internal.processors.cache.persistence.wal.FileInput)3 File (java.io.File)2 IOException (java.io.IOException)2 ByteBufferExpander (org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferExpander)2 DataInput (java.io.DataInput)1 EOFException (java.io.EOFException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 WALRecord (org.apache.ignite.internal.pagemem.wal.record.WALRecord)1 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)1 FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)1 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)1 FileWALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.FileWALPointer)1 FileWriteAheadLogManager (org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager)1 SegmentEofException (org.apache.ignite.internal.processors.cache.persistence.wal.SegmentEofException)1 WalSegmentTailReachedException (org.apache.ignite.internal.processors.cache.persistence.wal.WalSegmentTailReachedException)1