Search in sources :

Example 16 with GridLocalEventListener

use of org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener 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 17 with GridLocalEventListener

use of org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener in project ignite by apache.

the class GridReduceQueryExecutor method start.

/**
     * @param ctx Context.
     * @param h2 H2 Indexing.
     * @throws IgniteCheckedException If failed.
     */
public void start(final GridKernalContext ctx, final IgniteH2Indexing h2) throws IgniteCheckedException {
    this.ctx = ctx;
    this.h2 = h2;
    log = ctx.log(GridReduceQueryExecutor.class);
    ctx.io().addMessageListener(GridTopic.TOPIC_QUERY, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            if (!busyLock.enterBusy())
                return;
            try {
                if (msg instanceof GridCacheQueryMarshallable)
                    ((GridCacheQueryMarshallable) msg).unmarshall(ctx.config().getMarshaller(), ctx);
                GridReduceQueryExecutor.this.onMessage(nodeId, msg);
            } finally {
                busyLock.leaveBusy();
            }
        }
    });
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(final Event evt) {
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            for (ReduceQueryRun r : runs.values()) {
                for (GridMergeIndex idx : r.indexes()) {
                    if (idx.hasSource(nodeId)) {
                        handleNodeLeft(r, nodeId);
                        break;
                    }
                }
            }
        }
    }, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridCacheQueryMarshallable(org.apache.ignite.internal.processors.cache.query.GridCacheQueryMarshallable) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Example 18 with GridLocalEventListener

use of org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener in project ignite by apache.

the class GridMapQueryExecutor method start.

/**
     * @param ctx Context.
     * @param h2 H2 Indexing.
     * @throws IgniteCheckedException If failed.
     */
public void start(final GridKernalContext ctx, IgniteH2Indexing h2) throws IgniteCheckedException {
    this.ctx = ctx;
    this.h2 = h2;
    log = ctx.log(GridMapQueryExecutor.class);
    final UUID locNodeId = ctx.localNodeId();
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(final Event evt) {
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            GridH2QueryContext.clearAfterDeadNode(locNodeId, nodeId);
            NodeResults nodeRess = qryRess.remove(nodeId);
            if (nodeRess == null)
                return;
            nodeRess.cancelAll();
        }
    }, EventType.EVT_NODE_FAILED, EventType.EVT_NODE_LEFT);
    ctx.io().addMessageListener(GridTopic.TOPIC_QUERY, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            if (!busyLock.enterBusy())
                return;
            try {
                if (msg instanceof GridCacheQueryMarshallable)
                    ((GridCacheQueryMarshallable) msg).unmarshall(ctx.config().getMarshaller(), ctx);
                GridMapQueryExecutor.this.onMessage(nodeId, msg);
            } finally {
                busyLock.leaveBusy();
            }
        }
    });
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridCacheQueryMarshallable(org.apache.ignite.internal.processors.cache.query.GridCacheQueryMarshallable) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CacheQueryReadEvent(org.apache.ignite.events.CacheQueryReadEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridH2ValueCacheObject(org.apache.ignite.internal.processors.query.h2.opt.GridH2ValueCacheObject) UUID(java.util.UUID)

Example 19 with GridLocalEventListener

use of org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener in project ignite by apache.

the class GridContinuousProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    if (ctx.config().isDaemon())
        return;
    retryDelay = ctx.config().getNetworkSendRetryDelay();
    retryCnt = ctx.config().getNetworkSendRetryCount();
    marsh = ctx.config().getMarshaller();
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @SuppressWarnings({ "fallthrough", "TooBroadScope" })
        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent;
            assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            clientInfos.remove(nodeId);
            // Unregister handlers created by left node.
            for (Map.Entry<UUID, RemoteRoutineInfo> e : rmtInfos.entrySet()) {
                UUID routineId = e.getKey();
                RemoteRoutineInfo info = e.getValue();
                if (nodeId.equals(info.nodeId)) {
                    if (info.autoUnsubscribe)
                        unregisterRemote(routineId);
                    if (info.hnd.isQuery())
                        info.hnd.onNodeLeft();
                }
            }
            for (Map.Entry<IgniteUuid, SyncMessageAckFuture> e : syncMsgFuts.entrySet()) {
                SyncMessageAckFuture fut = e.getValue();
                if (fut.nodeId().equals(nodeId)) {
                    SyncMessageAckFuture fut0 = syncMsgFuts.remove(e.getKey());
                    if (fut0 != null) {
                        ClusterTopologyCheckedException err = new ClusterTopologyCheckedException("Node left grid while sending message to: " + nodeId);
                        fut0.onDone(err);
                    }
                }
            }
        }
    }, EVT_NODE_LEFT, EVT_NODE_FAILED);
    ctx.event().addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            cancelFutures(new IgniteCheckedException("Topology segmented"));
        }
    }, EVT_NODE_SEGMENTED);
    ctx.discovery().setCustomEventListener(StartRoutineDiscoveryMessage.class, new CustomEventListener<StartRoutineDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineDiscoveryMessage msg) {
            if (!snd.id().equals(ctx.localNodeId()) && !ctx.isStopping())
                processStartRequest(snd, msg);
        }
    });
    ctx.discovery().setCustomEventListener(StartRoutineAckDiscoveryMessage.class, new CustomEventListener<StartRoutineAckDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StartRoutineAckDiscoveryMessage msg) {
            StartFuture fut = startFuts.remove(msg.routineId());
            if (fut != null) {
                if (msg.errs().isEmpty()) {
                    LocalRoutineInfo routine = locInfos.get(msg.routineId());
                    // Update partition counters.
                    if (routine != null && routine.handler().isQuery()) {
                        Map<UUID, Map<Integer, T2<Long, Long>>> cntrsPerNode = msg.updateCountersPerNode();
                        Map<Integer, T2<Long, Long>> cntrs = msg.updateCounters();
                        GridCacheAdapter<Object, Object> interCache = ctx.cache().internalCache(routine.handler().cacheName());
                        GridCacheContext cctx = interCache != null ? interCache.context() : null;
                        if (cctx != null && cntrsPerNode != null && !cctx.isLocal() && cctx.affinityNode())
                            cntrsPerNode.put(ctx.localNodeId(), cctx.topology().updateCounters(false));
                        routine.handler().updateCounters(topVer, cntrsPerNode, cntrs);
                    }
                    fut.onRemoteRegistered();
                } else {
                    IgniteCheckedException firstEx = F.first(msg.errs().values());
                    fut.onDone(firstEx);
                    stopRoutine(msg.routineId());
                }
            }
        }
    });
    ctx.discovery().setCustomEventListener(StopRoutineDiscoveryMessage.class, new CustomEventListener<StopRoutineDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineDiscoveryMessage msg) {
            if (!snd.id().equals(ctx.localNodeId())) {
                UUID routineId = msg.routineId();
                unregisterRemote(routineId);
            }
            for (Map<UUID, LocalRoutineInfo> clientInfo : clientInfos.values()) {
                if (clientInfo.remove(msg.routineId()) != null)
                    break;
            }
        }
    });
    ctx.discovery().setCustomEventListener(StopRoutineAckDiscoveryMessage.class, new CustomEventListener<StopRoutineAckDiscoveryMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, StopRoutineAckDiscoveryMessage msg) {
            StopFuture fut = stopFuts.remove(msg.routineId());
            if (fut != null)
                fut.onDone();
        }
    });
    ctx.io().addMessageListener(TOPIC_CONTINUOUS, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object obj) {
            GridContinuousMessage msg = (GridContinuousMessage) obj;
            if (msg.data() == null && msg.dataBytes() != null) {
                try {
                    msg.data(U.unmarshal(marsh, msg.dataBytes(), U.resolveClassLoader(ctx.config())));
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to process message (ignoring): " + msg, e);
                    return;
                }
            }
            switch(msg.type()) {
                case MSG_EVT_NOTIFICATION:
                    processNotification(nodeId, msg);
                    break;
                case MSG_EVT_ACK:
                    processMessageAck(msg);
                    break;
                default:
                    assert false : "Unexpected message received: " + msg.type();
            }
        }
    });
    ctx.cacheObjects().onContinuousProcessorStarted(ctx);
    ctx.service().onContinuousProcessorStarted(ctx);
    if (log.isDebugEnabled())
        log.debug("Continuous processor started.");
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) AtomicLong(java.util.concurrent.atomic.AtomicLong) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 20 with GridLocalEventListener

use of org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener in project ignite by apache.

the class GridJobProcessor method requestJobSiblings.

/**
     * @param ses Session.
     * @return Siblings.
     * @throws IgniteCheckedException If failed.
     */
public Collection<ComputeJobSibling> requestJobSiblings(final ComputeTaskSession ses) throws IgniteCheckedException {
    assert ses != null;
    final UUID taskNodeId = ses.getTaskNodeId();
    ClusterNode taskNode = ctx.discovery().node(taskNodeId);
    if (taskNode == null)
        throw new IgniteCheckedException("Node that originated task execution has left grid: " + taskNodeId);
    // Tuple: error message-response.
    final IgniteBiTuple<String, GridJobSiblingsResponse> t = new IgniteBiTuple<>();
    final Lock lock = new ReentrantLock();
    final Condition cond = lock.newCondition();
    GridMessageListener msgLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            String err = null;
            GridJobSiblingsResponse res = null;
            if (!(msg instanceof GridJobSiblingsResponse))
                err = "Received unexpected message: " + msg;
            else if (!nodeId.equals(taskNodeId))
                err = "Received job siblings response from unexpected node [taskNodeId=" + taskNodeId + ", nodeId=" + nodeId + ']';
            else {
                // Sender and message type are fine.
                res = (GridJobSiblingsResponse) msg;
                if (res.jobSiblings() == null) {
                    try {
                        res.unmarshalSiblings(marsh);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to unmarshal job siblings.", e);
                        err = e.getMessage();
                    }
                }
            }
            lock.lock();
            try {
                if (t.isEmpty()) {
                    t.set(err, res);
                    cond.signalAll();
                }
            } finally {
                lock.unlock();
            }
        }
    };
    GridLocalEventListener discoLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent && (evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT) : "Unexpected event: " + evt;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            if (taskNodeId.equals(discoEvt.eventNode().id())) {
                lock.lock();
                try {
                    if (t.isEmpty()) {
                        t.set("Node that originated task execution has left grid: " + taskNodeId, null);
                        cond.signalAll();
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    };
    boolean loc = ctx.localNodeId().equals(taskNodeId);
    // 1. Create unique topic name.
    Object topic = TOPIC_JOB_SIBLINGS.topic(ses.getId(), topicIdGen.getAndIncrement());
    try {
        // 2. Register listener.
        ctx.io().addMessageListener(topic, msgLsnr);
        // 3. Send message.
        ctx.io().sendToGridTopic(taskNode, TOPIC_JOB_SIBLINGS, new GridJobSiblingsRequest(ses.getId(), loc ? topic : null, loc ? null : U.marshal(marsh, topic)), SYSTEM_POOL);
        // 4. Listen to discovery events.
        ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        // 5. Check whether node has left before disco listener has been installed.
        taskNode = ctx.discovery().node(taskNodeId);
        if (taskNode == null)
            throw new IgniteCheckedException("Node that originated task execution has left grid: " + taskNodeId);
        // 6. Wait for result.
        lock.lock();
        try {
            long netTimeout = ctx.config().getNetworkTimeout();
            if (t.isEmpty())
                cond.await(netTimeout, MILLISECONDS);
            if (t.isEmpty())
                throw new IgniteCheckedException("Timed out waiting for job siblings (consider increasing" + "'networkTimeout' configuration property) [ses=" + ses + ", netTimeout=" + netTimeout + ']');
            // Error is set?
            if (t.get1() != null)
                throw new IgniteCheckedException(t.get1());
            else
                // Return result
                return t.get2().jobSiblings();
        } catch (InterruptedException e) {
            throw new IgniteCheckedException("Interrupted while waiting for job siblings response: " + ses, e);
        } finally {
            lock.unlock();
        }
    } finally {
        ctx.io().removeMessageListener(topic, msgLsnr);
        ctx.event().removeLocalEventListener(discoLsnr);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridJobSiblingsResponse(org.apache.ignite.internal.GridJobSiblingsResponse) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridSpinReadWriteLock(org.apache.ignite.internal.util.GridSpinReadWriteLock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) GridJobSiblingsRequest(org.apache.ignite.internal.GridJobSiblingsRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Aggregations

GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)26 Event (org.apache.ignite.events.Event)25 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)22 UUID (java.util.UUID)17 ClusterNode (org.apache.ignite.cluster.ClusterNode)9 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Collection (java.util.Collection)5 Map (java.util.Map)4 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)4 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)4 HashMap (java.util.HashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 JobEvent (org.apache.ignite.events.JobEvent)3 TaskEvent (org.apache.ignite.events.TaskEvent)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Condition (java.util.concurrent.locks.Condition)2