Skip to content

Commit 6ee3291

Browse files
sunkai-caiskaic
andauthored
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]>
1 parent c95a944 commit 6ee3291

File tree

2 files changed

+99
-8
lines changed
  • elasticjob-infra/elasticjob-infra-common/src
    • main/java/org/apache/shardingsphere/elasticjob/infra/env
    • test/java/org/apache/shardingsphere/elasticjob/infra/env

2 files changed

+99
-8
lines changed

elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtils.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public final class IpUtils {
3939

4040
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})";
4141

42-
private static final String PREFERRED_NETWORK_INTERFACE = "elasticjob.preferred.network.interface";
43-
42+
public static final String PREFERRED_NETWORK_INTERFACE = "elasticjob.preferred.network.interface";
43+
44+
public static final String PREFERRED_NETWORK_IP = "elasticjob.preferred.network.ip";
45+
4446
private static volatile String cachedIpAddress;
4547

4648
private static volatile String cachedHostName;
@@ -59,7 +61,7 @@ public static String getIp() {
5961
Enumeration<InetAddress> ipAddresses = networkInterface.getInetAddresses();
6062
while (ipAddresses.hasMoreElements()) {
6163
InetAddress ipAddress = ipAddresses.nextElement();
62-
if (isValidAddress(ipAddress)) {
64+
if (isValidAddress(ipAddress) && isPreferredAddress(ipAddress)) {
6365
cachedIpAddress = ipAddress.getHostAddress();
6466
return cachedIpAddress;
6567
}
@@ -95,14 +97,14 @@ private static NetworkInterface findNetworkInterface() {
9597
}
9698
return result;
9799
}
98-
100+
99101
private static NetworkInterface getFirstNetworkInterface(final List<NetworkInterface> validNetworkInterfaces) {
100102
NetworkInterface result = null;
101103
for (NetworkInterface each : validNetworkInterfaces) {
102104
Enumeration<InetAddress> addresses = each.getInetAddresses();
103105
while (addresses.hasMoreElements()) {
104106
InetAddress inetAddress = addresses.nextElement();
105-
if (isValidAddress(inetAddress)) {
107+
if (isValidAddress(inetAddress) && isPreferredAddress(inetAddress)) {
106108
result = each;
107109
break;
108110
}
@@ -113,7 +115,7 @@ private static NetworkInterface getFirstNetworkInterface(final List<NetworkInter
113115
}
114116
return result;
115117
}
116-
118+
117119
private static boolean isPreferredNetworkInterface(final NetworkInterface networkInterface) {
118120
String preferredNetworkInterface = System.getProperty(PREFERRED_NETWORK_INTERFACE);
119121
return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface);
@@ -129,7 +131,17 @@ private static boolean ignoreNetworkInterface(final NetworkInterface networkInte
129131
return true;
130132
}
131133
}
132-
134+
135+
private static boolean isPreferredAddress(final InetAddress inetAddress) {
136+
String preferredNetworkIp = System.getProperty(PREFERRED_NETWORK_IP);
137+
if (preferredNetworkIp == null) {
138+
return true;
139+
}
140+
141+
String hostAddress = inetAddress.getHostAddress();
142+
return hostAddress.matches(preferredNetworkIp) || hostAddress.startsWith(preferredNetworkIp);
143+
}
144+
133145
private static boolean isValidAddress(final InetAddress inetAddress) {
134146
try {
135147
return !inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()

elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/env/IpUtilsTest.java

+80-1
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,97 @@
2121
import org.junit.Test;
2222

2323
import java.lang.reflect.Field;
24+
import java.lang.reflect.Method;
25+
import java.net.Inet4Address;
26+
import java.net.InetAddress;
27+
import java.net.NetworkInterface;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
import java.util.Vector;
2431

32+
import static org.junit.Assert.assertFalse;
2533
import static org.junit.Assert.assertNotNull;
2634
import static org.junit.Assert.assertThat;
2735
import static org.hamcrest.CoreMatchers.is;
36+
import static org.junit.Assert.assertTrue;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.when;
2839

2940
public final class IpUtilsTest {
3041

3142
@Test
3243
public void assertGetIp() {
3344
assertNotNull(IpUtils.getIp());
3445
}
35-
46+
47+
@Test
48+
@SneakyThrows
49+
public void assertPreferredNetworkInterface() {
50+
System.setProperty(IpUtils.PREFERRED_NETWORK_INTERFACE, "eth0");
51+
Method declaredMethod = IpUtils.class.getDeclaredMethod("isPreferredNetworkInterface", NetworkInterface.class);
52+
declaredMethod.setAccessible(true);
53+
NetworkInterface mockNetworkInterface = mock(NetworkInterface.class);
54+
when(mockNetworkInterface.getDisplayName()).thenReturn("eth0");
55+
boolean result = (boolean) declaredMethod.invoke("isPreferredNetworkInterface", mockNetworkInterface);
56+
assertTrue(result);
57+
System.clearProperty(IpUtils.PREFERRED_NETWORK_INTERFACE);
58+
}
59+
60+
@Test
61+
@SneakyThrows
62+
public void assertPreferredNetworkAddress() {
63+
Method declaredMethod = IpUtils.class.getDeclaredMethod("isPreferredAddress", InetAddress.class);
64+
declaredMethod.setAccessible(true);
65+
InetAddress inetAddress = mock(InetAddress.class);
66+
System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "192.168");
67+
when(inetAddress.getHostAddress()).thenReturn("192.168.0.100");
68+
assertTrue((boolean) declaredMethod.invoke("isPreferredAddress", inetAddress));
69+
when(inetAddress.getHostAddress()).thenReturn("10.10.0.100");
70+
assertFalse((boolean) declaredMethod.invoke("isPreferredAddress", inetAddress));
71+
System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
72+
System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "10.10.*");
73+
when(inetAddress.getHostAddress()).thenReturn("10.10.0.100");
74+
assertTrue((boolean) declaredMethod.invoke("isPreferredAddress", inetAddress));
75+
when(inetAddress.getHostAddress()).thenReturn("10.0.0.100");
76+
assertFalse((boolean) declaredMethod.invoke("isPreferredAddress", inetAddress));
77+
System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
78+
}
79+
80+
@Test
81+
@SneakyThrows
82+
public void assertGetFirstNetworkInterface() {
83+
InetAddress address1 = mock(Inet4Address.class);
84+
when(address1.isLoopbackAddress()).thenReturn(false);
85+
when(address1.isAnyLocalAddress()).thenReturn(false);
86+
when(address1.isReachable(100)).thenReturn(true);
87+
when(address1.getHostAddress()).thenReturn("10.10.0.1");
88+
Vector<InetAddress> addresses1 = new Vector<>();
89+
addresses1.add(address1);
90+
InetAddress address2 = mock(Inet4Address.class);
91+
when(address2.isLoopbackAddress()).thenReturn(false);
92+
when(address2.isAnyLocalAddress()).thenReturn(false);
93+
when(address2.isReachable(100)).thenReturn(true);
94+
when(address2.getHostAddress()).thenReturn("192.168.99.100");
95+
Vector<InetAddress> addresses2 = new Vector<>();
96+
addresses2.add(address2);
97+
NetworkInterface networkInterface1 = mock(NetworkInterface.class);
98+
NetworkInterface networkInterface2 = mock(NetworkInterface.class);
99+
when(networkInterface1.getInetAddresses()).thenReturn(addresses1.elements());
100+
when(networkInterface2.getInetAddresses()).thenReturn(addresses2.elements());
101+
when(networkInterface1.getDisplayName()).thenReturn("eth1");
102+
when(networkInterface2.getDisplayName()).thenReturn("eth2");
103+
Method declaredMethod = IpUtils.class.getDeclaredMethod("getFirstNetworkInterface", List.class);
104+
declaredMethod.setAccessible(true);
105+
List<NetworkInterface> validNetworkInterfaces = Arrays.asList(networkInterface1, networkInterface2);
106+
assertThat(declaredMethod.invoke("getFirstNetworkInterface", validNetworkInterfaces), is(networkInterface2));
107+
System.setProperty(IpUtils.PREFERRED_NETWORK_INTERFACE, "eth1");
108+
assertThat(declaredMethod.invoke("getFirstNetworkInterface", validNetworkInterfaces), is(networkInterface1));
109+
System.clearProperty(IpUtils.PREFERRED_NETWORK_INTERFACE);
110+
System.setProperty(IpUtils.PREFERRED_NETWORK_IP, "10.10.*");
111+
assertThat(declaredMethod.invoke("getFirstNetworkInterface", validNetworkInterfaces), is(networkInterface1));
112+
System.clearProperty(IpUtils.PREFERRED_NETWORK_IP);
113+
}
114+
36115
@Test
37116
@SneakyThrows
38117
public void assertGetHostName() {

0 commit comments

Comments
 (0)