Phillippko commented on code in PR #7483:
URL: https://github.com/apache/ignite-3/pull/7483#discussion_r2734909450
##########
modules/network/src/main/java/org/apache/ignite/internal/network/StaticNodeFinder.java:
##########
@@ -84,47 +112,53 @@ public void start() {
// No-op
}
- private static String[] resolveAll(String host) {
- InetAddress[] inetAddresses = null;
+ private List<NetworkAddress> resolveAddress(NetworkAddress address) {
+ InetAddress[] inetAddresses = resolveInetAddresses(address.host());
- int tryCount = 0;
- boolean resolved = false;
+ if (inetAddresses.length == 0) {
+ return List.of();
+ }
- do {
- tryCount++;
+ var addresses = new ArrayList<NetworkAddress>(inetAddresses.length);
- try {
- inetAddresses = InetAddress.getAllByName(host);
- resolved = true;
- } catch (UnknownHostException e) {
- if (tryCount == MAX_TRIES) {
- LOG.warn("Cannot resolve {}", host);
- return ArrayUtils.STRING_EMPTY_ARRAY;
- }
+ for (InetAddress inetAddress : inetAddresses) {
+ if (inetAddress.isLoopbackAddress()) {
+ // If it's a loopback address (like 127.0.0.1), only return
this address.
+ return List.of(new
NetworkAddress(inetAddress.getHostAddress(), address.port()));
+ }
+
+ addresses.add(new NetworkAddress(inetAddress.getHostAddress(),
address.port()));
+ }
+
+ return addresses;
+ }
+
+ private InetAddress[] resolveInetAddresses(String host) {
+ try {
+ int tryCount = 1;
+ while (true) {
try {
- Thread.sleep(tryCount * RETRY_WAIT_BASE_MILLIS);
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
+ return hostNameResolver.getAllByName(host);
+ } catch (UnknownHostException e) {
+ LOG.warn("Cannot resolve host \"{}\" (attempt {}/{}).",
host, tryCount, nameResolutionAttempts);
- return ArrayUtils.STRING_EMPTY_ARRAY;
- }
- }
- } while (!resolved);
+ if (tryCount >= nameResolutionAttempts) {
+ break;
+ }
- assert inetAddresses != null;
- String[] addresses = new String[inetAddresses.length];
- for (int i = 0; i < inetAddresses.length; i++) {
- InetAddress inetAddress = inetAddresses[i];
+ //noinspection BusyWait
+ Thread.sleep(tryCount * RETRY_WAIT_BASE_MILLIS);
Review Comment:
So for default 10 tries we will wait for total of 27500 millis, is it
desirable?
##########
modules/network/src/main/java/org/apache/ignite/internal/network/StaticNodeFinder.java:
##########
@@ -84,47 +112,53 @@ public void start() {
// No-op
}
- private static String[] resolveAll(String host) {
- InetAddress[] inetAddresses = null;
+ private List<NetworkAddress> resolveAddress(NetworkAddress address) {
+ InetAddress[] inetAddresses = resolveInetAddresses(address.host());
- int tryCount = 0;
- boolean resolved = false;
+ if (inetAddresses.length == 0) {
+ return List.of();
+ }
- do {
- tryCount++;
+ var addresses = new ArrayList<NetworkAddress>(inetAddresses.length);
- try {
- inetAddresses = InetAddress.getAllByName(host);
- resolved = true;
- } catch (UnknownHostException e) {
- if (tryCount == MAX_TRIES) {
- LOG.warn("Cannot resolve {}", host);
- return ArrayUtils.STRING_EMPTY_ARRAY;
- }
+ for (InetAddress inetAddress : inetAddresses) {
+ if (inetAddress.isLoopbackAddress()) {
+ // If it's a loopback address (like 127.0.0.1), only return
this address.
+ return List.of(new
NetworkAddress(inetAddress.getHostAddress(), address.port()));
+ }
+
+ addresses.add(new NetworkAddress(inetAddress.getHostAddress(),
address.port()));
+ }
+
+ return addresses;
+ }
+
+ private InetAddress[] resolveInetAddresses(String host) {
+ try {
+ int tryCount = 1;
+ while (true) {
try {
- Thread.sleep(tryCount * RETRY_WAIT_BASE_MILLIS);
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
+ return hostNameResolver.getAllByName(host);
Review Comment:
InetAddess.getAllByName by default uses cache, from the code it looks like
until value is expired we will get the same value with the same
UnknownHostException
##########
modules/network/src/main/java/org/apache/ignite/internal/network/StaticNodeFinder.java:
##########
@@ -44,19 +44,50 @@
*/
public class StaticNodeFinder implements NodeFinder {
private static final IgniteLogger LOG =
Loggers.forClass(StaticNodeFinder.class);
+
private static final long RETRY_WAIT_BASE_MILLIS = 500;
- private static final int MAX_TRIES = 3;
/** List of seed cluster members. */
- private final List<NetworkAddress> addresses;
+ private final Set<NetworkAddress> addresses;
+
+ private final int nameResolutionAttempts;
+
+ private final HostNameResolver hostNameResolver;
+
+ /**
+ * Class for resolving host names.
+ *
+ * <p>Needed for writing cleaner tests.
+ */
+ @FunctionalInterface
+ public interface HostNameResolver {
+ /**
+ * Given the name of a host, returns an array of its IP addresses,
based on the configured name service on the system.
+ */
+ InetAddress[] getAllByName(String host) throws UnknownHostException;
+ }
/**
* Constructor.
*
* @param addresses Addresses of initial cluster members.
*/
+ @TestOnly
Review Comment:
If we pass hostNameResolver just for tests, should we make constructor with
this param a TestOnly, and constuctor using InetAddress:getAllByName a
production one?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]