Search in sources :

Example 11 with CacheEntryUpdatedListener

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

the class CacheDataStructuresManager method queue0.

/**
 * @param name Queue name.
 * @param cap Capacity.
 * @param colloc Collocated flag.
 * @param create Create flag.
 * @return Queue header.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
@Nullable
public <T> GridCacheQueueProxy<T> queue0(final String name, final int cap, boolean colloc, final boolean create) throws IgniteCheckedException {
    cctx.gate().enter();
    try {
        GridCacheQueueHeaderKey key = new GridCacheQueueHeaderKey(name);
        GridCacheQueueHeader hdr;
        if (create) {
            hdr = new GridCacheQueueHeader(IgniteUuid.randomUuid(), cap, colloc, 0, 0, null);
            GridCacheQueueHeader old = queueHdrView.withNoRetries().getAndPutIfAbsent(key, hdr);
            if (old != null) {
                if (old.capacity() != cap || old.collocated() != colloc)
                    throw new IgniteCheckedException("Failed to create queue, queue with the same name but " + "different configuration already exists [name=" + name + ']');
                hdr = old;
            }
        } else
            hdr = queueHdrView.get(key);
        if (hdr == null)
            return null;
        if (queueQryGuard.compareAndSet(false, true)) {
            queueQryId = cctx.continuousQueries().executeInternalQuery(new CacheEntryUpdatedListener<Object, Object>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                    if (!busyLock.enterBusy())
                        return;
                    try {
                        for (CacheEntryEvent<?, ?> e : evts) {
                            GridCacheQueueHeaderKey key = (GridCacheQueueHeaderKey) e.getKey();
                            GridCacheQueueHeader hdr = (GridCacheQueueHeader) e.getValue();
                            for (final GridCacheQueueProxy queue : queuesMap.values()) {
                                if (queue.name().equals(key.queueName())) {
                                    if (hdr == null) {
                                        GridCacheQueueHeader oldHdr = (GridCacheQueueHeader) e.getOldValue();
                                        assert oldHdr != null;
                                        if (oldHdr.id().equals(queue.delegate().id())) {
                                            queue.delegate().onRemoved(false);
                                            queuesMap.remove(queue.delegate().id());
                                        }
                                    } else
                                        queue.delegate().onHeaderChanged(hdr);
                                }
                            }
                        }
                    } finally {
                        busyLock.leaveBusy();
                    }
                }
            }, new QueueHeaderPredicate(), cctx.isLocal() || (cctx.isReplicated() && cctx.affinityNode()), true, false);
        }
        GridCacheQueueProxy queue = queuesMap.get(hdr.id());
        if (queue == null) {
            queue = new GridCacheQueueProxy(cctx, cctx.atomic() ? new GridAtomicCacheQueueImpl<>(name, hdr, cctx) : new GridTransactionalCacheQueueImpl<>(name, hdr, cctx));
            GridCacheQueueProxy old = queuesMap.putIfAbsent(hdr.id(), queue);
            if (old != null)
                queue = old;
        }
        return queue;
    } finally {
        cctx.gate().leave();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) GridCacheQueueProxy(org.apache.ignite.internal.processors.datastructures.GridCacheQueueProxy) GridCacheQueueHeaderKey(org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeaderKey) GridCacheQueueHeader(org.apache.ignite.internal.processors.datastructures.GridCacheQueueHeader) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with CacheEntryUpdatedListener

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

the class CacheContinuousQueryRandomOperationsTest method testFilterAndFactoryProvided.

/**
 * @throws Exception If failed.
 */
public void testFilterAndFactoryProvided() throws Exception {
    final CacheConfiguration<Object, Object> ccfg = cacheConfiguration(PARTITIONED, 1, ATOMIC, false);
    grid(0).createCache(ccfg);
    try {
        final ContinuousQuery qry = new ContinuousQuery();
        qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter>() {

            @Override
            public CacheEntryEventFilter create() {
                return null;
            }
        });
        qry.setRemoteFilter(new CacheEntryEventSerializableFilter() {

            @Override
            public boolean evaluate(CacheEntryEvent event) throws CacheEntryListenerException {
                return false;
            }
        });
        qry.setLocalListener(new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable iterable) throws CacheEntryListenerException {
            // No-op.
            }
        });
        GridTestUtils.assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                return grid(0).cache(ccfg.getName()).query(qry);
            }
        }, IgniteException.class, null);
    } finally {
        grid(0).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheLoaderException(javax.cache.integration.CacheLoaderException) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener)

Example 13 with CacheEntryUpdatedListener

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

the class GridCacheContinuousQueryMultiNodesFilteringTest method startGrid.

/**
 */
