Skip to content

Commit 6bdab37

Browse files
committed
HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena.
1 parent b5b45c5 commit 6bdab37

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NodeBase.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.apache.hadoop.classification.InterfaceAudience;
2121
import org.apache.hadoop.classification.InterfaceStability;
2222

23+
import java.util.regex.Pattern;
24+
2325
/** A base class that implements interface Node
2426
*
2527
*/
@@ -38,6 +40,7 @@ public class NodeBase implements Node {
3840
protected String location; //string representation of this node's location
3941
protected int level; //which level of the tree the node resides
4042
protected Node parent; //its parent
43+
private static final Pattern SLASHES = Pattern.compile("/+");
4144

4245
/** Default constructor */
4346
public NodeBase() {
@@ -160,12 +163,15 @@ public static String normalize(String path) {
160163
if (path.length() == 0) {
161164
return ROOT;
162165
}
163-
166+
164167
if (path.charAt(0) != PATH_SEPARATOR) {
165168
throw new IllegalArgumentException(
166169
"Network Location path does not start with "
167170
+PATH_SEPARATOR_STR+ ": "+path);
168171
}
172+
173+
// Remove duplicated slashes.
174+
path = SLASHES.matcher(path).replaceAll("/");
169175

170176
int len = path.length();
171177
if (path.charAt(len-1) == PATH_SEPARATOR) {

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestClusterTopology.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ public void testChooseRandomExcluded() {
234234
assertSame("node3", node.getName());
235235
}
236236

237+
@Test
238+
public void testNodeBaseNormalizeRemoveLeadingSlash() {
239+
assertEquals("/d1", NodeBase.normalize("/d1///"));
240+
assertEquals("/d1", NodeBase.normalize("/d1/"));
241+
assertEquals("/d1", NodeBase.normalize("/d1"));
242+
assertEquals("", NodeBase.normalize("///"));
243+
assertEquals("", NodeBase.normalize("/"));
244+
}
245+
237246
private NodeElement getNewNode(String name, String rackLocation) {
238247
NodeElement node = new NodeElement(name);
239248
node.setNetworkLocation(rackLocation);

0 commit comments

Comments
 (0)