Search in sources :

Example 1 with WALDisableContext

use of org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext in project ignite by apache.

the class FileWriteAheadLogManager method log.

/**
 * {@inheritDoc}
 */
@Override
public WALPointer log(WALRecord rec, RolloverType rolloverType) throws IgniteCheckedException {
    if (serializer == null || mode == WALMode.NONE)
        return null;
    // Only delta-records, page snapshots and memory recovery are allowed to write in recovery mode.
    if (cctx.kernalContext().recoveryMode() && !(rec instanceof PageDeltaRecord || rec instanceof PageSnapshot || rec instanceof MemoryRecoveryRecord))
        return null;
    FileWriteHandle currWrHandle = currentHandle();
    WALDisableContext isDisable = walDisableContext;
    // Logging was not resumed yet.
    if (currWrHandle == null || (isDisable != null && isDisable.check()))
        return null;
    // Do page snapshots compression if configured.
    if (pageCompression != DiskPageCompression.DISABLED && rec instanceof PageSnapshot) {
        PageSnapshot pageSnapshot = (PageSnapshot) rec;
        int pageSize = pageSnapshot.realPageSize();
        ByteBuffer pageData = pageSnapshot.pageDataBuffer();
        ByteBuffer compressedPage = cctx.kernalContext().compress().compressPage(pageData, pageSize, 1, pageCompression, pageCompressionLevel);
        if (compressedPage != pageData) {
            assert compressedPage.isDirect() : "Is direct buffer: " + compressedPage.isDirect();
            rec = new PageSnapshot(pageSnapshot.fullPageId(), GridUnsafe.bufferAddress(compressedPage), compressedPage.limit(), pageSize);
        }
    }
    // Need to calculate record size first.
    rec.size(serializer.size(rec));
    while (true) {
        WALPointer ptr;
        if (rolloverType == RolloverType.NONE)
            ptr = currWrHandle.addRecord(rec);
        else {
            assert cctx.database().checkpointLockIsHeldByThread();
            if (rolloverType == RolloverType.NEXT_SEGMENT) {
                WALPointer pos = rec.position();
                do {
                    // This will change rec.position() unless concurrent rollover happened.
                    currWrHandle = closeBufAndRollover(currWrHandle, rec, rolloverType);
                } while (Objects.equals(pos, rec.position()));
                ptr = rec.position();
            } else if (rolloverType == RolloverType.CURRENT_SEGMENT) {
                if ((ptr = currWrHandle.addRecord(rec)) != null)
                    currWrHandle = closeBufAndRollover(currWrHandle, rec, rolloverType);
            } else
                throw new IgniteCheckedException("Unknown rollover type: " + rolloverType);
        }
        if (ptr != null) {
            metrics.onWalRecordLogged(rec.size());
            if (walAutoArchiveAfterInactivity > 0 || walForceArchiveTimeout > 0) {
                long millis = U.currentTimeMillis();
                lastRecordLoggedMs.set(millis);
                // No need to forcefully rollover for other record types.
                if (walForceArchiveTimeout > 0 && rec.type() == DATA_RECORD_V2)
                    lastDataRecordLoggedMs.set(millis);
            }
            return ptr;
        } else
            currWrHandle = rollOver(currWrHandle, null);
        checkNode();
        if (isStopping())
            throw new IgniteCheckedException("Stopping.");
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) PageDeltaRecord(org.apache.ignite.internal.pagemem.wal.record.delta.PageDeltaRecord) FileWriteHandle(org.apache.ignite.internal.processors.cache.persistence.wal.filehandle.FileWriteHandle) MemoryRecoveryRecord(org.apache.ignite.internal.pagemem.wal.record.MemoryRecoveryRecord) ByteBuffer(java.nio.ByteBuffer) PageSnapshot(org.apache.ignite.internal.pagemem.wal.record.PageSnapshot) WALDisableContext(org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext)

Example 2 with WALDisableContext

use of org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext in project ignite by apache.

the class GridCommonAbstractTest method disableWal.

/**
 * Disable/enable VAL.
 *
 * @param n Node.
 * @param disable Disable flag.
 * @throws Exception If failed.
 */
protected static void disableWal(IgniteEx n, boolean disable) throws Exception {
    WALDisableContext walDisableCtx = n.context().cache().context().walState().walDisableContext();
    setFieldValue(walDisableCtx, "disableWal", disable);
    assertEquals(disable, walDisableCtx.check());
    WALPointer logPtr = walMgr(n).log(new DataRecord(emptyList()));
    if (disable)
        assertNull(logPtr);
    else
        assertNotNull(logPtr);
}
Also used : DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) WALDisableContext(org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext)

Example 3 with WALDisableContext

use of org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext in project ignite by apache.

the class WriteAheadLogManagerSelfTest method disableWal.

/**
 * Disable WAL.
 *
 * @param n Node.
 */
