greenwich commented on code in PR #11838:
URL: https://github.com/apache/ozone/pull/11838#discussion_r4751259136


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/scm/net/NetworkTopologyImpl.java:
##########
@@ -734,35 +749,42 @@ public int getDistanceCost(Node node1, Node node2) {
     }
 
     int cost = 0;
-    netlock.readLock().lock();
-    try {
-      Node ancestor1 = node1.getAncestor(level1 - 1);
-      Node ancestor2 = node2.getAncestor(level2 - 1);
-      if (!Objects.equals(ancestor1, clusterTree) ||
-          !Objects.equals(ancestor2, clusterTree)) {
-        LOG.debug("One of the nodes is outside of network topology");
+    Node ancestor1 = node1.getAncestor(level1 - 1);
+    Node ancestor2 = node2.getAncestor(level2 - 1);
+    if (!isSamePath(ancestor1, clusterTree) || !isSamePath(ancestor2, 
clusterTree)) {
+      LOG.debug("One of the nodes is outside of network topology");
+      return Integer.MAX_VALUE;
+    }
+    // A null parent means the node was removed after the ancestor check.
+    while (level1 > level2) {
+      node1 = node1.getParent();
+      if (node1 == null) {
         return Integer.MAX_VALUE;
       }
-      while (level1 > level2 && node1 != null) {
-        node1 = node1.getParent();
-        level1--;
-        cost += node1 == null ? 0 : node1.getCost();
-      }
-      while (level2 > level1 && node2 != null) {
-        node2 = node2.getParent();
-        level2--;
-        cost += node2 == null ? 0 : node2.getCost();
+      level1--;
+      cost += node1.getCost();
+    }
+    while (level2 > level1) {
+      node2 = node2.getParent();
+      if (node2 == null) {
+        return Integer.MAX_VALUE;
       }
-      while (node1 != null && node2 != null && !Objects.equals(node1, node2)) {
-        node1 = node1.getParent();
-        node2 = node2.getParent();
-        cost += node1 == null ? 0 : node1.getCost();
-        cost += node2 == null ? 0 : node2.getCost();
+      level2--;
+      cost += node2.getCost();
+    }
+    while (!isSamePath(node1, node2)) {
+      node1 = node1.getParent();
+      node2 = node2.getParent();
+      if (node1 == null || node2 == null) {
+        return Integer.MAX_VALUE;
       }
-      return cost;
-    } finally {
-      netlock.readLock().unlock();
+      cost += node1.getCost() + node2.getCost();
     }
+    return cost;
+  }
+
+  private static boolean isSamePath(Node node1, Node node2) {

Review Comment:
   @yandrey321 thanks for catching this. My bad, I missed that edge case.
   
   1. NodeImpl#parent is now volatile. 
   2. isSamePath is null-safe. A node or path that isn't visible yet gives 
MAX_VALUE and can't fail the sort.
   
   Then I ran the benchmark again (the same one as in my earlier comment) to 
compare before and after the volatile change. Throughput in sorts/ms, linked 
datanodes, average ± standard deviation over 5 runs each:
   
   ### Unlinked datanodes
   
   | Threads | Before | With volatile |
   |---:|---:|---:|
   | 1 | 1,534 ± 118 | 1,417 ± 204 |
   | 8 | 5,772 ± 1,417 | 4,991 ± 742 |
   | 32 | 5,125 ± 1,092 | 4,957 ± 358 |
   | 100 | 4,503 ± 953 | 4,205 ± 680 |
   
   ### Linked datanodes while the topology changes
   
   | Threads | Before | With volatile |
   |---:|---:|---:|
   | 1 | 1,520 ± 171 | 1,500 ± 175 |
   | 8 | 5,705 ± 1,091 | 5,634 ± 794 |
   | 32 | 4,979 ± 1,114 | 5,248 ± 585 |
   | 100 | 4,521 ± 1,059 | 4,303 ± 610 |



-- 
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