Skip to content

Commit 25326dc

Browse files
authored
improve: Improve host address resolution logic for bookie id (apache#4588)
* improve: Improve host address resolution logic for bookie id * Use InetAddress instead of InetSocketAddress * Fix style * Catch exception
1 parent 4424ab7 commit 25326dc

File tree

1 file changed

+9
-12
lines changed
  • bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie

1 file changed

+9
-12
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieImpl.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.io.FilenameFilter;
3838
import java.io.IOException;
3939
import java.net.InetAddress;
40-
import java.net.InetSocketAddress;
4140
import java.net.UnknownHostException;
4241
import java.nio.ByteBuffer;
4342
import java.nio.file.FileStore;
@@ -267,25 +266,23 @@ public static BookieSocketAddress getBookieAddress(ServerConfiguration conf)
267266
iface = "default";
268267
}
269268

270-
String hostName = DNS.getDefaultHost(iface);
271-
InetSocketAddress inetAddr = new InetSocketAddress(hostName, conf.getBookiePort());
272-
if (inetAddr.isUnresolved()) {
273-
throw new UnknownHostException("Unable to resolve default hostname: "
274-
+ hostName + " for interface: " + iface);
275-
}
276-
String hostAddress = null;
277-
InetAddress iAddress = inetAddr.getAddress();
269+
String hostAddress = DNS.getDefaultIP(iface);
278270
if (conf.getUseHostNameAsBookieID()) {
279-
hostAddress = iAddress.getCanonicalHostName();
271+
try {
272+
hostAddress = InetAddress.getByName(hostAddress).getCanonicalHostName();
273+
} catch (Exception e) {
274+
UnknownHostException unknownHostException =
275+
new UnknownHostException("Unable to resolve hostname for interface: " + iface);
276+
unknownHostException.initCause(e);
277+
throw unknownHostException;
278+
}
280279
if (conf.getUseShortHostName()) {
281280
/*
282281
* if short hostname is used, then FQDN is not used. Short
283282
* hostname is the hostname cut at the first dot.
284283
*/
285284
hostAddress = hostAddress.split("\\.", 2)[0];
286285
}
287-
} else {
288-
hostAddress = iAddress.getHostAddress();
289286
}
290287

291288
BookieSocketAddress addr =

0 commit comments

Comments
 (0)