Repository navigation
Backport the recursive snapshot summary tool #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e8661df
Backport the native snapshot size comparer
arkmish 0570e41
Backport the recursive snapshot summary tool
arkmish 4f87fa4
Merge branch-3.6 into snapshot summary
arkmish 7c049ff
Merge branch 'branch-3.6' into armishra/zk-snapshot-summary
arkmish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| @echo off | ||
| REM Licensed to the Apache Software Foundation (ASF) under one or more | ||
| REM contributor license agreements. See the NOTICE file distributed with | ||
| REM this work for additional information regarding copyright ownership. | ||
| REM The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| REM (the "License"); you may not use this file except in compliance with | ||
| REM the License. You may obtain a copy of the License at | ||
| REM | ||
| REM http://www.apache.org/licenses/LICENSE-2.0 | ||
| REM | ||
| REM Unless required by applicable law or agreed to in writing, software | ||
| REM distributed under the License is distributed on an "AS IS" BASIS, | ||
| REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| REM See the License for the specific language governing permissions and | ||
| REM limitations under the License. | ||
|
|
||
| setlocal | ||
| call "%~dp0zkEnv.cmd" | ||
|
|
||
| set ZOOMAIN=org.apache.zookeeper.server.SnapshotRecursiveSummary | ||
| call %JAVA% -cp "%CLASSPATH%" %ZOOMAIN% %* | ||
|
|
||
| endlocal & exit /b %ERRORLEVEL% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # 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. | ||
|
|
||
| ZOOBIN="${BASH_SOURCE-$0}" | ||
| ZOOBIN="$(dirname "${ZOOBIN}")" | ||
| ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" | ||
|
|
||
| if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then | ||
| . "$ZOOBINDIR"/../libexec/zkEnv.sh | ||
| else | ||
| . "$ZOOBINDIR"/zkEnv.sh | ||
| fi | ||
|
|
||
| "$JAVA" -cp "$CLASSPATH" $JVMFLAGS \ | ||
| org.apache.zookeeper.server.SnapshotRecursiveSummary "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotRecursiveSummary.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * 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.zookeeper.server; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
| import org.apache.yetus.audience.InterfaceAudience; | ||
| import org.apache.zookeeper.ZKUtil; | ||
| import org.apache.zookeeper.common.PathUtils; | ||
| import org.apache.zookeeper.util.ServiceUtils; | ||
|
|
||
| /** | ||
| * Recursively summarizes snapshot subtree data sizes and descendant counts. | ||
| * Only non-leaf nodes are printed, but totals include all descendants and ephemeral nodes. | ||
| * The maximum depth limits output, not traversal; zero means unlimited output depth. | ||
| * | ||
| * <p>Backported from Apache ZooKeeper commit 05b215994f5e145c2758c4089828b57ba471b329 | ||
| * (ZOOKEEPER-4566). | ||
| */ | ||
| @InterfaceAudience.Public | ||
| public class SnapshotRecursiveSummary { | ||
|
|
||
| public static void main(String[] args) { | ||
| if (args.length != 3) { | ||
| System.err.println(getUsage()); | ||
| ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); | ||
| return; | ||
| } | ||
| try { | ||
| new SnapshotRecursiveSummary().run(args[0], args[1], Integer.parseInt(args[2])); | ||
| } catch (IllegalArgumentException e) { | ||
| System.err.println(e.getMessage()); | ||
| System.err.println(getUsage()); | ||
| ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); | ||
| } catch (IOException e) { | ||
| System.err.println("Unable to read snapshot: " + e.getMessage()); | ||
| ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); | ||
| } | ||
| } | ||
|
|
||
| public void run(String snapshotFileName, String startingNode, int maxDepth) throws IOException { | ||
| PathUtils.validatePath(startingNode); | ||
| if (maxDepth < 0) { | ||
| throw new IllegalArgumentException("max_depth must be a non-negative integer."); | ||
| } | ||
| String error = ZKUtil.validateFileInput(snapshotFileName); | ||
| if (error != null) { | ||
| throw new IllegalArgumentException(error); | ||
| } | ||
| DataTree dataTree = SnapshotComparer.getSnapshot(new File(snapshotFileName)); | ||
| if (dataTree.getNode(startingNode) == null) { | ||
| throw new IllegalArgumentException("Starting node does not exist: " + startingNode); | ||
| } | ||
| StringBuilder builder = new StringBuilder(); | ||
| printZnode(dataTree, startingNode, builder, 0, maxDepth); | ||
| System.out.println(builder); | ||
| } | ||
|
|
||
| private long[] printZnode(DataTree dataTree, String name, StringBuilder builder, int level, int maxDepth) { | ||
| DataNode node = dataTree.getNode(name); | ||
| Set<String> children; | ||
| long dataSize; | ||
| synchronized (node) { | ||
| dataSize = node.data == null ? 0 : node.data.length; | ||
| children = new TreeSet<>(node.getChildren()); | ||
| } | ||
| long[] result = {1L, dataSize}; | ||
| if (children.isEmpty()) { | ||
| return result; | ||
| } | ||
| StringBuilder childBuilder = new StringBuilder(); | ||
| for (String child : children) { | ||
| long[] childResult = printZnode(dataTree, name + (name.equals("/") ? "" : "/") + child, | ||
| childBuilder, level + 1, maxDepth); | ||
| result[0] += childResult[0]; | ||
| result[1] += childResult[1]; | ||
| } | ||
| if (maxDepth == 0 || level <= maxDepth) { | ||
| String indent = String.join("", Collections.nCopies(level, "--")); | ||
| builder.append(indent).append(" ").append(name).append("\n"); | ||
| builder.append(indent).append(" children: ").append(result[0] - 1).append("\n"); | ||
| builder.append(indent).append(" data: ").append(result[1]).append("\n"); | ||
| builder.append(childBuilder); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static String getUsage() { | ||
| String newLine = System.lineSeparator(); | ||
| return String.join(newLine, | ||
| "USAGE:", | ||
| "", | ||
| "SnapshotRecursiveSummary <snapshot_file> <starting_node> <max_depth>", | ||
| "", | ||
| "snapshot_file: path to the zookeeper snapshot", | ||
| "starting_node: the absolute path in the zookeeper tree where traversal should begin", | ||
| "max_depth: non-negative output depth. 0 displays every non-leaf node; " | ||
| + "1 displays the starting node and its non-leaf children; 2 adds another level, and so on. " | ||
| + "This ONLY affects the level of details displayed, NOT the calculation."); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.