-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HDFS-15520 Use visitor pattern to visit namespace tree #2203
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
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
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
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
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
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
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
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
179 changes: 179 additions & 0 deletions
179
...s/src/main/java/org/apache/hadoop/hdfs/server/namenode/visitor/NamespacePrintVisitor.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,179 @@ | ||
| /* | ||
| * 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.hadoop.hdfs.server.namenode.visitor; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo; | ||
| import org.apache.hadoop.hdfs.server.namenode.DirectoryWithQuotaFeature; | ||
| import org.apache.hadoop.hdfs.server.namenode.INode; | ||
| import org.apache.hadoop.hdfs.server.namenode.INodeDirectory; | ||
| import org.apache.hadoop.hdfs.server.namenode.INodeFile; | ||
| import org.apache.hadoop.hdfs.server.namenode.INodeReference; | ||
| import org.apache.hadoop.hdfs.server.namenode.INodeSymlink; | ||
| import org.apache.hadoop.hdfs.server.namenode.snapshot.DirectorySnapshottableFeature; | ||
| import org.apache.hadoop.hdfs.server.namenode.snapshot.DirectoryWithSnapshotFeature; | ||
| import org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature; | ||
| import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; | ||
|
|
||
| import java.io.PrintWriter; | ||
|
|
||
| /** | ||
| * To print the tree recursively for testing. | ||
| * | ||
| * \- foo (INodeDirectory@33dd2717) | ||
| * \- sub1 (INodeDirectory@442172) | ||
| * +- file1 (INodeFile@78392d4) | ||
| * +- file2 (INodeFile@78392d5) | ||
| * +- sub11 (INodeDirectory@8400cff) | ||
| * \- file3 (INodeFile@78392d6) | ||
| * \- z_file4 (INodeFile@45848712) | ||
| */ | ||
| public class NamespacePrintVisitor implements NamespaceVisitor { | ||
| static final String NON_LAST_ITEM = "+-"; | ||
| static final String LAST_ITEM = "\\-"; | ||
|
|
||
| private final PrintWriter out; | ||
| private final StringBuffer prefix = new StringBuffer(); | ||
|
|
||
| public NamespacePrintVisitor(PrintWriter out) { | ||
| this.out = out; | ||
| } | ||
|
|
||
| void print(INode iNode, int snapshot) { | ||
| out.print(prefix); | ||
| out.print(" "); | ||
| final String name = iNode.getLocalName(); | ||
| out.print(name != null && name.isEmpty()? "/": name); | ||
| out.print(" ("); | ||
| out.print(iNode.getObjectString()); | ||
| out.print("), "); | ||
| out.print(iNode.getParentString()); | ||
| out.print(", " + iNode.getPermissionStatus(snapshot)); | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(INodeFile file, int snapshot) { | ||
| print(file, snapshot); | ||
|
|
||
| out.print(", fileSize=" + file.computeFileSize(snapshot)); | ||
| // only compare the first block | ||
| out.print(", blocks="); | ||
| final BlockInfo[] blocks = file.getBlocks(); | ||
| out.print(blocks.length == 0 ? null: blocks[0]); | ||
| out.println(); | ||
|
|
||
| final FileWithSnapshotFeature snapshotFeature | ||
| = file.getFileWithSnapshotFeature(); | ||
| if (snapshotFeature != null) { | ||
| if (prefix.length() >= 2) { | ||
| prefix.setLength(prefix.length() - 2); | ||
| prefix.append(" "); | ||
| } | ||
| out.print(prefix); | ||
| out.print(snapshotFeature); | ||
| } | ||
| out.println(); | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(INodeSymlink symlink, int snapshot) { | ||
| print(symlink, snapshot); | ||
| out.print(" ~> "); | ||
| out.println(symlink.getSymlinkString()); | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(INodeReference ref, int snapshot) { | ||
| print(ref, snapshot); | ||
|
|
||
| if (ref instanceof INodeReference.DstReference) { | ||
| out.print(", dstSnapshotId=" + ref.getDstSnapshotId()); | ||
| } else if (ref instanceof INodeReference.WithCount) { | ||
| out.print(", " + ((INodeReference.WithCount)ref).getCountDetails()); | ||
| } | ||
| out.println(); | ||
|
|
||
| prefix.setLength(prefix.length() - 2); | ||
| prefix.append(" ->"); | ||
| ref.getReferredINode().accept(this, snapshot); | ||
| prefix.setLength(prefix.length() - 2); | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(INodeDirectory dir, int snapshot) { | ||
| print(dir, snapshot); | ||
|
|
||
| out.print(", childrenSize=" + dir.getChildrenList(snapshot).size()); | ||
| final DirectoryWithQuotaFeature q = dir.getDirectoryWithQuotaFeature(); | ||
| if (q != null) { | ||
| out.print(", " + q); | ||
| } | ||
| if (dir instanceof Snapshot.Root) { | ||
| out.print(", snapshotId=" + snapshot); | ||
| } | ||
| out.println(); | ||
|
|
||
| if (prefix.length() >= 2) { | ||
| prefix.setLength(prefix.length() - 2); | ||
| prefix.append(" "); | ||
| } | ||
|
|
||
| final DirectoryWithSnapshotFeature snapshotFeature | ||
| = dir.getDirectoryWithSnapshotFeature(); | ||
| if (snapshotFeature != null) { | ||
| out.print(prefix); | ||
| out.print(snapshotFeature); | ||
| } | ||
| out.println(); | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(INodeDirectory dir, | ||
| DirectorySnapshottableFeature snapshottable) { | ||
| out.println(); | ||
| out.print(prefix); | ||
|
|
||
| out.print("Snapshot of "); | ||
| final String name = dir.getLocalName(); | ||
| out.print(name != null && name.isEmpty()? "/": name); | ||
| out.print(": quota="); | ||
| out.print(snapshottable.getSnapshotQuota()); | ||
|
|
||
| int n = 0; | ||
| for(DirectoryWithSnapshotFeature.DirectoryDiff diff : snapshottable.getDiffs()) { | ||
| if (diff.isSnapshotRoot()) { | ||
| n++; | ||
| } | ||
| } | ||
| final int numSnapshots = snapshottable.getNumSnapshots(); | ||
| Preconditions.checkState(n == numSnapshots, | ||
| "numSnapshots = " + numSnapshots + " != " + n); | ||
| out.print(", #snapshot="); | ||
| out.println(n); | ||
| } | ||
|
|
||
| @Override | ||
| public void preVisitNextLevel(int index, boolean isLast) { | ||
| prefix.append(isLast? LAST_ITEM : NON_LAST_ITEM); | ||
| } | ||
|
|
||
| @Override | ||
| public void postVisitNextLevel(int index, boolean isLast) { | ||
| prefix.setLength(prefix.length() - 2); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have these constants also in InodeDirectory , can we move these constants into a common place ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will remove the constants in INodeDirectory with HDFS-15521.