ivandika3 commented on code in PR #10633:
URL: https://github.com/apache/ozone/pull/10633#discussion_r3540745310
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java:
##########
@@ -218,13 +224,27 @@ protected List< OmKeyLocationInfo >
allocateBlock(ScmClient scmClient,
}
throw ex;
}
+ // Cache the sorted order per pipeline so blocks sharing a pipeline are
+ // sorted once (mirrors the read path's per-pipeline caching).
+ final Map<List<DatanodeDetails>, List<? extends DatanodeDetails>>
sortedByNodes =
+ sortOnOm ? new HashMap<>() : null;
Review Comment:
In `KeyManagerImpl#sortDatanodes`, this logic uses `Map<Set<String>< List<?
extends DatanodeDetails>>` but in here it is `Map<List<DatanodeDetails>, List<?
extends DatanodeDetails>>`. Any reason for this? If so, can we standardize the
implementation?
I think there is no need to get the use the `DatanodeDetails::getUuidString`
in `KeyManagerImpl#sortedByNodes`.
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java:
##########
@@ -218,13 +224,27 @@ protected List< OmKeyLocationInfo >
allocateBlock(ScmClient scmClient,
}
throw ex;
}
+ // Cache the sorted order per pipeline so blocks sharing a pipeline are
+ // sorted once (mirrors the read path's per-pipeline caching).
+ final Map<List<DatanodeDetails>, List<? extends DatanodeDetails>>
sortedByNodes =
+ sortOnOm ? new HashMap<>() : null;
Review Comment:
Also this comment is not entirely accurate
> // Cache the sorted order per pipeline so blocks sharing a pipeline are
// sorted once (mirrors the read path's per-pipeline caching).
If there are two blocks with different pipelines, but consist of the same
three datanodes, they will share the same cache.
##########
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".
##########
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:
Also instead of returning the unresolved node, 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)? If so, then we can
simply use thie `NetworkTopologyImpl#getNode` and we don't need this function.
Also I saw that before this `toClusterMapNode` we already resolve the client
machine in `getClientNode`. If that's the case, is this still needed?
--
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]