|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.zookeeper.server; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.TreeSet; |
| 26 | +import org.apache.yetus.audience.InterfaceAudience; |
| 27 | +import org.apache.zookeeper.ZKUtil; |
| 28 | +import org.apache.zookeeper.common.PathUtils; |
| 29 | +import org.apache.zookeeper.util.ServiceUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * Recursively summarizes snapshot subtree data sizes and descendant counts. |
| 33 | + * Only non-leaf nodes are printed, but totals include all descendants and ephemeral nodes. |
| 34 | + * The maximum depth limits output, not traversal; zero means unlimited output depth. |
| 35 | + * |
| 36 | + * <p>Backported from Apache ZooKeeper commit 05b215994f5e145c2758c4089828b57ba471b329 |
| 37 | + * (ZOOKEEPER-4566). |
| 38 | + */ |
| 39 | +@InterfaceAudience.Public |
| 40 | +public class SnapshotRecursiveSummary { |
| 41 | + |
| 42 | + public static void main(String[] args) { |
| 43 | + if (args.length != 3) { |
| 44 | + System.err.println(getUsage()); |
| 45 | + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); |
| 46 | + return; |
| 47 | + } |
| 48 | + try { |
| 49 | + new SnapshotRecursiveSummary().run(args[0], args[1], Integer.parseInt(args[2])); |
| 50 | + } catch (IllegalArgumentException e) { |
| 51 | + System.err.println(e.getMessage()); |
| 52 | + System.err.println(getUsage()); |
| 53 | + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); |
| 54 | + } catch (IOException e) { |
| 55 | + System.err.println("Unable to read snapshot: " + e.getMessage()); |
| 56 | + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void run(String snapshotFileName, String startingNode, int maxDepth) throws IOException { |
| 61 | + PathUtils.validatePath(startingNode); |
| 62 | + if (maxDepth < 0) { |
| 63 | + throw new IllegalArgumentException("max_depth must be a non-negative integer."); |
| 64 | + } |
| 65 | + String error = ZKUtil.validateFileInput(snapshotFileName); |
| 66 | + if (error != null) { |
| 67 | + throw new IllegalArgumentException(error); |
| 68 | + } |
| 69 | + DataTree dataTree = SnapshotComparer.getSnapshot(new File(snapshotFileName)); |
| 70 | + if (dataTree.getNode(startingNode) == null) { |
| 71 | + throw new IllegalArgumentException("Starting node does not exist: " + startingNode); |
| 72 | + } |
| 73 | + StringBuilder builder = new StringBuilder(); |
| 74 | + printZnode(dataTree, startingNode, builder, 0, maxDepth); |
| 75 | + System.out.println(builder); |
| 76 | + } |
| 77 | + |
| 78 | + private long[] printZnode(DataTree dataTree, String name, StringBuilder builder, int level, int maxDepth) { |
| 79 | + DataNode node = dataTree.getNode(name); |
| 80 | + Set<String> children; |
| 81 | + long dataSize; |
| 82 | + synchronized (node) { |
| 83 | + dataSize = node.data == null ? 0 : node.data.length; |
| 84 | + children = new TreeSet<>(node.getChildren()); |
| 85 | + } |
| 86 | + long[] result = {1L, dataSize}; |
| 87 | + if (children.isEmpty()) { |
| 88 | + return result; |
| 89 | + } |
| 90 | + StringBuilder childBuilder = new StringBuilder(); |
| 91 | + for (String child : children) { |
| 92 | + long[] childResult = printZnode(dataTree, name + (name.equals("/") ? "" : "/") + child, |
| 93 | + childBuilder, level + 1, maxDepth); |
| 94 | + result[0] += childResult[0]; |
| 95 | + result[1] += childResult[1]; |
| 96 | + } |
| 97 | + if (maxDepth == 0 || level <= maxDepth) { |
| 98 | + String indent = String.join("", Collections.nCopies(level, "--")); |
| 99 | + builder.append(indent).append(" ").append(name).append("\n"); |
| 100 | + builder.append(indent).append(" children: ").append(result[0] - 1).append("\n"); |
| 101 | + builder.append(indent).append(" data: ").append(result[1]).append("\n"); |
| 102 | + builder.append(childBuilder); |
| 103 | + } |
| 104 | + return result; |
| 105 | + } |
| 106 | + |
| 107 | + public static String getUsage() { |
| 108 | + String newLine = System.lineSeparator(); |
| 109 | + return String.join(newLine, |
| 110 | + "USAGE:", |
| 111 | + "", |
| 112 | + "SnapshotRecursiveSummary <snapshot_file> <starting_node> <max_depth>", |
| 113 | + "", |
| 114 | + "snapshot_file: path to the zookeeper snapshot", |
| 115 | + "starting_node: the absolute path in the zookeeper tree where traversal should begin", |
| 116 | + "max_depth: non-negative output depth. 0 displays every non-leaf node; " |
| 117 | + + "1 displays the starting node and its non-leaf children; 2 adds another level, and so on. " |
| 118 | + + "This ONLY affects the level of details displayed, NOT the calculation."); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments