Search in sources :

Example 1 with PlatformContinuousQueryFilter

use of org.apache.ignite.internal.processors.platform.cache.query.PlatformContinuousQueryFilter in project ignite by apache.

the class CacheContinuousQueryHandler 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 (locLsnr != null) {
        if (locLsnr instanceof JCacheQueryLocalListener) {
            ctx.resource().injectGeneric(((JCacheQueryLocalListener) locLsnr).impl);
            asyncCb = ((JCacheQueryLocalListener) locLsnr).async();
        } else {
            ctx.resource().injectGeneric(locLsnr);
            asyncCb = U.hasAnnotation(locLsnr, IgniteAsyncCallback.class);
        }
    }
    final CacheEntryEventFilter filter = getEventFilter();
    if (filter != null) {
        if (filter instanceof JCacheQueryRemoteFilter) {
            if (((JCacheQueryRemoteFilter) filter).impl != null)
                ctx.resource().injectGeneric(((JCacheQueryRemoteFilter) filter).impl);
            if (!asyncCb)
                asyncCb = ((JCacheQueryRemoteFilter) filter).async();
        } else {
            ctx.resource().injectGeneric(filter);
            if (!asyncCb)
                asyncCb = U.hasAnnotation(filter, IgniteAsyncCallback.class);
        }
    }
    entryBufs = new ConcurrentHashMap<>();
    ackBuf = new CacheContinuousQueryAcknowledgeBuffer();
    rcvs = new ConcurrentHashMap<>();
    this.nodeId = nodeId;
    this.routineId = routineId;
    this.ctx = ctx;
    final boolean loc = nodeId.equals(ctx.localNodeId());
    assert !skipPrimaryCheck || loc;
    log = ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY);
    CacheContinuousQueryListener<K, V> lsnr = new CacheContinuousQueryListener<K, V>() {

        @Override
        public void onExecution() {
            if (ctx.event().isRecordable(EVT_CACHE_QUERY_EXECUTED)) {
                ctx.event().record(new CacheQueryExecutedEvent<>(ctx.discovery().localNode(), "Continuous query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.CONTINUOUS.name(), cacheName, null, null, null, filter instanceof CacheEntryEventSerializableFilter ? (CacheEntryEventSerializableFilter) filter : null, null, nodeId, taskName()));
            }
        }

        @Override
        public boolean keepBinary() {
            return keepBinary;
        }

        @Override
        public void onEntryUpdated(final CacheContinuousQueryEvent<K, V> evt, boolean primary, final boolean recordIgniteEvt, GridDhtAtomicAbstractUpdateFuture fut) {
            if (ignoreExpired && evt.getEventType() == EventType.EXPIRED)
                return;
            if (log.isDebugEnabled())
                log.debug("Entry updated on affinity node [evt=" + evt + ", primary=" + primary + ']');
            final GridCacheContext<K, V> cctx = cacheContext(ctx);
            // Check that cache stopped.
            if (cctx == null)
                return;
            // skipPrimaryCheck is set only when listen locally for replicated cache events.
            assert !skipPrimaryCheck || (cctx.isReplicated() && ctx.localNodeId().equals(nodeId));
            if (asyncCb) {
                ContinuousQueryAsyncClosure clsr = new ContinuousQueryAsyncClosure(primary, evt, recordIgniteEvt, fut);
                ctx.asyncCallbackPool().execute(clsr, evt.partitionId());
            } else {
                final boolean notify = filter(evt);
                if (log.isDebugEnabled())
                    log.debug("Filter invoked for event [evt=" + evt + ", primary=" + primary + ", notify=" + notify + ']');
                if (primary || skipPrimaryCheck) {
                    if (fut == null)
                        onEntryUpdate(evt, notify, loc, recordIgniteEvt);
                    else {
                        fut.addContinuousQueryClosure(new CI1<Boolean>() {

                            @Override
                            public void apply(Boolean suc) {
                                if (!suc)
                                    evt.entry().markFiltered();
                                onEntryUpdate(evt, notify, loc, recordIgniteEvt);
                            }
                        }, sync);
                    }
                } else
                    handleBackupEntry(cctx, evt.entry());
            }
        }

        @Override
        public void onUnregister() {
            if (filter instanceof PlatformContinuousQueryFilter)
                ((PlatformContinuousQueryFilter) filter).onQueryUnregister();
        }

        @Override
        public void cleanupBackupQueue(Map<Integer, Long> updateCntrs) {
            for (Map.Entry<Integer, Long> e : updateCntrs.entrySet()) {
                CacheContinuousQueryEventBuffer buf = entryBufs.get(e.getKey());
                if (buf != null)
                    buf.cleanupBackupQueue(e.getValue());
            }
        }

        @Override
        public void flushBackupQueue(GridKernalContext ctx, AffinityTopologyVersion topVer) {
            assert topVer != null;
            try {
                GridCacheContext<K, V> cctx = cacheContext(ctx);
                ClusterNode node = ctx.discovery().node(nodeId);
                for (Map.Entry<Integer, CacheContinuousQueryEventBuffer> bufE : entryBufs.entrySet()) {
                    CacheContinuousQueryEventBuffer buf = bufE.getValue();
                    Collection<CacheContinuousQueryEntry> backupQueue = buf.flushOnExchange();
                    if (backupQueue != null && node != null) {
                        for (CacheContinuousQueryEntry e : backupQueue) {
                            e.markBackup();
                            if (!e.isFiltered())
                                prepareEntry(cctx, nodeId, e);
                        }
                        ctx.continuous().addBackupNotification(nodeId, routineId, backupQueue, topic);
                    }
                }
            } catch (IgniteCheckedException e) {
                U.error(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), "Failed to send backup event notification to node: " + nodeId, e);
            }
        }

        @Override
        public void acknowledgeBackupOnTimeout(GridKernalContext ctx) {
            sendBackupAcknowledge(ackBuf.acknowledgeOnTimeout(), routineId, ctx);
        }

        @Override
        public void skipUpdateEvent(CacheContinuousQueryEvent<K, V> evt, AffinityTopologyVersion topVer, boolean primary) {
            assert evt != null;
            CacheContinuousQueryEntry e = evt.entry();
            e.markFiltered();
            onEntryUpdated(evt, primary, false, null);
        }

        @Override
        public void onPartitionEvicted(int part) {
            entryBufs.remove(part);
        }

        @Override
        public boolean oldValueRequired() {
            return oldValRequired;
        }

        @Override
        public boolean notifyExisting() {
            return notifyExisting;
        }

        private String taskName() {
            return ctx.security().enabled() ? ctx.task().resolveTaskName(taskHash) : null;
        }
    };
    CacheContinuousQueryManager mgr = manager(ctx);
    if (mgr == null)
        return RegisterStatus.DELAYED;
    return mgr.registerListener(routineId, lsnr, internal);
}
Also used : PlatformContinuousQueryFilter(org.apache.ignite.internal.processors.platform.cache.query.PlatformContinuousQueryFilter) GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) JCacheQueryLocalListener(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager.JCacheQueryLocalListener) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteAsyncCallback(org.apache.ignite.lang.IgniteAsyncCallback) ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtAtomicAbstractUpdateFuture(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicAbstractUpdateFuture) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) JCacheQueryRemoteFilter(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager.JCacheQueryRemoteFilter)

Aggregations

Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1 GridDhtAtomicAbstractUpdateFuture (org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicAbstractUpdateFuture)1 JCacheQueryLocalListener (org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager.JCacheQueryLocalListener)1 JCacheQueryRemoteFilter (org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager.JCacheQueryRemoteFilter)1 PlatformContinuousQueryFilter (org.apache.ignite.internal.processors.platform.cache.query.PlatformContinuousQueryFilter)1 IgniteAsyncCallback (org.apache.ignite.lang.IgniteAsyncCallback)1