szetszwo commented on code in PR #11840:
URL: https://github.com/apache/ozone/pull/11840#discussion_r4744545770


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java:
##########
@@ -823,6 +827,51 @@ public <N extends Node> List<N> sortByDistanceCost(Node 
reader,
     return ret;
   }
 
+  /**
+   * Distance cost for a node that is not linked into the tree, such as a 
datanode deserialized
+   * from RPC, computed from network locations and the schema's per-level 
costs, like HDFS
+   * {@code NetworkTopology#getWeightUsingNetworkLocation}. Inner nodes take 
their level's schema
+   * cost, so this equals {@link #getDistanceCost(Node, Node)} for linked 
nodes, without the lock.
+   */
+  private int getDistanceCostByLocation(Node reader, String[] readerPath, Node 
node) {
+    if (Objects.equals(reader, node)) {
+      return 0;
+    }
+    final String[] nodePath = getLocationPath(node);
+    if (readerPath == null || nodePath == null) {
+      return Integer.MAX_VALUE;
+    }
+    int commonDepth = 0;
+    while (commonDepth < readerPath.length && 
readerPath[commonDepth].equals(nodePath[commonDepth])) {
+      commonDepth++;
+    }
+    // Both sides climb from the leaves' parent level up to the common 
ancestor, inclusive.
+    int cost = 0;
+    for (int level = NetConstants.ROOT_LEVEL + commonDepth; level < maxLevel; 
level++) {
+      cost += 2 * schemaManager.getCost(level);
+    }
+    return cost;
+  }
+
+  /**
+   * @return the names of a leaf's ancestors below the root, or null if the 
node's location does
+   * not fit this topology's depth.
+   */
+  private String[] getLocationPath(Node node) {
+    if (node == null) {
+      return null;
+    }
+    final String location;
+    try {
+      location = NetUtils.normalize(node.getNetworkLocation());
+    } catch (IllegalArgumentException e) {
+      return null;
+    }
+    final String[] path = location.length() <= 1 ? new String[0]
+        : location.substring(1).split(NetConstants.PATH_SEPARATOR_STR);
+    return path.length == maxLevel - NetConstants.ROOT_LEVEL - 1 ? path : null;
+  }

Review Comment:
   @greenwich , thanks for working on this!
   
   - Are you using AI?  If yes, please mention it in the JIRA and the commit 
message.  It is an ASF policy.
   - getNetworkLocation is already normalized.  (If not, it probably is an 
existing bug.  Please check)
   - Avoid create intermediate objects such as String[].  Computing cost should 
not require creating such objects.  (BTW, I filed HDDS-17957 when reviewing 
this.)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to