use of org.apache.ignite.spi.communication.GridTestMessage 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.
*/
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);
if (j == 0) {
final TestListener lsnr0 = (TestListener) spi0.getListener();
final TestListener lsnr1 = (TestListener) spi1.getListener();
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return lsnr0.rcvCnt.get() >= 1 && lsnr1.rcvCnt.get() >= 1;
}
}, 1000);
}
}
expMsgs += msgPerIter;
final long totAcked0 = totAcked;
for (TcpCommunicationSpi spi : spis) {
GridNioServer<?> srv = ((GridNioServerWrapper) U.field(spi, "nioSrvWrapper")).nio();
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();
}
}
use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.
the class GridTcpCommunicationSpiMultithreadedSelfTest method testPassThroughPerformance.
/**
* @throws Exception If failed.
*/
@Test
public void testPassThroughPerformance() throws Exception {
reject = true;
info(">>> Starting pass through performance test. <<<");
assertEquals("Invalid listener count", getSpiCount(), lsnrs.size());
final AtomicInteger cntr = new AtomicInteger();
final int msgCnt = 5000;
long start = System.currentTimeMillis();
IgniteInternalFuture<?> fut = multithreadedAsync(() -> {
try {
ClusterNode from = nodes.get(0);
ClusterNode to = nodes.get(1);
CommunicationSpi<Message> spi = spis.get(from.id());
while (cntr.getAndIncrement() < msgCnt) {
GridTestMessage msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
msg.payload(new byte[10 * 1024]);
spi.sendMessage(to, msg);
}
} catch (IgniteException e) {
fail("Unable to send message: " + e.getMessage());
}
}, 5, "message-sender");
fut.get();
info(">>> Sent all messages in " + (System.currentTimeMillis() - start) + " milliseconds");
assertEquals("Invalid count of messages was sent", msgCnt, msgId.get());
}
use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.
the class GridTcpCommunicationSpiMultithreadedSelfTest method beforeTestsStarted.
/**
* {@inheritDoc}
*/
@Override
protected void beforeTestsStarted() throws Exception {
spis.clear();
nodes.clear();
spiRsrcs.clear();
lsnrs.clear();
Map<ClusterNode, GridSpiTestContext> ctxs = new HashMap<>();
timeoutProcessor = new GridTimeoutProcessor(new GridTestKernalContext(log));
timeoutProcessor.start();
timeoutProcessor.onKernalStart(true);
for (int i = 0; i < getSpiCount(); i++) {
CommunicationSpi<Message> spi = newCommunicationSpi();
GridTestUtils.setFieldValue(spi, IgniteSpiAdapter.class, "igniteInstanceName", "grid-" + i);
IgniteTestResources rsrcs = new IgniteTestResources();
GridTestNode node = new GridTestNode(rsrcs.getNodeId());
node.order(i);
GridSpiTestContext ctx = initSpiContext();
MessageFactoryProvider testMsgFactory = factory -> factory.register(GridTestMessage.DIRECT_TYPE, GridTestMessage::new);
ctx.messageFactory(new IgniteMessageFactoryImpl(new MessageFactory[] { new GridIoMessageFactory(), testMsgFactory }));
ctx.timeoutProcessor(timeoutProcessor);
ctx.setLocalNode(node);
info(">>> Initialized context: nodeId=" + ctx.localNode().id());
spiRsrcs.add(rsrcs);
rsrcs.inject(spi);
MessageListener lsnr = new MessageListener(rsrcs.getNodeId());
spi.setListener(lsnr);
lsnrs.put(rsrcs.getNodeId(), lsnr);
info("Lsnrs: " + lsnrs);
spi.spiStart(getTestIgniteInstanceName() + (i + 1));
nodes.add(node);
node.setAttributes(spi.getNodeAttributes());
node.setAttribute(ATTR_MACS, F.concat(U.allLocalMACs(), ", "));
spis.put(rsrcs.getNodeId(), spi);
spi.onContextInitialized(ctx);
ctxs.put(node, ctx);
}
// For each context set remote nodes.
for (Entry<ClusterNode, GridSpiTestContext> e : ctxs.entrySet()) {
for (ClusterNode n : nodes) {
if (!n.equals(e.getKey()))
e.getValue().remoteNodes().add(n);
}
}
}
use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.
the class GridTcpCommunicationSpiMultithreadedSelfTest method testSendToRandomNodesMultithreaded.
/**
* @throws Exception If failed.
*/
@Test
public void testSendToRandomNodesMultithreaded() throws Exception {
info(">>> Starting send to random nodes multithreaded test. <<<");
reject = false;
assertEquals("Invalid listener count", getSpiCount(), lsnrs.size());
final ConcurrentMap<UUID, Deque<GridTestMessage>> msgs = new ConcurrentHashMap<>();
final int iterationCnt = 5000;
long start = System.currentTimeMillis();
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
/**
* Randomizer.
*/
private Random rnd = new Random();
@Override
public void run() {
try {
for (int i = 0; i < iterationCnt; i++) {
ClusterNode from = randomNode(rnd);
ClusterNode to = randomNode(rnd);
GridTestMessage msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
spis.get(from.id()).sendMessage(to, msg);
Deque<GridTestMessage> queue = msgs.get(to.id());
if (queue == null) {
Deque<GridTestMessage> old = msgs.putIfAbsent(to.id(), queue = new ConcurrentLinkedDeque<>());
if (old != null)
queue = old;
}
queue.offer(msg);
}
} catch (IgniteException e) {
log().error("Unable to send message.", e);
fail("Unable to send message: " + e.getMessage());
}
}
}, getSpiCount() * 3, "message-sender");
fut.get();
info(">>> Sent all messages in " + (System.currentTimeMillis() - start) + " milliseconds");
assertEquals("Invalid count of messages was sent", iterationCnt * getSpiCount() * 3, msgId.get());
U.sleep(IDLE_CONN_TIMEOUT * 2);
// Now validate all sent and received messages.
for (Entry<UUID, Deque<GridTestMessage>> e : msgs.entrySet()) {
UUID to = e.getKey();
Deque<GridTestMessage> sent = e.getValue();
MessageListener lsnr = lsnrs.get(to);
Deque<GridTestMessage> rcvd = lsnr.receivedMsgs();
info(">>> Node " + to + " received " + lsnr.remoteMessageCount() + " remote messages of " + rcvd.size() + " total");
for (int i = 0; i < 3 && sent.size() != rcvd.size(); i++) {
info("Check failed for node [node=" + to + ", sent=" + sent.size() + ", rcvd=" + rcvd.size() + ']');
U.sleep(2000);
}
assertEquals("Sent and received messages count mismatch.", sent.size(), rcvd.size());
assertTrue("Listener did not receive some messages: " + lsnr, rcvd.containsAll(sent));
assertTrue("Listener received extra messages: " + lsnr, sent.containsAll(rcvd));
}
}
use of org.apache.ignite.spi.communication.GridTestMessage in project ignite by apache.
the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead1.
/**
* @throws Exception If failed.
*/
@Test
public void testBlockRead1() 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(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.
}
for (int j = 0; j < 100; j++) {
spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
sentCnt.incrementAndGet();
}
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());
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();
}
}
Aggregations