hevinhsu commented on code in PR #10598:
URL: https://github.com/apache/ozone/pull/10598#discussion_r3522869965


##########
hadoop-ozone/mini-cluster/src/main/java/org/apache/hadoop/ozone/FixedHostMapping.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+import org.apache.hadoop.net.CachedDNSToSwitchMapping;
+import org.apache.hadoop.net.DNSToSwitchMapping;
+import org.apache.hadoop.net.NetworkTopology;
+
+/**
+ * A {@link CachedDNSToSwitchMapping} implementation that resolves hostnames
+ * to rack locations using a statically configured map, bypassing DNS lookups.
+ *
+ * <p>This is intended for use in test environments (e.g. {@code 
MiniOzoneCluster})
+ * where DataNode hostnames may be synthetic or unresolvable via DNS. The 
standard
+ * {@link CachedDNSToSwitchMapping} performs DNS normalization before rack 
resolution,
+ * which can cause synthetic hostnames to be incorrectly resolved to a real IP 
address,
+ * leading to rack mapping failures. This class avoids that by resolving 
directly
+ * against the registered hostname.
+ *
+ * <p>The mapping is stored in a JVM-wide static map. Callers must invoke
+ * {@link #addNode(String, String)} before cluster startup to register 
hostname-to-rack
+ * entries, and should call {@link #clear()} after each test to avoid 
cross-test pollution.
+ *
+ * <p>Usage:
+ * <pre>{@code
+ * FixedHostMapping.addNode("dn-0.test", "/rack1");
+ * FixedHostMapping.addNode("dn-1.test", "/rack1");
+ * FixedHostMapping.addNode("dn-2.test", "/rack2");
+ *
+ * conf.setClass(
+ *     CommonConfigurationKeysPublic.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY,
+ *     FixedHostMapping.class,
+ *     DNSToSwitchMapping.class);
+ * }</pre>
+ */
+public class FixedHostMapping extends CachedDNSToSwitchMapping {

Review Comment:
   The main difference is that `StaticMapping` extends 
`AbstractDNSToSwitchMapping`, whereas our implementation extends 
`CachedDNSToSwitchMapping`.
   
   Because of this, `StaticMapping` is wrapped by `CachedDNSToSwitchMapping` in 
SCM 
([code](https://github.com/apache/ozone/blob/master/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java#L764-L766)).
   
   As a result, calls to `resolve()` first go through 
`CachedDNSToSwitchMapping#resolve()`, which normalizes the input hostnames 
using `NetUtils.normalizeHostNames()` 
([code](https://github.com/apache/hadoop/blob/4f9b20bf0a637ac6b0c4900d7236a728a2782a1d/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/CachedDNSToSwitchMapping.java#L109)).
 This performs a native DNS lookup, so a hostname such as `host1.com` is 
resolved to its IP address before the rack mapping is applied. As a result, the 
predefined hostname-to-rack mapping used by the test is no longer matched 
correctly.



-- 
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]

Reply via email to