use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.
the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead3.
/**
* @throws Exception If failed.
*/
@Test
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));
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return lsnr1.rcvCnt.get() >= 1;
}
}, 1000);
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(() -> {
for (int i1 = 0; i1 < 6000; i1++) {
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(() -> 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(() -> lsnr1.rcvCnt.get() >= expMsgs, 60_000);
assertEquals(expMsgs, lsnr1.rcvCnt.get());
assertTrue(waitForSessionsCount(spi1, 1));
} 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.spi.communication.GridTestMessage in project ignite by apache.
the class GridTcpCommunicationSpiConcurrentConnectSelfTest method concurrentConnect.
/**
* @param threads Number of threads.
* @param msgPerThread Messages per thread.
* @param iters Number of iterations.
* @param sleep If {@code true} sleeps random time before starts send messages.
* @param load Run load threads flag.
* @throws Exception If failed.
*/
private void concurrentConnect(final int threads, final int msgPerThread, final int iters, final boolean sleep, boolean load) throws Exception {
log.info("Concurrent connect [threads=" + threads + ", msgPerThread=" + msgPerThread + ", iters=" + iters + ", load=" + load + ", sleep=" + sleep + ']');
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<?> loadFut = null;
if (load) {
loadFut = GridTestUtils.runMultiThreadedAsync(new Callable<Long>() {
@Override
public Long call() throws Exception {
long dummyRes = 0;
List<String> list = new ArrayList<>();
while (!stop.get()) {
for (int i = 0; i < 100; i++) {
String str = new String(new byte[i]);
list.add(str);
dummyRes += str.hashCode();
}
if (list.size() > 1000_000) {
list = new ArrayList<>();
System.gc();
}
}
return dummyRes;
}
}, 2, "test-load");
}
try {
for (int i = 0; i < iters; i++) {
log.info("Iteration: " + i);
final AtomicInteger msgId = new AtomicInteger();
final int expMsgs = threads * msgPerThread;
CountDownLatch latch = new CountDownLatch(expMsgs);
MessageListener lsnr = new MessageListener(latch);
createSpis(lsnr);
final AtomicInteger idx = new AtomicInteger();
try {
final Callable<Void> c = new Callable<Void>() {
@Override
public Void call() throws Exception {
int idx0 = idx.getAndIncrement();
Thread.currentThread().setName("Test thread [idx=" + idx0 + ", grid=" + (idx0 % 2) + ']');
CommunicationSpi<Message> spi = spis.get(idx0 % 2);
ClusterNode srcNode = nodes.get(idx0 % 2);
ClusterNode dstNode = nodes.get((idx0 + 1) % 2);
if (sleep) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
long millis = rnd.nextLong(10);
if (millis > 0)
Thread.sleep(millis);
}
for (int i = 0; i < msgPerThread; i++) spi.sendMessage(dstNode, new GridTestMessage(srcNode.id(), msgId.incrementAndGet(), 0));
return null;
}
};
List<Thread> threadsList = new ArrayList<>();
final AtomicBoolean fail = new AtomicBoolean();
final AtomicLong tId = new AtomicLong();
for (int t = 0; t < threads; t++) {
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
try {
c.call();
} catch (Throwable e) {
log.error("Unexpected error: " + e, e);
fail.set(true);
}
}
}) {
@Override
public long getId() {
// Override getId to use all connections.
return tId.getAndIncrement();
}
};
threadsList.add(t0);
t0.start();
}
for (Thread t0 : threadsList) t0.join();
assertTrue(latch.await(10, TimeUnit.SECONDS));
for (CommunicationSpi<?> spi : spis) {
ConcurrentMap<UUID, GridCommunicationClient> clients = GridTestUtils.getFieldValue(spi, "clientPool", "clients");
assertEquals(1, clients.size());
final GridNioServer<?> srv = ((GridNioServerWrapper) U.field(spi, "nioSrvWrapper")).nio();
final int conns = pairedConnections ? 2 : 1;
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
Collection<?> sessions = U.field(srv, "sessions");
return sessions.size() == conns * connectionsPerNode;
}
}, 5000);
Collection<?> sessions = U.field(srv, "sessions");
assertEquals(conns * connectionsPerNode, sessions.size());
}
assertEquals(expMsgs, lsnr.cntr.get());
} finally {
stopSpis();
}
}
} finally {
stop.set(true);
if (loadFut != null)
loadFut.get();
}
}
Aggregations