private void disableWal(IgniteEx n) throws Exception {
    WALDisableContext walDisableCtx = n.context().cache().context().walState().walDisableContext();
    assertNotNull(walDisableCtx);
    setFieldValue(walDisableCtx, "disableWal", true);
    assertTrue(walDisableCtx.check());
    assertNull(walMgr(n).log(new DataRecord(emptyList())));
}
Also used : DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) WALDisableContext(org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext)

Example 4 with WALDisableContext

use of org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext in project ignite by apache.

the class IgniteDataStorageMetricsSelfTest method testWalWrittenBytes.

/**
 * Checking that the metrics of the total logged bytes are working correctly.
 *
 * @throws Exception If failed.
 */
@Test
public void testWalWrittenBytes() throws Exception {
    IgniteEx n = startGrid(0, (UnaryOperator<IgniteConfiguration>) cfg -> {
        cfg.getDataStorageConfiguration().setWalSegmentSize((int) (2 * U.MB));
        return cfg;
    });
    n.cluster().state(ACTIVE);
    awaitPartitionMapExchange();
    for (int i = 0; i < 10; i++) n.cache("cache").put(ThreadLocalRandom.current().nextLong(), new byte[(int) (32 * U.KB)]);
    WALDisableContext walDisableCtx = n.context().cache().context().walState().walDisableContext();
    assertNotNull(walDisableCtx);
    setFieldValue(walDisableCtx, "disableWal", true);
    assertTrue(walDisableCtx.check());
    assertNull(walMgr(n).log(new DataRecord(emptyList())));
    assertEquals(-1, walMgr(n).lastArchivedSegment());
    long exp = walMgr(n).lastWritePointer().fileOffset() - HEADER_RECORD_SIZE;
    assertEquals(exp, dbMgr(n).persistentStoreMetrics().getWalWrittenBytes());
    assertEquals(exp, dsMetricsMXBean(n).getWalWrittenBytes());
    assertEquals(exp, ((LongAdderMetric) dsMetricRegistry(n).findMetric("WalWrittenBytes")).value());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) Arrays(java.util.Arrays) UnaryOperator(java.util.function.UnaryOperator) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdderMetric(org.apache.ignite.internal.processors.metric.impl.LongAdderMetric) AtomicLongMetric(org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric) DataStorageMetricsMXBean(org.apache.ignite.mxbean.DataStorageMetricsMXBean) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) WALMode(org.apache.ignite.configuration.WALMode) HistogramMetric(org.apache.ignite.spi.metric.HistogramMetric) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) Collections.emptyList(java.util.Collections.emptyList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) Objects(java.util.Objects) DataRegionMetrics(org.apache.ignite.DataRegionMetrics) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Pattern(java.util.regex.Pattern) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) LongGauge(org.apache.ignite.internal.processors.metric.impl.LongGauge) WALDisableContext(org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) U(org.apache.ignite.internal.util.typedef.internal.U) HEADER_RECORD_SIZE(org.apache.ignite.internal.processors.cache.persistence.wal.serializer.RecordV1Serializer.HEADER_RECORD_SIZE) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) PAX(org.apache.ignite.internal.util.typedef.PAX) S(org.apache.ignite.internal.util.typedef.internal.S) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) SegmentRouter(org.apache.ignite.internal.processors.cache.persistence.wal.SegmentRouter) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) DataStorageMetrics(org.apache.ignite.DataStorageMetrics) Test(org.junit.Test) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) File(java.io.File) GridToStringInclude(org.apache.ignite.internal.util.tostring.GridToStringInclude) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridTestUtils.setFieldValue(org.apache.ignite.testframework.GridTestUtils.setFieldValue) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) DATASTORAGE_METRIC_PREFIX(org.apache.ignite.internal.processors.cache.persistence.DataStorageMetricsImpl.DATASTORAGE_METRIC_PREFIX) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) FileDescriptor(org.apache.ignite.internal.processors.cache.persistence.wal.FileDescriptor) CacheMode(org.apache.ignite.cache.CacheMode) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteEx(org.apache.ignite.internal.IgniteEx) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) WALDisableContext(org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with WALDisableContext

use of org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext in project ignite by apache.

the class IgniteNodeStoppedDuringDisableWALTest method testStopNodeWithDisableWAL.

/**
 * @param nodeStopPoint Stop point.
 * @throws Exception If failed.
 */
private void testStopNodeWithDisableWAL(NodeStopPoint nodeStopPoint) throws Exception {
    log.info("Start test crash " + nodeStopPoint);
    IgniteEx ig0 = startGrid(0);
    GridCacheSharedContext<Object, Object> sharedContext = ig0.context().cache().context();
    GridCacheDatabaseSharedManager dbMgr = (GridCacheDatabaseSharedManager) sharedContext.database();
    IgniteWriteAheadLogManager WALmgr = sharedContext.wal();
    WALDisableContext walDisableContext = new WALDisableContext(dbMgr, sharedContext.pageStore(), log) {

        @Override
        protected void writeMetaStoreDisableWALFlag() throws IgniteCheckedException {
            if (nodeStopPoint == NodeStopPoint.BEFORE_WRITE_KEY_TO_META_STORE)
                failNode(nodeStopPoint);
            super.writeMetaStoreDisableWALFlag();
            if (nodeStopPoint == NodeStopPoint.AFTER_WRITE_KEY_TO_META_STORE)
                failNode(nodeStopPoint);
        }

        @Override
        protected void removeMetaStoreDisableWALFlag() throws IgniteCheckedException {
            if (nodeStopPoint == NodeStopPoint.AFTER_CHECKPOINT_AFTER_ENABLE_WAL)
                failNode(nodeStopPoint);
            super.removeMetaStoreDisableWALFlag();
            if (nodeStopPoint == NodeStopPoint.AFTER_REMOVE_KEY_TO_META_STORE)
                failNode(nodeStopPoint);
        }

        @Override
        protected void disableWAL(boolean disable) throws IgniteCheckedException {
            if (disable) {
                if (nodeStopPoint == NodeStopPoint.AFTER_CHECKPOINT_BEFORE_DISABLE_WAL)
                    failNode(nodeStopPoint);
                super.disableWAL(disable);
                if (nodeStopPoint == NodeStopPoint.AFTER_DISABLE_WAL)
                    failNode(nodeStopPoint);
            } else {
                super.disableWAL(disable);
                if (nodeStopPoint == NodeStopPoint.AFTER_ENABLE_WAL)
                    failNode(nodeStopPoint);
            }
        }
    };
    setFieldValue(sharedContext.walState(), "walDisableContext", walDisableContext);
    setFieldValue(WALmgr, "walDisableContext", walDisableContext);
    ig0.context().internalSubscriptionProcessor().registerMetastorageListener(walDisableContext);
    ig0.cluster().active(true);
    try (IgniteDataStreamer<Integer, Integer> st = ig0.dataStreamer(DEFAULT_CACHE_NAME)) {
        st.allowOverwrite(true);
        for (int i = 0; i < GridTestUtils.SF.apply(10_000); i++) st.addData(i, -i);
    }
    boolean fail = false;
    try (WALIterator it = sharedContext.wal().replay(null)) {
        dbMgr.applyUpdatesOnRecovery(it, (ptr, rec) -> true, (entry) -> true);
    } catch (IgniteCheckedException e) {
        if (nodeStopPoint.needCleanUp)
            fail = true;
    }
    Assert.assertEquals(nodeStopPoint.needCleanUp, fail);
    Ignite ig1 = startGrid(0);
    String msg = nodeStopPoint.toString();
    int pageSize = ig1.configuration().getDataStorageConfiguration().getPageSize();
    if (nodeStopPoint.needCleanUp) {
        PdsFoldersResolver foldersResolver = ((IgniteEx) ig1).context().pdsFolderResolver();
        File root = foldersResolver.resolveFolders().persistentStoreRootPath();
        walkFileTree(root.toPath(), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                String name = path.toFile().getName();
                String filePath = path.toString();
                String parentDirName = path.toFile().getParentFile().getName();
                if (parentDirName.equals(METASTORAGE_DIR_NAME) || parentDirName.equals(TxLog.TX_LOG_CACHE_NAME))
                    return CONTINUE;
                if (WAL_NAME_PATTERN.matcher(name).matches() || WAL_TEMP_NAME_PATTERN.matcher(name).matches())
                    return CONTINUE;
                boolean failed = false;
                if (name.endsWith(FilePageStoreManager.TMP_SUFFIX))
                    failed = true;
                if (CP_FILE_NAME_PATTERN.matcher(name).matches())
                    failed = true;
                if (name.startsWith(PART_FILE_PREFIX) && path.toFile().length() > pageSize)
                    failed = true;
                if (name.startsWith(INDEX_FILE_NAME) && path.toFile().length() > pageSize)
                    failed = true;
                if (failed)
                    fail(msg + " " + filePath + " " + path.toFile().length());
                return CONTINUE;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) IgniteWriteAheadLogManager(org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) WALDisableContext(org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) IgniteEx(org.apache.ignite.internal.IgniteEx) PdsFoldersResolver(org.apache.ignite.internal.processors.cache.persistence.filename.PdsFoldersResolver) Ignite(org.apache.ignite.Ignite) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

WALDisableContext (org.apache.ignite.internal.processors.cache.WalStateManager.WALDisableContext)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 File (java.io.File)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 DataRecord (org.apache.ignite.internal.pagemem.wal.record.DataRecord)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ByteBuffer (java.nio.ByteBuffer)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Arrays (java.util.Arrays)1 Collections.emptyList (java.util.Collections.emptyList)1 Objects (java.util.Objects)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 UnaryOperator (java.util.function.UnaryOperator)1 Matcher (java.util.regex.Matcher)1