This is an automated email from the ASF dual-hosted git repository.

alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new efd8d46266c IGNITE-19111 Fix PDS corruption on checkpoint after 
deactivation - Fixes #10615.
efd8d46266c is described below

commit efd8d46266c8a84ec2b6abdec24491f210c80c8c
Author: Aleksey Plekhanov <[email protected]>
AuthorDate: Fri Mar 31 10:35:59 2023 +0300

    IGNITE-19111 Fix PDS corruption on checkpoint after deactivation - Fixes 
#10615.
    
    Signed-off-by: Aleksey Plekhanov <[email protected]>
---
 .../processors/cache/CacheGroupContext.java        |  15 +++
 .../processors/cache/GridCacheProcessor.java       |   2 +
 .../cache/IgniteCacheOffheapManager.java           |   9 +-
 .../cache/IgniteCacheOffheapManagerImpl.java       |  52 ++++++---
 .../IgnitePdsCheckpointAfterDeactivateTest.java    | 117 +++++++++++++++++++++
 .../ignite/testsuites/IgnitePdsTestSuite.java      |   2 +
 6 files changed, 178 insertions(+), 19 deletions(-)

diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheGroupContext.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheGroupContext.java
index 8e948c4fcda..64f5d9bbdd6 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheGroupContext.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheGroupContext.java
@@ -201,6 +201,9 @@ public class CacheGroupContext {
     /** Disk page compression method. */
     private final CompressionHandler compressHandler;
 
+    /** Cache is prepared to stop. */
+    private volatile boolean preparedToStop;
+
     /**
      * @param ctx Context.
      * @param grpId Group ID.
@@ -1360,6 +1363,18 @@ public class CacheGroupContext {
         return compressHandler;
     }
 
+    /** Prepare cache to stop (prohibit any futher updates). */
+    public void prepareToStop() {
+        preparedToStop = true;
+
+        offheap().prepareToStop();
+    }
+
+    /** */
+    public boolean isPreparedToStop() {
+        return preparedToStop;
+    }
+
     /**
      * @param ccfg Cache configuration.
      * @param plugins Ignite plugin processor.
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
index 518c8aa8989..d8bbc50a7f7 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
@@ -2787,6 +2787,8 @@ public class GridCacheProcessor extends 
GridProcessorAdapter {
                 }
             );
 
+            grpsToStop.forEach(g -> g.get1().prepareToStop());
+
             if (!exchActions.cacheStopRequests().isEmpty())
                 removeOffheapListenerAfterCheckpoint(grpsToStop);
 
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
index 3c4c732d092..d88788bdccd 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java
@@ -73,7 +73,7 @@ public interface IgniteCacheOffheapManager {
     public void onCacheStarted(GridCacheContext cctx) throws 
IgniteCheckedException;
 
     /**
-     *
+     * Callback on node stop.
      */
     public void onKernalStop();
 
@@ -84,7 +84,12 @@ public interface IgniteCacheOffheapManager {
     public void stopCache(int cacheId, boolean destroy);
 
     /**
-     *
+     * Prepare cache group to stop (due to cache destroy or cluster 
deactivate).
+     */
+    public void prepareToStop();
+
+    /**
+     * Stop cache group (due to cache destroy or cluster deactivate).
      */
     public void stop();
 
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
index b57a6aeb9b5..e785cf50c90 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.TreeMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.LongAdder;
@@ -163,9 +164,6 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
     /** */
     protected IgniteLogger log;
 
-    /** Cache data store for <tt>LOCAL</tt> caches only. */
-    private CacheDataStore locCacheDataStore;
-
     /** */
     private PendingEntriesTree pendingEntries;
 
@@ -178,6 +176,9 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
     /** */
     protected GridStripedLock partStoreLock = new 
GridStripedLock(Runtime.getRuntime().availableProcessors());
 
+    /** */
+    private final AtomicBoolean stopping = new AtomicBoolean();
+
     /** {@inheritDoc} */
     @Override public GridAtomicLong globalRemoveId() {
         return globalRmvId;
@@ -275,7 +276,14 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
 
     /** {@inheritDoc} */
     @Override public void onKernalStop() {
-        busyLock.block();
+        if (stopping.compareAndSet(false, true)) // Avoid concurrent blocking 
by prepareToStop and onKernalStop.
+            busyLock.block();
+    }
+
+    /** {@inheritDoc} */
+    @Override public void prepareToStop() {
+        if (stopping.compareAndSet(false, true)) // Avoid concurrent blocking 
by prepareToStop and onKernalStop.
+            busyLock.block();
     }
 
     /**
@@ -1674,11 +1682,21 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             return oldLen == newLen;
         }
 
+        /** */
+        private IgniteCheckedException operationCancelledException() {
+            if (grp.isPreparedToStop()) {
+                return new IgniteCheckedException("Operation has been 
cancelled (cache group: " +
+                    grp.cacheOrGroupName() + " is stopping).");
+            }
+            else
+                return new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+        }
+
         /** {@inheritDoc} */
         @Override public void invoke(GridCacheContext cctx, KeyCacheObject 
key, OffheapInvokeClosure c)
             throws IgniteCheckedException {
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             int cacheId = grp.sharedGroup() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
 
@@ -1765,7 +1783,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
         @Override public void insertRows(Collection<DataRowCacheAware> rows,
             IgnitePredicateX<CacheDataRow> initPred) throws 
IgniteCheckedException {
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 rowStore.addRows(F.view(rows, row -> row.value() != null), 
grp.statisticsHolderData());
@@ -1813,7 +1831,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             assert mvccVer != null || newMvccVer == null : newMvccVer;
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 CacheObjectContext coCtx = cctx.cacheObjectContext();
@@ -1879,7 +1897,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             assert !F.isEmpty(hist);
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 CacheObjectContext coCtx = cctx.cacheObjectContext();
@@ -1952,7 +1970,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             byte mvccTxState,
             byte newMvccTxState) throws IgniteCheckedException {
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 CacheObjectContext coCtx = cctx.cacheObjectContext();
@@ -2008,7 +2026,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             assert primary || !needHistory;
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 int cacheId = grp.sharedGroup() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
@@ -2218,7 +2236,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             assert primary || !needHistory;
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 int cacheId = grp.sharedGroup() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
@@ -2285,7 +2303,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             assert mvccSnapshot != null;
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 int cacheId = grp.sharedGroup() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
@@ -2335,7 +2353,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
         /** {@inheritDoc} */
         @Override public void mvccRemoveAll(GridCacheContext cctx, 
KeyCacheObject key) throws IgniteCheckedException {
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 key.valueBytes(cctx.cacheObjectContext());
@@ -2396,7 +2414,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
                 return 0;
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 return cleanup0(cctx, cleanupRows);
@@ -2471,7 +2489,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             assert oldRow == null || oldRow.link() != 0L : oldRow;
 
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 int cacheId = grp.storeCacheIdInDataPage() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
@@ -2528,7 +2546,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
             MvccVersion mvccVer
         ) throws IgniteCheckedException {
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 int cacheId = grp.sharedGroup() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
@@ -2676,7 +2694,7 @@ public class IgniteCacheOffheapManagerImpl implements 
IgniteCacheOffheapManager
         /** {@inheritDoc} */
         @Override public void remove(GridCacheContext cctx, KeyCacheObject 
key, int partId) throws IgniteCheckedException {
             if (!busyLock.enterBusy())
-                throw new NodeStoppingException("Operation has been cancelled 
(node is stopping).");
+                throw operationCancelledException();
 
             try {
                 int cacheId = grp.sharedGroup() ? cctx.cacheId() : 
CU.UNDEFINED_CACHE_ID;
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgnitePdsCheckpointAfterDeactivateTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgnitePdsCheckpointAfterDeactivateTest.java
new file mode 100644
index 00000000000..cc5731321c7
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgnitePdsCheckpointAfterDeactivateTest.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.persistence;
+
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteDataStreamer;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.failure.StopNodeFailureHandler;
+import org.apache.ignite.internal.IgniteEx;
+import 
org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+/**
+ * Test PDS consistency after checkpoint, triggered by timeout, after cluster 
deactivation.
+ */
+public class IgnitePdsCheckpointAfterDeactivateTest extends 
GridCommonAbstractTest {
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        cleanPersistenceDir();
+
+        super.beforeTest();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+
+        cleanPersistenceDir();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        return super.getConfiguration(igniteInstanceName)
+            .setDataStorageConfiguration(new DataStorageConfiguration()
+                .setDefaultDataRegionConfiguration(new 
DataRegionConfiguration().setPersistenceEnabled(true))
+                .setCheckpointFrequency(1_000L))
+            .setFailureHandler(new StopNodeFailureHandler());
+    }
+
+    /** */
+    @Test
+    public void testCpAfterClusterDeactivate() throws Exception {
+        IgniteEx ignite0 = startGrid(0);
+        IgniteEx ignite1 = startGrid(1);
+
+        ignite0.cluster().state(ClusterState.ACTIVE);
+
+        ignite0.getOrCreateCache(new 
CacheConfiguration<>(DEFAULT_CACHE_NAME).setBackups(1)
+            .setAffinity(new RendezvousAffinityFunction(false, 10)));
+
+        try (IgniteDataStreamer<Integer, Integer> streamer = 
ignite0.dataStreamer(DEFAULT_CACHE_NAME)) {
+            for (int i = 0; i < 100_000; i++)
+                streamer.addData(i, i);
+        }
+
+        stopGrid(0);
+
+        try (IgniteDataStreamer<Integer, Integer> streamer = 
ignite1.dataStreamer(DEFAULT_CACHE_NAME)) {
+            streamer.allowOverwrite(true);
+            for (int i = 0; i < 100_000; i++)
+                streamer.addData(i, i + 1);
+        }
+
+        ignite0 = startGrid(0);
+
+        
((GridCacheDatabaseSharedManager)ignite0.context().cache().context().database()).addCheckpointListener(new
 CheckpointListener() {
+            @Override public void onMarkCheckpointBegin(Context ctx) {
+                // No-op.
+            }
+
+            @Override public void onCheckpointBegin(Context ctx) {
+                // Delay last checkpoint to wait for other processes to mark 
some pages as dirty before actual 
+                // caches stop. 
+                if ("caches stop".equals(ctx.progress().reason()))
+                    doSleep(1_000L);   
+            }
+
+            @Override public void beforeCheckpointBegin(Context ctx) {
+                // No-op.
+            }
+        });
+
+        ignite0.cluster().state(ClusterState.INACTIVE);
+
+        doSleep(2_000L);
+
+        ignite0.cluster().state(ClusterState.ACTIVE);
+
+        IgniteCache<Integer, Integer> cache = 
ignite0.cache(DEFAULT_CACHE_NAME);
+
+        for (int i = 0; i < 100_000; i++)
+            assertEquals((Integer)(i + 1), cache.get(i));
+    }
+}
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgnitePdsTestSuite.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgnitePdsTestSuite.java
index 9bc0f91915a..bda9ad78dc0 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgnitePdsTestSuite.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgnitePdsTestSuite.java
@@ -26,6 +26,7 @@ import 
org.apache.ignite.internal.processors.cache.ClusterStateOnStartPropertyTe
 import 
org.apache.ignite.internal.processors.cache.IgniteClusterActivateDeactivateTestWithPersistence;
 import 
org.apache.ignite.internal.processors.cache.IgnitePdsDataRegionMetricsTxTest;
 import 
org.apache.ignite.internal.processors.cache.persistence.IgnitePdsCacheConfigurationFileConsistencyCheckTest;
+import 
org.apache.ignite.internal.processors.cache.persistence.IgnitePdsCheckpointAfterDeactivateTest;
 import 
org.apache.ignite.internal.processors.cache.persistence.IgnitePdsClientNearCachePutGetTest;
 import 
org.apache.ignite.internal.processors.cache.persistence.IgnitePdsDestroyCacheTest;
 import 
org.apache.ignite.internal.processors.cache.persistence.IgnitePdsDestroyCacheWithoutCheckpointsTest;
@@ -130,6 +131,7 @@ public class IgnitePdsTestSuite {
         GridTestUtils.addTestIfNeeded(suite, 
IgnitePdsRemoveDuringRebalancingTest.class, ignoredTests);
         GridTestUtils.addTestIfNeeded(suite, 
IgnitePdsDestroyCacheWithoutCheckpointsTest.class, ignoredTests);
         GridTestUtils.addTestIfNeeded(suite, 
IgnitePdsCacheConfigurationFileConsistencyCheckTest.class, ignoredTests);
+        GridTestUtils.addTestIfNeeded(suite, 
IgnitePdsCheckpointAfterDeactivateTest.class, ignoredTests);
 
         GridTestUtils.addTestIfNeeded(suite, 
DefaultPageSizeBackwardsCompatibilityTest.class, ignoredTests);
 

Reply via email to