Search in sources :

Example 6 with FileIODecorator

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

the class GridIoManagerFileTransmissionSelfTest method testChunkHandlerWithReconnect.

/**
 * @throws Exception If fails.
 */
@Test
public void testChunkHandlerWithReconnect() throws Exception {
    snd = startGrid(0);
    rcv = startGrid(1);
    final String filePrefix = "testFile";
    final AtomicInteger cnt = new AtomicInteger();
    final AtomicInteger acceptedChunks = new AtomicInteger();
    final File file = new File(tempStore, filePrefix + "_" + rcv.localNode().id());
    File fileToSend = createFileRandomData(filePrefix, 10 * 1024 * 1024);
    transmissionFileIoFactory(snd, new FileIOFactory() {

        @Override
        public FileIO create(File file, OpenOption... modes) throws IOException {
            FileIO fileIo = IO_FACTORY.create(file, modes);
            return new FileIODecorator(fileIo) {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
                    // Send 5 chunks and close the channel.
                    if (cnt.incrementAndGet() == 10)
                        target.close();
                    return super.transferTo(position, count, target);
                }
            };
        }
    });
    rcv.context().io().addTransmissionHandler(topic, new TransmissionHandlerAdapter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void onEnd(UUID rmtNodeId) {
            U.closeQuiet(fileIo[0]);
            fileIo[0] = null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onException(UUID nodeId, Throwable err) {
            U.closeQuiet(fileIo[0]);
            fileIo[0] = null;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Consumer<ByteBuffer> chunkHandler(UUID nodeId, TransmissionMeta initMeta) {
            if (fileIo[0] == null) {
                try {
                    fileIo[0] = IO_FACTORY.create(file);
                    fileIo[0].position(initMeta.offset());
                } catch (IOException e) {
                    throw new IgniteException(e);
                }
            }
            return new Consumer<ByteBuffer>() {

                @Override
                public void accept(ByteBuffer buff) {
                    try {
                        assertTrue(buff.order() == ByteOrder.nativeOrder());
                        assertEquals(0, buff.position());
                        assertEquals(buff.limit(), buff.capacity());
                        fileIo[0].writeFully(buff);
                        acceptedChunks.getAndIncrement();
                    } catch (Throwable e) {
                        throw new IgniteException(e);
                    }
                }
            };
        }
    });
    try (GridIoManager.TransmissionSender sender = snd.context().io().openTransmissionSender(rcv.localNode().id(), topic)) {
        sender.send(fileToSend, TransmissionPolicy.CHUNK);
    }
    assertEquals("Remote node must accept all chunks", fileToSend.length() / rcv.configuration().getDataStorageConfiguration().getPageSize(), acceptedChunks.get());
    assertEquals("Received file and sent files have not the same lengtgh", fileToSend.length(), file.length());
    assertCrcEquals(fileToSend, file);
    assertNull(fileIo[0]);
}
Also used : RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) FileIODecorator(org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) OpenOption(java.nio.file.OpenOption) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 7 with FileIODecorator

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

the class GridIoManagerFileTransmissionSelfTest method testFileHandlerCleanedUpIfSenderLeft.

/**
 * @throws Exception If fails.
 */
@Test
public void testFileHandlerCleanedUpIfSenderLeft() throws Exception {
    snd = startGrid(0);
    rcv = startGrid(1);
    File fileToSend = createFileRandomData("tempFile15Mb", 15 * 1024 * 1024);
    File downloadTo = U.resolveWorkDirectory(tempStore.getAbsolutePath(), "download", true);
    transmissionFileIoFactory(snd, new FileIOFactory() {

        @Override
        public FileIO create(File file, OpenOption... modes) throws IOException {
            FileIO fileIo = IO_FACTORY.create(file, modes);
            return new FileIODecorator(fileIo) {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
                    long transferred = super.transferTo(position, count, target);
                    stopGrid(snd.name(), true);
                    return transferred;
                }
            };
        }
    });
    rcv.context().io().addTransmissionHandler(topic, new DefaultTransmissionHandler(rcv, fileToSend, tempStore) {

        /**
         * {@inheritDoc}
         */
        @Override
        public String filePath(UUID nodeId, TransmissionMeta fileMeta) {
            return new File(downloadTo, fileMeta.name()).getAbsolutePath();
        }
    });
    Exception err = null;
    try (GridIoManager.TransmissionSender sender = snd.context().io().openTransmissionSender(rcv.localNode().id(), topic)) {
        sender.send(fileToSend, TransmissionPolicy.FILE);
    } catch (Exception e) {
        // Ignore node stopping exception.
        err = e;
    }
    assertEquals(NodeStoppingException.class, err.getClass());
    assertEquals("Incomplete resources must be cleaned up on sender left", 0, fileCount(downloadTo.toPath()));
}
Also used : RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) FileIODecorator(org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IOException(java.io.IOException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) OpenOption(java.nio.file.OpenOption) UUID(java.util.UUID) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with FileIODecorator

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

the class GridIoManagerFileTransmissionSelfTest method testFileHandlerOnReceiverLeft.

/**
 * @throws Exception If fails.
 */
@Test(expected = IgniteCheckedException.class)
public void testFileHandlerOnReceiverLeft() throws Exception {
    final int fileSizeBytes = 5 * 1024 * 1024;
    final AtomicInteger chunksCnt = new AtomicInteger();
    snd = startGrid(0);
    rcv = startGrid(1);
    File fileToSend = createFileRandomData("testFile", fileSizeBytes);
    transmissionFileIoFactory(snd, new FileIOFactory() {

        @Override
        public FileIO create(File file, OpenOption... modes) throws IOException {
            FileIO fileIo = IO_FACTORY.create(file, modes);
            // Blocking writer and stopping node FileIo.
            return new FileIODecorator(fileIo) {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
                    // Send 5 chunks than stop the rcv.
                    if (chunksCnt.incrementAndGet() == 5)
                        stopGrid(rcv.name(), true);
                    return super.transferTo(position, count, target);
                }
            };
        }
    });
    rcv.context().io().addTransmissionHandler(topic, new DefaultTransmissionHandler(rcv, fileToSend, tempStore));
    try (GridIoManager.TransmissionSender sender = snd.context().io().openTransmissionSender(rcv.localNode().id(), topic)) {
        sender.send(fileToSend, TransmissionPolicy.FILE);
    }
}
Also used : RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) FileIODecorator(org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) OpenOption(java.nio.file.OpenOption) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)8 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)8 FileIODecorator (org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator)8 Test (org.junit.Test)8 File (java.io.File)7 OpenOption (java.nio.file.OpenOption)7 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)7 FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)6 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)6 RandomAccessFile (java.io.RandomAccessFile)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 WritableByteChannel (java.nio.channels.WritableByteChannel)4 UUID (java.util.UUID)4 ByteBuffer (java.nio.ByteBuffer)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 StandardOpenOption (java.nio.file.StandardOpenOption)1