yandrey321 commented on code in PR #11838:
URL: https://github.com/apache/ozone/pull/11838#discussion_r4750486192
##########
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:
Without netlock there is no happens-before between SCM's topology writes and
the lock-free walk, so a sorter can see a new InnerNodeImpl before its fields;
NodeImpl#getNetworkFullPath() can then return null, and isSamePath throws
NullPointerException.
Scenario: SCM registers a datanode in a new rack. Under the write lock,
InnerNodeImpl#add -> createChildNode builds a rack node (NodeImpl.path, name,
location and level are plain, non-final fields), then calls
leaf.setParent(rack) on
the shared DatanodeDetails, also a plain field. Concurrently,
SCMBlockProtocolServer#sortDatanodes (line 247) or the
allocate path (line 428) walks that leaf without any lock. The JMM allows it
to read leaf.parent == rack while rack.path is still null. On x86 (TSO) this is
practically invisible. On aarch64 (e.g. Graviton) store-store reordering is
allowed, so isSamePath(node1, ...) calls null.equals(...) and the sort fails
with an NPE: block allocation or OM's sortDatanodes RPC fails. Stale but fully
built values, which the description covers, are fine.
Introduced by this PR (master's read lock gave the happens-before edge).
Fix option(-s): make NodeImpl#parent volatile. The publishing store in
add()/remove() then orders the new node's constructor writes before any reader
that follows the pointer, and a volatile read per walk step is far cheaper than
the fair RW lock. Additionally, or as a minimum, make isSamePath null-safe on
both paths and return MAX_VALUE.
Alternatively, keep the lock for SCM's map and use the lock-free walk only
for OM's immutable topology (for example behind a flag set by
ScmTopologyClient).
--
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]