Search in sources :

Example 1 with SegmentIO

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

the class FileWriteAheadLogManager method readFileDescriptor.

/**
 * @param file File to read.
 * @param ioFactory IO factory.
 */
@Nullable
private FileDescriptor readFileDescriptor(File file, FileIOFactory ioFactory) {
    FileDescriptor ds = new FileDescriptor(file);
    try (SegmentIO fileIO = ds.toReadOnlyIO(ioFactory)) {
        // File may be empty when LOG_ONLY mode is enabled and mmap is disabled.
        if (fileIO.size() == 0)
            return null;
        try (ByteBufferExpander buf = new ByteBufferExpander(HEADER_RECORD_SIZE, ByteOrder.nativeOrder())) {
            final DataInput in = segmentFileInputFactory.createFileInput(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);
                return null;
            }
            WALPointer ptr = readPosition(in);
            return new FileDescriptor(file, ptr.index());
        }
    } catch (IOException e) {
        U.warn(log, "Failed to read file header [" + file + "]. Skipping this file", e);
        return null;
    }
}
Also used : DataInput(java.io.DataInput) SegmentIO(org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with SegmentIO

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

the class IgniteWalIteratorFactory method readFileDescriptor.

/**
 * @param file File to read.
 * @param ioFactory IO factory.
 */
private FileDescriptor readFileDescriptor(File file, FileIOFactory ioFactory) {
    FileDescriptor ds = new FileDescriptor(file);
    try (SegmentIO fileIO = ds.toReadOnlyIO(ioFactory);
        ByteBufferExpander buf = new ByteBufferExpander(HEADER_RECORD_SIZE, ByteOrder.nativeOrder())) {
        final DataInput in = segmentFileInputFactory.createFileInput(fileIO, buf);
        // Header record must be agnostic to the serializer version.
        final int type = in.readUnsignedByte();
        if (type == RecordType.STOP_ITERATION_RECORD_TYPE) {
            if (log.isInfoEnabled())
                log.info("Reached logical end of the segment for file " + file);
            return null;
        }
        WALPointer ptr = readPosition(in);
        return new FileDescriptor(file, ptr.index());
    } catch (IOException e) {
        U.warn(log, "Failed to scan index from file [" + file + "]. Skipping this file during iteration", e);
        return null;
    }
}
Also used : ByteBufferExpander(org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferExpander) DataInput(java.io.DataInput) SegmentIO(org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO) IOException(java.io.IOException) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) FileDescriptor(org.apache.ignite.internal.processors.cache.persistence.wal.FileDescriptor)

Example 3 with SegmentIO

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

the class FileWriteAheadLogManager method initNextWriteHandle.

/**
 * Fills the file header for a new segment. Calling this method signals we are done with the segment and it can be
 * archived. If we don't have prepared file yet and achiever is busy this method blocks.
 *
 * @param cur Current file write handle released by WAL writer.
 * @return Initialized file handle.
 * @throws IgniteCheckedException If exception occurred.
 */
