-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
HDFS-15520 Use visitor pattern to visit namespace tree #2203
Merged
Merged
Changes from all commits
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 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 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 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 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 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 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 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
227 changes: 227 additions & 0 deletions
227
...s/src/main/java/org/apache/hadoop/hdfs/server/namenode/visitor/NamespacePrintVisitor.java
This file contains 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,227 @@ | ||
/* | ||
* 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.FSNamesystem; | ||
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.DirectoryWithSnapshotFeature.DirectoryDiff; | ||
import org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature; | ||
import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
/** | ||
* To print the namespace 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 = "\\-"; | ||
|
||
/** Print the tree from the given root to a {@link File}. */ | ||
public static void print2File(INode root, File f) throws IOException { | ||
try(final PrintWriter out = new PrintWriter(new FileWriter(f), true)) { | ||
new NamespacePrintVisitor(out).print(root); | ||
} | ||
} | ||
|
||
/** @return string of the tree in the given {@link FSNamesystem}. */ | ||
public static String print2Sting(FSNamesystem ns) { | ||
return print2Sting(ns.getFSDirectory().getRoot()); | ||
} | ||
|
||
/** @return string of the tree from the given root. */ | ||
public static String print2Sting(INode root) { | ||
final StringWriter out = new StringWriter(); | ||
new NamespacePrintVisitor(new PrintWriter(out)).print(root); | ||
return out.getBuffer().toString(); | ||
} | ||
|
||
/** | ||
* Print the tree in the given {@link FSNamesystem} | ||
* to the given {@link PrintStream}. | ||
*/ | ||
public static void print(FSNamesystem ns, PrintStream out) { | ||
new NamespacePrintVisitor(new PrintWriter(out)).print(ns); | ||
} | ||
|
||
private final PrintWriter out; | ||
private final StringBuffer prefix = new StringBuffer(); | ||
|
||
private NamespacePrintVisitor(PrintWriter out) { | ||
this.out = out; | ||
} | ||
|
||
private void print(FSNamesystem namesystem) { | ||
print(namesystem.getFSDirectory().getRoot()); | ||
} | ||
|
||
private void print(INode root) { | ||
root.accept(this, Snapshot.CURRENT_STATE_ID); | ||
} | ||
|
||
private void printINode(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 visitFile(INodeFile file, int snapshot) { | ||
printINode(file, snapshot); | ||
|
||
out.print(", fileSize=" + file.computeFileSize(snapshot)); | ||
// print only the first block, if it exists | ||
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 visitSymlink(INodeSymlink symlink, int snapshot) { | ||
printINode(symlink, snapshot); | ||
out.print(" ~> "); | ||
out.println(symlink.getSymlinkString()); | ||
} | ||
|
||
@Override | ||
public void visitReference(INodeReference ref, int snapshot) { | ||
printINode(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(); | ||
} | ||
|
||
@Override | ||
public void preVisitReferred(INode referred) { | ||
prefix.setLength(prefix.length() - 2); | ||
prefix.append(" ->"); | ||
} | ||
|
||
@Override | ||
public void postVisitReferred(INode referred) { | ||
prefix.setLength(prefix.length() - 2); | ||
} | ||
|
||
@Override | ||
public void visitDirectory(INodeDirectory dir, int snapshot) { | ||
printINode(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 visitSnapshottable(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(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 preVisitSub(Element sub, int index, boolean isLast) { | ||
prefix.append(isLast? LAST_ITEM : NON_LAST_ITEM); | ||
} | ||
|
||
@Override | ||
public void postVisitSub(Element sub, 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.