Vladsz83 commented on code in PR #13666: URL: https://github.com/apache/ignite/pull/13666#discussion_r4324377652
########## 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.")); Review Comment: Normally, we throw NodeStoppingException when the local node is stopping. Unfortunately, it extends IgniteCheckedException, which I dislike, so sometimes it needs to be wrapped. I unified a bit, see `checkNodeIsStopping()`. -- 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]
