use of org.apache.ignite.internal.util.nio.GridNioServer in project ignite by apache.
the class IgniteCommunicationBalanceTest method waitNioBalanceStop.
/**
* @param nodes Node.
* @param timeout Timeout.
* @throws Exception If failed.
*/
private void waitNioBalanceStop(List<Ignite> nodes, long timeout) throws Exception {
final List<GridNioServer> srvs = new ArrayList<>();
for (Ignite node : nodes) {
TcpCommunicationSpi spi = (TcpCommunicationSpi) node.configuration().getCommunicationSpi();
GridNioServer srv = GridTestUtils.getFieldValue(spi, "nioSrvr");
srvs.add(srv);
}
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
List<Long> rCnts = new ArrayList<>();
List<Long> wCnts = new ArrayList<>();
for (GridNioServer srv : srvs) {
long readerMovCnt1 = srv.readerMoveCount();
long writerMovCnt1 = srv.writerMoveCount();
rCnts.add(readerMovCnt1);
wCnts.add(writerMovCnt1);
}
U.sleep(2000);
for (int i = 0; i < srvs.size(); i++) {
GridNioServer srv = srvs.get(i);
long readerMovCnt1 = rCnts.get(i);
long writerMovCnt1 = wCnts.get(i);
long readerMovCnt2 = srv.readerMoveCount();
long writerMovCnt2 = srv.writerMoveCount();
if (readerMovCnt1 != readerMovCnt2) {
log.info("Readers balance is in progress [node=" + i + ", cnt1=" + readerMovCnt1 + ", cnt2=" + readerMovCnt2 + ']');
return false;
}
if (writerMovCnt1 != writerMovCnt2) {
log.info("Writers balance is in progress [node=" + i + ", cnt1=" + writerMovCnt1 + ", cnt2=" + writerMovCnt2 + ']');
return false;
}
}
return true;
}
}, timeout));
}
use of org.apache.ignite.internal.util.nio.GridNioServer in project ignite by apache.
the class IgniteCommunicationBalanceTest method testBalance1.
/**
* @throws Exception If failed.
*/
public void testBalance1() throws Exception {
System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "5000");
try {
selectors = 4;
final int SRVS = 6;
startGridsMultiThreaded(SRVS);
client = true;
final Ignite client = startGrid(SRVS);
for (int i = 0; i < SRVS; i++) {
ClusterNode node = client.cluster().node(ignite(i).cluster().localNode().id());
client.compute(client.cluster().forNode(node)).call(new DummyCallable(null));
}
waitNioBalanceStop(Collections.singletonList(client), 10_000);
final GridNioServer srv = GridTestUtils.getFieldValue(client.configuration().getCommunicationSpi(), "nioSrvr");
ThreadLocalRandom rnd = ThreadLocalRandom.current();
long readMoveCnt1 = srv.readerMoveCount();
long writeMoveCnt1 = srv.writerMoveCount();
int prevNodeIdx = -1;
for (int iter = 0; iter < 10; iter++) {
int nodeIdx = rnd.nextInt(SRVS);
while (prevNodeIdx == nodeIdx) nodeIdx = rnd.nextInt(SRVS);
prevNodeIdx = nodeIdx;
log.info("Iteration [iter=" + iter + ", node=" + nodeIdx + ']');
final long readMoveCnt = readMoveCnt1;
final long writeMoveCnt = writeMoveCnt1;
final int nodeIdx0 = nodeIdx;
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
byte[] data = new byte[100_000];
for (int j = 0; j < 10; j++) {
for (int i = 0; i < SRVS; i++) {
ClusterNode node = client.cluster().node(ignite(i).cluster().localNode().id());
IgniteCompute compute = client.compute(client.cluster().forNode(node));
compute.call(new DummyCallable(i == nodeIdx0 ? data : null));
}
}
if (usePairedConnections())
return srv.readerMoveCount() > readMoveCnt && srv.writerMoveCount() > writeMoveCnt;
else
return srv.readerMoveCount() > readMoveCnt || srv.writerMoveCount() > writeMoveCnt;
}
}, 30_000);
waitNioBalanceStop(Collections.singletonList(client), 30_000);
long readMoveCnt2 = srv.readerMoveCount();
long writeMoveCnt2 = srv.writerMoveCount();
log.info("Move counts [rc1=" + readMoveCnt1 + ", wc1=" + writeMoveCnt1 + ", rc2=" + readMoveCnt2 + ", wc2=" + writeMoveCnt2 + ']');
if (usePairedConnections()) {
assertTrue(readMoveCnt2 > readMoveCnt1);
assertTrue(writeMoveCnt2 > writeMoveCnt1);
} else
assertTrue(readMoveCnt2 > readMoveCnt1 || writeMoveCnt2 > writeMoveCnt1);
readMoveCnt1 = readMoveCnt2;
writeMoveCnt1 = writeMoveCnt2;
}
waitNioBalanceStop(G.allGrids(), 10_000);
} finally {
System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "");
}
}
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 IgniteSlowClientDetectionSelfTest method testSlowClient.
/**
* @throws Exception If failed.
*/
public void testSlowClient() throws Exception {
final IgniteEx slowClient = grid(nodeCount() - 1);
final ClusterNode slowClientNode = slowClient.localNode();
final CountDownLatch evtSegmentedLatch = new CountDownLatch(1);
slowClient.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assertEquals("Unexpected event: " + evt, evt.type(), EventType.EVT_NODE_SEGMENTED);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
assertEquals(slowClientNode, evt0.eventNode());
assertEquals(5L, evt0.topologyVersion());
evtSegmentedLatch.countDown();
return false;
}
}, EventType.EVT_NODE_SEGMENTED);
final CountDownLatch evtFailedLatch = new CountDownLatch(nodeCount() - 1);
for (int i = 0; i < nodeCount() - 1; i++) {
grid(i).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assertEquals("Unexpected event: " + evt, evt.type(), EventType.EVT_NODE_FAILED);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
assertEquals(slowClientNode, evt0.eventNode());
assertEquals(6L, evt0.topologyVersion());
assertEquals(4, evt0.topologyNodes().size());
evtFailedLatch.countDown();
return false;
}
}, EventType.EVT_NODE_FAILED);
}
assertTrue(slowClient.cluster().localNode().isClient());
IgniteCache<Object, Object> cache = slowClient.getOrCreateCache(PARTITIONED);
IgniteEx client0 = grid(nodeCount() - 2);
assertTrue(client0.cluster().localNode().isClient());
IgniteCache<Object, Object> cache0 = client0.getOrCreateCache(PARTITIONED);
cache.query(new ContinuousQuery<>().setLocalListener(new Listener()));
for (int i = 0; i < 100; i++) cache0.put(0, i);
GridIoManager ioMgr = slowClient.context().io();
TcpCommunicationSpi commSpi = (TcpCommunicationSpi) ((Object[]) U.field(ioMgr, "spis"))[0];
GridNioServer nioSrvr = U.field(commSpi, "nioSrvr");
GridTestUtils.setFieldValue(nioSrvr, "skipRead", true);
// Initiate messages for client.
for (int i = 0; i < 100; i++) cache0.put(0, new byte[10 * 1024]);
boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return Ignition.state(slowClient.name()) == IgniteState.STOPPED_ON_SEGMENTATION;
}
}, getTestTimeout());
assertTrue(wait);
assertTrue("Failed to wait for client failed event", evtFailedLatch.await(5000, MILLISECONDS));
assertTrue("Failed to wait for client segmented event", evtSegmentedLatch.await(5000, MILLISECONDS));
}
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());
}
}
}
}
Aggregations