Search in sources :

Example 61 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 62 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];
                ThreadLocalRandom rnd = ThreadLocalRandom.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) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) 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) 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 63 with CacheEntryEvent

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

the class CacheContinuousQueryFailoverAbstractSelfTest method checkEvents.

/**
 * @param logAll If {@code true} logs all unexpected values.
 * @param expEvts Expected values.
 * @param lsnr Listener.
 * @return Check status.
 */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private boolean checkEvents(boolean logAll, Map<Integer, List<T2<Integer, Integer>>> expEvts, CacheEventListener2 lsnr) {
    assertTrue(!expEvts.isEmpty());
    boolean pass = true;
    for (Map.Entry<Integer, List<T2<Integer, Integer>>> e : expEvts.entrySet()) {
        Integer key = e.getKey();
        List<T2<Integer, Integer>> exp = e.getValue();
        List<CacheEntryEvent<?, ?>> rcvdEvts = lsnr.evts.get(key);
        if (rcvdEvts == null) {
            pass = false;
            log.info("No events for key [key=" + key + ", exp=" + e.getValue() + ']');
            if (!logAll)
                return false;
        } else {
            synchronized (rcvdEvts) {
                if (rcvdEvts.size() != exp.size()) {
                    pass = false;
                    log.info("Missed or extra events for key [key=" + key + ", exp=" + e.getValue() + ", rcvd=" + rcvdEvts + ']');
                    if (!logAll)
                        return false;
                }
                int cnt = Math.min(rcvdEvts.size(), exp.size());
                for (int i = 0; i < cnt; i++) {
                    T2<Integer, Integer> expEvt = exp.get(i);
                    CacheEntryEvent<?, ?> rcvdEvt = rcvdEvts.get(i);
                    if (pass) {
                        assertEquals(key, rcvdEvt.getKey());
                        assertEquals(expEvt.get1(), rcvdEvt.getValue());
                    } else {
                        if (!key.equals(rcvdEvt.getKey()) || !expEvt.get1().equals(rcvdEvt.getValue()))
                            log.warning("Missed events. [key=" + key + ", actKey=" + rcvdEvt.getKey() + ", expVal=" + expEvt.get1() + ", actVal=" + rcvdEvt.getValue() + "]");
                    }
                }
                if (!pass) {
                    for (int i = cnt; i < exp.size(); i++) {
                        T2<Integer, Integer> val = exp.get(i);
                        log.warning("Missed events. [key=" + key + ", expVal=" + val.get1() + ", prevVal=" + val.get2() + "]");
                    }
                }
            }
        }
    }
    if (pass) {
        expEvts.clear();
        lsnr.evts.clear();
    }
    return pass;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) CachePartitionPartialCountersMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.CachePartitionPartialCountersMap) CacheEntryEvent(javax.cache.event.CacheEntryEvent) T2(org.apache.ignite.internal.util.typedef.T2)

Example 64 with CacheEntryEvent

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testRebalance.

/**
 * Test that during rebalancing correct old value passed to continuous query.
 *
 * @throws Exception If fail.
 */
public void testRebalance() throws Exception {
    for (int iter = 0; iter < 5; iter++) {
        log.info("Iteration: " + iter);
        final IgniteEx ignite = startGrid(1);
        final CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>("testCache");
        ccfg.setAtomicityMode(atomicityMode());
        ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
        ccfg.setCacheMode(cacheMode());
        ccfg.setRebalanceMode(CacheRebalanceMode.SYNC);
        ccfg.setBackups(2);
        final IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(ccfg);
        final int KEYS = 10_000;
        for (int i = 0; i < KEYS; i++) cache.put(i, i);
        final ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
        final AtomicBoolean err = new AtomicBoolean();
        final AtomicInteger cntr = new AtomicInteger();
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {

            @Override
            public void onUpdated(final Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> cacheEntryEvts) {
                try {
                    for (final CacheEntryEvent<? extends Integer, ? extends Integer> evt : cacheEntryEvts) {
                        final Integer oldVal = evt.getOldValue();
                        final Integer val = evt.getValue();
                        assertNotNull("No old value: " + evt, oldVal);
                        assertEquals("Unexpected old value: " + evt, (Integer) (oldVal + 1), val);
                        cntr.incrementAndGet();
                    }
                } catch (Throwable e) {
                    err.set(true);
                    error("Unexpected error: " + e, e);
                }
            }
        });
        final QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry);
        final CountDownLatch latch = new CountDownLatch(1);
        final IgniteInternalFuture<Object> updFut = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                latch.await();
                for (int i = 0; i < KEYS && !err.get(); i++) cache.put(i, i + 1);
                return null;
            }
        });
        final IgniteInternalFuture<Object> rebFut = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                latch.await();
                for (int i = 2; i <= 5 && !err.get(); i++) startGrid(i);
                return null;
            }
        });
        latch.countDown();
        updFut.get();
        rebFut.get();
        assertFalse("Unexpected error during test", err.get());
        assertTrue(cntr.get() > 0);
        cur.close();
        stopAllGrids();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MutableEntry(javax.cache.processor.MutableEntry) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheEntryEvent (javax.cache.event.CacheEntryEvent)64 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 CountDownLatch (java.util.concurrent.CountDownLatch)29 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)19 Ignite (org.apache.ignite.Ignite)19 ArrayList (java.util.ArrayList)18 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)16 HashMap (java.util.HashMap)12 List (java.util.List)11 IgniteCache (org.apache.ignite.IgniteCache)11 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)10 QueryCursor (org.apache.ignite.cache.query.QueryCursor)10 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)8 PA (org.apache.ignite.internal.util.typedef.PA)8 T2 (org.apache.ignite.internal.util.typedef.T2)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)6 Cache (javax.cache.Cache)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteException (org.apache.ignite.IgniteException)6