Vladsz83 commented on code in PR #13666:
URL: https://github.com/apache/ignite/pull/13666#discussion_r4324398996


##########
modules/core/src/main/java/org/apache/ignite/internal/management/snapshot/SnapshotListTask.java:
##########
@@ -0,0 +1,452 @@
+/*
+ * 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.management.snapshot;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.internal.NodeStoppingException;
+import 
org.apache.ignite.internal.processors.cache.persistence.filename.SnapshotFileTree;
+import 
org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager;
+import 
org.apache.ignite.internal.processors.cache.persistence.snapshot.IncrementalSnapshotMetadata;
+import 
org.apache.ignite.internal.processors.cache.persistence.snapshot.SnapshotListJobResult;
+import 
org.apache.ignite.internal.processors.cache.persistence.snapshot.SnapshotListTaskResult;
+import 
org.apache.ignite.internal.processors.cache.persistence.snapshot.SnapshotMetadata;
+import 
org.apache.ignite.internal.processors.rollingupgrade.feature.CoreFeatureRegistry;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.thread.pool.IgniteThreadPoolExecutor;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.T2;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorMultiNodeTask;
+import org.apache.ignite.internal.visor.VisorTaskArgument;
+import org.apache.ignite.resources.LoggerResource;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.util.lang.ClusterNodeFunc.nodeIds;
+
+/** */
+@GridInternal
+public class SnapshotListTask extends 
VisorMultiNodeTask<SnapshotListCommandArg, SnapshotListTaskResult, 
SnapshotListJobResult> {
+    /** Serial version uid. */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    public static final String FEATURE_NOT_ACTIVE_ERR_MSG = "Won't search for 
local snapshots. " +
+        "The snapshot list feature isn't activated yet.";
+
+    /** {@inheritDoc} */
+    @Override protected VisorJob<SnapshotListCommandArg, 
SnapshotListJobResult> job(SnapshotListCommandArg arg) {
+        return new SnapshotListJob(arg, debug);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected Collection<UUID> 
jobNodes(VisorTaskArgument<SnapshotListCommandArg> arg) {
+        if 
(!ignite.context().rollingUpgrade().features().isActive(CoreFeatureRegistry.SNAPSHOT_LIST_FEATURE))
+            throw new IgniteException(FEATURE_NOT_ACTIVE_ERR_MSG);
+
+        /** Allows {@link #map0(List, VisorTaskArgument)} to use the entire 
grid. */
+        return nodeIds(ignite.cluster().forServers().nodes());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected SnapshotListTaskResult reduce0(List<ComputeJobResult> 
nodesJobsResults) throws IgniteException {
+        String[] cstIds = new String[nodesJobsResults.size()];
+        UUID[] nodesIds = new UUID[nodesJobsResults.size()];
+        SnapshotListJobResult[] nodesResults = new 
SnapshotListJobResult[nodesJobsResults.size()];
+
+        for (int i = 0; i < nodesJobsResults.size(); i++) {
+            ComputeJobResult nodeJobRes = nodesJobsResults.get(i);
+
+            if (nodeJobRes.getException() != null) {
+                throw new IgniteException("Failed to execute snapshot list job 
on node [uuid=" + nodeJobRes.getNode().id() + ']',
+                    nodeJobRes.getException());
+            }
+
+            assert nodeJobRes.getData() != null;
+
+            cstIds[i] = nodeConsistentId(nodeJobRes.getNode());
+            nodesIds[i] = nodeJobRes.getNode().id();
+            nodesResults[i] = nodeJobRes.getData();
+        }
+
+        return new SnapshotListTaskResult(cstIds, nodesIds, nodesResults);
+    }
+
+    /** */
+    private String nodeConsistentId(ClusterNode n) {
+        UUID nodeId = n.id();
+
+        n = ignite.context().discovery().node(nodeId);
+
+        if (n == null)
+            n = ignite.context().discovery().historicalNode(nodeId);
+
+        return n == null ? "" : n.consistentId().toString();
+    }
+
+    /**
+     * Walk through a directory. Doesn't lock it or its content. Tries to find 
files and summarize their size.
+     * Tolerates and skips concurrent modification errors.
+     */
+    public static long calculateDirectorySize(File path) throws IOException {
+        AtomicLong size = new AtomicLong(0);
+        AtomicBoolean entered = new AtomicBoolean();
+
+        Files.walkFileTree(path.toPath(), new SimpleFileVisitor<>() {
+            @Override public FileVisitResult visitFile(Path file, 
BasicFileAttributes attrs) {
+                entered.compareAndSet(false, true);
+
+                // Use attrs instead of Files.size() for efficiency.
+                if (attrs.isRegularFile())
+                    size.addAndGet(attrs.size());
+
+                return FileVisitResult.CONTINUE;
+            }
+
+            @Override public FileVisitResult visitFileFailed(Path file, 
IOException err) throws IOException {
+                if (!entered.get() && file.toFile().equals(path)) {
+                    // Cannot even start snapshot size calculation - can't 
enter snapshot directory.
+                    throw err;
+                }
+
+                return FileVisitResult.CONTINUE;
+            }
+        });
+
+        return size.get();
+    }
+
+    /** */
+    private static class SnapshotListJob extends 
SnapshotJob<SnapshotListCommandArg, SnapshotListJobResult> {
+        /** Serial version uid. */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        @LoggerResource
+        private IgniteLogger log;
+
+        /**
+         * @param arg Snapshot list task argument.
+         * @param debug Flag indicating whether debug information should be 
printed into node log.
+         */
+        protected SnapshotListJob(SnapshotListCommandArg arg, boolean debug) {
+            super(arg, debug);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected SnapshotListJobResult run(SnapshotListCommandArg 
arg) {
+            assert !ignite.localNode().isClient();
+
+            if (ignite.context().isStopping())
+                throw new IgniteException("Won't search for local snapshots.", 
new NodeStoppingException("Node is stopping."));
+
+            // Read local snapshots.
+            List<T2<SnapshotFileTree, Long>> locSnps = 
findLocalSnapshots(arg.src());
+
+            if (locSnps.isEmpty())
+                return new SnapshotListJobResult(Collections.emptyMap());
+
+            // Main sizes of the local snapshots (per snapshot name).
+            Map<String, Long> sizes = new ConcurrentHashMap<>(locSnps.size(), 
1.0f);
+            // Optional incremental parts descriptions (per snapshot name).
+            Map<String, SnapshotListJobResult.SnapshotInfo> incs = new 
ConcurrentHashMap<>(locSnps.size(), 1.0f);
+            // Optional external storages descriptions (per snapshot name).
+            Map<String, SnapshotListJobResult.SnapshotInfo> extStors = new 
ConcurrentHashMap<>(locSnps.size(), 1.0f);
+
+            // Snapshot sizes, incremental parts and external storages futures.
+            List<Future<?>> futs = new ArrayList<>(locSnps.size() * 3);
+            // Names of snapshots with read failures.
+            Set<String> failedSnps = 
ConcurrentHashMap.newKeySet(locSnps.size() / 2);
+
+            IgniteThreadPoolExecutor exec = 
ignite.context().pools().getSnapshotExecutorService();
+
+            // Descriptions of the local snapshots: main sizes, external 
storages and incremental parts.
+            for (T2<SnapshotFileTree, Long> snpPair : locSnps) {
+                SnapshotFileTree sft = snpPair.get1();
+                String snpName = sft.name();
+
+                // Future for the snapshot main size. It is the largest part 
of the job, thus it goes first.
+                Future<?> fut = exec.submit(() -> {
+                    if (ignite.context().isStopping())
+                        throw new IgniteException("Won't search for local 
snapshots.", new NodeStoppingException("Node is stopping."));
+
+                    if (failedSnps.contains(snpName))
+                        return;
+
+                    try {
+                        sizes.put(snpName, calculateDirectorySize(sft.root()));
+                    }
+                    catch (Exception e) {
+                        failedSnps.add(snpName);
+
+                        log.warning("Failed to calculate snapshot's size, 
snapshot ignored [snpName='" + snpName + "']", e);
+                    }
+                });
+
+                futs.add(fut);
+
+                // Future for optional incremental parts.
+                fut = exec.submit(() -> {
+                    if (ignite.context().isStopping())
+                        throw new IgniteException("Won't search for local 
snapshots.", new NodeStoppingException("Node is stopping."));
+
+                    if (failedSnps.contains(snpName))
+                        return;
+
+                    try {
+                        SnapshotListJobResult.SnapshotInfo incDesc = 
incrementals(sft);
+
+                        if (incDesc != null)
+                            incs.put(snpName, incDesc);
+                    }
+                    catch (Exception e) {
+                        failedSnps.add(snpName);
+
+                        log.warning("Failed to read snapshot's incremental 
parts, snapshot ignored [snpName='" + snpName + "']", e);
+                    }
+                });
+
+                futs.add(fut);
+
+                // Future for optional external storages.
+                fut = exec.submit(() -> {
+                    if (ignite.context().isStopping())
+                        throw new IgniteException("Won't search for local 
snapshots.", new NodeStoppingException("Node is stopping."));
+
+                    if (failedSnps.contains(snpName))
+                        return;
+
+                    try {
+                        SnapshotListJobResult.SnapshotInfo extDesc = 
externalStorages(sft);
+
+                        if (extDesc != null)
+                            extStors.put(snpName, extDesc);
+                    }
+                    catch (Exception e) {
+                        failedSnps.add(snpName);
+
+                        log.warning("Failed to read snapshot's external 
storages, snapshot ignored [snpName='" + snpName + "']", e);
+                    }
+                });
+
+                futs.add(fut);
+            }
+
+            // Wait for the futures.
+            for (Future<?> fut : futs) {
+                try {
+                    fut.get();
+                }
+                catch (ExecutionException e) {
+                    throw new IgniteException("Failed to read local node's 
snapshots.", e);
+                }
+                catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+
+                    throw new IgniteException("Interrupted while reading local 
snapshots.", e);
+                }
+            }
+
+            // Result snapshot descriptions.
+            Map<String, SnapshotListJobResult.SnapshotInfo> resMap = new 
ConcurrentHashMap<>(locSnps.size() - failedSnps.size(), 1.0f);

Review Comment:
   Done.



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