Search in sources :

Example 26 with GridLocalEventListener

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

the class JobStealingCollisionSpi method onContextInitialized0.

/** {@inheritDoc} */
@Override
protected void onContextInitialized0(IgniteSpiContext spiCtx) throws IgniteSpiException {
    spiCtx.addLocalEventListener(discoLsnr = new GridLocalEventListener() {

        @SuppressWarnings("fallthrough")
        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            UUID evtNodeId = discoEvt.eventNode().id();
            switch(discoEvt.type()) {
                case EVT_NODE_JOINED:
                    ClusterNode node = getSpiContext().node(evtNodeId);
                    if (node != null) {
                        nodeQueue.offer(node);
                        sndMsgMap.putIfAbsent(node.id(), new MessageInfo());
                        rcvMsgMap.putIfAbsent(node.id(), new MessageInfo());
                    }
                    break;
                case EVT_NODE_LEFT:
                case EVT_NODE_FAILED:
                    Iterator<ClusterNode> iter = nodeQueue.iterator();
                    while (iter.hasNext()) {
                        ClusterNode nextNode = iter.next();
                        if (nextNode.id().equals(evtNodeId))
                            iter.remove();
                    }
                    sndMsgMap.remove(evtNodeId);
                    rcvMsgMap.remove(evtNodeId);
                    break;
                default:
                    assert false : "Unexpected event: " + evt;
            }
        }
    }, EVT_NODE_FAILED, EVT_NODE_JOINED, EVT_NODE_LEFT);
    Collection<ClusterNode> rmtNodes = spiCtx.remoteNodes();
    for (ClusterNode node : rmtNodes) {
        UUID id = node.id();
        if (spiCtx.node(id) != null) {
            sndMsgMap.putIfAbsent(id, new MessageInfo());
            rcvMsgMap.putIfAbsent(id, new MessageInfo());
            // Check if node has concurrently left.
            if (spiCtx.node(id) == null) {
                sndMsgMap.remove(id);
                rcvMsgMap.remove(id);
            }
        }
    }
    nodeQueue.addAll(rmtNodes);
    Iterator<ClusterNode> iter = nodeQueue.iterator();
    while (iter.hasNext()) {
        ClusterNode nextNode = iter.next();
        if (spiCtx.node(nextNode.id()) == null)
            iter.remove();
    }
    spiCtx.addMessageListener(msgLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            MessageInfo info = rcvMsgMap.get(nodeId);
            if (info == null) {
                if (log.isDebugEnabled())
                    log.debug("Ignoring message steal request as discovery event has not yet been received " + "for node: " + nodeId);
                return;
            }
            int stealReqs0;
            synchronized (info) {
                JobStealingRequest req = (JobStealingRequest) msg;
                // Increment total number of steal requests.
                // Note that it is critical to increment total
                // number of steal requests before resetting message info.
                stealReqs0 = stealReqs.addAndGet(req.delta() - info.jobsToSteal());
                info.reset(req.delta());
            }
            if (log.isDebugEnabled())
                log.debug("Received steal request [nodeId=" + nodeId + ", msg=" + msg + ", stealReqs=" + stealReqs0 + ']');
            CollisionExternalListener tmp = extLsnr;
            // Let grid know that collisions should be resolved.
            if (tmp != null)
                tmp.onExternalCollision();
        }
    }, JOB_STEALING_COMM_TOPIC);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) CollisionExternalListener(org.apache.ignite.spi.collision.CollisionExternalListener) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) 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