Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -145,6 +145,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.jar.JarFile;
import java.util.logging.ConsoleHandler;
Expand Down Expand Up @@ -598,6 +599,9 @@ public abstract class IgniteUtils {
private static final Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>>
exceptionConverters;

/** */
public static volatile BiFunction<String, Integer, InetSocketAddress> ADDR_RESOLVER = InetSocketAddress::new;

/** */
private static volatile IgniteBiTuple<Collection<String>, Collection<String>> cachedLocalAddr;

Expand Down Expand Up @@ -6642,9 +6646,10 @@ public static GridPeerDeployAware peerDeployAware0(@Nullable Iterable<?> c) {
}

// If all are nulls - don't throw an exception.
if (notAllNulls)
if (notAllNulls) {
throw new IllegalArgumentException("Failed to find common class loader for all elements in " +
"given collection. Peer deployment cannot be performed for such collection.");
}
}

return peerDeployAware(c);
Expand Down Expand Up @@ -6732,9 +6737,10 @@ public static GridPeerDeployAware peerDeployAware0(@Nullable Object... c) {
}

// If all are nulls - don't throw an exception.
if (notAllNulls)
if (notAllNulls) {
throw new IllegalArgumentException("Failed to find common class loader for all elements in " +
"given collection. Peer deployment cannot be performed for such collection.");
}
}

return peerDeployAware(new Object[0]);
Expand Down Expand Up @@ -9692,67 +9698,7 @@ public static Map<String, Collection<ClusterNode>> neighborhood(Iterable<Cluster
}

/**
* Returns tha list of resolved inet addresses. First addresses are resolved by host names,
* if this attempt fails then the addresses are resolved by ip addresses.
*
* @param node Grid node.
* @return Inet addresses for given addresses and host names.
* @throws IgniteCheckedException If non of addresses can be resolved.
*/
public static Collection<InetAddress> toInetAddresses(ClusterNode node) throws IgniteCheckedException {
return toInetAddresses(node.addresses(), node.hostNames());
}

/**
* Returns tha list of resolved inet addresses. First addresses are resolved by host names,
* if this attempt fails then the addresses are resolved by ip addresses.
*
* @param addrs Addresses.
* @param hostNames Host names.
* @return Inet addresses for given addresses and host names.
* @throws IgniteCheckedException If non of addresses can be resolved.
*/
public static Collection<InetAddress> toInetAddresses(Collection<String> addrs,
Collection<String> hostNames) throws IgniteCheckedException {
Set<InetAddress> res = new HashSet<>(addrs.size());

Iterator<String> hostNamesIt = hostNames.iterator();

for (String addr : addrs) {
String hostName = hostNamesIt.hasNext() ? hostNamesIt.next() : null;

InetAddress inetAddr = null;

if (!F.isEmpty(hostName)) {
try {
inetAddr = InetAddress.getByName(hostName);
}
catch (UnknownHostException ignored) {
}
}

if (inetAddr == null || inetAddr.isLoopbackAddress()) {
try {
inetAddr = InetAddress.getByName(addr);
}
catch (UnknownHostException ignored) {
}
}

if (inetAddr != null)
res.add(inetAddr);
}

if (res.isEmpty())
throw new IgniteCheckedException("Addresses can not be resolved [addr=" + addrs +
", hostNames=" + hostNames + ']');

return res;
}

/**
* Returns tha list of resolved socket addresses. First addresses are resolved by host names,
* if this attempt fails then the addresses are resolved by ip addresses.
* Returns the list of resolved socket addresses.
*
* @param node Grid node.
* @param port Port.
Expand All @@ -9763,37 +9709,38 @@ public static Collection<InetSocketAddress> toSocketAddresses(ClusterNode node,
}

/**
* Returns tha list of resolved socket addresses. First addresses are resolved by host names,
* if this attempt fails then the addresses are resolved by ip addresses.
* For testing purposes. Returns the list of resolved socket addresses.
*
* @param addrs Addresses.
* @param hostNames Host names.
* @param port Port.
* @return Socket addresses for given addresses and host names.
*/
public static Collection<InetSocketAddress> toSocketAddresses(Collection<String> addrs,
Collection<String> hostNames, int port) {
public static Collection<InetSocketAddress> toSocketAddresses(
Collection<String> addrs,
Collection<String> hostNames,
int port
) {
Set<InetSocketAddress> res = new HashSet<>(addrs.size());

Iterator<String> hostNamesIt = hostNames.iterator();
boolean hasAddr = false;

for (String addr : addrs) {
String hostName = hostNamesIt.hasNext() ? hostNamesIt.next() : null;
InetSocketAddress inetSockAddr = createResolved(addr, port);
res.add(inetSockAddr);

if (!F.isEmpty(hostName)) {
InetSocketAddress inetSockAddr = createResolved(hostName, port);
if (!inetSockAddr.isUnresolved() && !inetSockAddr.getAddress().isLoopbackAddress())
hasAddr = true;
}

if (inetSockAddr.isUnresolved() ||
(!inetSockAddr.isUnresolved() && inetSockAddr.getAddress().isLoopbackAddress())
)
inetSockAddr = createResolved(addr, port);
// Try to resolve addresses from host names if no external addresses found.
if (!hasAddr) {
for (String host : hostNames) {
InetSocketAddress inetSockAddr = createResolved(host, port);

res.add(inetSockAddr);
if (!inetSockAddr.isUnresolved())
res.add(inetSockAddr);
}

// Always append address because local and remote nodes may have the same hostname
// therefore remote hostname will always be resolved to local address.
res.add(createResolved(addr, port));
}

return res;
Expand All @@ -9808,6 +9755,8 @@ public static Collection<InetSocketAddress> toSocketAddresses(Collection<String>
* @return Resolved address.
*/
private static InetSocketAddress createResolved(String addr, int port) {
assert ADDR_RESOLVER != null : "No address resolver set";

log.log(Level.FINE, () -> S.toString(
"Resolving address",
"addr", addr, false,
Expand All @@ -9818,7 +9767,7 @@ private static InetSocketAddress createResolved(String addr, int port) {
long startNanos = System.nanoTime();

try {
return new InetSocketAddress(addr, port);
return ADDR_RESOLVER.apply(addr, port);
}
finally {
long endNanos = System.nanoTime();
Expand Down Expand Up @@ -10382,9 +10331,9 @@ public static <K, V> Map<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3) {
* @return a SingletonList containing the element in the original collection
*/
public static <T> Collection<T> convertToSingletonList(Collection<T> col) {
if (col.size() != 1) {
if (col.size() != 1)
throw new IllegalArgumentException("Unexpected collection size for singleton list, expecting 1 but was: " + col.size());
}

return Collections.singletonList(col.iterator().next());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,17 @@ public int discoveryPort() {
* @return Addresses that could be used by discovery.
*/
public Collection<InetSocketAddress> socketAddresses() {
if (this.sockAddrs == null)
sockAddrs = U.toSocketAddresses(this, discPort);
if (sockAddrs == null)
sockAddrs = initSocketAddrs();

return sockAddrs;
}

/** */
protected Collection<InetSocketAddress> initSocketAddrs() {
return U.toSocketAddresses(this, discoveryPort());
}

/**
* Gets node last update time. Used for logging purposes only.<br/>
* Note that this method tries to convert {@code nanoTime} internal JVM time format into a regular timestamp.
Expand Down
Loading