private Ignite startGrid(final int idx, boolean isClientMode) throws Exception {
    String igniteInstanceName = getTestIgniteInstanceName(idx);
    IgniteConfiguration cfg = optimize(getConfiguration(igniteInstanceName)).setClientMode(isClientMode);
    ((TcpDiscoverySpi) cfg.getDiscoverySpi()).setIpFinder(IP_FINDER);
    cfg.setUserAttributes(Collections.singletonMap("idx", idx));
    Ignite node = startGrid(igniteInstanceName, cfg);
    IgnitePredicate<ClusterNode> nodeFilter = new NodeFilter(idx);
    String partCacheName = "part" + idx;
    IgniteCache partCache = node.createCache(defaultCacheConfiguration().setName("part" + idx).setCacheMode(PARTITIONED).setBackups(1).setNodeFilter(nodeFilter));
    opCounts.put(partCacheName + "_ins", new AtomicInteger());
    opCounts.put(partCacheName + "_upd", new AtomicInteger());
    opCounts.put(partCacheName + "_rmv", new AtomicInteger());
    partCache.registerCacheEntryListener(new ListenerConfiguration(partCacheName, ListenerConfiguration.Op.INSERT));
    partCache.registerCacheEntryListener(new ListenerConfiguration(partCacheName, ListenerConfiguration.Op.UPDATE));
    partCache.registerCacheEntryListener(new ListenerConfiguration(partCacheName, ListenerConfiguration.Op.REMOVE));
    String replCacheName = "repl" + idx;
    IgniteCache replCache = node.createCache(defaultCacheConfiguration().setName("repl" + idx).setCacheMode(REPLICATED).setNodeFilter(nodeFilter));
    opCounts.put(replCacheName + "_ins", new AtomicInteger());
    opCounts.put(replCacheName + "_upd", new AtomicInteger());
    opCounts.put(replCacheName + "_rmv", new AtomicInteger());
    replCache.registerCacheEntryListener(new ListenerConfiguration(replCacheName, ListenerConfiguration.Op.INSERT));
    replCache.registerCacheEntryListener(new ListenerConfiguration(replCacheName, ListenerConfiguration.Op.UPDATE));
    replCache.registerCacheEntryListener(new ListenerConfiguration(replCacheName, ListenerConfiguration.Op.REMOVE));
    opCounts.put("qry" + idx + "_total", new AtomicInteger());
    ContinuousQuery qry = new ContinuousQuery();
    qry.setRemoteFilterFactory(new EntryEventFilterFactory(idx));
    qry.setLocalListener(new CacheEntryUpdatedListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void onUpdated(Iterable evts) {
            opCounts.get("qry" + idx + "_total").incrementAndGet();
        }
    });
    partCache.query(qry);
    replCache.query(qry);
    return node;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) IgniteCache(org.apache.ignite.IgniteCache) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 14 with CacheEntryUpdatedListener

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

the class IgniteCacheContinuousQueryNoUnsubscribeTest method checkNoUnsubscribe.

/**
 * @param client Client node flag.
 * @throws Exception If failed.
 */
private void checkNoUnsubscribe(boolean client) throws Exception {
    cntr.set(0);
    this.client = client;
    try (Ignite ignite = startGrid(3)) {
        ContinuousQuery qry = new ContinuousQuery();
        qry.setLocalListener(new CacheEntryUpdatedListener() {

            @Override
            public void onUpdated(Iterable evts) {
            // No-op.
            }
        });
        qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(CacheTestRemoteFilter.class));
        qry.setAutoUnsubscribe(false);
        ignite.cache(DEFAULT_CACHE_NAME).query(qry);
        ignite.cache(DEFAULT_CACHE_NAME).put(1, 1);
        assertEquals(1, cntr.get());
    }
    this.client = false;
    try (Ignite newSrv = startGrid(3)) {
        awaitPartitionMapExchange();
        Integer key = primaryKey(newSrv.cache(DEFAULT_CACHE_NAME));
        newSrv.cache(DEFAULT_CACHE_NAME).put(key, 1);
        assertEquals(2, cntr.get());
        for (int i = 0; i < 10; i++) ignite(0).cache(DEFAULT_CACHE_NAME).put(i, 1);
        assertEquals(12, cntr.get());
    }
    for (int i = 10; i < 20; i++) ignite(0).cache(DEFAULT_CACHE_NAME).put(i, 1);
    assertEquals(22, cntr.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Ignite(org.apache.ignite.Ignite)

Example 15 with CacheEntryUpdatedListener

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

the class TcpDiscoveryMultiThreadedTest method _testCustomEventNodeRestart.

/**
 * @throws Exception If failed.
 */
public void _testCustomEventNodeRestart() throws Exception {
    clientFlagGlobal = false;
    Ignite ignite = startGrid(0);
    ignite.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
    final long stopTime = System.currentTimeMillis() + 60_000;
    GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {

        @Override
        public void apply(Integer idx) {
            try {
                while (System.currentTimeMillis() < stopTime) {
                    Ignite ignite = startGrid(idx + 1);
                    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    int qryCnt = ThreadLocalRandom.current().nextInt(10) + 1;
                    for (int i = 0; i < qryCnt; i++) {
                        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                        qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                            @Override
                            public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                            // No-op.
                            }
                        });
                        QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry);
                        cur.close();
                    }
                    GridTestUtils.invoke(ignite.configuration().getDiscoverySpi(), "simulateNodeFailure");
                    ignite.close();
                }
            } catch (Exception e) {
                log.error("Unexpected error: " + e, e);
                throw new IgniteException(e);
            }
        }
    }, 5, "node-restart");
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)16 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 QueryCursor (org.apache.ignite.cache.query.QueryCursor)8 Ignite (org.apache.ignite.Ignite)7 IgniteCache (org.apache.ignite.IgniteCache)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteException (org.apache.ignite.IgniteException)6 Cache (javax.cache.Cache)4 ArrayList (java.util.ArrayList)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)3 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)3 PA (org.apache.ignite.internal.util.typedef.PA)3 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2