use of org.apache.ignite.events.Event in project ignite by apache.
the class IgniteSinkConnectorTest method testSinkPuts.
/**
* Tests the whole data flow from injecting data to Kafka to transferring it to the grid. It reads from two
* specified Kafka topics, because a sink task can read from multiple topics.
*
* @param sinkProps Sink properties.
* @param keyless Tests on Kafka stream with null keys if true.
* @throws Exception Thrown in case of the failure.
*/
private void testSinkPuts(Map<String, String> sinkProps, boolean keyless) throws Exception {
FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>(new Callback<Herder.Created<ConnectorInfo>>() {
@Override
public void onCompletion(Throwable error, Herder.Created<ConnectorInfo> info) {
if (error != null)
throw new RuntimeException("Failed to create a job!");
}
});
herder.putConnectorConfig(sinkProps.get(ConnectorConfig.NAME_CONFIG), sinkProps, false, cb);
cb.get();
final CountDownLatch latch = new CountDownLatch(EVENT_CNT * TOPICS.length);
final IgnitePredicate<Event> putLsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt != null;
latch.countDown();
return true;
}
};
grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(putLsnr, EVT_CACHE_OBJECT_PUT);
IgniteCache<String, String> cache = grid.cache(CACHE_NAME);
assertEquals(0, cache.size(CachePeekMode.PRIMARY));
Map<String, String> keyValMap = new HashMap<>(EVENT_CNT * TOPICS.length);
// Produces events for the specified number of topics
for (String topic : TOPICS) keyValMap.putAll(produceStream(topic, keyless));
// Checks all events successfully processed in 10 seconds.
assertTrue(latch.await(10, TimeUnit.SECONDS));
grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(putLsnr);
// Checks that each event was processed properly.
for (Map.Entry<String, String> entry : keyValMap.entrySet()) assertEquals(entry.getValue(), cache.get(entry.getKey()));
assertEquals(EVENT_CNT * TOPICS.length, cache.size(CachePeekMode.PRIMARY));
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class PartitionLossPolicyExample method events.
@Test
void events() {
// tag::events[]
Ignite ignite = Ignition.start();
IgnitePredicate<Event> locLsnr = evt -> {
CacheRebalancingEvent cacheEvt = (CacheRebalancingEvent) evt;
int lostPart = cacheEvt.partition();
ClusterNode node = cacheEvt.discoveryNode();
System.out.println(lostPart);
// Continue listening.
return true;
};
ignite.events().localListen(locLsnr, EventType.EVT_CACHE_REBALANCE_PART_DATA_LOST);
// end::events[]
ignite.close();
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class RebalanceCacheEventSecurityContextTest method checkCacheRebalanceEvent.
/**
*/
private void checkCacheRebalanceEvent(IgniteEx node, int evtType, int key, String initiatorLogin) throws Exception {
Collection<Event> nodeEvts = LISTENED_EVTS.get(node.localNode());
CacheEvent rebalanceEvt = (CacheEvent) nodeEvts.stream().findFirst().orElseThrow(NoSuchElementException::new);
assertEquals(evtType, rebalanceEvt.type());
assertEquals(key, (int) rebalanceEvt.<Integer>key());
assertEquals(initiatorLogin, node.context().security().authenticatedSubject(rebalanceEvt.subjectId()).login());
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridEventConsumeHandler method register.
/**
* {@inheritDoc}
*/
@Override
public RegisterStatus register(final UUID nodeId, final UUID routineId, final GridKernalContext ctx) throws IgniteCheckedException {
assert nodeId != null;
assert routineId != null;
assert ctx != null;
if (cb != null)
ctx.resource().injectGeneric(cb);
final boolean loc = nodeId.equals(ctx.localNodeId());
lsnr = new GridLocalEventListener() {
/**
* node ID, routine ID, event
*/
private final Queue<T3<UUID, UUID, Event>> notificationQueue = new LinkedList<>();
private boolean notificationInProgress;
@Override
public void onEvent(Event evt) {
if (filter != null && !filter.apply(evt))
return;
if (loc) {
if (!cb.apply(nodeId, evt))
ctx.continuous().stopRoutine(routineId);
} else {
if (ctx.discovery().node(nodeId) == null)
return;
synchronized (notificationQueue) {
notificationQueue.add(new T3<>(nodeId, routineId, evt));
if (!notificationInProgress) {
ctx.pools().getSystemExecutorService().execute(new Runnable() {
@Override
public void run() {
if (!ctx.continuous().lockStopping())
return;
try {
while (true) {
T3<UUID, UUID, Event> t3;
synchronized (notificationQueue) {
t3 = notificationQueue.poll();
if (t3 == null) {
notificationInProgress = false;
return;
}
}
try {
Event evt = t3.get3();
EventWrapper wrapper = new EventWrapper(evt);
if (evt instanceof CacheEvent) {
String cacheName = ((CacheEvent) evt).cacheName();
ClusterNode node = ctx.discovery().node(t3.get1());
if (node == null)
continue;
if (ctx.config().isPeerClassLoadingEnabled() && ctx.discovery().cacheNode(node, cacheName)) {
GridCacheAdapter cache = ctx.cache().internalCache(cacheName);
if (cache != null && cache.context().deploymentEnabled()) {
wrapper.p2pMarshal(ctx.config().getMarshaller());
wrapper.cacheName = cacheName;
cache.context().deploy().prepare(wrapper);
}
}
}
ctx.continuous().addNotification(t3.get1(), t3.get2(), wrapper, null, false, false);
} catch (ClusterTopologyCheckedException ignored) {
// No-op.
} catch (Throwable e) {
U.error(ctx.log(GridEventConsumeHandler.class), "Failed to send event notification to node: " + nodeId, e);
}
}
} finally {
ctx.continuous().unlockStopping();
}
}
});
notificationInProgress = true;
}
}
}
}
};
if (F.isEmpty(types))
types = EVTS_ALL;
p2pUnmarshalFut.listen((fut) -> {
if (fut.error() == null) {
try {
initFilter(filter, ctx);
} catch (IgniteCheckedException e) {
throw F.wrap(e);
}
ctx.event().addLocalEventListener(lsnr, types);
}
});
return RegisterStatus.REGISTERED;
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class GridDeploymentPerVersionStore method start.
/**
* {@inheritDoc}
*/
@Override
public void start() throws IgniteCheckedException {
discoLsnr = new GridLocalEventListener() {
@Override
public void onEvent(Event evt) {
assert evt instanceof DiscoveryEvent;
assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
Collection<SharedDeployment> undeployed = new LinkedList<>();
if (log.isDebugEnabled())
log.debug("Processing node departure event: " + evt);
synchronized (mux) {
for (Iterator<List<SharedDeployment>> i1 = cache.values().iterator(); i1.hasNext(); ) {
List<SharedDeployment> deps = i1.next();
for (Iterator<SharedDeployment> i2 = deps.iterator(); i2.hasNext(); ) {
SharedDeployment dep = i2.next();
dep.removeParticipant(discoEvt.eventNode().id());
if (!dep.hasParticipants()) {
if (dep.deployMode() == SHARED) {
if (!dep.undeployed()) {
dep.undeploy();
// Undeploy.
i2.remove();
assert !dep.isRemoved();
dep.onRemoved();
undeployed.add(dep);
if (log.isDebugEnabled())
log.debug("Undeployed class loader as there are no participating " + "nodes: " + dep);
}
} else {
if (log.isDebugEnabled())
log.debug("Preserving deployment without node participants: " + dep);
}
} else {
if (log.isDebugEnabled())
log.debug("Keeping deployment as it still has participants: " + dep);
}
}
if (deps.isEmpty())
i1.remove();
}
}
recordUndeployed(discoEvt.eventNode().id(), undeployed);
}
};
ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
if (log.isDebugEnabled())
log.debug(startInfo());
}
Aggregations