use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class TcpRestParserSelfTest method testCustomMessages.
/**
* @throws Exception If failed.
*/
public void testCustomMessages() throws Exception {
GridClientCacheRequest req = new GridClientCacheRequest(CAS);
req.key("key");
req.value(1);
req.value2(2);
req.clientId(UUID.randomUUID());
ByteBuffer raw = clientRequestPacket(req);
GridNioSession ses = new MockNioSession();
ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
GridClientMessage msg = parser.decode(ses, raw);
assertNotNull(msg);
assertEquals("Parser leaved unparsed bytes", 0, raw.remaining());
assertTrue(msg instanceof GridClientCacheRequest);
GridClientCacheRequest res = (GridClientCacheRequest) msg;
assertEquals("Invalid operation", req.operation(), res.operation());
assertEquals("Invalid clientId", req.clientId(), res.clientId());
assertEquals("Invalid key", req.key(), res.key());
assertEquals("Invalid value 1", req.value(), res.value());
assertEquals("Invalid value 2", req.value2(), res.value2());
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class GridTcpCommunicationSpiRecoveryAckSelfTest method communicationSession.
/**
* @param spi SPI.
* @return Session.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private GridNioSession communicationSession(TcpCommunicationSpi spi) throws Exception {
final GridNioServer srv = U.field(spi, "nioSrvr");
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
return !sessions.isEmpty();
}
}, 5000);
Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
assertEquals(1, sessions.size());
return sessions.iterator().next();
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead1.
/**
* @throws Exception If failed.
*/
public void testBlockRead1() throws Exception {
createSpis();
try {
final TcpCommunicationSpi spi0 = spis.get(0);
final TcpCommunicationSpi spi1 = spis.get(1);
final TestListener lsnr1 = (TestListener) spi1.getListener();
final ClusterNode node0 = nodes.get(0);
final ClusterNode node1 = nodes.get(1);
final AtomicInteger msgId = new AtomicInteger();
// Send message to establish connection.
spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
final AtomicInteger sentCnt = new AtomicInteger(1);
int errCnt = 0;
for (int i = 0; i < ITERS; i++) {
log.info("Iteration: " + i);
try {
final GridNioSession ses0 = communicationSession(spi0, false);
final GridNioSession ses1 = communicationSession(spi1, true);
ses1.pauseReads().get();
IgniteInternalFuture<?> sndFut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
for (int i = 0; i < 6000; i++) {
spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
sentCnt.incrementAndGet();
}
return null;
}
});
// Wait when session is closed because of write timeout.
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return ses0.closeTime() != 0;
}
}, awaitForSocketWriteTimeout());
assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
try {
ses1.resumeReads().get();
} catch (IgniteCheckedException ignore) {
// Can fail is ses1 was closed.
}
for (int j = 0; j < 100; j++) {
spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
sentCnt.incrementAndGet();
}
sndFut.get();
final int expMsgs = sentCnt.get();
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return lsnr1.rcvCnt.get() >= expMsgs;
}
}, 60_000);
assertEquals(expMsgs, lsnr1.rcvCnt.get());
} catch (IgniteCheckedException e) {
if (e.hasCause(BindException.class)) {
errCnt++;
if (errCnt > 3) {
log.warning("Got exception > 3 times, test fails.");
throw e;
}
if (i < ITERS - 1) {
info("Got exception caused by BindException, will retry after delay: " + e);
U.sleep(10_000);
} else
info("Got exception caused by BindException, will ignore: " + e);
} else {
log.warning("Unexpected exception: " + e, e);
throw e;
}
}
}
} finally {
stopSpis();
}
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class GridTcpCommunicationSpiRecoverySelfTest method communicationSession.
/**
* @param spi SPI.
* @param in {@code True} if need find inbound session.
* @return Session.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private GridNioSession communicationSession(TcpCommunicationSpi spi, boolean in) throws Exception {
final GridNioServer srv = U.field(spi, "nioSrvr");
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
return !sessions.isEmpty();
}
}, awaitForSocketWriteTimeout());
Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
for (GridNioSession ses : sessions) {
if (in == ses.accepted())
return ses;
}
fail("Failed to find session");
return null;
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class HadoopExternalCommunication method createShmemClient.
/**
* @param desc Process descriptor.
* @param port Port.
* @return Client.
* @throws IgniteCheckedException If failed.
*/
@Nullable
protected HadoopCommunicationClient createShmemClient(HadoopProcessDescriptor desc, int port) throws IgniteCheckedException {
int attempt = 1;
int connectAttempts = 1;
long connTimeout0 = connTimeout;
while (true) {
IpcEndpoint clientEndpoint;
try {
clientEndpoint = new IpcSharedMemoryClientEndpoint(port, (int) connTimeout, log);
} catch (IgniteCheckedException e) {
// Reconnect for the second time, if connection is not established.
if (connectAttempts < 2 && X.hasCause(e, ConnectException.class)) {
connectAttempts++;
continue;
}
throw e;
}
HadoopCommunicationClient client = null;
try {
ShmemWorker worker = new ShmemWorker(clientEndpoint, false);
shmemWorkers.add(worker);
GridNioSession ses = worker.session();
HandshakeFinish fin = new HandshakeFinish();
// We are in lock, it is safe to get session and attach
ses.addMeta(HANDSHAKE_FINISH_META, fin);
client = new HadoopTcpNioCommunicationClient(ses);
new IgniteThread(worker).start();
fin.await(connTimeout0);
} catch (HadoopHandshakeTimeoutException e) {
if (log.isDebugEnabled())
log.debug("Handshake timed out (will retry with increased timeout) [timeout=" + connTimeout0 + ", err=" + e.getMessage() + ", client=" + client + ']');
if (client != null)
client.forceClose();
if (attempt == reconCnt || connTimeout0 > maxConnTimeout) {
if (log.isDebugEnabled())
log.debug("Handshake timedout (will stop attempts to perform the handshake) " + "[timeout=" + connTimeout0 + ", maxConnTimeout=" + maxConnTimeout + ", attempt=" + attempt + ", reconCnt=" + reconCnt + ", err=" + e.getMessage() + ", client=" + client + ']');
throw e;
} else {
attempt++;
connTimeout0 *= 2;
continue;
}
} catch (RuntimeException | Error e) {
if (log.isDebugEnabled())
log.debug("Caught exception (will close client) [err=" + e.getMessage() + ", client=" + client + ']');
if (client != null)
client.forceClose();
throw e;
}
return client;
}
}
Aggregations