private FileWriteHandle initNextWriteHandle(FileWriteHandle cur) throws IgniteCheckedException {
    IgniteCheckedException error = null;
    try {
        File nextFile = pollNextFile(cur.getSegmentId());
        if (log.isDebugEnabled())
            log.debug("Switching to a new WAL segment: " + nextFile.getAbsolutePath());
        SegmentIO fileIO = null;
        FileWriteHandle hnd;
        boolean interrupted = false;
        if (switchSegmentRecordOffset != null)
            switchSegmentRecordOffset.set((int) ((cur.getSegmentId() + 1) % dsCfg.getWalSegments()), 0);
        while (true) {
            try {
                fileIO = new SegmentIO(cur.getSegmentId() + 1, ioFactory.create(nextFile));
                IgniteInClosure<FileIO> lsnr = createWalFileListener;
                if (lsnr != null)
                    lsnr.apply(fileIO);
                hnd = fileHandleManager.nextHandle(fileIO, serializer);
                if (interrupted)
                    Thread.currentThread().interrupt();
                break;
            } catch (ClosedByInterruptException ignore) {
                interrupted = true;
                Thread.interrupted();
                if (fileIO != null) {
                    try {
                        fileIO.close();
                    } catch (IOException ignored) {
                    // No-op.
                    }
                    fileIO = null;
                }
            }
        }
        hnd.writeHeader();
        return hnd;
    } catch (IgniteCheckedException e) {
        throw error = e;
    } catch (IOException e) {
        throw error = new StorageException("Unable to initialize WAL segment", e);
    } finally {
        if (error != null)
            cctx.kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, error));
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SegmentIO(org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) FailureContext(org.apache.ignite.failure.FailureContext) IOException(java.io.IOException) FileWriteHandle(org.apache.ignite.internal.processors.cache.persistence.wal.filehandle.FileWriteHandle) File(java.io.File) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO)

Example 4 with SegmentIO

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

the class FileWriteAheadLogManager method restoreWriteHandle.

/**
 * @param lastReadPtr Last read WAL file pointer.
 * @return Initialized file write handle.
 * @throws StorageException If failed to initialize WAL write handle.
 */
private FileWriteHandle restoreWriteHandle(@Nullable WALPointer lastReadPtr) throws StorageException {
    long absIdx = lastReadPtr == null ? 0 : lastReadPtr.index();
    @Nullable FileArchiver archiver0 = archiver;
    long segNo = archiver0 == null ? absIdx : absIdx % dsCfg.getWalSegments();
    File curFile = new File(walWorkDir, fileName(segNo));
    int off = lastReadPtr == null ? 0 : lastReadPtr.fileOffset();
    int len = lastReadPtr == null ? 0 : lastReadPtr.length();
    try {
        SegmentIO fileIO = new SegmentIO(absIdx, ioFactory.create(curFile));
        IgniteInClosure<FileIO> lsnr = createWalFileListener;
        if (lsnr != null)
            lsnr.apply(fileIO);
        try {
            int serVer = serializerVer;
            // If we have existing segment, try to read version from it.
            if (lastReadPtr != null) {
                try {
                    serVer = readSegmentHeader(fileIO, segmentFileInputFactory).getSerializerVersion();
                } catch (SegmentEofException | EOFException ignore) {
                    serVer = serializerVer;
                }
            }
            RecordSerializer ser = new RecordSerializerFactoryImpl(cctx).createSerializer(serVer);
            if (log.isInfoEnabled()) {
                log.info("Resuming logging to WAL segment [file=" + curFile.getAbsolutePath() + ", offset=" + off + ", ver=" + serVer + ']');
            }
            FileWriteHandle hnd = fileHandleManager.initHandle(fileIO, off + len, ser);
            segmentAware.curAbsWalIdx(absIdx);
            FileDescriptor[] walArchiveFiles = walArchiveFiles();
            segmentAware.minReserveIndex(F.isEmpty(walArchiveFiles) ? -1 : walArchiveFiles[0].idx - 1);
            segmentAware.lastTruncatedArchiveIdx(F.isEmpty(walArchiveFiles) ? -1 : walArchiveFiles[0].idx - 1);
            if (archiver0 == null)
                segmentAware.setLastArchivedAbsoluteIndex(absIdx - 1);
            // Getting segment sizes.
            F.asList(walArchiveDir.listFiles(WAL_SEGMENT_COMPACTED_OR_RAW_FILE_FILTER)).stream().map(FileDescriptor::new).forEach(fd -> {
                if (fd.isCompressed())
                    segmentSize.put(fd.idx(), fd.file().length());
                else
                    segmentSize.putIfAbsent(fd.idx(), fd.file().length());
            });
            // Size of the 8th segment will be set in #resumeLogging.
            if (archiver0 != null) {
                for (long i = absIdx - (absIdx % dsCfg.getWalSegments()); i < absIdx; i++) segmentSize.putIfAbsent(i, maxWalSegmentSize);
            }
            return hnd;
        } catch (IgniteCheckedException | IOException e) {
            try {
                fileIO.close();
            } catch (IOException suppressed) {
                e.addSuppressed(suppressed);
            }
            if (e instanceof StorageException)
                throw (StorageException) e;
            throw e instanceof IOException ? (IOException) e : new IOException(e);
        }
    } catch (IOException e) {
        throw new StorageException("Failed to restore WAL write handle: " + curFile.getAbsolutePath(), e);
    }
}
Also used : IOException(java.io.IOException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) SegmentIO(org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RecordSerializerFactoryImpl(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializerFactoryImpl) EOFException(java.io.EOFException) FileWriteHandle(org.apache.ignite.internal.processors.cache.persistence.wal.filehandle.FileWriteHandle) File(java.io.File) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) Nullable(org.jetbrains.annotations.Nullable) RecordSerializer(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializer)

