Search in sources :

Example 6 with FileIOFactory

use of org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory 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 7 with FileIOFactory

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

the class IgnitePdsTaskCancelingTest method testFilePageStoreInterruptThreads.

/**
 * Test FilePageStore with multiple interrupted threads.
 */
@Test
public void testFilePageStoreInterruptThreads() throws Exception {
    failure.set(false);
    FileIOFactory factory = new RandomAccessFileIOFactory();
    File file = new File(U.defaultWorkDirectory(), "file.bin");
    file.deleteOnExit();
    DataStorageConfiguration dbCfg = getDataStorageConfiguration();
    FilePageStore pageStore = new FilePageStore(PageMemory.FLAG_DATA, file::toPath, factory, dbCfg.getPageSize(), val -> {
    });
    int pageSize = dbCfg.getPageSize();
    PageIO pageIO = PageIO.getPageIO(PageIO.T_DATA, 1);
    long ptr = GridUnsafe.allocateMemory(NUM_TASKS * pageSize);
    try {
        List<Thread> threadList = new ArrayList<>(NUM_TASKS);
        AtomicBoolean stopThreads = new AtomicBoolean(false);
        for (int i = 0; i < NUM_TASKS; i++) {
            long pageId = PageIdUtils.pageId(0, PageMemory.FLAG_DATA, (int) pageStore.allocatePage());
            long pageAdr = ptr + i * pageSize;
            pageIO.initNewPage(pageAdr, pageId, pageSize, null);
            ByteBuffer buf = GridUnsafe.wrapPointer(pageAdr, pageSize);
            pageStore.write(pageId, buf, 0, true);
            threadList.add(new Thread(new Runnable() {

                @Override
                public void run() {
                    Random random = new Random();
                    while (!stopThreads.get()) {
                        buf.position(0);
                        try {
                            if (random.nextBoolean()) {
                                log.info(">>> Read page " + U.hexLong(pageId));
                                pageStore.read(pageId, buf, false);
                            } else {
                                log.info(">>> Write page " + U.hexLong(pageId));
                                pageStore.write(pageId, buf, 0, true);
                            }
                            Thread.interrupted();
                        } catch (Exception e) {
                            log.error("Error while reading/writing page", e);
                            failure.set(true);
                        }
                    }
                }
            }));
        }
        for (Thread thread : threadList) thread.start();
        for (int i = 0; i < 10; i++) {
            for (Thread thread : threadList) {
                doSleep(10L);
                log.info("Interrupting " + thread.getName());
                thread.interrupt();
            }
        }
        stopThreads.set(true);
        for (Thread thread : threadList) thread.join();
        assertFalse(failure.get());
    } finally {
        GridUnsafe.freeMemory(ptr);
    }
}
Also used : FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) ArrayList(java.util.ArrayList) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) File(java.io.File) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with FileIOFactory

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

the class MockWalIteratorFactory method iterator.

/**
 * Creates iterator
 * @param wal WAL directory without node consistent id
 * @param walArchive WAL archive without node consistent id
 * @return iterator
 * @throws IgniteCheckedException if IO failed
 */
@SuppressWarnings("unchecked")
public WALIterator iterator(File wal, File walArchive) throws IgniteCheckedException {
    final DataStorageConfiguration persistentCfg1 = Mockito.mock(DataStorageConfiguration.class);
    when(persistentCfg1.getWalPath()).thenReturn(wal.getAbsolutePath());
    when(persistentCfg1.getWalArchivePath()).thenReturn(walArchive.getAbsolutePath());
    when(persistentCfg1.getWalSegments()).thenReturn(segments);
    when(persistentCfg1.getWalBufferSize()).thenReturn(DataStorageConfiguration.DFLT_WAL_BUFF_SIZE);
    when(persistentCfg1.getWalRecordIteratorBufferSize()).thenReturn(DataStorageConfiguration.DFLT_WAL_RECORD_ITERATOR_BUFFER_SIZE);
    when(persistentCfg1.getWalSegmentSize()).thenReturn(DataStorageConfiguration.DFLT_WAL_SEGMENT_SIZE);
    final FileIOFactory fileIOFactory = new DataStorageConfiguration().getFileIOFactory();
    when(persistentCfg1.getFileIOFactory()).thenReturn(fileIOFactory);
    final IgniteConfiguration cfg = Mockito.mock(IgniteConfiguration.class);
    when(cfg.getDataStorageConfiguration()).thenReturn(persistentCfg1);
    final GridKernalContext ctx = Mockito.mock(GridKernalContext.class);
    when(ctx.config()).thenReturn(cfg);
    when(ctx.clientNode()).thenReturn(false);
    when(ctx.pdsFolderResolver()).thenReturn(new PdsFoldersResolver() {

        @Override
        public PdsFolderSettings resolveFolders() {
            return new PdsFolderSettings(new File("."), subfolderName, consistentId, null, false);
        }
    });
    final GridDiscoveryManager disco = Mockito.mock(GridDiscoveryManager.class);
    when(ctx.discovery()).thenReturn(disco);
    final IgniteWriteAheadLogManager mgr = new FileWriteAheadLogManager(ctx);
    final GridCacheSharedContext sctx = Mockito.mock(GridCacheSharedContext.class);
    when(sctx.kernalContext()).thenReturn(ctx);
    when(sctx.discovery()).thenReturn(disco);
    when(sctx.gridConfig()).thenReturn(cfg);
    final GridCacheDatabaseSharedManager db = Mockito.mock(GridCacheDatabaseSharedManager.class);
    when(db.pageSize()).thenReturn(pageSize);
    when(sctx.database()).thenReturn(db);
    when(sctx.logger(any(Class.class))).thenReturn(log);
    mgr.start(sctx);
    return mgr.replay(null);
}
Also used : FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) GridDiscoveryManager(org.apache.ignite.internal.managers.discovery.GridDiscoveryManager) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) PdsFoldersResolver(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFoldersResolver) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) PdsFolderSettings(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderSettings) File(java.io.File)

