From 2bd3c04d978c87e79a11e903e9414b8f870308e1 Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Sun, 31 Dec 2023 15:05:12 +0100 Subject: [PATCH] Caching interfaces and addresses (https://issues.redhat.com/browse/JGRP-2739) --- src/org/jgroups/util/Util.java | 49 ++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/org/jgroups/util/Util.java b/src/org/jgroups/util/Util.java index b838083c03..8816726d7f 100644 --- a/src/org/jgroups/util/Util.java +++ b/src/org/jgroups/util/Util.java @@ -110,13 +110,26 @@ public class Util { public enum AddressScope {GLOBAL,SITE_LOCAL,LINK_LOCAL,LOOPBACK,NON_LOOPBACK} - private static boolean ipv4_stack_available=false, ipv6_stack_available=false; - private static final StackType ip_stack_type=_getIpStackType(); - public static final boolean can_bind_to_mcast_addr; - protected static ResourceBundle resource_bundle; - static DateTimeFormatter UTF_FORMAT = DateTimeFormatter.ofPattern("E MMM d H:m:s 'UTC' y"); + private static boolean ipv4_stack_available=false, ipv6_stack_available=false; + private static final StackType ip_stack_type; + private static volatile List CACHED_INTERFACES=null; + private static volatile Collection CACHED_ADDRESSES=null; + + public static final boolean can_bind_to_mcast_addr; + protected static ResourceBundle resource_bundle; + static DateTimeFormatter UTF_FORMAT=DateTimeFormatter.ofPattern("E MMM d H:m:s 'UTC' y"); static { + + try { + CACHED_INTERFACES=getAllAvailableInterfaces(); + CACHED_ADDRESSES=getAllAvailableAddresses(null); + } + catch(SocketException e) { + throw new RuntimeException(e); + } + ip_stack_type=_getIpStackType(); + String tmp; resource_bundle=ResourceBundle.getBundle("jg-messages",Locale.getDefault(),Util.class.getClassLoader()); @@ -516,7 +529,7 @@ public static void shutdown(JChannel ch) throws Exception { ViewId new_vid=new ViewId(ch.getAddress(),vid.getId() + 1); View new_view=new View(new_vid,members); - // inject view in which the shut down member is the only element + // inject view in which the shut-down member is the only element GMS gms=stack.findProtocol(GMS.class); gms.installView(new_view); } @@ -3473,7 +3486,7 @@ public static void forAllComponents(Object target, BiConsumer fun if(target == null) return; Field[] fields=Util.getAllDeclaredFieldsWithAnnotations(target.getClass(), Component.class); - if(fields == null || fields.length == 0) + if(fields == null) return; for(Field f: fields) { Object comp=Util.getField(f, target); @@ -3491,7 +3504,7 @@ public static void forAllComponentTypes(Class cl, BiConsumer,String> if(cl == null) return; Field[] fields=Util.getAllDeclaredFieldsWithAnnotations(cl, Component.class); - if(fields == null || fields.length == 0) + if(fields == null) return; for(Field f: fields) { Class type=f.getType(); @@ -4320,7 +4333,7 @@ public static boolean checkForAndroid() { public static boolean checkForMac() {return checkForPresence("os.name", "mac", "macosx", "osx", "darwin");} private static boolean checkForPresence(String key, String ...values) { - if(values == null || values.length == 0) + if(values == null) return false; for(String val: values) if(checkForPresence(key, val)) @@ -4640,6 +4653,10 @@ public static boolean isStackAvailable(boolean ipv4) { public static List getAllAvailableInterfaces() throws SocketException { + List cached_interfaces=CACHED_INTERFACES; + if(cached_interfaces != null) + return cached_interfaces; + List retval=new ArrayList<>(10); for(Enumeration en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf=en.nextElement(); @@ -4650,11 +4667,21 @@ public static List getAllAvailableInterfaces() throws SocketEx retval.add(sub); } } - return retval; + return CACHED_INTERFACES=retval; + } + + public static void resetCacheAddresses(boolean reset_interfaces, boolean reset_addresses) { + if(reset_interfaces) + CACHED_INTERFACES=null; + if(reset_addresses) + CACHED_ADDRESSES=null; } /** 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) + return cached_addresses; Set retval=new HashSet<>(); try { List interfaces=getAllAvailableInterfaces(); @@ -4671,7 +4698,7 @@ public static Collection getAllAvailableAddresses(Predicate