Search in sources :

Example 36 with CacheEntryEvent

use of javax.cache.event.CacheEntryEvent in project ignite by apache.

the class GridContinuousOperationsLoadTest method main.

/**
     * Main method.
     *
     * @param args Command line arguments.
     * @throws Exception If error occurs.
     */
public static void main(String[] args) throws Exception {
    final String cfgPath = args.length > 0 ? args[0] : "examples/config/example-cache.xml";
    final String cacheName = getStringProperty(CACHE_NAME, "partitioned");
    final Integer valSize = getIntProperty(VALUE_SIZE, 1024);
    final Integer threadsCnt = getIntProperty(THREADS_CNT, 8);
    final Integer testDurSec = getIntProperty(TEST_DUR_SEC, 180);
    final Integer filterSkipProb = getIntProperty("FILTER_SKIP_PROBABILITY", 10, new C1<Integer, String>() {

        @Nullable
        @Override
        public String apply(Integer val) {
            if (val < 0 || val > 100)
                return "The value should be between 1 and 100.";
            return null;
        }
    });
    final boolean useQry = getBooleanProperty("IGNITE_USE_QUERIES", true);
    final int bufSize = getIntProperty("IGNITE_BUFFER_SIZE", 1);
    final long timeInterval = getLongProperty("IGNITE_TIME_INTERVAL", 0);
    final int parallelCnt = getIntProperty("IGNITE_PARALLEL_COUNT", 8);
    final int keyRange = getIntProperty("IGNITE_KEY_RANGE", 100000);
    final long updSleepMs = getLongProperty("IGNITE_UPDATE_SLEEP_MS", 0);
    final long filterSleepMs = getLongProperty("IGNITE_FILTER_SLEEP_MS", 0);
    final long cbSleepMs = getLongProperty("IGNITE_CALLBACK_SLEEP_MS", 0);
    X.println("The test will start with the following parameters:");
    dumpProperties(System.out);
    try (Ignite ignite = Ignition.start(cfgPath)) {
        final IgniteCache<Object, Object> cache = ignite.cache(cacheName);
        if (cache == null)
            throw new IgniteCheckedException("Cache is not configured: " + cacheName);
        // Continuous query manager, used to monitor queue size.
        final CacheContinuousQueryManager contQryMgr = ((IgniteKernal) ignite).context().cache().cache(cacheName).context().continuousQueries();
        if (contQryMgr == null)
            throw new IgniteCheckedException("Could not access CacheContinuousQueryManager");
        // Stop flag.
        final AtomicBoolean stop = new AtomicBoolean();
        // Callback counter.
        final AtomicLong cbCntr = new AtomicLong();
        // Update counter.
        final AtomicLong updCntr = new AtomicLong();
        for (int i = 0; i < parallelCnt; i++) {
            if (useQry) {
                ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                    @Override
                    public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        if (cbSleepMs > 0) {
                            try {
                                U.sleep(cbSleepMs);
                            } catch (IgniteInterruptedCheckedException e) {
                                throw new IgniteException(e);
                            }
                        }
                        for (CacheEntryEvent<?, ?> ignored : evts) cbCntr.incrementAndGet();
                    }
                });
                qry.setRemoteFilter(new CacheEntryEventSerializableFilter<Object, Object>() {

                    @Override
                    public boolean evaluate(CacheEntryEvent<?, ?> evt) {
                        if (filterSleepMs > 0) {
                            try {
                                U.sleep(filterSleepMs);
                            } catch (IgniteInterruptedCheckedException e) {
                                throw new IgniteException(e);
                            }
                        }
                        return Math.random() * 100 >= filterSkipProb;
                    }
                });
                qry.setPageSize(bufSize);
                qry.setTimeInterval(timeInterval);
                cache.query(qry);
            } else {
                ignite.events().remoteListen(bufSize, timeInterval, true, new PX2<UUID, Event>() {

                    @Override
                    public boolean applyx(UUID uuid, Event evt) throws IgniteInterruptedCheckedException {
                        if (cbSleepMs > 0)
                            U.sleep(cbSleepMs);
                        cbCntr.incrementAndGet();
                        // Continue listening.
                        return true;
                    }
                }, new PX1<Event>() {

                    @Override
                    public boolean applyx(Event evt) throws IgniteInterruptedCheckedException {
                        if (filterSleepMs > 0)
                            U.sleep(filterSleepMs);
                        return Math.random() * 100 >= filterSkipProb;
                    }
                }, EVT_CACHE_OBJECT_PUT);
            }
        }
        // Start collector thread.
        startDaemon(new Runnable() {

            @Override
            public void run() {
                try {
                    while (!stop.get() && !Thread.currentThread().isInterrupted()) {
                        long cbCntr0 = cbCntr.get();
                        long updCntr0 = updCntr.get();
                        U.sleep(1000);
                        long cbDelta = cbCntr.get() - cbCntr0;
                        long updDelta = updCntr.get() - updCntr0;
                        X.println("Stats [entriesPerSec=" + cbDelta + ", updatesPerSec=" + updDelta + ']');
                    }
                } catch (IgniteInterruptedCheckedException ignored) {
                // No-op.
                }
            }
        });
        X.println("Starting " + threadsCnt + " generator thread(s).");
        // Start generator threads.
        IgniteInternalFuture<Long> genFut = runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                byte[] val = new byte[valSize];
                ThreadLocalRandom8 rnd = ThreadLocalRandom8.current();
                while (!stop.get() && !Thread.currentThread().isInterrupted()) {
                    Integer key = rnd.nextInt(keyRange);
                    cache.put(key, val);
                    updCntr.incrementAndGet();
                    if (updSleepMs > 0)
                        U.sleep(updSleepMs);
                }
                return true;
            }
        }, threadsCnt, "load-test-generator");
        U.sleep(testDurSec * 1000);
        stop.set(true);
        genFut.get();
    }
}
Also used : CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheContinuousQueryManager(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom8(org.jsr166.ThreadLocalRandom8) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with CacheEntryEvent

use of javax.cache.event.CacheEntryEvent in project ignite by apache.

the class HadoopJobTracker method onKernalStart.

/** {@inheritDoc} */
@SuppressWarnings("deprecation")
@Override
public void onKernalStart() throws IgniteCheckedException {
    super.onKernalStart();
    jobMetaCache().context().continuousQueries().executeInternalQuery(new CacheEntryUpdatedListener<HadoopJobId, HadoopJobMetadata>() {

        @Override
        public void onUpdated(final Iterable<CacheEntryEvent<? extends HadoopJobId, ? extends HadoopJobMetadata>> evts) {
            if (!busyLock.tryReadLock())
                return;
            try {
                // Must process query callback in a separate thread to avoid deadlocks.
                evtProcSvc.execute(new EventHandler() {

                    @Override
                    protected void body() throws IgniteCheckedException {
                        processJobMetadataUpdates(evts);
                    }
                });
            } finally {
                busyLock.readUnlock();
            }
        }
    }, null, true, true, false);
    ctx.kernalContext().event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(final Event evt) {
            if (!busyLock.tryReadLock())
                return;
            try {
                // Must process discovery callback in a separate thread to avoid deadlock.
                evtProcSvc.execute(new EventHandler() {

                    @Override
                    protected void body() {
                        processNodeLeft((DiscoveryEvent) evt);
                    }
                });
            } finally {
                busyLock.readUnlock();
            }
        }
    }, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEvent(javax.cache.event.CacheEntryEvent) HadoopJobId(org.apache.ignite.internal.processors.hadoop.HadoopJobId)

Example 38 with CacheEntryEvent

use of javax.cache.event.CacheEntryEvent in project ignite by apache.

the class IgniteCacheExpireAndUpdateConsistencyTest method updateAndEventConsistencyTest.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private void updateAndEventConsistencyTest(CacheConfiguration<TestKey, TestValue> ccfg) throws Exception {
    ignite(0).createCache(ccfg);
    try {
        List<ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>>> nodesEvts = new ArrayList<>();
        for (int i = 0; i < NODES; i++) {
            Ignite ignite = ignite(i);
            IgniteCache<TestKey, TestValue> cache = ignite.cache(ccfg.getName());
            ContinuousQuery<TestKey, TestValue> qry = new ContinuousQuery<>();
            final ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> allEvts = new ConcurrentHashMap<>();
            qry.setLocalListener(new CacheEntryUpdatedListener<TestKey, TestValue>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends TestKey, ? extends TestValue>> evts) {
                    for (CacheEntryEvent<? extends TestKey, ? extends TestValue> e : evts) {
                        List<T2<TestValue, TestValue>> keyEvts = allEvts.get(e.getKey());
                        if (keyEvts == null) {
                            List<T2<TestValue, TestValue>> old = allEvts.putIfAbsent(e.getKey(), keyEvts = new ArrayList<>());
                            assertNull(old);
                        }
                        synchronized (keyEvts) {
                            keyEvts.add(new T2<TestValue, TestValue>(e.getValue(), e.getOldValue()));
                        }
                    }
                }
            });
            cache.query(qry);
            nodesEvts.add(allEvts);
        }
        final AtomicInteger keyVal = new AtomicInteger();
        for (int i = 0; i < NODES; i++) {
            Ignite ignite = ignite(i);
            log.info("Test with node: " + ignite.name());
            updateAndEventConsistencyTest(ignite, ccfg.getName(), keyVal, nodesEvts, false);
            if (ccfg.getAtomicityMode() == TRANSACTIONAL)
                updateAndEventConsistencyTest(ignite, ccfg.getName(), keyVal, nodesEvts, true);
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 39 with CacheEntryEvent

use of javax.cache.event.CacheEntryEvent in project ignite by apache.

the class IgniteCacheEntryListenerAbstractTest method checkEvents.

/**
     * @param cache Cache.
     * @param lsnrCfg Listener configuration.
     * @param key Key.
     * @param create {@code True} if listens for create events.
     * @param update {@code True} if listens for update events.
     * @param rmv {@code True} if listens for remove events.
     * @param expire {@code True} if listens for expire events.
     * @param oldVal {@code True} if old value should be provided for event.
     * @throws Exception If failed.
     */
private void checkEvents(final IgniteCache<Object, Object> cache, final CacheEntryListenerConfiguration<Object, Object> lsnrCfg, Integer key, boolean create, boolean update, boolean rmv, boolean expire, boolean oldVal) throws Exception {
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            cache.registerCacheEntryListener(lsnrCfg);
            return null;
        }
    }, IllegalArgumentException.class, null);
    final int UPDATES = 10;
    int expEvts = 0;
    if (create)
        expEvts += 4;
    if (update)
        expEvts += (UPDATES + 1);
    if (rmv)
        expEvts += 2;
    if (expire)
        expEvts += 2;
    evts = Collections.synchronizedList(new ArrayList<CacheEntryEvent<?, ?>>());
    evtsLatch = new CountDownLatch(expEvts);
    cache.put(key(key), value(0));
    for (int i = 0; i < UPDATES; i++) {
        if (i % 2 == 0)
            cache.put(key(key), value(i + 1));
        else
            cache.invoke(key(key), new EntrySetValueProcessor(value(i + 1)));
    }
    // Invoke processor does not update value, should not trigger event.
    assertEquals(String.valueOf(UPDATES), cache.invoke(key(key), new EntryToStringProcessor()));
    assertFalse(cache.putIfAbsent(key(key), value(-1)));
    assertFalse(cache.remove(key(key), value(-1)));
    assertTrue(cache.remove(key(key)));
    IgniteCache<Object, Object> expirePlcCache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100)));
    expirePlcCache.put(key(key), value(10));
    U.sleep(700);
    if (!eagerTtl())
        // Provoke expire event if eager ttl is disabled.
        assertNull(primaryCache(key, cache.getName()).get(key(key)));
    IgniteCache<Object, Object> cache1 = cache;
    if (gridCount() > 1)
        // Do updates from another node.
        cache1 = jcache(1);
    cache1.put(key(key), value(1));
    cache1.put(key(key), value(2));
    assertTrue(cache1.remove(key(key)));
    IgniteCache<Object, Object> expirePlcCache1 = cache1.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 100)));
    expirePlcCache1.put(key(key), value(20));
    U.sleep(200);
    if (!eagerTtl())
        // Provoke expire event if eager ttl is disabled.
        assertNull(primaryCache(key, cache.getName()).get(key(key)));
    evtsLatch.await(5000, MILLISECONDS);
    assertEquals(expEvts, evts.size());
    Iterator<CacheEntryEvent<?, ?>> iter = evts.iterator();
    if (create)
        checkEvent(iter, key, CREATED, 0, null);
    if (update) {
        for (int i = 0; i < UPDATES; i++) checkEvent(iter, key, UPDATED, i + 1, oldVal ? i : null);
    }
    if (rmv)
        checkEvent(iter, key, REMOVED, null, oldVal ? UPDATES : null);
    if (create)
        checkEvent(iter, key, CREATED, 10, null);
    if (expire)
        checkEvent(iter, key, EXPIRED, null, oldVal ? 10 : null);
    if (create)
        checkEvent(iter, key, CREATED, 1, null);
    if (update)
        checkEvent(iter, key, UPDATED, 2, oldVal ? 1 : null);
    if (rmv)
        checkEvent(iter, key, REMOVED, null, oldVal ? 2 : null);
    if (create)
        checkEvent(iter, key, CREATED, 20, null);
    if (expire)
        checkEvent(iter, key, EXPIRED, null, oldVal ? 20 : null);
    assertEquals(0, evts.size());
    log.info("Remove listener.");
    cache.deregisterCacheEntryListener(lsnrCfg);
    cache.put(key(key), value(1));
    cache.put(key(key), value(2));
    assertTrue(cache.remove(key(key)));
    // Sleep some time to ensure listener was really removed.
    U.sleep(500);
    assertEquals(0, evts.size());
    cache.registerCacheEntryListener(lsnrCfg);
    cache.deregisterCacheEntryListener(lsnrCfg);
}
Also used : ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IOException(java.io.IOException)

