From 0c6221007c0b1d5421c3cb40ef0c19048f43a71e Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 13 Oct 2026 15:09:50 +0800 Subject: [PATCH 1/3] fix(commons): skip unavailable MAC addresses - Skip interfaces without hardware addresses in MAC lists. - Publish the MAC cache only after a complete lookup. - Preserve direct lookup failures and cover real interface boundaries. --- .../apache/hugegraph/license/MachineInfo.java | 19 ++-- .../unit/license/MachineInfoTest.java | 89 +++++++++++++++++-- 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java index cd106921ea..3efcac8f17 100755 --- a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java +++ b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java @@ -57,20 +57,19 @@ public List getMacAddress() { if (this.macAddressList != null) { return this.macAddressList; } - this.macAddressList = new ArrayList<>(); + List list = new ArrayList<>(); List inetAddresses = this.getLocalAllInetAddress(); if (inetAddresses != null && !inetAddresses.isEmpty()) { // Get the Mac address of all network interfaces - List list = new ArrayList<>(); Set uniqueValues = new HashSet<>(); for (InetAddress inetAddress : inetAddresses) { - String macByInetAddress = this.getMacByInetAddress(inetAddress); - if (uniqueValues.add(macByInetAddress)) { + String macByInetAddress = this.getMacByInetAddress(inetAddress, false); + if (macByInetAddress != null && uniqueValues.add(macByInetAddress)) { list.add(macByInetAddress); } } - this.macAddressList = list; } + this.macAddressList = list; return this.macAddressList; } @@ -99,10 +98,20 @@ public List getLocalAllInetAddress() { } public String getMacByInetAddress(InetAddress inetAddr) { + return this.getMacByInetAddress(inetAddr, true); + } + + private String getMacByInetAddress(InetAddress inetAddr, boolean required) { byte[] mac; try { mac = NetworkInterface.getByInetAddress(inetAddr) .getHardwareAddress(); + if (mac == null) { + if (required) { + throw new IllegalStateException("Network interface has no hardware address"); + } + return null; + } } catch (Exception e) { throw new RuntimeException(String.format( "Failed to get mac address for inet address '%s'", diff --git a/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java b/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java index 9dc69cbdb9..b855aadd8d 100644 --- a/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java +++ b/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java @@ -18,10 +18,15 @@ package org.apache.hugegraph.unit.license; import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.regex.Pattern; +import org.junit.Assume; import org.junit.Test; import org.apache.hugegraph.license.MachineInfo; @@ -73,15 +78,87 @@ public void testGetLocalAllInetAddress() { } @Test - public void testGetMacByInetAddress() throws UnknownHostException { + public void testGetMacByInetAddress() throws Exception { List addressList = machineInfo.getLocalAllInetAddress(); for (InetAddress address : addressList) { - String mac = machineInfo.getMacByInetAddress(address); - Assert.assertTrue(MAC_PATTERN.matcher(mac).matches()); + byte[] hardwareAddress = NetworkInterface.getByInetAddress(address) + .getHardwareAddress(); + if (hardwareAddress == null) { + Throwable exception = Assert.assertThrows(RuntimeException.class, + () -> machineInfo.getMacByInetAddress(address)); + Assert.assertFalse(exception instanceof NullPointerException); + } else { + String mac = machineInfo.getMacByInetAddress(address); + Assert.assertTrue(MAC_PATTERN.matcher(mac).matches()); + } } InetAddress address = InetAddress.getByAddress(new byte[]{0, 0, 0, 0}); - Assert.assertThrows(RuntimeException.class, () -> { - machineInfo.getMacByInetAddress(address); - }); + Throwable exception = Assert.assertThrows(RuntimeException.class, + () -> machineInfo.getMacByInetAddress(address)); + Assert.assertFalse(exception instanceof NullPointerException); + } + + @Test + public void testGetMacAddressWithoutMac() throws SocketException { + InetAddress loopback = InetAddress.getLoopbackAddress(); + Assume.assumeTrue(NetworkInterface.getByInetAddress(loopback) + .getHardwareAddress() == null); + MachineInfo info = new MachineInfo() { + @Override + public List getLocalAllInetAddress() { + return Collections.singletonList(loopback); + } + }; + Throwable exception = Assert.assertThrows(RuntimeException.class, + () -> info.getMacByInetAddress(loopback)); + Assert.assertFalse(exception instanceof NullPointerException); + List addresses = info.getMacAddress(); + Assert.assertTrue(addresses.isEmpty()); + Assert.assertSame(addresses, info.getMacAddress()); + } + + @Test + public void testGetMacAddressSkipsMissingAndDuplicateMac() throws SocketException { + InetAddress loopback = InetAddress.getLoopbackAddress(); + Assume.assumeTrue(NetworkInterface.getByInetAddress(loopback) + .getHardwareAddress() == null); + InetAddress addressWithMac = null; + for (InetAddress address : machineInfo.getLocalAllInetAddress()) { + if (NetworkInterface.getByInetAddress(address).getHardwareAddress() != null) { + addressWithMac = address; + break; + } + } + Assume.assumeNotNull(addressWithMac); + InetAddress address = addressWithMac; + MachineInfo info = new MachineInfo() { + @Override + public List getLocalAllInetAddress() { + return Arrays.asList(loopback, address, loopback, address); + } + }; + List addresses = info.getMacAddress(); + Assert.assertEquals(1, addresses.size()); + Assert.assertTrue(addresses.contains(machineInfo.getMacByInetAddress(address))); + Assert.assertSame(addresses, info.getMacAddress()); + } + + @Test + public void testGetMacAddressRetriesAfterLookupFailure() throws UnknownHostException { + InetAddress unknown = InetAddress.getByAddress(new byte[]{0, 0, 0, 0}); + List localAddresses = machineInfo.getLocalAllInetAddress(); + int[] attempts = {0}; + MachineInfo info = new MachineInfo() { + @Override + public List getLocalAllInetAddress() { + if (++attempts[0] == 1) { + return Collections.singletonList(unknown); + } + return localAddresses; + } + }; + Assert.assertThrows(RuntimeException.class, info::getMacAddress); + Assert.assertTrue(info.getMacAddress().equals(new MachineInfo().getMacAddress())); + Assert.assertEquals(2, attempts[0]); } } From 661e8b35266c28365e51de33a7f4f63286a7b96a Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 13 Oct 2026 15:56:17 +0800 Subject: [PATCH 2/3] fix(commons): make MAC retry test deterministic - Inject a controlled first enumeration failure. - Recover with fixed input instead of live re-enumeration. - Verify the retried result stays cached. --- .../hugegraph/unit/license/MachineInfoTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java b/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java index b855aadd8d..a1afbfc559 100644 --- a/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java +++ b/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java @@ -20,7 +20,6 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; -import java.net.UnknownHostException; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -144,21 +143,22 @@ public List getLocalAllInetAddress() { } @Test - public void testGetMacAddressRetriesAfterLookupFailure() throws UnknownHostException { - InetAddress unknown = InetAddress.getByAddress(new byte[]{0, 0, 0, 0}); - List localAddresses = machineInfo.getLocalAllInetAddress(); + public void testGetMacAddressRetriesAfterLookupFailure() { + RuntimeException failure = new RuntimeException("Controlled interface enumeration failure"); int[] attempts = {0}; MachineInfo info = new MachineInfo() { @Override public List getLocalAllInetAddress() { if (++attempts[0] == 1) { - return Collections.singletonList(unknown); + throw failure; } - return localAddresses; + return Collections.emptyList(); } }; - Assert.assertThrows(RuntimeException.class, info::getMacAddress); - Assert.assertTrue(info.getMacAddress().equals(new MachineInfo().getMacAddress())); + Assert.assertSame(failure, Assert.assertThrows(RuntimeException.class, info::getMacAddress)); + List addresses = info.getMacAddress(); + Assert.assertTrue(addresses.isEmpty()); + Assert.assertSame(addresses, info.getMacAddress()); Assert.assertEquals(2, attempts[0]); } } From fbb1cb14f882f9cf6e23c8306ead0ea04ef99ead Mon Sep 17 00:00:00 2001 From: imbajin Date: Wed, 14 Oct 2026 00:19:03 +0800 Subject: [PATCH 3/3] fix(commons): preserve missing MAC diagnosis - keep required MAC validation outside lookup wrapping - retain optional null MAC and retry cache behavior - verify the existing loopback diagnostic regression --- .../org/apache/hugegraph/license/MachineInfo.java | 12 ++++++------ .../hugegraph/unit/license/MachineInfoTest.java | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java index 3efcac8f17..1b2f4a3271 100755 --- a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java +++ b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/license/MachineInfo.java @@ -106,17 +106,17 @@ private String getMacByInetAddress(InetAddress inetAddr, boolean required) { try { mac = NetworkInterface.getByInetAddress(inetAddr) .getHardwareAddress(); - if (mac == null) { - if (required) { - throw new IllegalStateException("Network interface has no hardware address"); - } - return null; - } } catch (Exception e) { throw new RuntimeException(String.format( "Failed to get mac address for inet address '%s'", inetAddr)); } + if (mac == null) { + if (required) { + throw new IllegalStateException("Network interface has no hardware address"); + } + return null; + } StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { diff --git a/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java b/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java index a1afbfc559..3e5a0ba4ea 100644 --- a/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java +++ b/hugegraph-commons/hugegraph-common/src/test/java/org/apache/hugegraph/unit/license/MachineInfoTest.java @@ -111,6 +111,7 @@ public List getLocalAllInetAddress() { Throwable exception = Assert.assertThrows(RuntimeException.class, () -> info.getMacByInetAddress(loopback)); Assert.assertFalse(exception instanceof NullPointerException); + Assert.assertEquals("Network interface has no hardware address", exception.getMessage()); List addresses = info.getMacAddress(); Assert.assertTrue(addresses.isEmpty()); Assert.assertSame(addresses, info.getMacAddress());