Search in sources :

Example 16 with Event

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));
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Event(org.apache.ignite.events.Event) Herder(org.apache.kafka.connect.runtime.Herder) StandaloneHerder(org.apache.kafka.connect.runtime.standalone.StandaloneHerder) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) FutureCallback(org.apache.kafka.connect.util.FutureCallback)

Example 17 with Event

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();
}
Also used : Arrays(java.util.Arrays) EventType(org.apache.ignite.events.EventType) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Test(org.junit.jupiter.api.Test) Ignition(org.apache.ignite.Ignition) ClusterNode(org.apache.ignite.cluster.ClusterNode) PartitionLossPolicy(org.apache.ignite.cache.PartitionLossPolicy) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) CacheException(javax.cache.CacheException) ClusterNode(org.apache.ignite.cluster.ClusterNode) Event(org.apache.ignite.events.Event) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) Ignite(org.apache.ignite.Ignite) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) Test(org.junit.jupiter.api.Test)

Example 18 with Event

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());
}
Also used : CacheEvent(org.apache.ignite.events.CacheEvent) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event)

Example 19 with Event

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;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) LinkedList(java.util.LinkedList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) CacheEvent(org.apache.ignite.events.CacheEvent) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) T3(org.apache.ignite.internal.util.typedef.T3) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 20 with Event

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());
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) Iterator(java.util.Iterator) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) DeploymentEvent(org.apache.ignite.events.DeploymentEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

Event (org.apache.ignite.events.Event)273 Test (org.junit.Test)148 CountDownLatch (java.util.concurrent.CountDownLatch)146 Ignite (org.apache.ignite.Ignite)144 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)125 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)103 UUID (java.util.UUID)87 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)76 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 ClusterNode (org.apache.ignite.cluster.ClusterNode)48 IgniteException (org.apache.ignite.IgniteException)34 ArrayList (java.util.ArrayList)33 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)31 JobEvent (org.apache.ignite.events.JobEvent)28 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)28 HashMap (java.util.HashMap)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 Collection (java.util.Collection)23 Map (java.util.Map)23