Search in sources :

Example 11 with OpenOption

use of java.nio.file.OpenOption in project cryptofs by cryptomator.

the class CryptoFileSystemProviderTest method testNewAsyncFileChannelReturnsAsyncDelegatingFileChannelWithNewFileChannelAndExecutor.

@Test
@SuppressWarnings("deprecation")
public void testNewAsyncFileChannelReturnsAsyncDelegatingFileChannelWithNewFileChannelAndExecutor() throws IOException {
    @SuppressWarnings("unchecked") Set<OpenOption> options = mock(Set.class);
    ExecutorService executor = mock(ExecutorService.class);
    FileChannel channel = mock(FileChannel.class);
    when(cryptoFileSystem.newFileChannel(cryptoPath, options)).thenReturn(channel);
    AsynchronousFileChannel result = inTest.newAsynchronousFileChannel(cryptoPath, options, executor);
    assertThat(result, is(instanceOf(AsyncDelegatingFileChannel.class)));
    AsyncDelegatingFileChannel asyncDelegatingFileChannel = (AsyncDelegatingFileChannel) result;
    assertThat(asyncDelegatingFileChannel.getChannel(), is(channel));
    assertThat(asyncDelegatingFileChannel.getExecutor(), is(executor));
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) OpenOption(java.nio.file.OpenOption) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) FileChannel(java.nio.channels.FileChannel) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 12 with OpenOption

use of java.nio.file.OpenOption in project ignite by apache.

the class GridIoManagerFileTransmissionSelfTest method testFileHandlerReconnectOnReadFail.

/**
 * @throws Exception If fails.
 */
@Test(expected = IgniteCheckedException.class)
public void testFileHandlerReconnectOnReadFail() throws Exception {
    final String chunkDownloadExMsg = "Test exception. Chunk processing error.";
    snd = startGrid(0);
    rcv = startGrid(1);
    File fileToSend = createFileRandomData("testFile", 5 * 1024 * 1024);
    final AtomicInteger readChunks = new AtomicInteger();
    transmissionFileIoFactory(rcv, new FileIOFactory() {

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

                @Override
                public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
                    // Read 4 chunks than throw an exception to emulate error processing.
                    if (readChunks.incrementAndGet() == 4)
                        throw new IgniteException(chunkDownloadExMsg);
                    return super.transferFrom(src, position, count);
                }
            };
        }
    });
    rcv.context().io().addTransmissionHandler(topic, new DefaultTransmissionHandler(rcv, fileToSend, tempStore) {

        @Override
        public void onException(UUID nodeId, Throwable err) {
            assertEquals(chunkDownloadExMsg, err.getMessage());
        }
    });
    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) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileIODecorator(org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator) 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) 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 13 with OpenOption

use of java.nio.file.OpenOption in project ignite by apache.

the class CorruptedTreeFailureHandlingTest method testCorruptedPage.

/**
 * Check that if a corrupted page exists, an {@link CorruptedTreeException}
 * will be thrown and a diagnostic file will be generated.
 *
 * @throws Exception If failed.
 */
