From 0c03f271f976fb0da0b33542a2f8c8ff400f3e79 Mon Sep 17 00:00:00 2001 From: Pedro Ruivo Date: Fri, 26 Jan 2024 14:25:23 +0000 Subject: [PATCH 1/3] JGRP-2739 Cache addresses on first access --- src/org/jgroups/util/Util.java | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/org/jgroups/util/Util.java b/src/org/jgroups/util/Util.java index a4e51f0463..bcbeb98752 100644 --- a/src/org/jgroups/util/Util.java +++ b/src/org/jgroups/util/Util.java @@ -4703,26 +4703,31 @@ public static void resetCacheAddresses(boolean reset_interfaces, boolean reset_a /** Returns all addresses of all interfaces (that are up) that satisfy a given filter (ignored if null) */ public static Collection getAllAvailableAddresses(Predicate filter) { - Collection cached_addresses=CACHED_ADDRESSES; - if(cached_addresses != null && filter == null) - return cached_addresses; - Set retval=new HashSet<>(); + Collection cached_addresses=getAllAvailableAddresses(); + assert cached_addresses != null; + return filter == null ? + cached_addresses : + cached_addresses.stream().filter(filter).collect(Collectors.toList()); + } + + private static synchronized Collection getAllAvailableAddresses() { + if (CACHED_ADDRESSES != null) { + return CACHED_ADDRESSES; + } + Set addresses = new HashSet<>(); try { List interfaces=getAllAvailableInterfaces(); for(NetworkInterface intf: interfaces) { if(!isUp(intf) /*!intf.isUp()*/) continue; - Enumeration addrs=intf.getInetAddresses(); - while(addrs.hasMoreElements()) { - InetAddress addr=addrs.nextElement(); - if(filter == null || filter.test(addr)) - retval.add(addr); - } + intf.getInetAddresses().asIterator().forEachRemaining(addresses::add); } } catch(SocketException e) { } - return CACHED_ADDRESSES=retval; + // immutable list + CACHED_ADDRESSES = List.copyOf(addresses); + return CACHED_ADDRESSES; } public static void checkIfValidAddress(InetAddress bind_addr,String prot_name) throws Exception { From 8e7deaae05ebbc49aff3d5abddc46dd5b13c6ba0 Mon Sep 17 00:00:00 2001 From: Tristan Tarrant Date: Fri, 26 Jan 2024 11:00:43 +0100 Subject: [PATCH 2/3] JGRP-2756 Look for JDBC drivers in multiple classloaders --- src/org/jgroups/protocols/JDBC_PING.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/jgroups/protocols/JDBC_PING.java b/src/org/jgroups/protocols/JDBC_PING.java index 6ca2e0f989..b11cf75a30 100644 --- a/src/org/jgroups/protocols/JDBC_PING.java +++ b/src/org/jgroups/protocols/JDBC_PING.java @@ -271,7 +271,7 @@ protected void loadDriver() { return; log.debug("Registering JDBC Driver named '%s'", connection_driver); try { - Class.forName(connection_driver); + Util.loadClass(connection_driver, this.getClass().getClassLoader()); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("JDBC Driver required for JDBC_PING " + " protocol could not be loaded: '" + connection_driver + "'"); From b43aba991f9844f3f1523016918514536f0e9265 Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Mon, 29 Jan 2024 09:32:12 +0100 Subject: [PATCH 3/3] Using CACHED_ADDRESSES correctly --- src/org/jgroups/util/Util.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/org/jgroups/util/Util.java b/src/org/jgroups/util/Util.java index bcbeb98752..dd10acdb5f 100644 --- a/src/org/jgroups/util/Util.java +++ b/src/org/jgroups/util/Util.java @@ -123,7 +123,7 @@ public enum AddressScope {GLOBAL,SITE_LOCAL,LINK_LOCAL,LOOPBACK,NON_LOOPBACK} try { CACHED_INTERFACES=getAllAvailableInterfaces(); - CACHED_ADDRESSES=getAllAvailableAddresses(null); + CACHED_ADDRESSES=getAllAvailableAddresses(); } catch(SocketException e) { throw new RuntimeException(e); @@ -4644,7 +4644,7 @@ public static StackType getIpStackType() { * if the type cannot be detected */ private static StackType _getIpStackType() { - Collection all_addresses=getAllAvailableAddresses(null); + Collection all_addresses=getAllAvailableAddresses(); for(InetAddress addr: all_addresses) { if(addr instanceof Inet4Address) ipv4_stack_available=true; @@ -4668,7 +4668,7 @@ else if(addr instanceof Inet6Address) public static boolean isStackAvailable(boolean ipv4) { - Collection all_addrs=getAllAvailableAddresses(null); + Collection all_addrs=getAllAvailableAddresses(); for(InetAddress addr : all_addrs) if(ipv4 && addr instanceof Inet4Address || (!ipv4 && addr instanceof Inet6Address)) return true; @@ -4711,10 +4711,9 @@ public static Collection getAllAvailableAddresses(Predicate getAllAvailableAddresses() { - if (CACHED_ADDRESSES != null) { + if(CACHED_ADDRESSES != null) return CACHED_ADDRESSES; - } - Set addresses = new HashSet<>(); + Set addresses=new HashSet<>(); try { List interfaces=getAllAvailableInterfaces(); for(NetworkInterface intf: interfaces) { @@ -4726,15 +4725,14 @@ private static synchronized Collection getAllAvailableAddresses() { catch(SocketException e) { } // immutable list - CACHED_ADDRESSES = List.copyOf(addresses); - return CACHED_ADDRESSES; + return CACHED_ADDRESSES=List.copyOf(addresses); } public static void checkIfValidAddress(InetAddress bind_addr,String prot_name) throws Exception { // N.B. bind_addr.isAnyLocalAddress() is not OK if (bind_addr.isLoopbackAddress()) return; - Collection addrs=getAllAvailableAddresses(null); + Collection addrs=getAllAvailableAddresses(); for(InetAddress addr : addrs) { if(addr.equals(bind_addr)) return;