From 177efc5d1acbe7b93f2cf0614784448fa2afe4a5 Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Tue, 29 Jan 2019 11:21:24 +0800 Subject: [PATCH 1/4] enhance unit test and logging --- .../apache/dubbo/common/utils/NetUtils.java | 11 +++++------ .../registry/multicast/MulticastRegistry.java | 19 ++++++++++++++++--- .../multicast/MulticastRegistryTest.java | 19 ++++++++++++++----- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index 4c6a56a7c37..06450e2ef72 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -346,25 +346,24 @@ public static String toURL(String protocol, String host, int port, String path) } public static void joinMulticastGroup (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException { - setInterface(multicastSocket, multicastAddress); + setInterface(multicastSocket, multicastAddress instanceof Inet6Address); multicastSocket.setLoopbackMode(false); multicastSocket.joinGroup(multicastAddress); } - public static void setInterface (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException{ + public static void setInterface (MulticastSocket multicastSocket, boolean preferIpv6) throws IOException{ boolean interfaceSet = false; - boolean ipV6 = multicastAddress instanceof Inet6Address; Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface i = (NetworkInterface) interfaces.nextElement(); Enumeration addresses = i.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = (InetAddress) addresses.nextElement(); - if (ipV6 && address instanceof Inet6Address) { + if (preferIpv6 && address instanceof Inet6Address) { multicastSocket.setInterface(address); interfaceSet = true; break; - } else if (!ipV6 && address instanceof Inet4Address) { + } else if (!preferIpv6 && address instanceof Inet4Address) { multicastSocket.setInterface(address); interfaceSet = true; break; @@ -376,4 +375,4 @@ public static void setInterface (MulticastSocket multicastSocket, InetAddress mu } } -} \ No newline at end of file +} diff --git a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java index 33c42577ee5..3381ce85485 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java +++ b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.net.DatagramPacket; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MulticastSocket; @@ -81,9 +82,8 @@ public MulticastRegistry(URL url) { } try { multicastAddress = InetAddress.getByName(url.getHost()); - if (!multicastAddress.isMulticastAddress()) { - throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + ", ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255."); - } + checkMulticastAddress(multicastAddress); + multicastPort = url.getPort() <= 0 ? DEFAULT_MULTICAST_PORT : url.getPort(); multicastSocket = new MulticastSocket(multicastPort); NetUtils.joinMulticastGroup(multicastSocket, multicastAddress); @@ -132,6 +132,19 @@ public void run() { } } + private void checkMulticastAddress(InetAddress multicastAddress) { + if (!multicastAddress.isMulticastAddress()) { + String message = "Invalid multicast address " + multicastAddress; + if (!(multicastAddress instanceof Inet4Address)) { + throw new IllegalArgumentException(message + ", " + + "ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255."); + } else { + throw new IllegalArgumentException(message + ", " + "ipv6 multicast address must start with ff, " + + "for example: ffx3::/16"); + } + } + } + /** * Remove the expired providers, only when "clean" parameter is true. */ diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java index 743ff37d1de..d314802bdbf 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java +++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java @@ -17,6 +17,8 @@ package org.apache.dubbo.registry.multicast; import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.NotifyListener; @@ -37,6 +39,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class MulticastRegistryTest { + private static final Logger logger = LoggerFactory.getLogger(MulticastRegistryTest.class); private String service = "org.apache.dubbo.test.injvmServie"; private URL registryUrl = URL.valueOf("multicast://239.255.255.255/"); @@ -231,7 +234,7 @@ public void testMulticastAddress() { multicastAddress = InetAddress.getByName("224.55.66.77"); multicastSocket = new MulticastSocket(2345); multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, multicastAddress); + NetUtils.setInterface(multicastSocket, false); multicastSocket.joinGroup(multicastAddress); } finally { if (multicastSocket != null) { @@ -240,17 +243,23 @@ public void testMulticastAddress() { } // multicast ipv6 address, - /*try { - multicastAddress = InetAddress.getByName("ff01::1"); + try { + try { + multicastAddress = InetAddress.getByName("ff01::1"); + } catch (Throwable t) { + logger.info("bypass ipv6 test, possibly because the current network stack doesn't support ipv6"); + return; + } + multicastSocket = new MulticastSocket(); multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, multicastAddress); + NetUtils.setInterface(multicastSocket, true); multicastSocket.joinGroup(multicastAddress); } finally { if (multicastSocket != null) { multicastSocket.close(); } - }*/ + } } catch (Exception e) { Assertions.fail(e); From 84347acb1126524d05bd67043fc0f1ebf164665c Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Tue, 29 Jan 2019 11:29:02 +0800 Subject: [PATCH 2/4] enhance logging message --- .../org/apache/dubbo/registry/multicast/MulticastRegistry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java index 3381ce85485..338b556f193 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java +++ b/dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java @@ -140,7 +140,7 @@ private void checkMulticastAddress(InetAddress multicastAddress) { "ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255."); } else { throw new IllegalArgumentException(message + ", " + "ipv6 multicast address must start with ff, " + - "for example: ffx3::/16"); + "for example: ff01::1"); } } } From cc9d2a2868ecaa306e347f3c50b2047c78b9c1ae Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Tue, 29 Jan 2019 14:12:24 +0800 Subject: [PATCH 3/4] fix unit test --- .../multicast/MulticastRegistryTest.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java index d314802bdbf..f19523f603b 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java +++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java @@ -246,19 +246,18 @@ public void testMulticastAddress() { try { try { multicastAddress = InetAddress.getByName("ff01::1"); - } catch (Throwable t) { - logger.info("bypass ipv6 test, possibly because the current network stack doesn't support ipv6"); - return; - } - multicastSocket = new MulticastSocket(); - multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, true); - multicastSocket.joinGroup(multicastAddress); - } finally { - if (multicastSocket != null) { - multicastSocket.close(); + multicastSocket = new MulticastSocket(); + multicastSocket.setLoopbackMode(false); + NetUtils.setInterface(multicastSocket, true); + multicastSocket.joinGroup(multicastAddress); + } finally { + if (multicastSocket != null) { + multicastSocket.close(); + } } + } catch (Throwable t) { + logger.info("ipv6 test fails but ignore it by now", t); } } catch (Exception e) { From e74c79ab62cda1bfd85f9524565da26c148a9f2c Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Tue, 29 Jan 2019 14:58:49 +0800 Subject: [PATCH 4/4] make code clean --- .../multicast/MulticastRegistryTest.java | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java index f19523f603b..93c59bce316 100644 --- a/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java +++ b/dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java @@ -17,8 +17,6 @@ package org.apache.dubbo.registry.multicast; import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.registry.NotifyListener; @@ -39,7 +37,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class MulticastRegistryTest { - private static final Logger logger = LoggerFactory.getLogger(MulticastRegistryTest.class); private String service = "org.apache.dubbo.test.injvmServie"; private URL registryUrl = URL.valueOf("multicast://239.255.255.255/"); @@ -230,39 +227,35 @@ public void testMulticastAddress() { MulticastSocket multicastSocket = null; try { // ipv4 multicast address - try { - multicastAddress = InetAddress.getByName("224.55.66.77"); - multicastSocket = new MulticastSocket(2345); - multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, false); - multicastSocket.joinGroup(multicastAddress); - } finally { - if (multicastSocket != null) { - multicastSocket.close(); - } + multicastAddress = InetAddress.getByName("224.55.66.77"); + multicastSocket = new MulticastSocket(2345); + multicastSocket.setLoopbackMode(false); + NetUtils.setInterface(multicastSocket, false); + multicastSocket.joinGroup(multicastAddress); + } catch (Exception e) { + Assertions.fail(e); + } finally { + if (multicastSocket != null) { + multicastSocket.close(); } + } - // multicast ipv6 address, - try { - try { - multicastAddress = InetAddress.getByName("ff01::1"); + // multicast ipv6 address, + try { + multicastAddress = InetAddress.getByName("ff01::1"); - multicastSocket = new MulticastSocket(); - multicastSocket.setLoopbackMode(false); - NetUtils.setInterface(multicastSocket, true); - multicastSocket.joinGroup(multicastAddress); - } finally { - if (multicastSocket != null) { - multicastSocket.close(); - } - } - } catch (Throwable t) { - logger.info("ipv6 test fails but ignore it by now", t); + multicastSocket = new MulticastSocket(); + multicastSocket.setLoopbackMode(false); + NetUtils.setInterface(multicastSocket, true); + multicastSocket.joinGroup(multicastAddress); + } catch (Throwable t) { + t.printStackTrace(); + } finally { + if (multicastSocket != null) { + multicastSocket.close(); } - - } catch (Exception e) { - Assertions.fail(e); } + } }