Search in sources :

Example 1 with CorruptedTreeException

use of org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException in project ignite by apache.

the class InlineIndexTree method corruptedTreeException.

/**
 * Construct the exception and invoke failure processor.
 *
 * @param msg Message.
 * @param cause Cause.
 * @param grpId Group id.
 * @param pageIds Pages ids.
 * @return New CorruptedTreeException instance.
 */
@Override
protected CorruptedTreeException corruptedTreeException(String msg, Throwable cause, int grpId, long... pageIds) {
    CorruptedTreeException e = new CorruptedTreeException(msg, cause, grpName, def.idxName().cacheName(), def.idxName().idxName(), grpId, pageIds);
    processFailure(FailureType.CRITICAL_ERROR, e);
    return e;
}
Also used : CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException)

Example 2 with CorruptedTreeException

use of org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException 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 3 with CorruptedTreeException

use of org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException in project ignite by apache.

the class DiagnosticProcessorTest method testOutputDiagnosticCorruptedPagesInfo.

/**
 * Check that when an CorruptedTreeException is thrown, a "corruptedPages_TIMESTAMP.txt"
 * will be created and a warning will be in the log.
 *
 * @throws Exception If failed.
 */
@Test
public void testOutputDiagnosticCorruptedPagesInfo() throws Exception {
    ListeningTestLogger listeningTestLog = new ListeningTestLogger(GridAbstractTest.log);
    IgniteEx n = startGrid(0, cfg -> {
        cfg.setGridLogger(listeningTestLog);
    });
    n.cluster().state(ClusterState.ACTIVE);
    awaitPartitionMapExchange();
    for (int i = 0; i < 10_000; i++) n.cache(DEFAULT_CACHE_NAME).put(i, "val_" + i);
    assertNotNull(n.context().diagnostic());
    T2<Integer, Long> anyPageId = findAnyPageId(n);
    assertNotNull(anyPageId);
    LogListener logLsnr = LogListener.matches("CorruptedTreeException has occurred. " + "To diagnose it, make a backup of the following directories: ").build();
    listeningTestLog.registerListener(logLsnr);
    n.context().failure().process(new FailureContext(FailureType.CRITICAL_ERROR, new CorruptedTreeException("Test ex", null, DEFAULT_CACHE_NAME, anyPageId.get1(), anyPageId.get2())));
    assertTrue(logLsnr.check());
    Path diagnosticPath = getFieldValue(n.context().diagnostic(), "diagnosticPath");
    List<File> corruptedPagesFiles = Arrays.stream(diagnosticPath.toFile().listFiles()).filter(f -> corruptedPagesFileNamePattern().matcher(f.getName()).matches()).collect(toList());
    assertEquals(1, corruptedPagesFiles.size());
    assertTrue(corruptedPagesFiles.get(0).length() > 0);
}
Also used : Path(java.nio.file.Path) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) LogListener(org.apache.ignite.testframework.LogListener) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) Arrays(java.util.Arrays) DiagnosticProcessor.walDirs(org.apache.ignite.internal.processors.diagnostic.DiagnosticProcessor.walDirs) ClusterState(org.apache.ignite.cluster.ClusterState) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) FailureContext(org.apache.ignite.failure.FailureContext) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) Path(java.nio.file.Path) DiagnosticProcessor.corruptedPagesFile(org.apache.ignite.internal.processors.diagnostic.DiagnosticProcessor.corruptedPagesFile) SegmentRouter(org.apache.ignite.internal.processors.cache.persistence.wal.SegmentRouter) DFLT_WAL_PATH(org.apache.ignite.configuration.DataStorageConfiguration.DFLT_WAL_PATH) F(org.apache.ignite.internal.util.typedef.F) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) LongStream(java.util.stream.LongStream) FailureType(org.apache.ignite.failure.FailureType) RandomAccessFileIOFactory(org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) DEFAULT_TARGET_FOLDER(org.apache.ignite.internal.processors.diagnostic.DiagnosticProcessor.DEFAULT_TARGET_FOLDER) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) Test(org.junit.Test) File(java.io.File) T2(org.apache.ignite.internal.util.typedef.T2) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Collectors.toList(java.util.stream.Collectors.toList) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridTestUtils.getFieldValue(org.apache.ignite.testframework.GridTestUtils.getFieldValue) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Pattern(java.util.regex.Pattern) PageSnapshot(org.apache.ignite.internal.pagemem.wal.record.PageSnapshot) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) LogListener(org.apache.ignite.testframework.LogListener) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) FailureContext(org.apache.ignite.failure.FailureContext) IgniteEx(org.apache.ignite.internal.IgniteEx) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) DiagnosticProcessor.corruptedPagesFile(org.apache.ignite.internal.processors.diagnostic.DiagnosticProcessor.corruptedPagesFile) File(java.io.File) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CorruptedTreeException (org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException)3 File (java.io.File)2 Pattern (java.util.regex.Pattern)2 ClusterState (org.apache.ignite.cluster.ClusterState)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 PageSnapshot (org.apache.ignite.internal.pagemem.wal.record.PageSnapshot)2 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)2 F (org.apache.ignite.internal.util.typedef.F)2 ListeningTestLogger (org.apache.ignite.testframework.ListeningTestLogger)2 LogListener (org.apache.ignite.testframework.LogListener)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ByteBuffer (java.nio.ByteBuffer)1