Search in sources :

Example 6 with StorageException

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

the class FilePageStore method read.

/**
 * @param pageId Page ID.
 * @param pageBuf Page buffer to read into.
 * @param checkCrc Check CRC on page.
 * @param keepCrc By default reading zeroes CRC which was on file, but you can keep it in pageBuf if set keepCrc
 * @return {@code true} if page has been read successfully, {@code false} if page hasn't been written yet.
 * @throws IgniteCheckedException If reading failed (IO error occurred).
 */
public boolean read(long pageId, ByteBuffer pageBuf, boolean checkCrc, boolean keepCrc) throws IgniteCheckedException {
    init();
    try {
        long off = pageOffset(pageId);
        assert pageBuf.capacity() == pageSize;
        assert pageBuf.remaining() == pageSize;
        assert pageBuf.position() == 0;
        assert pageBuf.order() == ByteOrder.nativeOrder();
        assert off <= allocated.get() : "calculatedOffset=" + off + ", allocated=" + allocated.get() + ", headerSize=" + headerSize() + ", cfgFile=" + pathProvider.apply().toAbsolutePath();
        int n = readWithFailover(pageBuf, off);
        // If page was not written yet, nothing to read.
        if (n < 0) {
            pageBuf.put(new byte[pageBuf.remaining()]);
            return false;
        }
        int savedCrc32 = PageIO.getCrc(pageBuf);
        PageIO.setCrc(pageBuf, 0);
        pageBuf.position(0);
        if (checkCrc) {
            int curCrc32 = FastCrc.calcCrc(pageBuf, getCrcSize(pageId, pageBuf));
            if ((savedCrc32 ^ curCrc32) != 0)
                throw new IgniteDataIntegrityViolationException("Failed to read page (CRC validation failed) " + "[id=" + U.hexLong(pageId) + ", off=" + (off - pageSize) + ", file=" + getFileAbsolutePath() + ", fileSize=" + fileIO.size() + ", savedCrc=" + U.hexInt(savedCrc32) + ", curCrc=" + U.hexInt(curCrc32) + ", page=" + U.toHexString(pageBuf) + "]");
        }
        assert PageIO.getCrc(pageBuf) == 0;
        if (keepCrc)
            PageIO.setCrc(pageBuf, savedCrc32);
        return true;
    } catch (IOException e) {
        throw new StorageException("Failed to read page [file=" + getFileAbsolutePath() + ", pageId=" + pageId + "]", e);
    }
}
Also used : IgniteDataIntegrityViolationException(org.apache.ignite.internal.processors.cache.persistence.wal.crc.IgniteDataIntegrityViolationException) IOException(java.io.IOException) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException)

Example 7 with StorageException

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

the class FilePageStore method init.

/**
 * @throws StorageException If failed to initialize store file.
 */
