From 6bdab3723eff78c79aa48c24aad87373b983fe6c Mon Sep 17 00:00:00 2001 From: Ayush Saxena Date: Thu, 30 Apr 2020 19:55:20 +0530 Subject: [PATCH] HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena. --- .../src/main/java/org/apache/hadoop/net/NodeBase.java | 8 +++++++- .../java/org/apache/hadoop/net/TestClusterTopology.java | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java index 9da9ca2948..cc14df8519 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java @@ -20,6 +20,8 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; +import java.util.regex.Pattern; + /** A base class that implements interface Node * */ @@ -38,6 +40,7 @@ public class NodeBase implements Node { protected String location; //string representation of this node's location protected int level; //which level of the tree the node resides protected Node parent; //its parent + private static final Pattern SLASHES = Pattern.compile("/+"); /** Default constructor */ public NodeBase() { @@ -160,12 +163,15 @@ public static String normalize(String path) { if (path.length() == 0) { return ROOT; } - + if (path.charAt(0) != PATH_SEPARATOR) { throw new IllegalArgumentException( "Network Location path does not start with " +PATH_SEPARATOR_STR+ ": "+path); } + + // Remove duplicated slashes. + path = SLASHES.matcher(path).replaceAll("/"); int len = path.length(); if (path.charAt(len-1) == PATH_SEPARATOR) { diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java index 6d9dc7739e..328cf11c20 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java @@ -234,6 +234,15 @@ public void testChooseRandomExcluded() { assertSame("node3", node.getName()); } + @Test + public void testNodeBaseNormalizeRemoveLeadingSlash() { + assertEquals("/d1", NodeBase.normalize("/d1///")); + assertEquals("/d1", NodeBase.normalize("/d1/")); + assertEquals("/d1", NodeBase.normalize("/d1")); + assertEquals("", NodeBase.normalize("///")); + assertEquals("", NodeBase.normalize("/")); + } + private NodeElement getNewNode(String name, String rackLocation) { NodeElement node = new NodeElement(name); node.setNetworkLocation(rackLocation);