Skip to content

Commit 936bf09

Browse files
committed
HDFS-15300. RBF: updateActiveNamenode() is invalid when RPC address is IP. Contributed by xuzq.
1 parent a3f945f commit 936bf09

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,22 @@ public static String getHostNameOfIP(String ipPort) {
638638
}
639639
}
640640

641+
/**
642+
* Attempt to normalize the given string to "host:port"
643+
* if it like "ip:port".
644+
*
645+
* @param ipPort maybe lik ip:port or host:port.
646+
* @return host:port
647+
*/
648+
public static String normalizeIP2HostName(String ipPort) {
649+
if (null == ipPort || !ipPortPattern.matcher(ipPort).matches()) {
650+
return ipPort;
651+
}
652+
653+
InetSocketAddress address = createSocketAddr(ipPort);
654+
return getHostPortString(address);
655+
}
656+
641657
/**
642658
* Return hostname without throwing exception.
643659
* The returned hostname String format is "hostname".

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MembershipNamenodeResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.hadoop.hdfs.server.federation.store.protocol.UpdateNamenodeRegistrationRequest;
4848
import org.apache.hadoop.hdfs.server.federation.store.records.MembershipState;
4949
import org.apache.hadoop.hdfs.server.federation.store.records.MembershipStats;
50+
import org.apache.hadoop.net.NetUtils;
5051
import org.apache.hadoop.util.Time;
5152
import org.slf4j.Logger;
5253
import org.slf4j.LoggerFactory;
@@ -263,7 +264,8 @@ public boolean registerNamenode(NamenodeStatusReport report)
263264

264265
MembershipState record = MembershipState.newInstance(
265266
routerId, report.getNameserviceId(), report.getNamenodeId(),
266-
report.getClusterId(), report.getBlockPoolId(), report.getRpcAddress(),
267+
report.getClusterId(), report.getBlockPoolId(),
268+
NetUtils.normalizeIP2HostName(report.getRpcAddress()),
267269
report.getServiceAddress(), report.getLifelineAddress(),
268270
report.getWebScheme(), report.getWebAddress(), report.getState(),
269271
report.getSafemode());

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/FederationTestUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,15 @@ public static void verifyException(Object obj, String methodName,
138138
public static NamenodeStatusReport createNamenodeReport(String ns, String nn,
139139
HAServiceState state) {
140140
Random rand = new Random();
141-
NamenodeStatusReport report = new NamenodeStatusReport(ns, nn,
142-
"localhost:" + rand.nextInt(10000), "localhost:" + rand.nextInt(10000),
141+
return createNamenodeReport(ns, nn, "localhost:"
142+
+ rand.nextInt(10000), state);
143+
}
144+
145+
public static NamenodeStatusReport createNamenodeReport(String ns, String nn,
146+
String rpcAddress, HAServiceState state) {
147+
Random rand = new Random();
148+
NamenodeStatusReport report = new NamenodeStatusReport(ns, nn, rpcAddress,
149+
"localhost:" + rand.nextInt(10000),
143150
"localhost:" + rand.nextInt(10000), "http",
144151
"testwebaddress-" + ns + nn);
145152
if (state == null) {

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/resolver/TestNamenodeResolver.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,23 @@ public void testCacheUpdateOnNamenodeStateUpdate() throws IOException {
307307
FederationNamenodeServiceState.ACTIVE, namenode1.getState());
308308
}
309309

310+
@Test
311+
public void testCacheUpdateOnNamenodeStateUpdateWithIp()
312+
throws IOException {
313+
final String rpcAddress = "127.0.0.1:10000";
314+
assertTrue(namenodeResolver.registerNamenode(
315+
createNamenodeReport(NAMESERVICES[0], NAMENODES[0], rpcAddress,
316+
HAServiceState.STANDBY)));
317+
stateStore.refreshCaches(true);
318+
319+
InetSocketAddress inetAddr = getInetSocketAddress(rpcAddress);
320+
namenodeResolver.updateActiveNamenode(NAMESERVICES[0], inetAddr);
321+
FederationNamenodeContext namenode =
322+
namenodeResolver.getNamenodesForNameserviceId(NAMESERVICES[0]).get(0);
323+
assertEquals("The namenode state should be ACTIVE post update.",
324+
FederationNamenodeServiceState.ACTIVE, namenode.getState());
325+
}
326+
310327
/**
311328
* Creates InetSocketAddress from the given RPC address.
312329
* @param rpcAddr RPC address (host:port).

0 commit comments

Comments
 (0)