Example 5 with SegmentIO

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

the class AbstractWalRecordsIterator method initReadHandle.

/**
 * Assumes file descriptor will be opened in this method. The caller of this method must be responsible for closing
 * opened file descriptor File descriptor will be closed ONLY in case of error occurred.
 *
 * @param desc File descriptor.
 * @param start Optional start pointer. Null means read from the beginning
 * @return Initialized file read header.
 * @throws FileNotFoundException If segment file is missing.
 * @throws IgniteCheckedException If initialized failed due to another unexpected error.
 */
protected AbstractReadFileHandle initReadHandle(@NotNull final AbstractFileDescriptor desc, @Nullable final WALPointer start) throws IgniteCheckedException, FileNotFoundException {
    SegmentIO fileIO = null;
    try {
        fileIO = desc.toReadOnlyIO(ioFactory);
        SegmentHeader segmentHeader;
        try {
            segmentHeader = readSegmentHeader(fileIO, segmentFileInputFactory);
        } catch (SegmentEofException | EOFException ignore) {
            try {
                fileIO.close();
            } catch (IOException ce) {
                throw new IgniteCheckedException(ce);
            }
            return null;
        } catch (IOException | IgniteCheckedException e) {
            U.closeWithSuppressingException(fileIO, e);
            throw e;
        }
        return initReadHandle(desc, start, fileIO, segmentHeader);
    } catch (FileNotFoundException e) {
        U.closeQuiet(fileIO);
        throw e;
    } catch (IOException e) {
        U.closeQuiet(fileIO);
        throw new IgniteCheckedException("Failed to initialize WAL segment: " + desc.file().getAbsolutePath(), e);
    }
}
Also used : SegmentIO(org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) RecordV1Serializer.readSegmentHeader(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordV1Serializer.readSegmentHeader) SegmentHeader(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.SegmentHeader) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)6 SegmentIO (org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 DataInput (java.io.DataInput)2 EOFException (java.io.EOFException)2 File (java.io.File)2 StorageException (org.apache.ignite.internal.processors.cache.persistence.StorageException)2 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)2 FileWriteHandle (org.apache.ignite.internal.processors.cache.persistence.wal.filehandle.FileWriteHandle)2 RecordV1Serializer.readSegmentHeader (org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordV1Serializer.readSegmentHeader)2 SegmentHeader (org.apache.ignite.internal.processors.cache.persistence.wal.serializer.SegmentHeader)2 Nullable (org.jetbrains.annotations.Nullable)2 FileNotFoundException (java.io.FileNotFoundException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 FailureContext (org.apache.ignite.failure.FailureContext)1 ByteBufferExpander (org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferExpander)1 FileDescriptor (org.apache.ignite.internal.processors.cache.persistence.wal.FileDescriptor)1 WALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)1 RecordSerializer (org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializer)1 RecordSerializerFactoryImpl (org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializerFactoryImpl)1