alex-plekhanov commented on a change in pull request #6554: IGNITE-11073: 
Backup page store manager, initial
URL: https://github.com/apache/ignite/pull/6554#discussion_r387733936
 
 

 ##########
 File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotFutureTask.java
 ##########
 @@ -0,0 +1,852 @@
+/*
+ * 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.snapshot;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicIntegerArray;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BooleanSupplier;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.pagemem.PageIdUtils;
+import org.apache.ignite.internal.pagemem.store.PageStore;
+import org.apache.ignite.internal.pagemem.store.PageWriteListener;
+import org.apache.ignite.internal.processors.cache.CacheGroupContext;
+import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition;
+import 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState;
+import 
org.apache.ignite.internal.processors.cache.persistence.DbCheckpointListener;
+import 
org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager;
+import org.apache.ignite.internal.processors.cache.persistence.file.FileIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory;
+import 
org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager;
+import 
org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId;
+import 
org.apache.ignite.internal.processors.cache.persistence.partstate.PagesAllocationRange;
+import 
org.apache.ignite.internal.processors.cache.persistence.partstate.PartitionAllocationMap;
+import org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO;
+import org.apache.ignite.internal.processors.cache.persistence.wal.crc.FastCrc;
+import org.apache.ignite.internal.util.GridIntIterator;
+import org.apache.ignite.internal.util.GridIntList;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.lang.IgniteThrowableRunner;
+import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import static 
org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION;
+import static 
org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheDirName;
+import static 
org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheWorkDir;
+import static 
org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.getPartitionFile;
+import static 
org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.getPartionDeltaFile;
+import static 
org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.relativeNodePath;
+
+/**
+ *
+ */
+class SnapshotFutureTask extends GridFutureAdapter<Boolean> implements 
DbCheckpointListener {
+    /** Shared context. */
+    private final GridCacheSharedContext<?, ?> cctx;
+
+    /** Ignite logger */
+    private final IgniteLogger log;
+
+    /** Node id which cause snapshot operation. */
+    private final UUID srcNodeId;
+
+    /** Unique identifier of snapshot process. */
+    private final String snpName;
+
+    /** Snapshot working directory on file system. */
+    private final File tmpTaskWorkDir;
+
+    /**
+     * The length of file size per each cache partiton file.
+     * Partition has value greater than zero only for partitons in OWNING 
state.
+     * Information collected under checkpoint write lock.
+     */
+    private final Map<GroupPartitionId, Long> partFileLengths = new 
HashMap<>();
+
+    /**
+     * Map of partitions to snapshot and theirs corresponding delta PageStores.
+     * Writers are pinned to the snapshot context due to controlling partition
+     * processing supplier.
+     */
+    private final Map<GroupPartitionId, PageStoreSerialWriter> 
partDeltaWriters = new HashMap<>();
+
+    /** Snapshot data sender. */
+    @GridToStringExclude
+    private final SnapshotFileSender snpSndr;
+
+    /** Collection of partition to be snapshotted. */
+    private final List<GroupPartitionId> parts = new ArrayList<>();
+
+    /** Checkpoint end future. */
+    private final CompletableFuture<Boolean> cpEndFut = new 
CompletableFuture<>();
+
+    /** Future to wait until checkpoint mark pahse will be finished and 
snapshot tasks scheduled. */
+    private final GridFutureAdapter<Void> startedFut = new 
GridFutureAdapter<>();
+
+    /** Absolute snapshot storage path. */
+    private File tmpSnpDir;
+
+    /** {@code true} if operation has been cancelled. */
+    private volatile boolean cancelled;
+
+    /** An exception which has been ocurred during snapshot processing. */
+    private final AtomicReference<Throwable> err = new AtomicReference<>();
+
+    /** Flag indicates the task must be interrupted. */
+    private final BooleanSupplier stopping = () -> cancelled || err.get() != 
null;
+
+    /**
+     * @param e Finished snapshot tosk future with particular exception.
+     */
+    public SnapshotFutureTask(IgniteCheckedException e) {
+        A.notNull(e, "Exception for a finished snapshot task must be not 
null");
+
+        cctx = null;
+        log = null;
+        snpName = null;
+        srcNodeId = null;
+        tmpTaskWorkDir = null;
+        snpSndr = null;
+
+        err.set(e);
+        startedFut.onDone(e);
+        onDone(e);
+    }
+
+    /**
+     * @param snpName Unique identifier of snapshot task.
+     * @param ioFactory Factory to working with delta as file storage.
+     */
+    public SnapshotFutureTask(
+        GridCacheSharedContext<?, ?> cctx,
+        UUID srcNodeId,
+        String snpName,
+        File tmpWorkDir,
+        FileIOFactory ioFactory,
+        SnapshotFileSender snpSndr,
+        Map<Integer, GridIntList> parts
+    ) {
+        A.notNull(snpName, "Snapshot name cannot be empty or null");
+        A.notNull(snpSndr, "Snapshot sender which handles execution tasks must 
be not null");
+        A.notNull(snpSndr.executor(), "Executor service must be not null");
+
+        this.cctx = cctx;
+        this.log = cctx.logger(SnapshotFutureTask.class);
+        this.snpName = snpName;
+        this.srcNodeId = srcNodeId;
+        this.tmpTaskWorkDir = new File(tmpWorkDir, snpName);
+        this.snpSndr = snpSndr;
+
+        for (Map.Entry<Integer, GridIntList> e : parts.entrySet()) {
+            GridIntIterator iter = e.getValue().iterator();
+
+            while (iter.hasNext())
+                this.parts.add(new GroupPartitionId(e.getKey(), iter.next()));
+        }
+
+        try {
+            tmpSnpDir = 
U.resolveWorkDirectory(tmpTaskWorkDir.getAbsolutePath(),
+                
relativeNodePath(cctx.kernalContext().pdsFolderResolver().resolveFolders()),
+                false);
+
+            this.snpSndr.init();
+
+            Map<Integer, File> dirs = new HashMap<>();
+
+            for (Integer grpId : parts.keySet()) {
+                CacheGroupContext gctx = cctx.cache().cacheGroup(grpId);
+
+                if (gctx == null)
+                    throw new IgniteCheckedException("Cache group context has 
not found. Cache group is stopped: " + grpId);
+
+                if (!CU.isPersistentCache(gctx.config(), 
cctx.kernalContext().config().getDataStorageConfiguration()))
+                    throw new IgniteCheckedException("In-memory cache groups 
are not allowed to be snapshotted: " + grpId);
+
+                if (gctx.config().isEncryptionEnabled())
+                    throw new IgniteCheckedException("Encrypted cache groups 
are note allowed to be snapshotted: " + grpId);
+
+                // Create cache snapshot directory if not.
+                File grpDir = 
U.resolveWorkDirectory(tmpSnpDir.getAbsolutePath(),
+                    cacheDirName(gctx.config()), false);
+
+                U.ensureDirectory(grpDir,
+                    "snapshot directory for cache group: " + gctx.groupId(),
+                    null);
+
+                dirs.put(grpId, grpDir);
+            }
+
+            CompletableFuture<Boolean> cpEndFut0 = cpEndFut;
+
+            for (GroupPartitionId pair : this.parts) {
+                PageStore store = 
((FilePageStoreManager)cctx.pageStore()).getStore(pair.getGroupId(),
+                    pair.getPartitionId());
+
+                partDeltaWriters.put(pair,
+                    new PageStoreSerialWriter(log,
+                        store,
+                        () -> cpEndFut0.isDone() && 
!cpEndFut0.isCompletedExceptionally(),
+                        stopping,
+                        this::acceptException,
+                        getPartionDeltaFile(dirs.get(pair.getGroupId()), 
pair.getPartitionId()),
+                        ioFactory,
+                        cctx.kernalContext()
+                            .config()
+                            .getDataStorageConfiguration()
+                            .getPageSize()));
+            }
+
+            if (log.isInfoEnabled()) {
+                log.info("Snapshot task has been created [sctx=" + this +
+                    ", topVer=" + cctx.discovery().topologyVersionEx() + ']');
+            }
+        }
+        catch (IgniteCheckedException e) {
+            acceptException(e);
+        }
+    }
+
+    /**
+     * @return Node id which triggers this operation..
+     */
+    public UUID sourceNodeId() {
+        return srcNodeId;
+    }
+
+    /**
+     * @return Type of snapshot operation.
+     */
+    public Class<? extends SnapshotFileSender> type() {
+        return snpSndr.getClass();
+    }
+
+    /**
+     * @return List of partitions to be processed.
+     */
+    public List<GroupPartitionId> partitions() {
+        return parts;
+    }
+
+    /**
+     * @param th An exception which occurred during snapshot processing.
+     */
+    public void acceptException(Throwable th) {
+        if (th == null)
+            return;
+
+        if (err.compareAndSet(null, th))
+            closeAsync();
+
+        startedFut.onDone(th);
+
+        log.error("Exception occurred during snapshot operation", th);
+    }
+
+    /**
+     * Close snapshot operation and release resources being used.
+     */
+    private void close() {
+        if (isDone())
+            return;
+
+        Throwable err0 = err.get();
+
+        if (onDone(true, err0, cancelled)) {
+            for (PageStoreSerialWriter writer : partDeltaWriters.values())
+                U.closeQuiet(writer);
+
+            snpSndr.close(err0);
+
+            if (tmpSnpDir != null)
+                U.delete(tmpSnpDir);
+
+            // Delete snapshot directory if no other files exists.
+            try {
+                if (U.fileCount(tmpTaskWorkDir.toPath()) == 0 || err0 != null)
+                    U.delete(tmpTaskWorkDir.toPath());
+            }
+            catch (IOException e) {
+                log.error("Snapshot directory doesn't exist [snpName=" + 
snpName + ", dir=" + tmpTaskWorkDir + ']');
+            }
+
+            if (err0 != null)
+                startedFut.onDone(err0);
+        }
+    }
+
+    /**
+     * @throws IgniteCheckedException If fails.
+     */
+    public void awaitStarted() throws IgniteCheckedException {
+        startedFut.get();
+    }
+
+    /**
+     * Initiates snapshot task.
+     */
+    public void start() {
+        if (stopping.getAsBoolean())
+            return;
+
+        startedFut.listen(f ->
+            
((GridCacheDatabaseSharedManager)cctx.database()).removeCheckpointListener(this)
+        );
+
+        // Listener will be removed right after first execution
+        
((GridCacheDatabaseSharedManager)cctx.database()).addCheckpointListener(this);
+
+        if (log.isInfoEnabled()) {
+            log.info("Snapshot operation is scheduled on local node and will 
be handled by the checkpoint " +
+                "listener [sctx=" + this + ", topVer=" + 
cctx.discovery().topologyVersionEx() + ']');
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void beforeCheckpointBegin(Context ctx) {
+        if (stopping.getAsBoolean())
+            return;
+
+        // Gather partitions metainfo for thouse which will be copied.
+        ctx.collectPartStat(parts);
+
+        ctx.finishedStateFut().listen(f -> {
+            if (f.error() == null)
+                cpEndFut.complete(true);
+            else
+                cpEndFut.completeExceptionally(f.error());
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onMarkCheckpointBegin(Context ctx) {
+        // Write lock is helded. Partition counters has been collected under 
write lock
+        // in another checkpoint listeners.
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onMarkCheckpointEnd(Context ctx) {
+        if (stopping.getAsBoolean())
+            return;
+
+        // Under the write lock here. It's safe to add new stores.
+        try {
+            PartitionAllocationMap allocationMap = ctx.partitionStatMap();
+
+            allocationMap.prepareForSnapshot();
+
+            for (GroupPartitionId pair : parts) {
+                PagesAllocationRange allocRange = allocationMap.get(pair);
+
+                GridDhtLocalPartition part = pair.getPartitionId() == 
INDEX_PARTITION ? null :
+                    cctx.cache()
+                        .cacheGroup(pair.getGroupId())
+                        .topology()
+                        .localPartition(pair.getPartitionId());
+
+                // Partition can be reserved.
+                // Partition can be MOVING\RENTING states.
+                // Index partition will be excluded if not all partition 
OWNING.
+                // There is no data assigned to partition, thus it haven't 
been created yet.
+                assert allocRange != null || part == null || part.state() != 
GridDhtPartitionState.OWNING :
+                    "Partition counters has not been collected " +
+                        "[pair=" + pair + ", snpName=" + snpName + ", part=" + 
part + ']';
+
+                if (allocRange == null) {
+                    List<GroupPartitionId> missed = parts.stream()
+                        .filter(allocationMap::containsKey)
+                        .collect(Collectors.toList());
+
+                    acceptException(new IgniteCheckedException("Snapshot 
operation cancelled due to " +
+                        "not all of requested partitions has OWNING state 
[missed=" + missed + ']'));
+
+                    break;
+                }
+
+                PageStore store = 
((FilePageStoreManager)cctx.pageStore()).getStore(pair.getGroupId(), 
pair.getPartitionId());
+
+                partFileLengths.put(pair, store.size());
+                
partDeltaWriters.get(pair).init(allocRange.getCurrAllocatedPageCnt());
+            }
+        }
+        catch (IgniteCheckedException e) {
+            acceptException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onCheckpointBegin(Context ctx) {
+        if (stopping.getAsBoolean())
+            return;
+
+        // Snapshot task is now started since checkpoint writelock released.
+        if (!startedFut.onDone())
+            return;
+
+        // Submit all tasks for partitions and deltas processing.
+        List<CompletableFuture<Void>> futs = new ArrayList<>();
+        FilePageStoreManager storeMgr = (FilePageStoreManager)cctx.pageStore();
+
+        if (log.isInfoEnabled())
+            log.info("Submit partition processings tasks with partition 
allocated lengths: " + partFileLengths);
+
+        // Process binary meta.
+        futs.add(CompletableFuture.runAsync(
+            wrapExceptionIfStarted(() ->
+                    snpSndr.sendBinaryMeta(cctx.kernalContext()
+                        .cacheObjects()
+                        .metadata(Collections.emptyList()))),
 
 Review comment:
   Use `metadata()` method here, it's more lightweight.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to