chihsuan commented on code in PR #10633:
URL: https://github.com/apache/ozone/pull/10633#discussion_r3544535824
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java:
##########
@@ -2204,32 +2204,96 @@ private void sortDatanodes(String clientMachine,
List<OmKeyInfo> keyInfos) {
@VisibleForTesting
public List<? extends DatanodeDetails> sortDatanodes(List<? extends
DatanodeDetails> nodes,
String clientMachine) {
- final Node client = getClientNode(clientMachine, nodes);
- return ozoneManager.getClusterMap()
- .sortByDistanceCost(client, nodes, nodes.size());
+ final NetworkTopology clusterMap = ozoneManager.getClusterMap();
+ final Node client = getClientNode(clientMachine, nodes, clusterMap);
+ return clusterMap.sortByDistanceCost(client, nodes, nodes.size());
+ }
+
+ @Override
+ public List<? extends DatanodeDetails> sortDatanodesForWrite(
+ List<? extends DatanodeDetails> nodes, String clientMachine) {
+ if (StringUtils.isEmpty(clientMachine)) {
+ // No client address: keep the pipeline order (the first node is the
write
+ // primary). Mirrors SCMBlockProtocolServer#getClientNode's empty guard.
+ return nodes;
+ }
+ return captureLatencyNs(
+ metrics.getAllocateBlockSortDatanodesLatencyNs(), () -> {
+ final NetworkTopology clusterMap = ozoneManager.getClusterMap();
+ final Node client = getClientNode(clientMachine, nodes, clusterMap);
+ if (client == null) {
+ // Preserve pipeline order for writes: the first node is the write
+ // primary, so do not shuffle when the client cannot be resolved.
+ return nodes;
+ }
+ return sortByClusterMapDistance(clusterMap, client, nodes);
+ });
+ }
+
+ @Override
+ public boolean isSortDatanodesForWriteEnabled() {
+ return ozoneManager.getConfig().isSortDatanodesForWriteEnabled();
+ }
+
+ /**
+ * Sort a pipeline's nodes by topology distance to the client. The nodes come
+ * from SCM over RPC, so they are deserialized {@link DatanodeDetails} with
no
+ * parent/level and would be treated as outside the topology (distance
+ * {@link Integer#MAX_VALUE}) and shuffled. Resolve each node (and a
co-located
+ * client) to its canonical instance in OM's cluster map before sorting, then
+ * map the sorted order back to the original pipeline nodes.
+ */
+ private List<? extends DatanodeDetails> sortByClusterMapDistance(
+ NetworkTopology clusterMap, Node client,
+ List<? extends DatanodeDetails> nodes) {
+ final Node reader = toClusterMapNode(clusterMap, client);
+ final List<Node> topologyNodes = new ArrayList<>(nodes.size());
+ final Map<String, DatanodeDetails> nodeByPath = new HashMap<>();
+ for (DatanodeDetails node : nodes) {
+ final Node resolved = clusterMap.getNode(node.getNetworkFullPath());
+ if (resolved == null) {
+ return nodes;
+ }
+ topologyNodes.add(resolved);
+ nodeByPath.put(resolved.getNetworkFullPath(), node);
+ }
+ final List<Node> sorted =
+ clusterMap.sortByDistanceCost(reader, topologyNodes,
topologyNodes.size());
+ final List<DatanodeDetails> result = new ArrayList<>(sorted.size());
+ for (Node node : sorted) {
+ result.add(nodeByPath.get(node.getNetworkFullPath()));
+ }
+ return result;
+ }
+
+ /**
+ * Resolve a node to its canonical, topology-linked instance in the given
+ * cluster map, or return the input node if it is not in the map.
+ */
+ private Node toClusterMapNode(NetworkTopology clusterMap, Node node) {
+ final Node resolved = clusterMap.getNode(node.getNetworkFullPath());
+ return resolved != null ? resolved : node;
}
Review Comment:
> should we return null instead and skip the whole sort by distance since it
should fail (e.g. the unresolved node should not have the correct ancestor
information)?
That works for datanodes, which we already skip the sort if one is missing
from the map, but not for the client. A client that isn't a datanode is never
in the map, yet it still sorts fine because `getOtherNode` attaches it to its
rack. Returning `null` would skip sorting for all external clients.
> is this still needed?
Yes, `getClientNode` could return an RPC-deserialized node that isn't linked
to the topology. But you're right it's better done inside `getClientNode`
itself, so I moved the lookup there and removed `toClusterMapNode` in
356023d1c3. Hope this makes it easier to read, thanks for the feedback!
--
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]