use of org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder in project ignite by apache.
the class ClientImpl method joinTopology.
/**
* @param prevAddr If reconnect is in progress, then previous address of the router the client was connected to
* and {@code null} otherwise.
* @param timeout Timeout.
* @return Opened socket or {@code null} if timeout.
* @throws InterruptedException If interrupted.
* @throws IgniteSpiException If failed.
* @see TcpDiscoverySpi#joinTimeout
*/
@SuppressWarnings("BusyWait")
@Nullable
private T2<SocketStream, Boolean> joinTopology(InetSocketAddress prevAddr, long timeout) throws IgniteSpiException, InterruptedException {
List<InetSocketAddress> addrs = null;
long startTime = U.currentTimeMillis();
while (true) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedException();
while (addrs == null || addrs.isEmpty()) {
addrs = new ArrayList<>(spi.resolvedAddresses());
if (!F.isEmpty(addrs)) {
if (log.isDebugEnabled())
log.debug("Resolved addresses from IP finder: " + addrs);
} else {
if (timeout > 0 && (U.currentTimeMillis() - startTime) > timeout)
return null;
LT.warn(log, "IP finder returned empty addresses list. " + "Please check IP finder configuration" + (spi.ipFinder instanceof TcpDiscoveryMulticastIpFinder ? " and make sure multicast works on your network. " : ". ") + "Will retry every " + spi.getReconnectDelay() + " ms. " + "Change 'reconnectDelay' to configure the frequency of retries.", true);
Thread.sleep(spi.getReconnectDelay());
}
}
// Process failed node last.
if (prevAddr != null) {
int idx = addrs.indexOf(prevAddr);
if (idx != -1)
Collections.swap(addrs, idx, 0);
}
Collection<InetSocketAddress> addrs0 = new ArrayList<>(addrs);
boolean wait = false;
for (int i = addrs.size() - 1; i >= 0; i--) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedException();
InetSocketAddress addr = addrs.get(i);
boolean recon = prevAddr != null;
T3<SocketStream, Integer, Boolean> sockAndRes = sendJoinRequest(recon, addr);
if (sockAndRes == null) {
addrs.remove(i);
continue;
}
assert sockAndRes.get1() != null && sockAndRes.get2() != null : sockAndRes;
Socket sock = sockAndRes.get1().socket();
if (log.isDebugEnabled())
log.debug("Received response to join request [addr=" + addr + ", res=" + sockAndRes.get2() + ']');
switch(sockAndRes.get2()) {
case RES_OK:
return new T2<>(sockAndRes.get1(), sockAndRes.get3());
case RES_CONTINUE_JOIN:
case RES_WAIT:
wait = true;
U.closeQuiet(sock);
break;
default:
if (log.isDebugEnabled())
log.debug("Received unexpected response to join request: " + sockAndRes.get2());
U.closeQuiet(sock);
}
}
if (timeout > 0 && (U.currentTimeMillis() - startTime) > timeout)
return null;
if (wait) {
if (log.isDebugEnabled())
log.debug("Will wait before retry join.");
Thread.sleep(spi.getReconnectDelay());
} else if (addrs.isEmpty()) {
LT.warn(log, "Failed to connect to any address from IP finder (will retry to join topology " + "every " + spi.getReconnectDelay() + " ms; change 'reconnectDelay' to configure the frequency " + "of retries): " + toOrderedList(addrs0), true);
Thread.sleep(spi.getReconnectDelay());
}
}
}
use of org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder in project ignite by apache.
the class PlatformConfigurationUtils method readDiscoveryConfiguration.
/**
* Reads discovery configuration from a stream and updates provided IgniteConfiguration.
*
* @param cfg IgniteConfiguration to update.
* @param in Reader.
*/
private static void readDiscoveryConfiguration(BinaryRawReader in, IgniteConfiguration cfg) {
boolean hasCfg = in.readBoolean();
if (!hasCfg)
return;
TcpDiscoverySpi disco = new TcpDiscoverySpi();
boolean hasIpFinder = in.readBoolean();
if (hasIpFinder) {
byte ipFinderType = in.readByte();
int addrCnt = in.readInt();
ArrayList<String> addrs = null;
if (addrCnt > 0) {
addrs = new ArrayList<>(addrCnt);
for (int i = 0; i < addrCnt; i++) addrs.add(in.readString());
}
TcpDiscoveryVmIpFinder finder = null;
if (ipFinderType == 1)
finder = new TcpDiscoveryVmIpFinder();
else if (ipFinderType == 2) {
TcpDiscoveryMulticastIpFinder finder0 = new TcpDiscoveryMulticastIpFinder();
finder0.setLocalAddress(in.readString());
finder0.setMulticastGroup(in.readString());
finder0.setMulticastPort(in.readInt());
finder0.setAddressRequestAttempts(in.readInt());
finder0.setResponseWaitTime(in.readInt());
boolean hasTtl = in.readBoolean();
if (hasTtl)
finder0.setTimeToLive(in.readInt());
finder = finder0;
} else
assert false;
finder.setAddresses(addrs);
disco.setIpFinder(finder);
}
disco.setSocketTimeout(in.readLong());
disco.setAckTimeout(in.readLong());
disco.setMaxAckTimeout(in.readLong());
disco.setNetworkTimeout(in.readLong());
disco.setJoinTimeout(in.readLong());
disco.setForceServerMode(in.readBoolean());
disco.setClientReconnectDisabled(in.readBoolean());
disco.setLocalAddress(in.readString());
disco.setReconnectCount(in.readInt());
disco.setLocalPort(in.readInt());
disco.setLocalPortRange(in.readInt());
disco.setStatisticsPrintFrequency(in.readLong());
disco.setIpFinderCleanFrequency(in.readLong());
disco.setThreadPriority(in.readInt());
disco.setTopHistorySize(in.readInt());
cfg.setDiscoverySpi(disco);
}
use of org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder in project ignite by apache.
the class TcpDiscoverySpi method initializeImpl.
/**
*/
private void initializeImpl() {
if (impl != null)
return;
sslEnable = ignite().configuration().getSslContextFactory() != null;
initFailureDetectionTimeout();
if (!forceSrvMode && (Boolean.TRUE.equals(ignite.configuration().isClientMode()))) {
if (ackTimeout == 0)
ackTimeout = DFLT_ACK_TIMEOUT_CLIENT;
if (sockTimeout == 0)
sockTimeout = DFLT_SOCK_TIMEOUT_CLIENT;
impl = new ClientImpl(this);
ctxInitLatch.countDown();
} else {
if (ackTimeout == 0)
ackTimeout = DFLT_ACK_TIMEOUT;
if (sockTimeout == 0)
sockTimeout = DFLT_SOCK_TIMEOUT;
impl = new ServerImpl(this);
}
metricsUpdateFreq = ignite.configuration().getMetricsUpdateFrequency();
if (!failureDetectionTimeoutEnabled()) {
assertParameter(sockTimeout > 0, "sockTimeout > 0");
assertParameter(ackTimeout > 0, "ackTimeout > 0");
assertParameter(maxAckTimeout > ackTimeout, "maxAckTimeout > ackTimeout");
assertParameter(reconCnt > 0, "reconnectCnt > 0");
}
assertParameter(netTimeout > 0, "networkTimeout > 0");
assertParameter(ipFinder != null, "ipFinder != null");
assertParameter(metricsUpdateFreq > 0, "metricsUpdateFreq > 0" + " (inited from igniteConfiguration.metricsUpdateFrequency)");
assertParameter(ipFinderCleanFreq > 0, "ipFinderCleanFreq > 0");
assertParameter(locPort > 1023, "localPort > 1023");
assertParameter(locPortRange >= 0, "localPortRange >= 0");
assertParameter(locPort + locPortRange <= 0xffff, "locPort + locPortRange <= 0xffff");
assertParameter(threadPri > 0, "threadPri > 0");
assertParameter(statsPrintFreq >= 0, "statsPrintFreq >= 0");
if (isSslEnabled()) {
try {
SSLContext sslCtx = ignite().configuration().getSslContextFactory().create();
sslSockFactory = sslCtx.getSocketFactory();
sslSrvSockFactory = sslCtx.getServerSocketFactory();
} catch (IgniteException e) {
throw new IgniteSpiException("Failed to create SSL context. SSL factory: " + ignite.configuration().getSslContextFactory(), e);
}
}
try {
locHost = U.resolveLocalHost(locAddr);
} catch (IOException e) {
throw new IgniteSpiException("Unknown local address: " + locAddr, e);
}
if (log.isDebugEnabled()) {
log.debug(configInfo("localHost", locHost.getHostAddress()));
log.debug(configInfo("localPort", locPort));
log.debug(configInfo("localPortRange", locPortRange));
log.debug(configInfo("threadPri", threadPri));
if (!failureDetectionTimeoutEnabled()) {
log.debug("Failure detection timeout is ignored because at least one of the parameters from this list" + " has been set explicitly: 'sockTimeout', 'ackTimeout', 'maxAckTimeout', 'reconnectCount'.");
log.debug(configInfo("networkTimeout", netTimeout));
log.debug(configInfo("sockTimeout", sockTimeout));
log.debug(configInfo("ackTimeout", ackTimeout));
log.debug(configInfo("maxAckTimeout", maxAckTimeout));
log.debug(configInfo("reconnectCount", reconCnt));
} else
log.debug(configInfo("failureDetectionTimeout", failureDetectionTimeout()));
log.debug(configInfo("ipFinder", ipFinder));
log.debug(configInfo("ipFinderCleanFreq", ipFinderCleanFreq));
log.debug(configInfo("metricsUpdateFreq", metricsUpdateFreq));
log.debug(configInfo("statsPrintFreq", statsPrintFreq));
}
// Warn on odd network timeout.
if (netTimeout < 3000)
U.warn(log, "Network timeout is too low (at least 3000 ms recommended): " + netTimeout);
if (ipFinder instanceof TcpDiscoveryMulticastIpFinder) {
TcpDiscoveryMulticastIpFinder mcastIpFinder = ((TcpDiscoveryMulticastIpFinder) ipFinder);
if (mcastIpFinder.getLocalAddress() == null)
mcastIpFinder.setLocalAddress(locAddr);
}
cfgNodeId = ignite.configuration().getNodeId();
}
use of org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder in project ignite by apache.
the class ClientImpl method joinTopology.
/**
* @param prevAddr If reconnect is in progress, then previous address of the router the client was connected to
* and {@code null} otherwise.
* @param timeout Timeout.
* @param beforeEachSleep code to be run before each sleep span.
* @param afterEachSleep code to be run before each sleep span.
* @return Opened socket or {@code null} if timeout.
* @throws InterruptedException If interrupted.
* @throws IgniteSpiException If failed.
* @see TcpDiscoverySpi#joinTimeout
*/
@Nullable
private T2<SocketStream, Boolean> joinTopology(InetSocketAddress prevAddr, long timeout, @Nullable Runnable beforeEachSleep, @Nullable Runnable afterEachSleep) throws IgniteSpiException, InterruptedException {
List<InetSocketAddress> addrs = null;
long startNanos = System.nanoTime();
while (true) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedException();
while (addrs == null || addrs.isEmpty()) {
addrs = new ArrayList<>(spi.resolvedAddresses());
if (!F.isEmpty(addrs)) {
if (log.isDebugEnabled())
log.debug("Resolved addresses from IP finder: " + addrs);
} else {
if (timeout > 0 && U.millisSinceNanos(startNanos) > timeout)
return null;
LT.warn(log, "IP finder returned empty addresses list. " + "Please check IP finder configuration" + (spi.ipFinder instanceof TcpDiscoveryMulticastIpFinder ? " and make sure multicast works on your network. " : ". ") + "Will retry every " + spi.getReconnectDelay() + " ms. " + "Change 'reconnectDelay' to configure the frequency of retries.", true);
sleepEx(spi.getReconnectDelay(), beforeEachSleep, afterEachSleep);
}
}
// Process failed node last.
if (prevAddr != null) {
int idx = addrs.indexOf(prevAddr);
if (idx != -1)
Collections.swap(addrs, idx, 0);
}
Collection<InetSocketAddress> addrs0 = new ArrayList<>(addrs);
boolean wait = false;
for (int i = addrs.size() - 1; i >= 0; i--) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedException();
InetSocketAddress addr = addrs.get(i);
boolean recon = prevAddr != null;
T3<SocketStream, Integer, Boolean> sockAndRes = sendJoinRequest(recon, addr);
if (sockAndRes == null) {
addrs.remove(i);
continue;
}
assert sockAndRes.get1() != null && sockAndRes.get2() != null : sockAndRes;
Socket sock = sockAndRes.get1().socket();
if (log.isDebugEnabled())
log.debug("Received response to join request [addr=" + addr + ", res=" + sockAndRes.get2() + ']');
switch(sockAndRes.get2()) {
case RES_OK:
return new T2<>(sockAndRes.get1(), sockAndRes.get3());
case RES_CONTINUE_JOIN:
case RES_WAIT:
wait = true;
U.closeQuiet(sock);
break;
default:
if (log.isDebugEnabled())
log.debug("Received unexpected response to join request: " + sockAndRes.get2());
U.closeQuiet(sock);
}
}
if (timeout > 0 && U.millisSinceNanos(startNanos) > timeout)
return null;
if (wait) {
if (log.isDebugEnabled())
log.debug("Will wait before retry join.");
sleepEx(spi.getReconnectDelay(), beforeEachSleep, afterEachSleep);
} else if (addrs.isEmpty()) {
LT.warn(log, "Failed to connect to any address from IP finder (will retry to join topology " + "every " + spi.getReconnectDelay() + " ms; change 'reconnectDelay' to configure the frequency " + "of retries): " + toOrderedList(addrs0), true);
sleepEx(spi.getReconnectDelay(), beforeEachSleep, afterEachSleep);
}
}
}
use of org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder in project ignite by apache.
the class TcpDiscoverySelfTest method testMulticastIpFinder.
/**
* @throws Exception If any error occurs.
*/
@Test
public void testMulticastIpFinder() throws Exception {
try {
for (int i = 0; i < 5; i++) {
Ignite g = startGrid("MulticastIpFinder-" + i);
assertEquals(i + 1, g.cluster().nodes().size());
TcpDiscoverySpi spi = (TcpDiscoverySpi) g.configuration().getDiscoverySpi();
TcpDiscoveryMulticastIpFinder ipFinder = (TcpDiscoveryMulticastIpFinder) spi.getIpFinder();
boolean found = false;
for (GridPortRecord rec : ((IgniteKernal) g).context().ports().records()) {
if ((rec.protocol() == UDP) && rec.port() == ipFinder.getMulticastPort()) {
found = true;
break;
}
}
assertTrue("TcpDiscoveryMulticastIpFinder should register port.", found);
}
} finally {
stopAllGrids();
}
}
Aggregations