@Test
public void testCorruptedPage() throws Exception {
    IgniteEx srv = startGrid(0);
    File diagnosticDir = new File(srv.context().config().getWorkDirectory(), "diagnostic");
    FileUtils.deleteDirectory(diagnosticDir);
    srv.cluster().state(ClusterState.ACTIVE);
    IgniteCache<Integer, Integer> cache = srv.getOrCreateCache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 10; i++) cache.put(i, i);
    int pageSize = srv.configuration().getDataStorageConfiguration().getPageSize();
    int grpId = srv.context().cache().cacheGroups().stream().filter(context -> context.cacheOrGroupName().equals(DEFAULT_CACHE_NAME)).findAny().orElseThrow(() -> new RuntimeException("Cache group not found")).groupId();
    stopGrid(0, false);
    // Node is stopped, we're ready to corrupt partition data.
    long link = linkRef.get();
    long pageId = PageIdUtils.pageId(link);
    int itemId = PageIdUtils.itemId(link);
    ByteBuffer pageBuf = ByteBuffer.allocateDirect(pageSize);
    OpenOption[] options = { StandardOpenOption.READ, StandardOpenOption.WRITE };
    try (RandomAccessFileIO fileIO = new RandomAccessFileIO(fileRef.get(), options)) {
        DataPageIO dataPageIO = DataPageIO.VERSIONS.latest();
        long pageOff = pageSize + PageIdUtils.pageIndex(pageId) * pageSize;
        // Read index page.
        fileIO.position(pageOff);
        fileIO.readFully(pageBuf);
        long pageAddr = GridUnsafe.bufferAddress(pageBuf);
        // Remove existing item from index page.
        dataPageIO.removeRow(pageAddr, itemId, pageSize);
        // Recalculate CRC.
        PageIO.setCrc(pageAddr, 0);
        pageBuf.rewind();
        PageIO.setCrc(pageAddr, FastCrc.calcCrc(pageBuf, pageSize));
        // Write it back.
        pageBuf.rewind();
        fileIO.position(pageOff);
        fileIO.writeFully(pageBuf);
    }
    LogListener logLsnr = LogListener.matches("CorruptedTreeException has occurred. " + "To diagnose it, make a backup of the following directories: ").build();
    srv = startGrid(0, cfg -> {
        cfg.setGridLogger(new ListeningTestLogger(cfg.getGridLogger(), logLsnr));
    });
    // Add modified page to WAL so it won't be restored to previous (valid) state.
    pageBuf.rewind();
    ByteBuffer cpBuf = ByteBuffer.allocate(pageBuf.capacity());
    cpBuf.put(pageBuf);
    PageSnapshot pageSnapshot = new PageSnapshot(new FullPageId(pageId, grpId), cpBuf.array(), pageSize);
    srv.context().cache().context().wal().log(pageSnapshot);
    // Access cache.
    cache = srv.cache(DEFAULT_CACHE_NAME);
    try {
        for (int i = 0; i < CACHE_ENTRIES; i++) cache.get(i);
        fail("Cache operations are expected to fail");
    } catch (Throwable e) {
        assertTrue(X.hasCause(e, CorruptedTreeException.class));
    }
    assertTrue(GridTestUtils.waitForCondition(() -> G.allGrids().isEmpty(), 10_000L));
    assertTrue(diagnosticDir.exists());
    assertTrue(diagnosticDir.isDirectory());
    Pattern corruptedPagesFileNamePtrn = corruptedPagesFileNamePattern();
    File[] txtFiles = diagnosticDir.listFiles((dir, name) -> corruptedPagesFileNamePtrn.matcher(name).matches());
    assertFalse(F.isEmpty(txtFiles));
    assertEquals(1, txtFiles.length);
    assertTrue(logLsnr.check());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) LogListener(org.apache.ignite.testframework.LogListener) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) RandomAccessFileIO(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIO) ClusterState(org.apache.ignite.cluster.ClusterState) IgniteEx(org.apache.ignite.internal.IgniteEx) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) MvccDataLeafIO(org.apache.ignite.internal.processors.cache.tree.mvcc.data.MvccDataLeafIO) X(org.apache.ignite.internal.util.typedef.X) After(org.junit.After) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) FileIODecorator(org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator) AbstractDataLeafIO(org.apache.ignite.internal.processors.cache.tree.AbstractDataLeafIO) Before(org.junit.Before) G(org.apache.ignite.internal.util.typedef.G) F(org.apache.ignite.internal.util.typedef.F) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) GridUnsafe(org.apache.ignite.internal.util.GridUnsafe) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FailureHandler(org.apache.ignite.failure.FailureHandler) Test(org.junit.Test) File(java.io.File) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) StopNodeFailureHandler(org.apache.ignite.failure.StopNodeFailureHandler) DataLeafIO(org.apache.ignite.internal.processors.cache.tree.DataLeafIO) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) FastCrc(org.apache.ignite.internal.processors.cache.persistence.wal.crc.FastCrc) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) PageIdUtils(org.apache.ignite.internal.pagemem.PageIdUtils) DataPageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPageIO) Pattern(java.util.regex.Pattern) PageSnapshot(org.apache.ignite.internal.pagemem.wal.record.PageSnapshot) DataPageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPageIO) Pattern(java.util.regex.Pattern) LogListener(org.apache.ignite.testframework.LogListener) RandomAccessFileIO(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIO) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) ByteBuffer(java.nio.ByteBuffer) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) IgniteEx(org.apache.ignite.internal.IgniteEx) File(java.io.File) PageSnapshot(org.apache.ignite.internal.pagemem.wal.record.PageSnapshot) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 14 with OpenOption

