xtern commented on a change in pull request #7941:
URL: https://github.com/apache/ignite/pull/7941#discussion_r504727035



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/managers/encryption/CacheGroupPageScanner.java
##########
@@ -0,0 +1,479 @@
+/*
+ * 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.managers.encryption;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.NodeStoppingException;
+import org.apache.ignite.internal.managers.communication.GridIoPolicy;
+import org.apache.ignite.internal.pagemem.PageIdAllocator;
+import org.apache.ignite.internal.pagemem.PageIdUtils;
+import org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager;
+import org.apache.ignite.internal.processors.cache.CacheGroupContext;
+import 
org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager;
+import 
org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener;
+import 
org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx;
+import 
org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId;
+import org.apache.ignite.internal.util.BasicRateLimiter;
+import org.apache.ignite.internal.util.GridConcurrentHashSet;
+import org.apache.ignite.internal.util.future.GridFinishedFuture;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.lang.IgniteInClosureX;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static org.apache.ignite.internal.util.IgniteUtils.MB;
+
+/**
+ * Cache group page stores scanner.
+ * Scans a range of pages and marks them as dirty to re-encrypt them with the 
last encryption key on disk.
+ */
+public class CacheGroupPageScanner implements CheckpointListener {
+    /** Thread prefix for scanning tasks. */
+    private static final String REENCRYPT_THREAD_PREFIX = "reencrypt";
+
+    /** Kernal context. */
+    private final GridKernalContext ctx;
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /** Lock. */
+    private final ReentrantLock lock = new ReentrantLock();
+
+    /** Mapping of cache group ID to group scanning task. */
+    private final Map<Integer, GroupScanTask> grps = new ConcurrentHashMap<>();
+
+    /** Collection of groups waiting for a checkpoint. */
+    private final Collection<GroupScanTask> cpWaitGrps = new 
ConcurrentLinkedQueue<>();
+
+    /** Page scanning speed limiter. */
+    private final BasicRateLimiter limiter;
+
+    /** Single-threaded executor to run cache group scan task. */
+    private final ThreadPoolExecutor singleExecSvc;
+
+    /** Number of pages that is scanned during reencryption under checkpoint 
lock. */
+    private final int batchSize;
+
+    /** Stop flag. */
+    private boolean stopped;
+
+    /**
+     * @param ctx Grid kernal context.
+     */
+    public CacheGroupPageScanner(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        DataStorageConfiguration dsCfg = 
ctx.config().getDataStorageConfiguration();
+
+        if (!CU.isPersistenceEnabled(dsCfg)) {
+            batchSize = -1;
+            limiter = null;
+            singleExecSvc = null;
+
+            return;
+        }
+
+        double rateLimit = 
dsCfg.getEncryptionConfiguration().getReencryptionRateLimit();
+
+        limiter = rateLimit > 0 ? new BasicRateLimiter(rateLimit * MB /
+            (dsCfg.getPageSize() == 0 ? 
DataStorageConfiguration.DFLT_PAGE_SIZE : dsCfg.getPageSize())) : null;
+
+        batchSize = 
dsCfg.getEncryptionConfiguration().getReencryptionBatchSize();
+
+        singleExecSvc = new IgniteThreadPoolExecutor(REENCRYPT_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            GridIoPolicy.SYSTEM_POOL,
+            new OomExceptionHandler(ctx));
+
+        singleExecSvc.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onCheckpointBegin(Context cpCtx) {
+        Set<GroupScanTask> completeCandidates = new HashSet<>();
+
+        cpWaitGrps.removeIf(completeCandidates::add);

Review comment:
       As mentioned in the previous comment, we now use beforeCheckpointBegin 
to wait for a checkpoint, which is called before the checkpoint mark-phase is 
executed (and before the write lock is acquired), so we have now 
synchronization between releasing the read lock (while scanning) and acquiring 
a write lock (on mark phase).




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to