Bladeren bron

HADOOP-16957. NodeBase.normalize doesn't removing all trailing slashes. Contributed by Ayush Saxena.

Ayush Saxena 5 jaren geleden
bovenliggende
commit
6bdab3723e

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

@@ -20,6 +20,8 @@ package org.apache.hadoop.net;
 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 class NodeBase implements Node {
     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) {

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

@@ -234,6 +234,15 @@ public class TestClusterTopology extends Assert {
     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);