Search in sources :

Example 1 with PageWriteListener

use of org.apache.ignite.internal.pagemem.store.PageWriteListener in project ignite by apache.

the class FilePageStore method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(long pageId, ByteBuffer pageBuf, int tag, boolean calculateCrc) throws IgniteCheckedException {
    init();
    boolean interrupted = false;
    while (true) {
        FileIO fileIO = this.fileIO;
        try {
            lock.readLock().lock();
            try {
                if (tag < this.tag)
                    return;
                long off = pageOffset(pageId);
                assert (off >= 0 && off <= allocated.get()) || recover : "off=" + U.hexLong(off) + ", allocated=" + U.hexLong(allocated.get()) + ", pageId=" + U.hexLong(pageId) + ", file=" + getFileAbsolutePath();
                assert pageBuf.position() == 0;
                assert pageBuf.order() == ByteOrder.nativeOrder() : "Page buffer order " + pageBuf.order() + " should be same with " + ByteOrder.nativeOrder();
                assert PageIO.getType(pageBuf) != 0 : "Invalid state. Type is 0! pageId = " + U.hexLong(pageId);
                assert PageIO.getVersion(pageBuf) != 0 : "Invalid state. Version is 0! pageId = " + U.hexLong(pageId);
                if (calculateCrc && !skipCrc) {
                    assert PageIO.getCrc(pageBuf) == 0 : U.hexLong(pageId);
                    PageIO.setCrc(pageBuf, calcCrc32(pageBuf, getCrcSize(pageId, pageBuf)));
                }
                // Check whether crc was calculated somewhere above the stack if it is forcibly skipped.
                assert skipCrc || PageIO.getCrc(pageBuf) != 0 || calcCrc32(pageBuf, pageSize) == 0 : "CRC hasn't been calculated, crc=0";
                assert pageBuf.position() == 0 : pageBuf.position();
                for (PageWriteListener lsnr : lsnrs) {
                    lsnr.accept(pageId, pageBuf);
                    pageBuf.rewind();
                }
                fileIO.writeFully(pageBuf, off);
                PageIO.setCrc(pageBuf, 0);
                if (interrupted)
                    Thread.currentThread().interrupt();
                return;
            } finally {
                lock.readLock().unlock();
            }
        } catch (IOException e) {
            if (e instanceof ClosedChannelException) {
                try {
                    if (e instanceof ClosedByInterruptException) {
                        interrupted = true;
                        Thread.interrupted();
                    }
                    reinit(fileIO);
                    pageBuf.position(0);
                    PageIO.setCrc(pageBuf, 0);
                    continue;
                } catch (IOException e0) {
                    e0.addSuppressed(e);
                    e = e0;
                }
            }
            throw new StorageException("Failed to write page [file=" + getFileAbsolutePath() + ", pageId=" + pageId + ", tag=" + tag + "]", e);
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ClosedChannelException(java.nio.channels.ClosedChannelException) PageWriteListener(org.apache.ignite.internal.pagemem.store.PageWriteListener) IOException(java.io.IOException) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException)

Aggregations

IOException (java.io.IOException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 PageWriteListener (org.apache.ignite.internal.pagemem.store.PageWriteListener)1 StorageException (org.apache.ignite.internal.processors.cache.persistence.StorageException)1