public void init() throws StorageException {
    if (!inited) {
        lock.writeLock().lock();
        try {
            if (!inited) {
                FileIO fileIO = null;
                StorageException err = null;
                long newSize;
                try {
                    boolean interrupted = false;
                    while (true) {
                        try {
                            File cfgFile = pathProvider.apply().toFile();
                            this.fileIO = fileIO = ioFactory.create(cfgFile, CREATE, READ, WRITE);
                            fileExists = true;
                            newSize = (cfgFile.length() == 0 ? initFile(fileIO) : checkFile(fileIO, cfgFile)) - headerSize();
                            if (interrupted)
                                Thread.currentThread().interrupt();
                            break;
                        } catch (ClosedByInterruptException e) {
                            interrupted = true;
                            Thread.interrupted();
                        }
                    }
                    assert allocated.get() == 0;
                    allocated.set(newSize);
                    inited = true;
                    // Order is important, update of total allocated pages must be called after allocated update
                    // and setting inited to true, because it affects pages() returned value.
                    allocatedTracker.accept(pages());
                } catch (IOException e) {
                    err = new StorageException("Failed to initialize partition file: " + getFileAbsolutePath(), e);
                    throw err;
                } finally {
                    if (err != null && fileIO != null)
                        try {
                            fileIO.close();
                        } catch (IOException e) {
                            err.addSuppressed(e);
                        }
                }
            }
        } finally {
            lock.writeLock().unlock();
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) IOException(java.io.IOException) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) File(java.io.File)

Example 8 with StorageException

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

the class CheckpointMarkersStorage method removeCheckpointFiles.

/**
 * Removes checkpoint start/end files belongs to given {@code cpEntry}.
 *
 * @param cpEntry Checkpoint entry.
 * @throws IgniteCheckedException If failed to delete.
 */
private void removeCheckpointFiles(CheckpointEntry cpEntry) throws IgniteCheckedException {
    Path startFile = new File(cpDir.getAbsolutePath(), checkpointFileName(cpEntry, CheckpointEntryType.START)).toPath();
    Path endFile = new File(cpDir.getAbsolutePath(), checkpointFileName(cpEntry, CheckpointEntryType.END)).toPath();
    try {
        if (Files.exists(startFile))
            Files.delete(startFile);
        if (Files.exists(endFile))
            Files.delete(endFile);
    } catch (IOException e) {
        throw new StorageException("Failed to delete stale checkpoint files: " + cpEntry, e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) File(java.io.File) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException)

Example 9 with StorageException

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

the class FileWriteAheadLogManager method renameLastSegment.

/**
 * Renaming last segment if it is only one and its index is greater than {@link DataStorageConfiguration#getWalSegments()}.
 *
 * @throws StorageException If an error occurs while renaming.
 */
private void renameLastSegment() throws StorageException {
    assert isArchiverEnabled();
    FileDescriptor[] workSegments = scan(walWorkDir.listFiles(WAL_SEGMENT_FILE_FILTER));
    if (workSegments.length == 1 && workSegments[0].idx() != workSegments[0].idx() % dsCfg.getWalSegments()) {
        FileDescriptor toRen = workSegments[0];
        if (log.isInfoEnabled()) {
            log.info("Last WAL segment file has to be renamed from " + toRen.file().getName() + " to " + fileName(toRen.idx() % dsCfg.getWalSegments()) + '.');
        }
        String toRenFileName = fileName(toRen.idx() % dsCfg.getWalSegments());
        File tmpDst = new File(walWorkDir, toRenFileName + TMP_SUFFIX);
        File dst = new File(walWorkDir, toRenFileName);
        try {
            Files.copy(toRen.file().toPath(), tmpDst.toPath());
            Files.move(tmpDst.toPath(), dst.toPath());
            Files.delete(toRen.file().toPath());
            if (log.isInfoEnabled()) {
                log.info("WAL segment renamed [src=" + toRen.file().getAbsolutePath() + ", dst=" + dst.getAbsolutePath() + ']');
            }
        } catch (IOException e) {
            throw new StorageException("Failed to rename WAL segment [src=" + toRen.file().getAbsolutePath() + ", dst=" + dst.getAbsolutePath() + ']', e);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException)

Example 10 with StorageException

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

the class FileWriteAheadLogManager method prepareAndCheckWalFiles.

/**
 * Prepare and check WAL files.
 *
 * @throws StorageException If failed.
 */
private void prepareAndCheckWalFiles() throws StorageException {
    Collection<File> tmpFiles = new HashSet<>();
    for (File walDir : F.asList(walWorkDir, walArchiveDir)) {
        tmpFiles.addAll(F.asList(walDir.listFiles(WAL_SEGMENT_TEMP_FILE_FILTER)));
        tmpFiles.addAll(F.asList(walDir.listFiles(WAL_SEGMENT_TEMP_FILE_COMPACTED_FILTER)));
    }
    for (File tmpFile : tmpFiles) {
        if (tmpFile.exists() && !tmpFile.delete()) {
            throw new StorageException("Failed to delete previously created temp file " + "(make sure Ignite process has enough rights): " + tmpFile.getAbsolutePath());
        }
    }
    if (F.isEmpty(walWorkDir.listFiles(WAL_SEGMENT_FILE_FILTER)))
        createFile(new File(walWorkDir, fileName(0)));
    if (isArchiverEnabled()) {
        moveSegmentsToArchive();
        renameLastSegment();
        formatWorkSegments();
        checkFiles(0, false, null, null);
    }
}
Also used : File(java.io.File) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) HashSet(java.util.HashSet)

Aggregations

StorageException (org.apache.ignite.internal.processors.cache.persistence.StorageException)26 IOException (java.io.IOException)21 File (java.io.File)9 FailureContext (org.apache.ignite.failure.FailureContext)6 ByteBuffer (java.nio.ByteBuffer)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 WALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)5 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)4 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)4 RecordSerializer (org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializer)4 RecordSerializerFactoryImpl (org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordSerializerFactoryImpl)4 ArrayList (java.util.ArrayList)3 SwitchSegmentRecord (org.apache.ignite.internal.pagemem.wal.record.SwitchSegmentRecord)3 EOFException (java.io.EOFException)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 PageStore (org.apache.ignite.internal.pagemem.store.PageStore)2 FileWriteHandle (org.apache.ignite.internal.processors.cache.persistence.wal.filehandle.FileWriteHandle)2 SegmentIO (org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO)2 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)2