Skip to content
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 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.hadoop.hdfs.server.namenode.INodeReference.WithCount;
import org.apache.hadoop.hdfs.server.namenode.INodeReference.WithName;
import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot;
import org.apache.hadoop.hdfs.server.namenode.visitor.NamespaceVisitor;
import org.apache.hadoop.hdfs.util.Diff;
import org.apache.hadoop.security.AccessControlException;
import org.apache.hadoop.util.ChunkedArrayList;
Expand Down Expand Up @@ -76,7 +77,7 @@ final boolean isRoot() {
}

/** Get the {@link PermissionStatus} */
abstract PermissionStatus getPermissionStatus(int snapshotId);
public abstract PermissionStatus getPermissionStatus(int snapshotId);

/** The same as getPermissionStatus(null). */
final PermissionStatus getPermissionStatus() {
Expand Down Expand Up @@ -1123,6 +1124,14 @@ public void clear() {
}
}

/** Accept a visitor to visit this {@link INode}. */
public void accept(NamespaceVisitor visitor, int snapshot) {
final Class<?> clazz = visitor != null? visitor.getClass()
: NamespaceVisitor.class;
throw new UnsupportedOperationException(getClass().getSimpleName()
+ " does not support " + clazz.getSimpleName());
}

/**
* INode feature such as {@link FileUnderConstructionFeature}
* and {@link DirectoryWithQuotaFeature}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.hadoop.hdfs.protocol.SnapshotException;
import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite;
import org.apache.hadoop.hdfs.server.namenode.INodeReference.WithCount;
import org.apache.hadoop.hdfs.server.namenode.visitor.NamespaceVisitor;
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.DirectoryDiffList;
Expand Down Expand Up @@ -996,6 +997,11 @@ public SnapshotAndINode(int snapshot, INode inode) {
}
}

@Override
public void accept(NamespaceVisitor visitor, int snapshot) {
visitor.visitDirectoryRecursively(this, snapshot);
}

public final int getChildrenNum(final int snapshotId) {
return getChildrenList(snapshotId).size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature;
import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot;
import org.apache.hadoop.hdfs.server.namenode.snapshot.DiffList;
import org.apache.hadoop.hdfs.server.namenode.visitor.NamespaceVisitor;
import org.apache.hadoop.hdfs.util.LongBitFormat;
import org.apache.hadoop.util.StringUtils;
import static org.apache.hadoop.io.erasurecode.ErasureCodeConstants.REPLICATION_POLICY_ID;
Expand Down Expand Up @@ -1111,6 +1112,11 @@ public void dumpTreeRecursively(PrintWriter out, StringBuilder prefix,
out.println();
}

@Override
public void accept(NamespaceVisitor visitor, int snapshot) {
visitor.visitFile(this, snapshot);
}

/**
* Remove full blocks at the end file up to newLength
* @return sum of sizes of the remained blocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot;

import com.google.common.base.Preconditions;
import org.apache.hadoop.hdfs.server.namenode.visitor.NamespaceVisitor;
import org.apache.hadoop.security.AccessControlException;

/**
Expand Down Expand Up @@ -368,7 +369,12 @@ public void dumpTreeRecursively(PrintWriter out, StringBuilder prefix,
b.append("->");
getReferredINode().dumpTreeRecursively(out, b, snapshot);
}


@Override
public void accept(NamespaceVisitor visitor, int snapshot) {
visitor.visitReferenceRecursively(this, snapshot);
}

public int getDstSnapshotId() {
return Snapshot.CURRENT_STATE_ID;
}
Expand Down Expand Up @@ -399,7 +405,7 @@ public WithCount(INodeReference parent, INode referred) {
INodeReferenceValidation.add(this, WithCount.class);
}

private String getCountDetails() {
public String getCountDetails() {
final StringBuilder b = new StringBuilder("[");
if (!withNameList.isEmpty()) {
final Iterator<WithName> i = withNameList.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.hadoop.hdfs.DFSUtil;
import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite;
import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot;
import org.apache.hadoop.hdfs.server.namenode.visitor.NamespaceVisitor;

/**
* An {@link INode} representing a symbolic link.
Expand Down Expand Up @@ -104,7 +105,13 @@ public ContentSummaryComputationContext computeContentSummary(int snapshotId,
public void dumpTreeRecursively(PrintWriter out, StringBuilder prefix,
final int snapshot) {
super.dumpTreeRecursively(out, prefix, snapshot);
out.println();
out.print(" ~> ");
out.println(getSymlinkString());
}

@Override
public void accept(NamespaceVisitor visitor, int snapshot) {
visitor.visitSymlink(this, snapshot);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ final void clonePermissionStatus(INodeWithAdditionalFields that) {
}

@Override
final PermissionStatus getPermissionStatus(int snapshotId) {
public final PermissionStatus getPermissionStatus(int snapshotId) {
return new PermissionStatus(getUserName(snapshotId), getGroupName(snapshotId),
getFsPermission(snapshotId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void setSnapshotRoot(INodeDirectoryAttributes root) {
this.isSnapshotRoot = true;
}

boolean isSnapshotRoot() {
public boolean isSnapshotRoot() {
return isSnapshotRoot;
}

Expand Down
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 = "+-";
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

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);
}
}
Loading