use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.
the class TxDeadlockDetectionMessageMarshallingTest method testMessageUnmarshallWithoutCacheContext.
/**
* @throws Exception If failed.
*/
@Test
public void testMessageUnmarshallWithoutCacheContext() throws Exception {
try {
Ignite ignite = startGrid(0);
CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(ccfg);
Ignite client = startClientGrid(1);
final GridCacheSharedContext<Object, Object> clientCtx = ((IgniteKernal) client).context().cache().context();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean res = new AtomicBoolean();
clientCtx.gridIO().addMessageListener(TOPIC, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
if (msg instanceof TxLocksResponse) {
try {
((TxLocksResponse) msg).finishUnmarshal(clientCtx, clientCtx.deploy().globalLoader());
res.set(true);
} catch (Exception e) {
log.error("Message unmarshal failed", e);
} finally {
latch.countDown();
}
}
}
});
GridCacheContext cctx = ((IgniteCacheProxy) cache).context();
KeyCacheObject key = cctx.toCacheKeyObject(1);
TxLocksResponse msg = new TxLocksResponse();
msg.addKey(cctx.txKey(key));
msg.prepareMarshal(cctx.shared());
((IgniteKernal) ignite).context().cache().context().gridIO().sendToCustomTopic(((IgniteKernal) client).localNode(), TOPIC, msg, GridIoPolicy.PUBLIC_POOL);
boolean await = latch.await(1, TimeUnit.SECONDS);
assertTrue(await && res.get());
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.
the class GridCachePartitionedGetSelfTest method prepare.
/**
* Puts value to primary node and registers listener
* that sets {@link #received} flag to {@code true}
* if {@link GridNearGetRequest} was received on primary node.
*
* @throws Exception If failed.
*/
private void prepare() throws Exception {
for (int i = 0; i < GRID_CNT; i++) {
Ignite g = grid(i);
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimary(grid(i).localNode(), KEY)) {
info("Primary node: " + g.cluster().localNode().id());
// Put value.
g.cache(DEFAULT_CACHE_NAME).put(KEY, VAL);
// Register listener.
((IgniteKernal) g).context().io().addMessageListener(TOPIC_CACHE, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
info("Received message from node [nodeId=" + nodeId + ", msg=" + msg + ']');
if (msg instanceof GridNearSingleGetRequest) {
info("Setting flag: " + System.identityHashCode(received));
received.set(true);
}
}
});
break;
}
}
}
use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.
the class GridIoManagerBenchmark0 method testVariableLoad.
/**
* @throws Exception If failed.
*/
@Test
public void testVariableLoad() throws Exception {
final IgniteKernal sndKernal = (IgniteKernal) grid(0);
final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
final ClusterNode sndNode = sndKernal.localNode();
final ClusterNode rcvNode = rcvKernal.localNode();
final GridIoManager snd = sndKernal.context().io();
final GridIoManager rcv = rcvKernal.context().io();
info("Senders: " + THREADS);
info("Messages: " + CONCUR_MSGS);
final Semaphore sem = new Semaphore(CONCUR_MSGS);
final LongAdder msgCntr = new LongAdder();
final String topic = "test-topic";
final Map<IgniteUuid, CountDownLatch> latches = new ConcurrentHashMap<>();
rcv.addMessageListener(topic, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
try {
rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
} catch (IgniteCheckedException e) {
error("Failed to send message.", e);
}
}
});
snd.addMessageListener(topic, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
msgCntr.increment();
sem.release();
CountDownLatch latch = latches.get(((GridTestMessage) msg).id());
if (latch != null)
latch.countDown();
}
});
final AtomicBoolean finish = new AtomicBoolean();
final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>();
IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!finish.get()) {
CountDownLatch latch = latchRef.get();
if (latch != null)
U.await(latch);
IgniteUuid msgId = IgniteUuid.randomUuid();
sem.acquire();
snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
}
return null;
}
}, THREADS, "send-thread");
IgniteInternalFuture<?> f1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
private long ts = System.currentTimeMillis();
@Override
public Object call() throws Exception {
try {
while (!finish.get()) {
info(U.nl() + ">>>" + U.nl() + ">>> High load." + U.nl() + ">>>");
U.sleep(15 * 1000);
reportNumbers();
info(U.nl() + ">>>" + U.nl() + ">>> Low load." + U.nl() + ">>>");
CountDownLatch latch = new CountDownLatch(1);
try {
// Here will be a pause.
latchRef.set(latch);
U.sleep(7 * 1000);
reportNumbers();
} finally {
latch.countDown();
}
}
} catch (IgniteCheckedException e) {
X.println("Message send failed", e);
}
return null;
}
/**
*/
void reportNumbers() {
long newTs = System.currentTimeMillis();
long qrys = msgCntr.sumThenReset();
long time = newTs - ts;
X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
ts = newTs;
}
}, 1, "load-dispatcher");
IgniteInternalFuture<?> f2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!finish.get()) {
U.sleep(1000);
IgniteUuid msgId = IgniteUuid.randomUuid();
CountDownLatch latch = new CountDownLatch(1);
latches.put(msgId, latch);
snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
long start = System.currentTimeMillis();
latch.await();
info("Response time: " + (System.currentTimeMillis() - start));
}
return null;
}
}, THREADS, "low-loader");
Thread.sleep(TEST_TIMEOUT);
finish.set(true);
sem.release(CONCUR_MSGS * 2);
f.get();
f1.get();
f2.get();
}
use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.
the class GridIoManagerBenchmark0 method testThroughput.
/**
* @throws Exception If failed.
*/
@Test
public void testThroughput() throws Exception {
final IgniteKernal sndKernal = (IgniteKernal) grid(0);
final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
final ClusterNode sndNode = sndKernal.localNode();
final ClusterNode rcvNode = rcvKernal.localNode();
final GridIoManager snd = sndKernal.context().io();
final GridIoManager rcv = rcvKernal.context().io();
info("Senders: " + THREADS);
info("Messages: " + CONCUR_MSGS);
final Semaphore sem = new Semaphore(CONCUR_MSGS);
final LongAdder msgCntr = new LongAdder();
final String topic = "test-topic";
rcv.addMessageListener(topic, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
try {
rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
} catch (IgniteCheckedException e) {
error("Failed to send message.", e);
}
}
});
snd.addMessageListener(topic, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
msgCntr.increment();
sem.release();
}
});
Timer t = new Timer("results-reporter");
t.schedule(new TimerTask() {
private long ts = System.currentTimeMillis();
@Override
public void run() {
long newTs = System.currentTimeMillis();
long qrys = msgCntr.sumThenReset();
long time = newTs - ts;
X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
ts = newTs;
}
}, 10000, 10000);
final AtomicBoolean finish = new AtomicBoolean();
IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
IgniteUuid msgId = IgniteUuid.randomUuid();
while (!finish.get()) {
sem.acquire();
snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
}
} catch (IgniteCheckedException e) {
X.println("Message send failed", e);
} catch (InterruptedException ignored) {
// No-op.
}
return null;
}
}, THREADS, "send-thread");
Thread.sleep(TEST_TIMEOUT);
finish.set(true);
sem.release(CONCUR_MSGS * 2);
t.cancel();
f.get();
}
use of org.apache.ignite.internal.managers.communication.GridMessageListener in project ignite by apache.
the class GridCacheMessageSelfTest method doSend.
/**
* @throws Exception If failed.
*/
private void doSend() throws Exception {
GridIoManager mgr0 = grid(0).context().io();
GridIoManager mgr1 = grid(1).context().io();
String topic = "test-topic";
final CountDownLatch latch = new CountDownLatch(SAMPLE_CNT);
mgr1.addMessageListener(topic, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
try {
latch.countDown();
Collection<TestMessage1> messages = ((TestMessage) msg).entries();
assertEquals(10, messages.size());
int cnt = 0;
for (TestMessage1 msg1 : messages) {
assertTrue(msg1.body().contains(TEST_BODY));
int i = Integer.parseInt(msg1.body().substring(TEST_BODY.length() + 1));
assertEquals(cnt, i);
TestMessage2 msg2 = (TestMessage2) msg1.message();
assertEquals(TEST_BODY + "_" + i + "_2", msg2.body());
assertEquals(grid(0).localNode().id(), msg2.nodeId());
assertEquals(i, msg2.id());
GridTestMessage msg3 = (GridTestMessage) msg2.message();
assertEquals(cnt, msg3.getMsgId());
assertEquals(grid(1).localNode().id(), msg3.getSourceNodeId());
cnt++;
}
} catch (Exception e) {
fail("Exception " + e.getMessage());
}
}
});
TestMessage msg = new TestMessage();
for (int i = 0; i < 10; i++) {
TestMessage2 mes1 = new TestMessage2();
mes1.init(new GridTestMessage(grid(1).localNode().id(), i, 0), grid(0).localNode().id(), i, TEST_BODY + "_" + i + "_2");
TestMessage1 mes2 = new TestMessage1();
mes2.init(mes1, TEST_BODY + "_" + i);
msg.add(mes2);
}
mgr0.sendToCustomTopic(grid(1).localNode(), topic, msg, GridIoPolicy.PUBLIC_POOL);
assert latch.await(3, SECONDS);
}
Aggregations