Search in sources :

Example 76 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteSinkTest method testSink.

/**
 * @throws Exception {@link Exception}.
 */
public void testSink() throws Exception {
    IgniteConfiguration cfg = loadConfiguration("modules/flume/src/test/resources/example-ignite.xml");
    cfg.setClientMode(false);
    final Ignite grid = startGrid("igniteServerNode", cfg);
    Context channelContext = new Context();
    channelContext.put("capacity", String.valueOf(EVENT_CNT));
    channelContext.put("transactionCapacity", String.valueOf(EVENT_CNT));
    Channel memoryChannel = new MemoryChannel();
    Configurables.configure(memoryChannel, channelContext);
    final CountDownLatch latch = new CountDownLatch(EVENT_CNT);
    final IgnitePredicate<Event> putLsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt != null;
            latch.countDown();
            return true;
        }
    };
    IgniteSink sink = new IgniteSink() {

        // Setting the listener on cache before sink processing starts.
        @Override
        public synchronized void start() {
            super.start();
            grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(putLsnr, EVT_CACHE_OBJECT_PUT);
        }
    };
    sink.setName("IgniteSink");
    sink.setChannel(memoryChannel);
    Context ctx = new Context();
    ctx.put(IgniteSinkConstants.CFG_CACHE_NAME, CACHE_NAME);
    ctx.put(IgniteSinkConstants.CFG_PATH, "example-ignite.xml");
    ctx.put(IgniteSinkConstants.CFG_EVENT_TRANSFORMER, "org.apache.ignite.stream.flume.TestEventTransformer");
    Configurables.configure(sink, ctx);
    sink.start();
    try {
        Transaction tx = memoryChannel.getTransaction();
        tx.begin();
        for (int i = 0; i < EVENT_CNT; i++) memoryChannel.put(EventBuilder.withBody((String.valueOf(i) + ": " + i).getBytes()));
        tx.commit();
        tx.close();
        Sink.Status status = Sink.Status.READY;
        while (status != Sink.Status.BACKOFF) {
            status = sink.process();
        }
    } finally {
        sink.stop();
    }
    // Checks that 10000 events successfully processed in 10 seconds.
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(putLsnr);
    IgniteCache<String, Integer> cache = grid.cache(CACHE_NAME);
    // Checks that each event was processed properly.
    for (int i = 0; i < EVENT_CNT; i++) {
        assertEquals(i, (int) cache.get(String.valueOf(i)));
    }
    assertEquals(EVENT_CNT, cache.size(CachePeekMode.PRIMARY));
}
Also used : Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) MemoryChannel(org.apache.flume.channel.MemoryChannel) Channel(org.apache.flume.Channel) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Transaction(org.apache.flume.Transaction) Sink(org.apache.flume.Sink) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite)

Example 77 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class CacheUtils method foreach.

/**
 * @param cacheName Cache name.
 * @param fun An operation that accepts a cache entry and processes it.
 * @param keyFilter Cache keys filter.
 * @param <K> Cache key object type.
 * @param <V> Cache value object type.
 */
protected static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
    bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) fun.accept(new CacheEntry<>(entry, cache));
        }
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) IgniteCallable(org.apache.ignite.lang.IgniteCallable) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) DataStructureCacheKey(org.apache.ignite.ml.math.distributed.keys.DataStructureCacheKey) ClusterNode(org.apache.ignite.cluster.ClusterNode) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Map(java.util.Map) Cache(javax.cache.Cache) IgniteBinaryOperator(org.apache.ignite.ml.math.functions.IgniteBinaryOperator) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) A(org.apache.ignite.internal.util.typedef.internal.A) IgniteSupplier(org.apache.ignite.ml.math.functions.IgniteSupplier) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Set(java.util.Set) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDoubleFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 78 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class GridEventConsumeSelfTest method testNodeJoinWithP2P.

/**
 * TODO: IGNITE-585.
 *
 * @throws Exception If failed.
 */
