Search in sources :

Example 31 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project canal by alibaba.

the class BufferedFileDataInput method seek.

public void seek(long seekBytes) throws FileNotFoundException, IOException, InterruptedException {
    fileInput = new FileInputStream(file);
    fileChannel = fileInput.getChannel();
    try {
        fileChannel.position(seekBytes);
    } catch (ClosedByInterruptException e) {
        throw new InterruptedException();
    }
    bufferedInput = new BufferedInputStream(fileInput, size);
    dataInput = new DataInputStream(bufferedInput);
    offset = seekBytes;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Example 32 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project Aeron by real-logic.

the class RecordingWriter method onBlock.

/**
 * {@inheritDoc}
 */
public void onBlock(final DirectBuffer termBuffer, final int termOffset, final int length, final int sessionId, final int termId) {
    try {
        final boolean isPaddingFrame = termBuffer.getShort(typeOffset(termOffset)) == PADDING_FRAME_TYPE;
        final int dataLength = isPaddingFrame ? HEADER_LENGTH : length;
        final ByteBuffer byteBuffer;
        if (null == checksum || isPaddingFrame) {
            byteBuffer = termBuffer.byteBuffer();
            byteBuffer.limit(termOffset + dataLength).position(termOffset);
        } else {
            checksumBuffer.putBytes(0, termBuffer, termOffset, dataLength);
            computeChecksum(checksum, checksumBuffer, dataLength);
            byteBuffer = checksumBuffer.byteBuffer();
            byteBuffer.limit(dataLength).position(0);
        }
        int fileOffset = segmentOffset;
        do {
            fileOffset += recordingFileChannel.write(byteBuffer, fileOffset);
        } while (byteBuffer.remaining() > 0);
        if (forceWrites) {
            recordingFileChannel.force(forceMetadata);
        }
        segmentOffset += length;
        if (segmentOffset >= segmentLength) {
            onFileRollOver();
        }
    } catch (final ClosedByInterruptException ex) {
        close();
        throw new ArchiveException("file closed by interrupt, recording aborted", ex, ArchiveException.GENERIC);
    } catch (final IOException ex) {
        close();
        checkErrorType(ex, length);
    } catch (final Exception ex) {
        close();
        LangUtil.rethrowUnchecked(ex);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) IOException(java.io.IOException) ArchiveException(io.aeron.archive.client.ArchiveException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ArchiveException(io.aeron.archive.client.ArchiveException)

Example 33 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException 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)

Example 34 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project ignite by apache.

the class FilePageStore method initFile.

/**
 * Initializes header and writes it into the file store.
 *
 * @return Next available position in the file to store a data.
 * @throws IOException If initialization is failed.
 */
private long initFile(FileIO fileIO) throws IOException {
    try {
        ByteBuffer hdr = header(type, pageSize);
        fileIO.writeFully(hdr);
        // there is 'super' page in every file
        return headerSize() + pageSize;
    } catch (ClosedByInterruptException e) {
        // If thread was interrupted written header can be inconsistent.
        lock.writeLock().lock();
        try {
            Files.delete(pathProvider.apply());
            fileExists = false;
        } finally {
            lock.writeLock().unlock();
        }
        throw e;
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ByteBuffer(java.nio.ByteBuffer)

Example 35 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException 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)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)79 IOException (java.io.IOException)48 ByteBuffer (java.nio.ByteBuffer)15 ClosedChannelException (java.nio.channels.ClosedChannelException)11 SocketTimeoutException (java.net.SocketTimeoutException)9 InetSocketAddress (java.net.InetSocketAddress)7 MappedByteBuffer (java.nio.MappedByteBuffer)7 SocketChannel (java.nio.channels.SocketChannel)7 File (java.io.File)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 ServerSocket (java.net.ServerSocket)5 FileChannel (java.nio.channels.FileChannel)5 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)5 InterruptedIOException (java.io.InterruptedIOException)4 Path (java.nio.file.Path)4 Test (org.junit.Test)4 BuildId (com.facebook.buck.model.BuildId)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)3