use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.
the class TcpClientDiscoverySpiFailureTimeoutSelfTest method clientReconnectOnCoordinatorRouterFail.
/**
* Test tries to provoke scenario when client sends reconnect message before router failure detected.
*
* @param srvNodes Number of additional server nodes.
* @throws Exception If failed.
*/
public void clientReconnectOnCoordinatorRouterFail(int srvNodes) throws Exception {
startServerNodes(1);
Ignite srv = G.ignite("server-0");
final TcpDiscoveryNode srvNode = (TcpDiscoveryNode) srv.cluster().localNode();
final UUID srvNodeId = srvNode.id();
clientIpFinder = new TcpDiscoveryVmIpFinder();
clientIpFinder.setAddresses(Collections.singleton("localhost:" + srvNode.discoveryPort() + ".." + (srvNode.discoveryPort() + 1)));
failureThreshold = 1000L;
netTimeout = 1000L;
// Client should connect to coordinator.
startClientNodes(1);
failureThreshold = 10_000L;
netTimeout = 5000L;
List<String> nodes = new ArrayList<>();
for (int i = 0; i < srvNodes; i++) {
Ignite g = startGrid("server-" + srvIdx.getAndIncrement());
nodes.add(g.name());
srvNodeIds.add(g.cluster().localNode().id());
}
checkNodes(1 + srvNodes, 1);
nodes.add("client-0");
final CountDownLatch latch = new CountDownLatch(nodes.size());
final AtomicBoolean err = new AtomicBoolean();
for (String node : nodes) {
G.ignite(node).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
DiscoveryEvent disoEvt = (DiscoveryEvent) evt;
if (disoEvt.eventNode().id().equals(srvNodeId)) {
info("Expected node failed event: " + ((DiscoveryEvent) evt).eventNode());
latch.countDown();
} else {
log.info("Unexpected node failed event: " + evt);
err.set(true);
}
return true;
}
}, EVT_NODE_FAILED);
}
Thread.sleep(5000);
Ignite client = G.ignite("client-0");
UUID nodeId = client.cluster().localNode().id();
log.info("Fail coordinator: " + srvNodeId);
TestTcpDiscoverySpi srvSpi = (TestTcpDiscoverySpi) srv.configuration().getDiscoverySpi();
srvSpi.pauseAll(false);
try {
Thread.sleep(2000);
} finally {
srvSpi.simulateNodeFailure();
srvSpi.resumeAll();
}
try {
assertTrue(latch.await(failureThreshold + 3000, TimeUnit.MILLISECONDS));
assertFalse("Unexpected event, see log for details.", err.get());
assertEquals(nodeId, client.cluster().localNode().id());
} finally {
srvSpi.resumeAll();
}
}
use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.
the class TcpClientDiscoverySpiSelfTest method clientMessageWorkerStart.
/**
* @param srvs Number of server nodes.
* @param connectTo What server connect to.
* @throws Exception If failed.
*/
private void clientMessageWorkerStart(int srvs, int connectTo) throws Exception {
startServerNodes(srvs);
Ignite srv = G.ignite("server-" + (connectTo - 1));
final TcpDiscoveryNode srvNode = (TcpDiscoveryNode) srv.cluster().localNode();
assertEquals((long) connectTo, srvNode.order());
TestTcpDiscoverySpi srvSpi = ((TestTcpDiscoverySpi) srv.configuration().getDiscoverySpi());
final String client0 = "client-" + clientIdx.getAndIncrement();
srvSpi.delayJoinAckFor = client0;
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
clientIpFinder = new TcpDiscoveryVmIpFinder();
clientIpFinder.setAddresses(Collections.singleton("localhost:" + srvNode.discoveryPort()));
Ignite client = startGrid(client0);
clientIpFinder = null;
clientNodeIds.add(client.cluster().localNode().id());
TestTcpDiscoverySpi clientSpi = ((TestTcpDiscoverySpi) client.configuration().getDiscoverySpi());
assertFalse(clientSpi.invalidResponse());
TcpDiscoveryNode clientNode = (TcpDiscoveryNode) client.cluster().localNode();
assertEquals(srvNode.id(), clientNode.clientRouterNodeId());
return null;
}
});
final String client1 = "client-" + clientIdx.getAndIncrement();
while (!fut.isDone()) {
startGrid(client1);
stopGrid(client1);
}
fut.get();
checkNodes(srvs, 1);
}
use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.
the class TcpClientDiscoverySpiSelfTest method reconnectAfterFail.
/**
* @param changeTop If {@code true} topology is changed after client disconnects.
* @throws Exception If failed.
*/
private void reconnectAfterFail(final boolean changeTop) throws Exception {
startServerNodes(1);
startClientNodes(1);
Ignite srv = G.ignite("server-0");
TestTcpDiscoverySpi srvSpi = ((TestTcpDiscoverySpi) srv.configuration().getDiscoverySpi());
Ignite client = G.ignite("client-0");
final ClusterNode clientNode = client.cluster().localNode();
final UUID clientId = clientNode.id();
final TestTcpDiscoverySpi clientSpi = ((TestTcpDiscoverySpi) client.configuration().getDiscoverySpi());
assertEquals(2L, clientNode.order());
final CountDownLatch failLatch = new CountDownLatch(1);
final CountDownLatch joinLatch = new CountDownLatch(1);
srv.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
info("Server event: " + evt);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
if (evt0.eventNode().id().equals(clientId) && (evt.type() == EVT_NODE_FAILED)) {
if (evt.type() == EVT_NODE_FAILED)
failLatch.countDown();
} else if (evt.type() == EVT_NODE_JOINED) {
TcpDiscoveryNode node = (TcpDiscoveryNode) evt0.eventNode();
if ("client-0".equals(node.attribute(IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME))) {
assertEquals(changeTop ? 5L : 4L, node.order());
joinLatch.countDown();
}
}
return true;
}
}, EVT_NODE_FAILED, EVT_NODE_JOINED);
final CountDownLatch reconnectLatch = new CountDownLatch(1);
final CountDownLatch disconnectLatch = new CountDownLatch(1);
client.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
info("Client event: " + evt);
if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
assertEquals(1, reconnectLatch.getCount());
disconnectLatch.countDown();
if (changeTop)
clientSpi.pauseAll(false);
} else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
assertEquals(0, disconnectLatch.getCount());
reconnectLatch.countDown();
}
return true;
}
}, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
srvSpi.failNode(client.cluster().localNode().id(), null);
if (changeTop) {
Ignite g = startGrid("server-" + srvIdx.getAndIncrement());
srvNodeIds.add(g.cluster().localNode().id());
clientSpi.resumeAll();
}
assertTrue(disconnectLatch.await(5000, MILLISECONDS));
assertTrue(reconnectLatch.await(5000, MILLISECONDS));
assertTrue(failLatch.await(5000, MILLISECONDS));
assertTrue(joinLatch.await(5000, MILLISECONDS));
long topVer = changeTop ? 5L : 4L;
assertEquals(topVer, client.cluster().localNode().order());
assertEquals(topVer, client.cluster().topologyVersion());
Collection<ClusterNode> clientTop = client.cluster().topology(topVer);
assertEquals(changeTop ? 3 : 2, clientTop.size());
clientNodeIds.remove(clientId);
clientNodeIds.add(client.cluster().localNode().id());
checkNodes(changeTop ? 2 : 1, 1);
Ignite g = startGrid("server-" + srvIdx.getAndIncrement());
srvNodeIds.add(g.cluster().localNode().id());
checkNodes(changeTop ? 3 : 2, 1);
}
use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.
the class TcpDiscoverySpi method initLocalNode.
/**
* @param srvPort Server port.
* @param addExtAddrAttr If {@code true} adds {@link #ATTR_EXT_ADDRS} attribute.
*/
protected void initLocalNode(int srvPort, boolean addExtAddrAttr) {
// Init local node.
initAddresses();
locNode = new TcpDiscoveryNode(ignite.configuration().getNodeId(), addrs.get1(), addrs.get2(), srvPort, metricsProvider, locNodeVer, consistentId());
if (addExtAddrAttr) {
Collection<InetSocketAddress> extAddrs = addrRslvr == null ? null : U.resolveAddresses(addrRslvr, F.flat(Arrays.asList(addrs.get1(), addrs.get2())), locNode.discoveryPort());
locNodeAddrs = new LinkedHashSet<>();
locNodeAddrs.addAll(locNode.socketAddresses());
if (extAddrs != null) {
locNodeAttrs.put(createSpiAttributeName(ATTR_EXT_ADDRS), extAddrs);
locNodeAddrs.addAll(extAddrs);
}
}
locNode.setAttributes(locNodeAttrs);
locNode.local(true);
DiscoverySpiListener lsnr = this.lsnr;
if (lsnr != null)
lsnr.onLocalNodeInitialized(locNode);
if (log.isDebugEnabled())
log.debug("Local node initialized: " + locNode);
}
use of org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode in project ignite by apache.
the class ClientImpl method sendJoinRequest.
/**
* @param recon {@code True} if reconnects.
* @param addr Address.
* @return Socket, connect response and client acknowledge support flag.
*/
@Nullable
private T3<SocketStream, Integer, Boolean> sendJoinRequest(boolean recon, InetSocketAddress addr) {
assert addr != null;
if (log.isDebugEnabled())
log.debug("Send join request [addr=" + addr + ", reconnect=" + recon + ", locNodeId=" + getLocalNodeId() + ']');
Collection<Throwable> errs = null;
long ackTimeout0 = spi.getAckTimeout();
int reconCnt = 0;
int connectAttempts = 1;
int sslConnectAttempts = 3;
UUID locNodeId = getLocalNodeId();
IgniteSpiOperationTimeoutHelper timeoutHelper = new IgniteSpiOperationTimeoutHelper(spi, true);
while (true) {
boolean openSock = false;
Socket sock = null;
try {
long tstamp = U.currentTimeMillis();
sock = spi.openSocket(addr, timeoutHelper);
openSock = true;
TcpDiscoveryHandshakeRequest req = new TcpDiscoveryHandshakeRequest(locNodeId);
req.client(true);
spi.writeToSocket(sock, req, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
TcpDiscoveryHandshakeResponse res = spi.readMessage(sock, null, ackTimeout0);
UUID rmtNodeId = res.creatorNodeId();
assert rmtNodeId != null;
assert !getLocalNodeId().equals(rmtNodeId);
spi.stats.onClientSocketInitialized(U.currentTimeMillis() - tstamp);
locNode.clientRouterNodeId(rmtNodeId);
tstamp = U.currentTimeMillis();
TcpDiscoveryAbstractMessage msg;
if (!recon) {
TcpDiscoveryNode node = locNode;
if (locNode.order() > 0) {
node = locNode.clientReconnectNode(spi.spiCtx.nodeAttributes());
marshalCredentials(node);
}
msg = new TcpDiscoveryJoinRequestMessage(node, spi.collectExchangeData(new DiscoveryDataPacket(getLocalNodeId())));
} else
msg = new TcpDiscoveryClientReconnectMessage(getLocalNodeId(), rmtNodeId, lastMsgId);
msg.client(true);
spi.writeToSocket(sock, msg, timeoutHelper.nextTimeoutChunk(spi.getSocketTimeout()));
spi.stats.onMessageSent(msg, U.currentTimeMillis() - tstamp);
if (log.isDebugEnabled())
log.debug("Message has been sent to address [msg=" + msg + ", addr=" + addr + ", rmtNodeId=" + rmtNodeId + ']');
return new T3<>(new SocketStream(sock), spi.readReceipt(sock, timeoutHelper.nextTimeoutChunk(ackTimeout0)), res.clientAck());
} catch (IOException | IgniteCheckedException e) {
U.closeQuiet(sock);
if (log.isDebugEnabled())
log.error("Exception on joining: " + e.getMessage(), e);
onException("Exception on joining: " + e.getMessage(), e);
if (errs == null)
errs = new ArrayList<>();
errs.add(e);
if (X.hasCause(e, SSLException.class)) {
if (--sslConnectAttempts == 0)
throw new IgniteSpiException("Unable to establish secure connection. " + "Was remote cluster configured with SSL? [rmtAddr=" + addr + ", errMsg=\"" + e.getMessage() + "\"]", e);
continue;
}
if (X.hasCause(e, StreamCorruptedException.class)) {
if (--sslConnectAttempts == 0)
throw new IgniteSpiException("Unable to establish plain connection. " + "Was remote cluster configured with SSL? [rmtAddr=" + addr + ", errMsg=\"" + e.getMessage() + "\"]", e);
continue;
}
if (timeoutHelper.checkFailureTimeoutReached(e))
break;
if (!spi.failureDetectionTimeoutEnabled() && ++reconCnt == spi.getReconnectCount())
break;
if (!openSock) {
// Reconnect for the second time, if connection is not established.
if (connectAttempts < 2) {
connectAttempts++;
continue;
}
// Don't retry if we can not establish connection.
break;
}
if (!spi.failureDetectionTimeoutEnabled() && (e instanceof SocketTimeoutException || X.hasCause(e, SocketTimeoutException.class))) {
ackTimeout0 *= 2;
if (!checkAckTimeout(ackTimeout0))
break;
}
}
}
if (log.isDebugEnabled())
log.debug("Failed to join to address [addr=" + addr + ", recon=" + recon + ", errs=" + errs + ']');
return null;
}
Aggregations