use of org.apache.ignite.internal.util.nio.GridNioServer in project ignite by apache.
the class HadoopExternalCommunication method resetNioServer.
/**
* Recreates tpcSrvr socket instance.
*
* @return Server instance.
* @throws IgniteCheckedException Thrown if it's not possible to create server.
*/
private GridNioServer<HadoopMessage> resetNioServer() throws IgniteCheckedException {
if (boundTcpPort >= 0)
throw new IgniteCheckedException("Tcp NIO server was already created on port " + boundTcpPort);
IgniteCheckedException lastEx = null;
// If configured TCP port is busy, find first available in range.
for (int port = locPort; port < locPort + locPortRange; port++) {
try {
GridNioServer<HadoopMessage> srvr = GridNioServer.<HadoopMessage>builder().address(locHost).port(port).listener(srvLsnr).logger(log.getLogger(GridNioServer.class)).selectorCount(selectorsCnt).igniteInstanceName(igniteInstanceName).serverName("hadoop").tcpNoDelay(tcpNoDelay).directBuffer(directBuf).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(sockSndBuf).socketReceiveBufferSize(sockRcvBuf).sendQueueLimit(msgQueueLimit).directMode(false).filters(filters()).build();
boundTcpPort = port;
// Ack Port the TCP server was bound to.
if (log.isInfoEnabled())
log.info("Successfully bound to TCP port [port=" + boundTcpPort + ", locHost=" + locHost + ']');
return srvr;
} catch (IgniteCheckedException e) {
lastEx = e;
if (log.isDebugEnabled())
log.debug("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']');
}
}
// If free port wasn't found.
throw new IgniteCheckedException("Failed to bind to any port within range [startPort=" + locPort + ", portRange=" + locPortRange + ", locHost=" + locHost + ']', lastEx);
}
use of org.apache.ignite.internal.util.nio.GridNioServer in project ignite by apache.
the class GridTcpCommunicationSpiMultithreadedSelfTest method testFlowSend.
/**
* @throws Exception If failed.
*/
public void testFlowSend() throws Exception {
reject = true;
final CyclicBarrier barrier = new CyclicBarrier(THREAD_CNT);
final Random rnd = new Random();
final ClusterNode from = randomNode(rnd);
ClusterNode tmp;
do {
tmp = randomNode(rnd);
} while (tmp.id().equals(from.id()));
final ClusterNode to = tmp;
final int iterationCnt = 1000;
final AtomicInteger threadId = new AtomicInteger();
final int interval = 50;
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
try {
// Only first thread will print messages.
int id = threadId.getAndIncrement();
for (int i = 0; i < iterationCnt; i++) {
if (id == 0 && (i % 50) == 0)
info(">>> Running iteration " + i);
try {
for (ClusterNode node : nodes) {
Message msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
spis.get(from.id()).sendMessage(node, msg);
}
} catch (IgniteException e) {
log.warning(">>> Oops, unable to send message (safe to ignore).", e);
}
barrier.await();
}
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
} catch (BrokenBarrierException e) {
info("Wait on barrier failed: " + e);
Thread.currentThread().interrupt();
}
}
}, THREAD_CNT, "message-sender");
final AtomicBoolean run = new AtomicBoolean(true);
IgniteInternalFuture<?> fut2 = multithreadedAsync(new Runnable() {
@Override
public void run() {
try {
while (run.get() && !Thread.currentThread().isInterrupted()) {
U.sleep(interval * 3 / 2);
((TcpCommunicationSpi) spis.get(from.id())).onNodeLeft(to.id());
}
} catch (IgniteInterruptedCheckedException ignored) {
Thread.currentThread().interrupt();
}
}
}, 1);
fut.get();
run.set(false);
fut2.get();
// Wait when all messages are acknowledged to do not break next tests' logic.
for (CommunicationSpi<Message> spi : spis.values()) {
GridNioServer srv = U.field(spi, "nioSrvr");
Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
for (GridNioSession ses : sessions) {
final GridNioRecoveryDescriptor snd = ses.outRecoveryDescriptor();
if (snd != null) {
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return snd.messagesRequests().isEmpty();
}
}, 10_000);
assertEquals("Unexpected messages: " + snd.messagesRequests(), 0, snd.messagesRequests().size());
}
}
}
}
use of org.apache.ignite.internal.util.nio.GridNioServer in project ignite by apache.
the class ClientReconnectContinuousQueryTest method skipRead.
/**
* @param igniteClient Ignite client.
* @param skip Skip.
*/
private void skipRead(IgniteEx igniteClient, boolean skip) {
GridIoManager ioMgr = igniteClient.context().io();
TcpCommunicationSpi commSpi = (TcpCommunicationSpi) ((Object[]) U.field(ioMgr, "spis"))[0];
GridNioServer nioSrvr = U.field(commSpi, "nioSrvr");
GridTestUtils.setFieldValue(nioSrvr, "skipRead", skip);
}
use of org.apache.ignite.internal.util.nio.GridNioServer 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 = U.field(spi, "clients");
assertEquals(1, clients.size());
final GridNioServer srv = U.field(spi, "nioSrvr");
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.internal.util.nio.GridNioServer 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;
}
Aggregations