Copilot commented on code in PR #10633:
URL: https://github.com/apache/ozone/pull/10633#discussion_r3559186448
##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/scm/client/ScmTopologyClient.java:
##########
@@ -60,8 +59,7 @@ public ScmTopologyClient(
}
public NetworkTopology getClusterMap() {
- return requireNonNull(cache.get(),
- "ScmBlockLocationClient must have been initialized already.");
+ return cache.get();
}
Review Comment:
`ScmTopologyClient#getClusterMap()` now returns `cache.get()` directly,
which can be `null` before `start()` populates the cache. Since this method
name doesn’t indicate nullability, callers can easily hit a less-informative
NPE downstream. Consider keeping `getClusterMap()` non-null (fail fast with a
clear message) and adding a separate `getClusterMapAllowNull()` (or similar)
for the allocateBlock fallback path.
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/KeyManagerImpl.java:
##########
@@ -2204,32 +2201,86 @@ 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) {
+ Preconditions.checkArgument(!StringUtils.isEmpty(clientMachine),
+ "clientMachine is empty");
+ final NetworkTopology clusterMap = ozoneManager.getClusterMap();
+ return captureLatencyNs(
+ metrics.getAllocateBlockSortDatanodesLatencyNs(), () -> {
+ 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: the topology treats them as unknown (distance
+ * {@link Integer#MAX_VALUE}) and the order comes out random. Look each node
+ * up in OM's cluster map to get the topology-linked instance, sort those,
+ * then map the order back to the original nodes.
+ */
+ private List<? extends DatanodeDetails> sortByClusterMapDistance(
+ NetworkTopology clusterMap, Node client,
+ List<? extends DatanodeDetails> nodes) {
+ 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(client, topologyNodes,
topologyNodes.size());
+ final List<DatanodeDetails> result = new ArrayList<>(sorted.size());
+ for (Node node : sorted) {
+ result.add(nodeByPath.get(node.getNetworkFullPath()));
+ }
+ return result;
}
private Node getClientNode(String clientMachine,
- List<? extends DatanodeDetails> nodes) {
- List<DatanodeDetails> matchingNodes = new ArrayList<>();
- boolean useHostname = ozoneManager.getConfiguration().getBoolean(
- HddsConfigKeys.HDDS_DATANODE_USE_DN_HOSTNAME,
- HddsConfigKeys.HDDS_DATANODE_USE_DN_HOSTNAME_DEFAULT);
+ List<? extends DatanodeDetails> nodes, NetworkTopology clusterMap) {
for (DatanodeDetails node : nodes) {
- if ((useHostname ? node.getHostName() : node.getIpAddress()).equals(
- clientMachine)) {
- matchingNodes.add(node);
+ // Match by either IP or hostname, like SCM's getNodesByAddress; the
client
+ // address is always an IP even when use.datanode.hostname is enabled.
+ if (clientMachine.equals(node.getIpAddress())
Review Comment:
The comment here says the client address is always an IP even when
`use.datanode.hostname` is enabled, but the implementation (and tests)
explicitly support matching by hostname too. This is misleading for future
maintainers; consider rewording to reflect that `clientMachine` may be an IP or
hostname (even if streaming-write remoteAddress is typically an IP).
--
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]