use of java.nio.file.OpenOption in project ignite by apache.

the class IgniteSnapshotManagerSelfTest method testSnapshotLocalPartitionNotEnoughSpace.

/**
 * @throws Exception If fails.
 */
@Test
public void testSnapshotLocalPartitionNotEnoughSpace() throws Exception {
    String err_msg = "Test exception. Not enough space.";
    AtomicInteger throwCntr = new AtomicInteger();
    RandomAccessFileIOFactory ioFactory = new RandomAccessFileIOFactory();
    IgniteEx ig = startGridWithCache(dfltCacheCfg.setAffinity(new ZeroPartitionAffinityFunction()), CACHE_KEYS_RANGE);
    // Change data after backup.
    for (int i = 0; i < CACHE_KEYS_RANGE; i++) ig.cache(DEFAULT_CACHE_NAME).put(i, 2 * i);
    GridCacheSharedContext<?, ?> cctx0 = ig.context().cache().context();
    IgniteSnapshotManager mgr = snp(ig);
    mgr.ioFactory(new FileIOFactory() {

        @Override
        public FileIO create(File file, OpenOption... modes) throws IOException {
            FileIO fileIo = ioFactory.create(file, modes);
            if (file.getName().equals(IgniteSnapshotManager.partDeltaFileName(0)))
                return new FileIODecorator(fileIo) {

                    @Override
                    public int writeFully(ByteBuffer srcBuf) throws IOException {
                        if (throwCntr.incrementAndGet() == 3)
                            throw new IOException(err_msg);
                        return super.writeFully(srcBuf);
                    }
                };
            return fileIo;
        }
    });
    IgniteInternalFuture<?> snpFut = startLocalSnapshotTask(cctx0, SNAPSHOT_NAME, F.asMap(CU.cacheId(DEFAULT_CACHE_NAME), null), encryption, mgr.localSnapshotSenderFactory().apply(SNAPSHOT_NAME));
    // Check the right exception thrown.
    assertThrowsAnyCause(log, snpFut::get, IOException.class, err_msg);
}
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) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO) OpenOption(java.nio.file.OpenOption) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) File(java.io.File) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) Test(org.junit.Test)

Example 15 with OpenOption

use of java.nio.file.OpenOption in project cloudsim-plus by manoelcampos.

the class UtilizationModelPlanetLabTest method createTempTraceFile.

private String createTempTraceFile(final int numLines, boolean includeHeaderWithLinesNumber) {
    final OpenOption[] options = { StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING };
    final String dir = ResourceLoader.getResourcePath(getClass(), "./");
    final var path = Paths.get(dir, TEMP_TRACE);
    try (BufferedWriter writer = Files.newBufferedWriter(path, options)) {
        if (includeHeaderWithLinesNumber) {
            writer.write("#" + numLines + System.lineSeparator());
        }
        for (int i = 0; i < numLines; i++) {
            writer.write(i + System.lineSeparator());
        }
        return path.toString();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

OpenOption (java.nio.file.OpenOption)104 StandardOpenOption (java.nio.file.StandardOpenOption)77 IOException (java.io.IOException)41 Path (java.nio.file.Path)32 HashSet (java.util.HashSet)32 File (java.io.File)19 Test (org.junit.Test)19 FileChannel (java.nio.channels.FileChannel)18 NoSuchFileException (java.nio.file.NoSuchFileException)16 OutputStream (java.io.OutputStream)15 ArrayList (java.util.ArrayList)11 InputStream (java.io.InputStream)10 SeekableByteChannel (java.nio.channels.SeekableByteChannel)10 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)10 FileIODecorator (org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator)10 ByteBuffer (java.nio.ByteBuffer)9 Set (java.util.Set)8 FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)8 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7