public void testNodeJoinWithP2P() throws Exception {
    fail("https://issues.apache.org/jira/browse/IGNITE-585");
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT + 1);
    ClassLoader ldr = getExternalClassLoader();
    IgnitePredicate<ClusterNode> prjPred = (IgnitePredicate<ClusterNode>) ldr.loadClass(PRJ_PRED_CLS_NAME).newInstance();
    IgnitePredicate<Event> filter = (IgnitePredicate<Event>) ldr.loadClass(FILTER_CLS_NAME).newInstance();
    UUID consumeId = events(grid(0).cluster().forPredicate(prjPred)).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;
        }
    }, filter, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        startGrid("anotherGrid");
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT + 1, nodeIds.size());
        assertEquals(GRID_CNT + 1, cnt.get());
    } finally {
        stopGrid("anotherGrid");
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 79 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testFieldsQueryEvents.

/**
 * @throws Exception If failed.
 */
public void testFieldsQueryEvents() throws Exception {
    final IgniteCache<UUID, Person> cache = jcache(UUID.class, Person.class);
    final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
    final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
    IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryExecutedEvent;
                if (evtsDisabled)
                    fail("Cache events are disabled");
                CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
                assertEquals(cache.getName(), qe.cacheName());
                assertNotNull(qe.clause());
                assertNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertArrayEquals(new Integer[] { 10 }, qe.arguments());
                execLatch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(pred, EVT_CACHE_QUERY_EXECUTED);
        qryExecLsnrs[i] = pred;
    }
    try {
        for (int i = 1; i <= 20; i++) cache.put(UUID.randomUUID(), new Person("Person " + i, i));
        QueryCursor<List<?>> q = cache.query(new SqlFieldsQuery("select _key, name from Person where salary > ?").setArgs(10));
        q.getAll();
        assert execLatch.await(1000, MILLISECONDS);
    } finally {
        for (int i = 0; i < gridCount(); i++) grid(i).events().stopLocalListen(qryExecLsnrs[i]);
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 80 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class IgniteCacheAbstractQuerySelfTest method testScanQueryEvents.

/**
 * @throws Exception If failed.
 */
public void testScanQueryEvents() throws Exception {
    final Map<Integer, Integer> map = new ConcurrentHashMap<>();
    final IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    final boolean evtsDisabled = cache.getConfiguration(CacheConfiguration.class).isEventsDisabled();
    final CountDownLatch latch = new CountDownLatch(evtsDisabled ? 0 : 10);
    final CountDownLatch execLatch = new CountDownLatch(evtsDisabled ? 0 : cacheMode() == REPLICATED ? 1 : gridCount());
    IgnitePredicate[] objReadLsnrs = new IgnitePredicate[gridCount()];
    IgnitePredicate[] qryExecLsnrs = new IgnitePredicate[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryReadEvent;
                if (evtsDisabled)
                    fail("Cache events are disabled");
                CacheQueryReadEvent<Integer, Integer> qe = (CacheQueryReadEvent<Integer, Integer>) evt;
                assertEquals(SCAN.name(), qe.queryType());
                assertEquals(cache.getName(), qe.cacheName());
                assertNull(qe.className());
                assertNull(null, qe.clause());
                assertNotNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertNull(qe.arguments());
                map.put(qe.key(), qe.value());
                latch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(pred, EVT_CACHE_QUERY_OBJECT_READ);
        objReadLsnrs[i] = pred;
        IgnitePredicate<Event> execPred = new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt instanceof CacheQueryExecutedEvent;
                if (evtsDisabled)
                    fail("Cache events are disabled");
                CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
                assertEquals(SCAN.name(), qe.queryType());
                assertEquals(cache.getName(), qe.cacheName());
                assertNull(qe.className());
                assertNull(null, qe.clause());
                assertNotNull(qe.scanQueryFilter());
                assertNull(qe.continuousQueryFilter());
                assertNull(qe.arguments());
                execLatch.countDown();
                return true;
            }
        };
        grid(i).events().localListen(execPred, EVT_CACHE_QUERY_EXECUTED);
        qryExecLsnrs[i] = execPred;
    }
    try {
        for (int i = 0; i < 20; i++) cache.put(i, i);
        IgniteBiPredicate<Integer, Integer> filter = new IgniteBiPredicate<Integer, Integer>() {

            @Override
            public boolean apply(Integer k, Integer v) {
                return k >= 10;
            }
        };
        QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new ScanQuery<>(filter));
        q.getAll();
        assert latch.await(1000, MILLISECONDS);
        assert execLatch.await(1000, MILLISECONDS);
        if (!evtsDisabled) {
            assertEquals(10, map.size());
            for (int i = 10; i < 20; i++) assertEquals(i, map.get(i).intValue());
        }
    } finally {
        for (int i = 0; i < gridCount(); i++) {
            grid(i).events().stopLocalListen(objReadLsnrs[i]);
            grid(i).events().stopLocalListen(qryExecLsnrs[i]);
        }
    }
}
Also used : CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)81 Event (org.apache.ignite.events.Event)44 CountDownLatch (java.util.concurrent.CountDownLatch)35 Ignite (org.apache.ignite.Ignite)32 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)18 ArrayList (java.util.ArrayList)15 Ignition (org.apache.ignite.Ignition)10 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)10 Collection (java.util.Collection)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)8 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)7 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7