Example 9 with FileIOFactory

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

the class WalTestUtils method corruptWalSegmentFile.

/**
 * Put zero CRC in one of records for the specified segment.
 *
 * @param desc WAL segment descriptor.
 * @param pointer WAL pointer.
 */
public static void corruptWalSegmentFile(FileDescriptor desc, WALPointer pointer) throws IOException {
    int crc32Off = pointer.fileOffset() + pointer.length() - CRC_SIZE;
    // Has 0 value by default.
    ByteBuffer zeroCrc32 = allocate(CRC_SIZE);
    FileIOFactory ioFactory = new RandomAccessFileIOFactory();
    try (FileIO io = ioFactory.create(desc.file(), WRITE)) {
        io.write(zeroCrc32, crc32Off);
        io.force(true);
    }
}
Also used : RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) ByteBuffer(java.nio.ByteBuffer) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO)

Example 10 with FileIOFactory

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

the class DefragmentationMXBeanTest method testDefragmentationCancelInProgress.

/**
 * Test that ongong defragmentation can be stopped via JMX bean.
 * Description:
 * 1. Start one node.
 * 2. Put a load of a data on it.
 * 3. Schedule defragmentation.
 * 4. Make IO factory slow down after 128 partitions are processed, so we have time to stop the defragmentation.
 * 5. Stop the defragmentation.
 * @throws Exception If failed.
 */
@Test
public void testDefragmentationCancelInProgress() throws Exception {
    IgniteEx ig = startGrid(0);
    ig.cluster().state(ClusterState.ACTIVE);
    IgniteCache<Object, Object> cache = ig.getOrCreateCache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 1024; i++) cache.put(i, i);
    forceCheckpoint(ig);
    DefragmentationMXBean mxBean = defragmentationMXBean(ig.name());
    mxBean.schedule("");
    stopGrid(0);
    blockCdl = new CountDownLatch(128);
    UnaryOperator<IgniteConfiguration> cfgOp = cfg -> {
        DataStorageConfiguration dsCfg = cfg.getDataStorageConfiguration();
        FileIOFactory delegate = dsCfg.getFileIOFactory();
        dsCfg.setFileIOFactory((file, modes) -> {
            if (file.getName().contains("dfrg")) {
                if (blockCdl.getCount() == 0) {
                    try {
                        // Slow down defragmentation process.
                        // This'll be enough for the test since we have, like, 900 partitions left.
                        Thread.sleep(100);
                    } catch (InterruptedException ignore) {
                    // No-op.
                    }
                } else
                    blockCdl.countDown();
            }
            return delegate.create(file, modes);
        });
        return cfg;
    };
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
        try {
            startGrid(0, cfgOp);
        } catch (Exception e) {
            // No-op.
            throw new RuntimeException(e);
        }
    });
    blockCdl.await();
    mxBean = defragmentationMXBean(ig.name());
    assertTrue(mxBean.cancel());
    fut.get();
    assertTrue(mxBean.cancel());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteKernal(org.apache.ignite.internal.IgniteKernal) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) IgnitionEx(org.apache.ignite.internal.IgnitionEx) DefragmentationParameters(org.apache.ignite.internal.processors.cache.persistence.defragmentation.maintenance.DefragmentationParameters) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) ClusterState(org.apache.ignite.cluster.ClusterState) UnaryOperator(java.util.function.UnaryOperator) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) MaintenanceTask(org.apache.ignite.maintenance.MaintenanceTask) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) DefragmentationMXBean(org.apache.ignite.mxbean.DefragmentationMXBean) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) Collections(java.util.Collections) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) FileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory) CountDownLatch(java.util.concurrent.CountDownLatch) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteEx(org.apache.ignite.internal.IgniteEx) DefragmentationMXBean(org.apache.ignite.mxbean.DefragmentationMXBean) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)20 Test (org.junit.Test)14 File (java.io.File)13 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)12 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)11 IOException (java.io.IOException)10 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)10 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)8 IgniteEx (org.apache.ignite.internal.IgniteEx)8 ByteBuffer (java.nio.ByteBuffer)7 OpenOption (java.nio.file.OpenOption)7 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)7 FileIODecorator (org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Ignite (org.apache.ignite.Ignite)6 IgniteCache (org.apache.ignite.IgniteCache)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 GridCacheDatabaseSharedManager (org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager)6 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5