Search in sources :

Example 1 with CollisionExternalListener

use of org.apache.ignite.spi.collision.CollisionExternalListener in project ignite by apache.

the class JobStealingCollisionSpi method onContextInitialized0.

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

        @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, byte plc) {
            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

UUID (java.util.UUID)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 Event (org.apache.ignite.events.Event)1 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)1 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)1 CollisionExternalListener (org.apache.ignite.spi.collision.CollisionExternalListener)1