Example 40 with CacheEntryEvent

use of javax.cache.event.CacheEntryEvent in project ignite by apache.

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatePartition.

/**
     * @param atomicityMode Cache atomicity mode.
     * @throws Exception If failed.
     */
private void concurrentUpdatePartition(CacheAtomicityMode atomicityMode) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    IgniteCache clientCache = client.createCache(ccfg);
    final AtomicInteger evtCnt = new AtomicInteger();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent evt : evts) {
                assertNotNull(evt.getKey());
                assertNotNull(evt.getValue());
                evtCnt.incrementAndGet();
            }
        }
    });
    clientCache.query(qry);
    Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 15; i++) {
        log.info("Iteration: " + i);
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                return null;
            }
        }, THREADS, "update");
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                log.info("Events: " + evtCnt.get());
                return evtCnt.get() >= THREADS * UPDATES;
            }
        }, 5000);
        assertEquals(THREADS * UPDATES, evtCnt.get());
        evtCnt.set(0);
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheEntryEvent (javax.cache.event.CacheEntryEvent)55 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 CountDownLatch (java.util.concurrent.CountDownLatch)24 ArrayList (java.util.ArrayList)18 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)14 Ignite (org.apache.ignite.Ignite)13 HashMap (java.util.HashMap)11 List (java.util.List)11 IgniteCache (org.apache.ignite.IgniteCache)8 PA (org.apache.ignite.internal.util.typedef.PA)8 QueryCursor (org.apache.ignite.cache.query.QueryCursor)7 T2 (org.apache.ignite.internal.util.typedef.T2)7 IgniteException (org.apache.ignite.IgniteException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4