This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ee3291  support  setting the system variable 
`elasticjob.preferred.network.ip` (#1938)
6ee3291 is described below

commit 6ee3291798b51ecdc82fab758abfd412e68a36bd
Author: skai <[email protected]>
AuthorDate: Sat Jul 24 11:05:07 2021 +0800

    support  setting the system variable `elasticjob.preferred.network.ip` 
(#1938)
    
    * build IpUtils.isPreferredNetworkInterface tests
    
    * support  setting the system variable `elasticjob.preferred.network.ip` && 
build tests
    
    * update code comment
    
    * Remove redundant
    
    Co-authored-by: 蔡顺铠 <[email protected]>
---
 .../elasticjob/infra/env/IpUtils.java              | 26 +++++--
 .../elasticjob/infra/env/IpUtilsTest.java          | 81 +++++++++++++++++++++-
 2 files changed, 99 insertions(+), 8 deletions(-)

diff --git 
a/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
 
b/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
index 68af52c..15c5797 100644
--- 
a/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
+++ 
b/elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java
@@ -39,8 +39,10 @@ public final class IpUtils {
     
     public static final String IP_REGEX = 
"((\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)){3})";
     
-    private static final String PREFERRED_NETWORK_INTERFACE = 
"elasticjob.preferred.network.interface";
-    
+    public static final String PREFERRED_NETWORK_INTERFACE = 
"elasticjob.preferred.network.interface";
+
+    public static final String PREFERRED_NETWORK_IP = 
"elasticjob.preferred.network.ip";
+
     private static volatile String cachedIpAddress;
     
     private static volatile String cachedHostName;
@@ -59,7 +61,7 @@ public final class IpUtils {
             Enumeration<InetAddress> ipAddresses = 
networkInterface.getInetAddresses();
             while (ipAddresses.hasMoreElements()) {
                 InetAddress ipAddress = ipAddresses.nextElement();
-                if (isValidAddress(ipAddress)) {
+                if (isValidAddress(ipAddress) && 
isPreferredAddress(ipAddress)) {
                     cachedIpAddress = ipAddress.getHostAddress();
                     return cachedIpAddress;
                 }
@@ -95,14 +97,14 @@ public final class IpUtils {
         }
         return result;
     }
-    
+
     private static NetworkInterface getFirstNetworkInterface(final 
List<NetworkInterface> validNetworkInterfaces) {
         NetworkInterface result = null;
         for (NetworkInterface each : validNetworkInterfaces) {
             Enumeration<InetAddress> addresses = each.getInetAddresses();
             while (addresses.hasMoreElements()) {
                 InetAddress inetAddress = addresses.nextElement();
-                if (isValidAddress(inetAddress)) {
+                if (isValidAddress(inetAddress) && 
isPreferredAddress(inetAddress)) {
                     result = each;
                     break;
                 }
@@ -113,7 +115,7 @@ public final class IpUtils {
         }
         return result;
     }
-    
+
     private static boolean isPreferredNetworkInterface(final NetworkInterface 
networkInterface) {
         String preferredNetworkInterface = 
System.getProperty(PREFERRED_NETWORK_INTERFACE);
         return Objects.equals(networkInterface.getDisplayName(), 
preferredNetworkInterface);
@@ -129,7 +131,17 @@ public final class IpUtils {
             return true;
         }
     }
-    
+
+    private static boolean isPreferredAddress(final InetAddress inetAddress) {
+        String preferredNetworkIp = System.getProperty(PREFERRED_NETWORK_IP);
+        if (preferredNetworkIp == null) {
+            return true;
+        }
+
+        String hostAddress = inetAddress.getHostAddress();
+        return hostAddress.matches(preferredNetworkIp) || 
hostAddress.startsWith(preferredNetworkIp);
+    }
+
     private static boolean isValidAddress(final InetAddress inetAddress) {
         try {
             return !inetAddress.isLoopbackAddress() && 
!inetAddress.isAnyLocalAddress()
diff --git 
a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
 
b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
index e0ef10e..29e8014 100644
--- 
a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
+++ 
b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java
@@ -21,10 +21,21 @@ import lombok.SneakyThrows;
 import org.junit.Test;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Vector;
 
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public final class IpUtilsTest {
     
@@ -32,7 +43,75 @@ public final class IpUtilsTest {
     public void assertGetIp() {
         assertNotNull(IpUtils.getIp());
     }
-    
+
+    @Test
+    @SneakyThrows
+    public void assertPreferredNetworkInterface() {
+        System.setProperty(IpUtils.PREFERRED_NETWORK_INTERFACE, "eth0");
+        Method declaredMethod = 
IpUtils.class.getDeclaredMethod("isPreferredNetworkInterface", 
NetworkInterface.class);
+        declaredMethod.setAccessible(true);
+        NetworkInterface mockNetworkInterface = mock(NetworkInterface.class);
+        when(mockNetworkInterface.getDisplayName()).thenReturn("eth0");
+        boolean result = (boolean) 
declaredMethod.invoke("isPreferredNetworkInterface", mockNetworkInterface);
+        assertTrue(result);
+        System.clearProperty(IpUtils.PREFERRED_NETWORK_INTERFACE);
+    }
+
+    @Test
+    @SneakyThrows
+    public void assertPreferredNetworkAddress() {
+        Method declaredMethod = 
IpUtils.class.getDeclaredMethod("isPreferredAddress", InetAddress.class);
+        declaredMethod.setAccessible(true);
+        InetAddress inetAddress = mock(InetAddress.class);
+        System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "192.168");
+        when(inetAddress.getHostAddress()).thenReturn("192.168.0.100");
+        assertTrue((boolean) declaredMethod.invoke("isPreferredAddress", 
inetAddress));
+        when(inetAddress.getHostAddress()).thenReturn("10.10.0.100");
+        assertFalse((boolean) declaredMethod.invoke("isPreferredAddress", 
inetAddress));
+        System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
+        System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "10.10.*");
+        when(inetAddress.getHostAddress()).thenReturn("10.10.0.100");
+        assertTrue((boolean) declaredMethod.invoke("isPreferredAddress", 
inetAddress));
+        when(inetAddress.getHostAddress()).thenReturn("10.0.0.100");
+        assertFalse((boolean) declaredMethod.invoke("isPreferredAddress", 
inetAddress));
+        System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
+    }
+
+    @Test
+    @SneakyThrows
+    public void assertGetFirstNetworkInterface() {
+        InetAddress address1 = mock(Inet4Address.class);
+        when(address1.isLoopbackAddress()).thenReturn(false);
+        when(address1.isAnyLocalAddress()).thenReturn(false);
+        when(address1.isReachable(100)).thenReturn(true);
+        when(address1.getHostAddress()).thenReturn("10.10.0.1");
+        Vector<InetAddress> addresses1 = new Vector<>();
+        addresses1.add(address1);
+        InetAddress address2 = mock(Inet4Address.class);
+        when(address2.isLoopbackAddress()).thenReturn(false);
+        when(address2.isAnyLocalAddress()).thenReturn(false);
+        when(address2.isReachable(100)).thenReturn(true);
+        when(address2.getHostAddress()).thenReturn("192.168.99.100");
+        Vector<InetAddress> addresses2 = new Vector<>();
+        addresses2.add(address2);
+        NetworkInterface networkInterface1 = mock(NetworkInterface.class);
+        NetworkInterface networkInterface2 = mock(NetworkInterface.class);
+        
when(networkInterface1.getInetAddresses()).thenReturn(addresses1.elements());
+        
when(networkInterface2.getInetAddresses()).thenReturn(addresses2.elements());
+        when(networkInterface1.getDisplayName()).thenReturn("eth1");
+        when(networkInterface2.getDisplayName()).thenReturn("eth2");
+        Method declaredMethod = 
IpUtils.class.getDeclaredMethod("getFirstNetworkInterface", List.class);
+        declaredMethod.setAccessible(true);
+        List<NetworkInterface> validNetworkInterfaces = 
Arrays.asList(networkInterface1, networkInterface2);
+        assertThat(declaredMethod.invoke("getFirstNetworkInterface", 
validNetworkInterfaces), is(networkInterface2));
+        System.setProperty(IpUtils.PREFERRED_NETWORK_INTERFACE, "eth1");
+        assertThat(declaredMethod.invoke("getFirstNetworkInterface", 
validNetworkInterfaces), is(networkInterface1));
+        System.clearProperty(IpUtils.PREFERRED_NETWORK_INTERFACE);
+        System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "10.10.*");
+        assertThat(declaredMethod.invoke("getFirstNetworkInterface", 
validNetworkInterfaces), is(networkInterface1));
+        System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
+    }
+
     @Test
     @SneakyThrows
     public void assertGetHostName() {

Reply via email to