tkalkirill commented on code in PR #800:
URL: https://github.com/apache/ignite-3/pull/800#discussion_r870222107


##########
modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointWorkflow.java:
##########
@@ -0,0 +1,355 @@
+/*
+ * 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.pagememory.persistence.checkpoint;
+
+import static java.util.stream.Collectors.toUnmodifiableList;
+import static 
org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointState.FINISHED;
+import static 
org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointState.LOCK_RELEASED;
+import static 
org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointState.LOCK_TAKEN;
+import static 
org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointState.MARKER_STORED_TO_DISK;
+import static 
org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointState.PAGE_SNAPSHOT_TAKEN;
+import static 
org.apache.ignite.internal.pagememory.persistence.checkpoint.GridConcurrentMultiPairQueue.EMPTY;
+import static org.apache.ignite.lang.IgniteSystemProperties.getInteger;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.ForkJoinWorkerThread;
+import java.util.concurrent.Future;
+import java.util.function.Supplier;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.internal.pagememory.FullPageId;
+import org.apache.ignite.internal.pagememory.PageMemoryDataRegion;
+import org.apache.ignite.internal.pagememory.persistence.PageMemoryImpl;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * This class responsibility is to complement {@link Checkpointer} class with 
side logic of checkpointing like checkpoint listeners
+ * notifications, collect dirt pages etc.
+ *
+ * <p>It allows {@link Checkpointer} class is to focus on its main 
responsibility: synchronizing memory with disk.
+ *
+ * <p>Additional actions needed during checkpoint are implemented in this 
class.
+ *
+ * <p>Two main blocks of logic this class is responsible for:
+ *
+ * <p>{@link CheckpointWorkflow#markCheckpointBegin} - Initialization of next 
checkpoint. It collects all required info.
+ *
+ * <p>{@link CheckpointWorkflow#markCheckpointEnd} - Finalization of last 
checkpoint.
+ */
+class CheckpointWorkflow implements IgniteComponent {
+    /**
+     * Starting from this number of dirty pages in checkpoint, array will be 
sorted with {@link Arrays#parallelSort(Comparable[])} in case
+     * of {@link CheckpointWriteOrder#SEQUENTIAL}.
+     */
+    public static final String CHECKPOINT_PARALLEL_SORT_THRESHOLD = 
"CHECKPOINT_PARALLEL_SORT_THRESHOLD";
+
+    /**
+     * Starting from this number of dirty pages in checkpoint, array will be 
sorted with {@link Arrays#parallelSort(Comparable[])} in case
+     * of {@link CheckpointWriteOrder#SEQUENTIAL}.
+     */
+    // TODO: IGNITE-16935 Move to configuration
+    private final int parallelSortThreshold = 
getInteger(CHECKPOINT_PARALLEL_SORT_THRESHOLD, 512 * 1024);
+
+    /** This number of threads will be created and used for parallel sorting. 
*/
+    private static final int PARALLEL_SORT_THREADS = 
Math.min(Runtime.getRuntime().availableProcessors(), 8);
+
+    /** Checkpoint marker storage. */
+    private final CheckpointMarkersStorage checkpointMarkersStorage;
+
+    /** Checkpoint lock. */
+    private final CheckpointReadWriteLock checkpointReadWriteLock;
+
+    /** Supplier of persistent data regions for the checkpointing. */
+    private final Supplier<Collection<PageMemoryDataRegion>> 
dataRegionsSupplier;
+
+    /** Checkpoint write order configuration. */
+    private final CheckpointWriteOrder checkpointWriteOrder;
+
+    /** Collections of checkpoint listeners. */
+    private final List<IgniteBiTuple<CheckpointListener, 
PageMemoryDataRegion>> listeners = new CopyOnWriteArrayList<>();
+
+    /**
+     * Constructor.
+     *
+     * @param checkpointMarkersStorage Checkpoint marker storage.
+     * @param checkpointReadWriteLock Checkpoint read write lock.
+     * @param checkpointWriteOrder Checkpoint write order.
+     * @param dataRegionsSupplier Supplier of persistent data regions for the 
checkpointing.
+     */
+    public CheckpointWorkflow(
+            CheckpointMarkersStorage checkpointMarkersStorage,
+            CheckpointReadWriteLock checkpointReadWriteLock,
+            CheckpointWriteOrder checkpointWriteOrder,
+            Supplier<Collection<PageMemoryDataRegion>> dataRegionsSupplier
+    ) {
+        this.checkpointMarkersStorage = checkpointMarkersStorage;
+        this.checkpointReadWriteLock = checkpointReadWriteLock;
+        this.checkpointWriteOrder = checkpointWriteOrder;
+        this.dataRegionsSupplier = dataRegionsSupplier;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void start() {
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void stop() {
+        listeners.clear();
+    }
+
+    /**
+     * First stage of checkpoint which collects demanded information (dirty 
pages mostly).
+     *
+     * @param startCheckpointTimestamp Checkpoint start timestamp.
+     * @param curr Current checkpoint event info.
+     * @return Checkpoint collected info.
+     * @throws IgniteInternalCheckedException If failed.
+     */
+    public Checkpoint markCheckpointBegin(
+            long startCheckpointTimestamp,
+            CheckpointProgressImpl curr
+    ) throws IgniteInternalCheckedException {
+        Collection<PageMemoryDataRegion> dataRegions = 
dataRegionsSupplier.get();
+
+        List<CheckpointListener> listeners = 
collectCheckpointListeners(dataRegions);
+
+        checkpointReadWriteLock.readLock();
+
+        try {
+            for (CheckpointListener listener : listeners) {
+                listener.beforeCheckpointBegin(curr);
+            }
+        } finally {
+            checkpointReadWriteLock.readUnlock();
+        }
+
+        checkpointReadWriteLock.writeLock();
+
+        CheckpointDirtyPagesInfoHolder dirtyPages;
+
+        try {
+            curr.transitTo(LOCK_TAKEN);
+
+            for (CheckpointListener listener : listeners) {
+                listener.onMarkCheckpointBegin(curr);
+            }
+
+            // There are allowable to replace pages only after checkpoint 
entry was stored to disk.
+            dirtyPages = beginCheckpoint(dataRegions, 
curr.futureFor(MARKER_STORED_TO_DISK));
+
+            curr.currentCheckpointPagesCount(dirtyPages.pageCount);
+
+            curr.transitTo(PAGE_SNAPSHOT_TAKEN);
+        } finally {
+            checkpointReadWriteLock.writeUnlock();
+        }
+
+        curr.transitTo(LOCK_RELEASED);
+
+        for (CheckpointListener listener : listeners) {
+            listener.onCheckpointBegin(curr);
+        }
+
+        if (dirtyPages.pageCount > 0) {
+            checkpointMarkersStorage.onCheckpointBegin(curr.id());
+
+            curr.transitTo(MARKER_STORED_TO_DISK);
+
+            return new Checkpoint(splitAndSortCpPagesIfNeeded(dirtyPages), 
curr);
+        }
+
+        return new Checkpoint(EMPTY, curr);
+    }
+
+    /**
+     * Do some actions on checkpoint finish (After all pages were written to 
disk).
+     *
+     * @param chp Checkpoint snapshot.
+     * @throws IgniteInternalCheckedException If failed.
+     */
+    public void markCheckpointEnd(Checkpoint chp) throws 
IgniteInternalCheckedException {
+        Collection<PageMemoryDataRegion> dataRegions = 
dataRegionsSupplier.get();

Review Comment:
   No reason, fixed it



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to