use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class GridNioFilterChainSelfTest method testChainEvents.
/**
* @throws Exception If failed.
*/
public void testChainEvents() throws Exception {
final AtomicReference<String> connectedEvt = new AtomicReference<>();
final AtomicReference<String> disconnectedEvt = new AtomicReference<>();
final AtomicReference<String> msgEvt = new AtomicReference<>();
final AtomicReference<String> idleEvt = new AtomicReference<>();
final AtomicReference<String> writeTimeoutEvt = new AtomicReference<>();
final AtomicReference<String> sndEvt = new AtomicReference<>();
final AtomicReference<String> closeEvt = new AtomicReference<>();
final AtomicReference<ByteBuffer> rcvdMsgObj = new AtomicReference<>();
final AtomicReference<Object> sndMsgObj = new AtomicReference<>();
GridNioServerListener<Object> testLsnr = new GridNioServerListenerAdapter<Object>() {
@Override
public void onConnected(GridNioSession ses) {
connectedEvt.compareAndSet(null, ses.<String>meta(OPENED_META_NAME));
}
@Override
public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
disconnectedEvt.compareAndSet(null, ses.<String>meta(CLOSED_META_NAME));
}
@Override
public void onMessage(GridNioSession ses, Object msg) {
msgEvt.compareAndSet(null, ses.<String>meta(MESSAGE_RECEIVED_META_NAME));
rcvdMsgObj.compareAndSet(null, (ByteBuffer) msg);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) {
writeTimeoutEvt.compareAndSet(null, ses.<String>meta(WRITE_TIMEOUT_META_NAME));
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) {
idleEvt.compareAndSet(null, ses.<String>meta(IDLE_META_NAME));
}
};
GridNioFilterAdapter testHead = new GridNioFilterAdapter("TestHead") {
@Override
public void onSessionOpened(GridNioSession ses) throws IgniteCheckedException {
proceedSessionOpened(ses);
}
@Override
public void onSessionClosed(GridNioSession ses) throws IgniteCheckedException {
proceedSessionClosed(ses);
}
@Override
public void onExceptionCaught(GridNioSession ses, IgniteCheckedException ex) throws IgniteCheckedException {
proceedExceptionCaught(ses, ex);
}
@Override
public GridNioFuture<?> onSessionWrite(GridNioSession ses, Object msg, boolean fut, IgniteInClosure<IgniteException> ackC) {
sndEvt.compareAndSet(null, ses.<String>meta(MESSAGE_WRITE_META_NAME));
sndMsgObj.compareAndSet(null, msg);
return null;
}
@Override
public void onMessageReceived(GridNioSession ses, Object msg) throws IgniteCheckedException {
proceedMessageReceived(ses, msg);
}
@Override
public GridNioFuture<Boolean> onSessionClose(GridNioSession ses) {
closeEvt.compareAndSet(null, ses.<String>meta(CLOSE_META_NAME));
return null;
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) throws IgniteCheckedException {
proceedSessionIdleTimeout(ses);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) throws IgniteCheckedException {
proceedSessionWriteTimeout(ses);
}
};
GridNioFilterChain<Object> chain = new GridNioFilterChain<>(log, testLsnr, testHead, new AppendingFilter("A"), new AppendingFilter("B"), new AppendingFilter("C"), new AppendingFilter("D"));
GridNioSession ses = new MockNioSession();
ByteBuffer snd = ByteBuffer.wrap(new byte[1]);
ByteBuffer rcvd = ByteBuffer.wrap(new byte[1]);
chain.onSessionOpened(ses);
chain.onSessionClosed(ses);
chain.onMessageReceived(ses, rcvd);
chain.onSessionIdleTimeout(ses);
chain.onSessionWriteTimeout(ses);
assertNull(chain.onSessionClose(ses));
assertNull(chain.onSessionWrite(ses, snd, true, null));
assertEquals("DCBA", connectedEvt.get());
assertEquals("DCBA", disconnectedEvt.get());
assertEquals("DCBA", msgEvt.get());
assertEquals("DCBA", idleEvt.get());
assertEquals("DCBA", writeTimeoutEvt.get());
assertEquals("ABCD", sndEvt.get());
assertEquals("ABCD", closeEvt.get());
assertTrue(snd == sndMsgObj.get());
assertTrue(rcvd == rcvdMsgObj.get());
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class GridNioBenchmarkTest method run.
/**
* Runs the benchmark.
*
* @throws UnknownHostException If can't connect to given hist,
* @throws IgniteCheckedException If NIO server initialisation failed.
*/
@SuppressWarnings("ConstantConditions")
public void run() throws UnknownHostException, IgniteCheckedException {
GridNioServerListener<ByteBuffer> lsnr = new GridNioServerListenerAdapter<ByteBuffer>() {
@Override
public void onConnected(GridNioSession ses) {
X.print("New connection accepted.");
}
@Override
public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
// No-op.
}
@Override
public void onMessage(GridNioSession ses, ByteBuffer msg) {
ByteBuffer buf = ByteBuffer.allocate(msg.remaining()).put(msg);
buf.position(0);
ses.send(buf);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) {
X.error("Session write timeout. Closing.");
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) {
X.error("Session idle timeout. Closing.");
}
};
IgniteLogger log = new GridTestLog4jLogger(U.resolveIgniteUrl("config/ignite-log4j.xml"));
GridNioServer.<ByteBuffer>builder().address(InetAddress.getByName("localhost")).port(port).listener(lsnr).logger(log).selectorCount(selectorCnt).igniteInstanceName("").tcpNoDelay(false).directBuffer(false).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(0).socketReceiveBufferSize(0).sendQueueLimit(0).build().start();
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead3.
/**
* @throws Exception If failed.
*/
public void testBlockRead3() 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.
}
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 IgniteTcpCommunicationRecoveryAckClosureSelfTest method checkOverflow.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void checkOverflow() throws Exception {
TcpCommunicationSpi spi0 = spis.get(0);
TcpCommunicationSpi spi1 = spis.get(1);
ClusterNode node0 = nodes.get(0);
ClusterNode node1 = nodes.get(1);
final GridNioServer srv1 = U.field(spi1, "nioSrvr");
final AtomicInteger ackMsgs = new AtomicInteger(0);
IgniteInClosure<IgniteException> ackC = new CI1<IgniteException>() {
@Override
public void apply(IgniteException o) {
assert o == null;
ackMsgs.incrementAndGet();
}
};
int msgId = 0;
// Send message to establish connection.
spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
// Prevent node1 from send
GridTestUtils.setFieldValue(srv1, "skipWrite", true);
final GridNioSession ses0 = communicationSession(spi0);
int sentMsgs = 1;
for (int i = 0; i < 150; i++) {
try {
spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
sentMsgs++;
} catch (IgniteSpiException e) {
log.info("Send error [err=" + e + ", sentMsgs=" + sentMsgs + ']');
break;
}
}
// Wait when session is closed because of queue overflow.
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return ses0.closeTime() != 0;
}
}, 5000);
assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
GridTestUtils.setFieldValue(srv1, "skipWrite", false);
for (int i = 0; i < 100; i++) spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
final int expMsgs = sentMsgs + 100;
final TestListener lsnr = (TestListener) spi1.getListener();
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return lsnr.rcvCnt.get() >= expMsgs;
}
}, 5000);
assertEquals(expMsgs, lsnr.rcvCnt.get());
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return expMsgs == ackMsgs.get();
}
}, 5000);
assertEquals(expMsgs, ackMsgs.get());
}
use of org.apache.ignite.internal.util.nio.GridNioSession in project ignite by apache.
the class IgniteTcpCommunicationRecoveryAckClosureSelfTest method checkAck.
/**
* @param ackCnt Recovery acknowledgement count.
* @param idleTimeout Idle connection timeout.
* @param msgPerIter Messages per iteration.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void checkAck(int ackCnt, int idleTimeout, int msgPerIter) throws Exception {
createSpis(ackCnt, idleTimeout, TcpCommunicationSpi.DFLT_MSG_QUEUE_LIMIT);
try {
TcpCommunicationSpi spi0 = spis.get(0);
TcpCommunicationSpi spi1 = spis.get(1);
ClusterNode node0 = nodes.get(0);
ClusterNode node1 = nodes.get(1);
int msgId = 0;
int expMsgs = 0;
long totAcked = 0;
for (int i = 0; i < 5; i++) {
info("Iteration: " + i);
final AtomicInteger ackMsgs = new AtomicInteger(0);
IgniteInClosure<IgniteException> ackC = new CI1<IgniteException>() {
@Override
public void apply(IgniteException o) {
assert o == null;
ackMsgs.incrementAndGet();
}
};
for (int j = 0; j < msgPerIter; j++) {
spi0.sendMessage(node1, new GridTestMessage(node0.id(), ++msgId, 0), ackC);
spi1.sendMessage(node0, new GridTestMessage(node1.id(), ++msgId, 0), ackC);
}
expMsgs += msgPerIter;
final long totAcked0 = totAcked;
for (TcpCommunicationSpi spi : spis) {
GridNioServer srv = U.field(spi, "nioSrvr");
Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
assertFalse(sessions.isEmpty());
boolean found = false;
for (GridNioSession ses : sessions) {
final GridNioRecoveryDescriptor recoveryDesc = ses.outRecoveryDescriptor();
if (recoveryDesc != null) {
found = true;
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
long acked = GridTestUtils.getFieldValue(recoveryDesc, "acked");
return acked > totAcked0;
}
}, 5000);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return recoveryDesc.messagesRequests().isEmpty();
}
}, 10_000);
assertEquals("Unexpected messages: " + recoveryDesc.messagesRequests(), 0, recoveryDesc.messagesRequests().size());
break;
}
}
assertTrue(found);
}
final int expMsgs0 = expMsgs;
for (TcpCommunicationSpi spi : spis) {
final TestListener lsnr = (TestListener) spi.getListener();
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return lsnr.rcvCnt.get() >= expMsgs0;
}
}, 5000);
assertEquals(expMsgs, lsnr.rcvCnt.get());
}
assertEquals(msgPerIter * 2, ackMsgs.get());
totAcked += msgPerIter;
}
} finally {
stopSpis();
}
}
Aggregations