use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper 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();
}
}
use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper in project ignite by apache.
the class TcpDiscoveryNetworkIssuesTest method processNetworkThreads.
/**
* Simulates network failure on certain node.
*/
private void processNetworkThreads(Ignite ignite, Consumer<Thread> proc) {
DiscoverySpi disco = ignite.configuration().getDiscoverySpi();
ServerImpl serverImpl = U.field(disco, "impl");
for (Thread thread : serverImpl.threads()) proc.accept(thread);
CommunicationSpi<?> comm = ignite.configuration().getCommunicationSpi();
GridNioServerWrapper nioServerWrapper = U.field(comm, "nioSrvWrapper");
for (GridWorker worker : nioServerWrapper.nio().workers()) proc.accept(worker.runner());
}
Aggregations