use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridMemoryEventStorageMultiThreadedSelfTest method testMultiThreaded.
/**
* @throws Exception If test failed
*/
public void testMultiThreaded() throws Exception {
GridTestUtils.runMultiThreaded(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < 100000; i++) getSpi().record(new DiscoveryEvent(null, "Test event", 1, null));
return null;
}
}, 10, "event-thread");
Collection<Event> evts = getSpi().localEvents(F.<Event>alwaysTrue());
info("Events count in memory: " + evts.size());
assert evts.size() <= 10000 : "Incorrect number of events: " + evts.size();
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridEventConsumeSelfTest method testMasterNodeLeave.
/**
* @throws Exception If failed.
*/
public void testMasterNodeLeave() throws Exception {
final CountDownLatch latch = new CountDownLatch(GRID_CNT);
Ignite g = startGrid("anotherGrid");
try {
final UUID nodeId = g.cluster().localNode().id();
for (int i = 0; i < GRID_CNT; i++) {
grid(i).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (nodeId.equals(((DiscoveryEvent) evt).eventNode().id()))
latch.countDown();
return true;
}
}, EVT_NODE_LEFT, EVT_NODE_FAILED);
}
g.events().remoteListen(null, new P1<Event>() {
@Override
public boolean apply(Event evt) {
return true;
}
}, EVTS_ALL);
} finally {
stopGrid("anotherGrid");
}
assert latch.await(3000, MILLISECONDS);
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridEventConsumeSelfTest method testApiAsync.
/**
* @throws Exception If failed.
*/
public void testApiAsync() throws Exception {
IgniteEvents evt = grid(0).events();
try {
evt.stopRemoteListenAsync(null).get();
} catch (NullPointerException ignored) {
// No-op.
}
evt.stopRemoteListenAsync(UUID.randomUUID()).get();
UUID consumeId = null;
try {
consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {
@Override
public boolean apply(UUID uuid, DiscoveryEvent evt) {
return false;
}
}, new P1<DiscoveryEvent>() {
@Override
public boolean apply(DiscoveryEvent e) {
return false;
}
}, EVTS_DISCOVERY).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
try {
consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {
@Override
public boolean apply(UUID uuid, DiscoveryEvent evt) {
return false;
}
}, new P1<DiscoveryEvent>() {
@Override
public boolean apply(DiscoveryEvent e) {
return false;
}
}).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
try {
consumeId = evt.remoteListenAsync(new P2<UUID, Event>() {
@Override
public boolean apply(UUID uuid, Event evt) {
return false;
}
}, new P1<Event>() {
@Override
public boolean apply(Event e) {
return false;
}
}).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridDiscoveryEventSelfTest method testJoinSequenceEvents.
/**
* @throws Exception If failed.
*/
public void testJoinSequenceEvents() throws Exception {
try {
Ignite g0 = startGrid(0);
UUID id0 = g0.cluster().localNode().id();
final ConcurrentMap<Integer, Collection<ClusterNode>> evts = new ConcurrentHashMap<>();
final CountDownLatch latch = new CountDownLatch(3);
g0.events().localListen(new IgnitePredicate<Event>() {
private AtomicInteger cnt = new AtomicInteger();
@Override
public boolean apply(Event evt) {
assert evt.type() == EVT_NODE_JOINED : evt;
evts.put(cnt.getAndIncrement(), ((DiscoveryEvent) evt).topologyNodes());
latch.countDown();
return true;
}
}, EVT_NODE_JOINED);
UUID id1 = startGrid(1).cluster().localNode().id();
UUID id2 = startGrid(2).cluster().localNode().id();
UUID id3 = startGrid(3).cluster().localNode().id();
assertTrue("Wrong count of events received: " + evts, latch.await(3000, MILLISECONDS));
Collection<ClusterNode> top0 = evts.get(0);
assertNotNull(top0);
assertEquals(2, top0.size());
assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id0));
assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id1));
Collection<ClusterNode> top1 = evts.get(1);
assertNotNull(top1);
assertEquals(3, top1.size());
assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id0));
assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id1));
assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id2));
Collection<ClusterNode> top2 = evts.get(2);
assertNotNull(top2);
assertEquals(4, top2.size());
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id0));
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id1));
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id2));
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id3));
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridContinuousProcessor method start.
/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
if (ctx.config().isDaemon())
return;
retryDelay = ctx.config().getNetworkSendRetryDelay();
retryCnt = ctx.config().getNetworkSendRetryCount();
marsh = ctx.config().getMarshaller();
ctx.event().addLocalEventListener(new GridLocalEventListener() {
@SuppressWarnings({ "fallthrough", "TooBroadScope" })
@Override
public void onEvent(Event evt) {
assert evt instanceof DiscoveryEvent;
assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
clientInfos.remove(nodeId);
// Unregister handlers created by left node.
for (Map.Entry<UUID, RemoteRoutineInfo> e : rmtInfos.entrySet()) {
UUID routineId = e.getKey();
RemoteRoutineInfo info = e.getValue();
if (nodeId.equals(info.nodeId)) {
if (info.autoUnsubscribe)
unregisterRemote(routineId);
if (info.hnd.isQuery())
info.hnd.onNodeLeft();
}
}
for (Map.Entry<IgniteUuid, SyncMessageAckFuture> e : syncMsgFuts.entrySet()) {
SyncMessageAckFuture fut = e.getValue();
if (fut.nodeId().equals(nodeId)) {
SyncMessageAckFuture fut0 = syncMsgFuts.remove(e.getKey());
if (fut0 != null) {
ClusterTopologyCheckedException err = new ClusterTopologyCheckedException("Node left grid while sending message to: " + nodeId);
fut0.onDone(err);
}
}
}
}
}, EVT_NODE_LEFT, EVT_NODE_FAILED);
ctx.event().addLocalEventListener(new GridLocalEventListener() {
@Override
public void onEvent(Event evt) {
cancelFutures(new IgniteCheckedException("Topology segmented"));
}
}, EVT_NODE_SEGMENTED);
ctx.discovery().setCustomEventListener(StartRoutineDiscoveryMessage.class, new CustomEventListener<StartRoutineDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineDiscoveryMessage msg) {
if (!snd.id().equals(ctx.localNodeId()) && !ctx.isStopping())
processStartRequest(snd, msg);
}
});
ctx.discovery().setCustomEventListener(StartRoutineAckDiscoveryMessage.class, new CustomEventListener<StartRoutineAckDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineAckDiscoveryMessage msg) {
StartFuture fut = startFuts.remove(msg.routineId());
if (fut != null) {
if (msg.errs().isEmpty()) {
LocalRoutineInfo routine = locInfos.get(msg.routineId());
// Update partition counters.
if (routine != null && routine.handler().isQuery()) {
Map<UUID, Map<Integer, T2<Long, Long>>> cntrsPerNode = msg.updateCountersPerNode();
Map<Integer, T2<Long, Long>> cntrs = msg.updateCounters();
GridCacheAdapter<Object, Object> interCache = ctx.cache().internalCache(routine.handler().cacheName());
GridCacheContext cctx = interCache != null ? interCache.context() : null;
if (cctx != null && cntrsPerNode != null && !cctx.isLocal() && cctx.affinityNode())
cntrsPerNode.put(ctx.localNodeId(), cctx.topology().updateCounters(false));
routine.handler().updateCounters(topVer, cntrsPerNode, cntrs);
}
fut.onRemoteRegistered();
} else {
IgniteCheckedException firstEx = F.first(msg.errs().values());
fut.onDone(firstEx);
stopRoutine(msg.routineId());
}
}
}
});
ctx.discovery().setCustomEventListener(StopRoutineDiscoveryMessage.class, new CustomEventListener<StopRoutineDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineDiscoveryMessage msg) {
if (!snd.id().equals(ctx.localNodeId())) {
UUID routineId = msg.routineId();
unregisterRemote(routineId);
}
for (Map<UUID, LocalRoutineInfo> clientInfo : clientInfos.values()) {
if (clientInfo.remove(msg.routineId()) != null)
break;
}
}
});
ctx.discovery().setCustomEventListener(StopRoutineAckDiscoveryMessage.class, new CustomEventListener<StopRoutineAckDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineAckDiscoveryMessage msg) {
StopFuture fut = stopFuts.remove(msg.routineId());
if (fut != null)
fut.onDone();
}
});
ctx.io().addMessageListener(TOPIC_CONTINUOUS, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object obj) {
GridContinuousMessage msg = (GridContinuousMessage) obj;
if (msg.data() == null && msg.dataBytes() != null) {
try {
msg.data(U.unmarshal(marsh, msg.dataBytes(), U.resolveClassLoader(ctx.config())));
} catch (IgniteCheckedException e) {
U.error(log, "Failed to process message (ignoring): " + msg, e);
return;
}
}
switch(msg.type()) {
case MSG_EVT_NOTIFICATION:
processNotification(nodeId, msg);
break;
case MSG_EVT_ACK:
processMessageAck(msg);
break;
default:
assert false : "Unexpected message received: " + msg.type();
}
}
});
ctx.cacheObjects().onContinuousProcessorStarted(ctx);
ctx.service().onContinuousProcessorStarted(ctx);
if (log.isDebugEnabled())
log.debug("Continuous processor started.");
}
Aggregations