Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public class NetworkAddress {
* @param port Port.
*/
public NetworkAddress(String host, int port) {
if (host == null || host.isEmpty()) {
throw new IllegalArgumentException("Host address cannot be null or empty");
}

if (port < 1024 || port > 65535) {
throw new IllegalArgumentException("Invalid port number: " + port);
}

this.host = host;
this.port = port;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,57 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.net.InetSocketAddress;
import org.apache.ignite.network.NetworkAddress;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Test suite for {@link NetworkAddress}.
*/
class NetworkAddressTest {
/**
* Test constructing a new {@link NetworkAddress} with good parameters.
*/
@Test
void testConstructorWithGoodParams() {
String host = "some.host";
int port = 1234;

assertDoesNotThrow(() -> new NetworkAddress(host, port));
}

/**
* Test constructing a new {@link NetworkAddress} with bad host parameters.
host */
@ParameterizedTest
@ValueSource(strings = {"null", ""})
void testConstructorWithBadHostParam(String host) {
if (host.equals("null")) {
host = null;
}

int port = 1234;

String finalHost = host;
assertThrows(IllegalArgumentException.class, () -> new NetworkAddress(finalHost, port));
}

/**
* Test constructing a new {@link NetworkAddress} with bad parameters.
*/
@ParameterizedTest
@ValueSource(ints = {-1, 0, 1023})
void testConstructorWithBadPortParams(int port) {
String host = "some.host";

assertThrows(IllegalArgumentException.class, () -> new NetworkAddress(host, port));
}

/**
* Test parsing of a {@link NetworkAddress} from a string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

package org.apache.ignite.internal.network;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
import static org.apache.ignite.lang.ErrorGroups.Network.ADDRESS_UNRESOLVED_ERR;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.ignite.internal.lang.IgniteInternalException;
import org.apache.ignite.internal.logger.IgniteLogger;
import org.apache.ignite.internal.logger.Loggers;
import org.apache.ignite.internal.util.ArrayUtils;
Expand Down Expand Up @@ -56,12 +60,22 @@ public StaticNodeFinder(List<NetworkAddress> addresses) {

@Override
public Collection<NetworkAddress> findNodes() {
return addresses.parallelStream()
if (addresses.isEmpty()) {
return Collections.emptyList();
}

Collection<NetworkAddress> networkAddresses = addresses.parallelStream()
.flatMap(
originalAddress -> Arrays.stream(resolveAll(originalAddress.host()))
.map(ip -> new NetworkAddress(ip, originalAddress.port()))
)
.collect(toList());
.collect(toSet());

if (networkAddresses.isEmpty()) {
throw new IgniteInternalException(ADDRESS_UNRESOLVED_ERR, "No network address found");
}

return networkAddresses;
}

@Override
Expand All @@ -70,14 +84,33 @@ public void start() {
}

private static String[] resolveAll(String host) {
InetAddress[] inetAddresses;
try {
inetAddresses = InetAddress.getAllByName(host);
} catch (UnknownHostException e) {
LOG.warn("Cannot resolve {}", host);
return ArrayUtils.STRING_EMPTY_ARRAY;
}
InetAddress[] inetAddresses = null;

final int maxTries = 3;
int tryCount = 0;
boolean resolved = false;

do {
tryCount++;

try {
inetAddresses = InetAddress.getAllByName(host);
resolved = true;
} catch (UnknownHostException e) {
if (tryCount == maxTries) {
LOG.warn("Cannot resolve {}", host);
return ArrayUtils.STRING_EMPTY_ARRAY;
}

try {
Thread.sleep(tryCount * 500L);
} catch (InterruptedException ex) {
throw new IgniteInternalException(INTERNAL_ERR, ex);
}
}
} while (!resolved);

assert inetAddresses != null;
String[] addresses = new String[inetAddresses.length];
for (int i = 0; i < inetAddresses.length; i++) {
InetAddress inetAddress = inetAddresses[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,6 +35,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.ignite.internal.lang.IgniteInternalException;
import org.apache.ignite.internal.testframework.WorkDirectory;
import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
import org.apache.ignite.network.NetworkAddress;
Expand All @@ -52,6 +56,40 @@ void returnsIpAddresses() {
assertThat(finder.findNodes(), contains(ipv4, ipv6));
}

@Test
void removesDuplicateIpAddresses() {
NetworkAddress ip1 = new NetworkAddress("1.2.3.4", 3001);
NetworkAddress ip2 = new NetworkAddress("1.2.3.4", 3001);
NodeFinder finder = new StaticNodeFinder(List.of(ip1, ip2));

assertThat(finder.findNodes(), contains(ip1));
}

@Test
void returnsEmptyResultForEmptyInput() {
NodeFinder finder = new StaticNodeFinder(List.of());

assertEquals(0, finder.findNodes().size());
}

@Test
void failsForNoResolvedIpAddresses() {
NetworkAddress ip1 = new NetworkAddress("badIpString", 3001);
NodeFinder finder = new StaticNodeFinder(List.of(ip1));

assertThrows(IgniteInternalException.class, finder::findNodes);
}

@Test
void succeedsForAtLeastOneResolvedIpAddresses() {
NetworkAddress ip1 = new NetworkAddress("badIpString", 3001);
NetworkAddress ip2 = new NetworkAddress("1.2.3.4", 3001);
NodeFinder finder = new StaticNodeFinder(List.of(ip1, ip2));

assertDoesNotThrow(finder::findNodes);
assertThat(finder.findNodes(), contains(ip2));
}

@Test
void resolvesLocalHostToJustOneAddress() throws Exception {
Path hostsFilePath = writeHostsFile(Map.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void setUp(
String nodeName = testNodeName(testInfo, 0);

when(topologyService.localMember())
.thenReturn(new ClusterNodeImpl(randomUUID(), nodeName, new NetworkAddress("localhost", 0)));
.thenReturn(new ClusterNodeImpl(randomUUID(), nodeName, new NetworkAddress("localhost", 65535)));
when(clusterService.topologyService()).thenReturn(topologyService);
when(topologyAwareRaftGroupService.unsubscribeLeader()).thenReturn(nullCompletedFuture());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void startReplicaManager(
when(clusterService.messagingService()).thenReturn(messagingService);
when(clusterService.topologyService()).thenReturn(topologyService);

when(topologyService.localMember()).thenReturn(new ClusterNodeImpl(randomUUID(), nodeName, new NetworkAddress("foo", 0)));
when(topologyService.localMember()).thenReturn(new ClusterNodeImpl(randomUUID(), nodeName, new NetworkAddress("foo", 65535)));

when(cmgManager.metaStorageNodes()).thenReturn(emptySetCompletedFuture());

Expand Down