From 421a22c73bfb5d1c4748f7d23b0a2a4fd5586192 Mon Sep 17 00:00:00 2001 From: yukon Date: Tue, 1 Aug 2017 20:35:28 +0800 Subject: [PATCH 1/4] Closes some timeout PRs, closes #125, closes #124, closes #122, closes #71, closes #58 From 5874ce3747a45250b7d77da3d716d92e09f70cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=86=B2?= Date: Thu, 24 Aug 2017 17:24:44 +0800 Subject: [PATCH 2/4] #ROCKETMQ-277# fix bug --- .../org/apache/rocketmq/common/MixAll.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/common/src/main/java/org/apache/rocketmq/common/MixAll.java b/common/src/main/java/org/apache/rocketmq/common/MixAll.java index f8e9b4e1f67..9acd5845cf5 100644 --- a/common/src/main/java/org/apache/rocketmq/common/MixAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/MixAll.java @@ -26,6 +26,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; @@ -407,6 +408,36 @@ private static String localhost() { InetAddress addr = InetAddress.getLocalHost(); return addr.getHostAddress(); } catch (Throwable e) { + try { + List candidatesHost = new ArrayList(); + Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); + + while (enumeration.hasMoreElements()) { + NetworkInterface networkInterface = enumeration.nextElement(); + if (networkInterface.isUp()) { + Enumeration addrs = networkInterface.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress address = addrs.nextElement(); + if (address.isLoopbackAddress()) { + continue; + } + //ip4 highter priority + if (address instanceof Inet6Address) { + candidatesHost.add(address.getHostAddress()); + continue; + } + return address.getHostAddress(); + } + } + } + + if (!candidatesHost.isEmpty()) { + return candidatesHost.get(0); + } + + } catch (Exception ignored) { + } + throw new RuntimeException("InetAddress java.net.InetAddress.getLocalHost() throws UnknownHostException" + FAQUrl.suggestTodo(FAQUrl.UNKNOWN_HOST_EXCEPTION), e); From 8e1ae4cafbd52f529758a79e68176d27ad9a1441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=86=B2?= Date: Thu, 24 Aug 2017 17:45:46 +0800 Subject: [PATCH 3/4] #ROCKETMQ-277# fix bug & add unit test --- .../org/apache/rocketmq/common/MixAll.java | 58 +++++++++++-------- .../apache/rocketmq/common/MixAllTest.java | 14 +++-- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/MixAll.java b/common/src/main/java/org/apache/rocketmq/common/MixAll.java index 9acd5845cf5..5c4795baf29 100644 --- a/common/src/main/java/org/apache/rocketmq/common/MixAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/MixAll.java @@ -409,31 +409,9 @@ private static String localhost() { return addr.getHostAddress(); } catch (Throwable e) { try { - List candidatesHost = new ArrayList(); - Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); - - while (enumeration.hasMoreElements()) { - NetworkInterface networkInterface = enumeration.nextElement(); - if (networkInterface.isUp()) { - Enumeration addrs = networkInterface.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress address = addrs.nextElement(); - if (address.isLoopbackAddress()) { - continue; - } - //ip4 highter priority - if (address instanceof Inet6Address) { - candidatesHost.add(address.getHostAddress()); - continue; - } - return address.getHostAddress(); - } - } - } - - if (!candidatesHost.isEmpty()) { - return candidatesHost.get(0); - } + String candidatesHost = getLocalhostByNetworkInterface(); + if (candidatesHost != null) + return candidatesHost; } catch (Exception ignored) { } @@ -444,6 +422,36 @@ private static String localhost() { } } + private static String getLocalhostByNetworkInterface() throws SocketException { + List candidatesHost = new ArrayList(); + Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); + + while (enumeration.hasMoreElements()) { + NetworkInterface networkInterface = enumeration.nextElement(); + if ("docker0".equals(networkInterface.getName()) || !networkInterface.isUp()) { + continue; + } + Enumeration addrs = networkInterface.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress address = addrs.nextElement(); + if (address.isLoopbackAddress()) { + continue; + } + //ip4 highter priority + if (address instanceof Inet6Address) { + candidatesHost.add(address.getHostAddress()); + continue; + } + return address.getHostAddress(); + } + } + + if (!candidatesHost.isEmpty()) { + return candidatesHost.get(0); + } + return null; + } + public static boolean compareAndIncreaseOnly(final AtomicLong target, final long value) { long prev = target.get(); while (value > prev) { diff --git a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java index 218b36d2d8b..1030640e5b4 100644 --- a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java @@ -20,11 +20,8 @@ import java.io.File; import java.io.IOException; import java.io.PrintWriter; +import java.lang.reflect.Method; import java.net.InetAddress; -import java.nio.ByteOrder; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import org.junit.Test; @@ -93,4 +90,13 @@ public void testString2File() throws IOException { MixAll.string2File("MixAll_testString2File", fileName); assertThat(MixAll.file2String(fileName)).isEqualTo("MixAll_testString2File"); } + + @Test + public void test_getLocalhostByNetworkInterface() throws Exception { + Method method = MixAll.class.getDeclaredMethod("getLocalhostByNetworkInterface"); + method.setAccessible(true); + Object invoke = method.invoke(null); + System.out.println(invoke); + assertThat(invoke).isNotNull(); + } } From 32e63fdc1ad0856939e58c0beb5919b8d425b074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=86=B2?= Date: Mon, 28 Aug 2017 10:45:46 +0800 Subject: [PATCH 4/4] [ROCKETMQ-277] delete sys.out --- common/src/test/java/org/apache/rocketmq/common/MixAllTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java index 1030640e5b4..5158ce0e6f3 100644 --- a/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/MixAllTest.java @@ -96,7 +96,6 @@ public void test_getLocalhostByNetworkInterface() throws Exception { Method method = MixAll.class.getDeclaredMethod("getLocalhostByNetworkInterface"); method.setAccessible(true); Object invoke = method.invoke(null); - System.out.println(invoke); assertThat(invoke).isNotNull(); } }