chihsuan commented on code in PR #10633:
URL: https://github.com/apache/ozone/pull/10633#discussion_r3544488728
##########
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:
> Nit: The javadoc is kind of confusing with terms like "canonical",
"topology-linked".
Thanks, I've updated both javadocs to describe the lookup plainly and
dropped "canonical". d310622
--
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]