use of org.apache.ignite.events.Event in project ignite by apache.
the class GridEventConsumeSelfTest method testEventsByType.
/**
* @throws Exception If failed.
*/
public void testEventsByType() throws Exception {
final Collection<UUID> nodeIds = new HashSet<>();
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(GRID_CNT);
UUID consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {
@Override
public boolean apply(UUID nodeId, Event evt) {
info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
assertEquals(EVT_JOB_STARTED, evt.type());
nodeIds.add(nodeId);
cnt.incrementAndGet();
latch.countDown();
return true;
}
}, null, EVT_JOB_STARTED);
try {
assertNotNull(consumeId);
grid(0).compute().broadcast(F.noop());
assert latch.await(2, SECONDS);
assertEquals(GRID_CNT, nodeIds.size());
assertEquals(GRID_CNT, cnt.get());
} finally {
grid(0).events().stopRemoteListen(consumeId);
}
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridEventConsumeSelfTest method testProjectionWithLocalNode.
/**
* @throws Exception If failed.
*/
public void testProjectionWithLocalNode() throws Exception {
final Collection<UUID> nodeIds = new HashSet<>();
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(GRID_CNT - 1);
UUID consumeId = events(grid(0).cluster().forAttribute("include", null)).remoteListen(new P2<UUID, Event>() {
@Override
public boolean apply(UUID nodeId, Event evt) {
info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
assertEquals(EVT_JOB_STARTED, evt.type());
nodeIds.add(nodeId);
cnt.incrementAndGet();
latch.countDown();
return true;
}
}, null, EVT_JOB_STARTED);
try {
assertNotNull(consumeId);
grid(0).compute().broadcast(F.noop());
assert latch.await(2, SECONDS);
assertEquals(GRID_CNT - 1, nodeIds.size());
assertEquals(GRID_CNT - 1, cnt.get());
} finally {
grid(0).events().stopRemoteListen(consumeId);
}
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridTaskCancelSingleNodeSelfTest method checkCancellation.
/**
* @param timeoutBeforeCancel Timeout.
* @throws Exception If failed.
*/
@SuppressWarnings("ErrorNotRethrown")
private void checkCancellation(long timeoutBeforeCancel) throws Exception {
final AtomicInteger finished = new AtomicInteger();
final AtomicInteger cancelled = new AtomicInteger();
final AtomicInteger rejected = new AtomicInteger();
final AtomicBoolean jobStarted = new AtomicBoolean();
grid().events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
info("Received event: " + evt);
switch(evt.type()) {
case EVT_JOB_STARTED:
jobStarted.set(true);
break;
case EVT_JOB_FINISHED:
finished.incrementAndGet();
break;
case EVT_JOB_CANCELLED:
cancelled.incrementAndGet();
break;
case EVT_JOB_REJECTED:
rejected.incrementAndGet();
break;
default:
assert false : "Unexpected event: " + evt;
}
return true;
}
}, EVT_JOB_STARTED, EVT_JOB_FINISHED, EVT_JOB_CANCELLED, EVT_JOB_REJECTED);
ComputeTaskFuture<?> fut = grid().compute().executeAsync(TestTask.class, null);
if (timeoutBeforeCancel > 0L)
Thread.sleep(timeoutBeforeCancel);
assert fut.cancel();
for (int i = 0; i < 3; i++) {
try {
if (timeoutBeforeCancel == 0L) {
if (jobStarted.get())
assertTrue("Failed on iteration [i=" + i + ", finished=" + finished.get() + ", cancelled=" + cancelled.get() + ", rejected=" + rejected.get() + ']', finished.get() == 1 && cancelled.get() == 1 && rejected.get() == 0);
else {
// job can be rejected if was concurrently cancelled before started
assertTrue("Failed on iteration [i=" + i + ", finished=" + finished.get() + ", cancelled=" + cancelled.get() + ", rejected=" + rejected.get() + ']', finished.get() == 0 && cancelled.get() == 0 && rejected.get() <= 1);
}
} else
assertTrue("Failed on iteration [i=" + i + ", finished=" + finished.get() + ", cancelled=" + cancelled.get() + ", rejected=" + rejected.get() + ']', finished.get() == 1 && cancelled.get() == 1 && rejected.get() == 0);
} catch (AssertionError e) {
info("Check failed: " + e.getMessage());
if (timeoutBeforeCancel == 0L && i == 2)
throw e;
}
if (i < 2)
U.sleep(500);
}
try {
fut.get();
fail();
} catch (IgniteFutureCancelledException e) {
info("Caught expected exception: " + e);
}
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridTaskJobRejectSelfTest method testReject.
/**
* @throws Exception If failed.
*/
public void testReject() throws Exception {
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
X.println("Task event: " + evt);
return true;
}
}, EVTS_TASK_EXECUTION);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
X.println("Job event: " + evt);
return true;
}
}, EVTS_JOB_EXECUTION);
final CountDownLatch startedLatch = new CountDownLatch(1);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
startedLatch.countDown();
return true;
}
}, EVT_JOB_STARTED);
final AtomicInteger failedOver = new AtomicInteger(0);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
failedOver.incrementAndGet();
return true;
}
}, EVT_JOB_FAILED_OVER);
final CountDownLatch finishedLatch = new CountDownLatch(1);
grid(1).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
finishedLatch.countDown();
return true;
}
}, EVT_TASK_FINISHED, EVT_TASK_FAILED);
final ClusterNode node = grid(1).localNode();
ComputeTaskFuture<?> fut = grid(1).compute().executeAsync(new ComputeTaskAdapter<Void, Void>() {
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable Void arg) {
return F.asMap(new SleepJob(), node, new SleepJob(), node);
}
/**
* {@inheritDoc}
*/
@Nullable
@Override
public Void reduce(List<ComputeJobResult> results) {
return null;
}
}, null);
assert startedLatch.await(2, SECONDS);
fut.cancel();
assert finishedLatch.await(2, SECONDS);
assert failedOver.get() == 0;
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridContinuousProcessor method start.
/**
* {@inheritDoc}
*/
@Override
public void start() throws IgniteCheckedException {
if (ctx.config().isDaemon())
return;
retryDelay = ctx.config().getNetworkSendRetryDelay();
retryCnt = ctx.config().getNetworkSendRetryCount();
marsh = ctx.config().getMarshaller();
ctx.event().addLocalEventListener(new DiscoveryListener(), 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 (ctx.isStopping())
return;
processStartRequest(snd, msg);
}
});
ctx.discovery().setCustomEventListener(StartRoutineAckDiscoveryMessage.class, new CustomEventListener<StartRoutineAckDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineAckDiscoveryMessage msg) {
if (ctx.isStopping())
return;
processStartAckRequest(topVer, msg);
}
});
ctx.discovery().setCustomEventListener(StopRoutineDiscoveryMessage.class, new CustomEventListener<StopRoutineDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineDiscoveryMessage msg) {
if (ctx.isStopping())
return;
processStopRequest(snd, msg);
}
});
ctx.discovery().setCustomEventListener(StopRoutineAckDiscoveryMessage.class, new CustomEventListener<StopRoutineAckDiscoveryMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineAckDiscoveryMessage msg) {
if (ctx.isStopping())
return;
processStopAckRequest(msg);
}
});
ctx.io().addMessageListener(TOPIC_CONTINUOUS, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object obj, byte plc) {
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