Search in sources :

Example 11 with GridLocalEventListener

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

the class GridIoManager method onKernalStart0.

/** {@inheritDoc} */
@SuppressWarnings({ "deprecation", "SynchronizationOnLocalVariableOrMethodParameter" })
@Override
public void onKernalStart0() throws IgniteCheckedException {
    discoLsnr = new GridLocalEventListener() {

        @SuppressWarnings({ "TooBroadScope", "fallthrough" })
        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent : "Invalid event: " + evt;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            UUID nodeId = discoEvt.eventNode().id();
            switch(evt.type()) {
                case EVT_NODE_JOINED:
                    // We can't receive messages from undiscovered nodes.
                    assert waitMap.get(nodeId) == null;
                    break;
                case EVT_NODE_LEFT:
                case EVT_NODE_FAILED:
                    for (Map.Entry<Object, ConcurrentMap<UUID, GridCommunicationMessageSet>> e : msgSetMap.entrySet()) {
                        ConcurrentMap<UUID, GridCommunicationMessageSet> map = e.getValue();
                        GridCommunicationMessageSet set;
                        boolean empty;
                        synchronized (map) {
                            set = map.remove(nodeId);
                            empty = map.isEmpty();
                        }
                        if (set != null) {
                            if (log.isDebugEnabled())
                                log.debug("Removed message set due to node leaving grid: " + set);
                            // Unregister timeout listener.
                            ctx.timeout().removeTimeoutObject(set);
                            // Node may still send stale messages for this topic
                            // even after discovery notification is done.
                            closedTopics.add(set.topic());
                        }
                        if (empty)
                            msgSetMap.remove(e.getKey(), map);
                    }
                    // Clean up delayed and ordered messages (need exclusive lock).
                    lock.writeLock().lock();
                    try {
                        ConcurrentLinkedDeque8<DelayedMessage> waitList = waitMap.remove(nodeId);
                        if (log.isDebugEnabled())
                            log.debug("Removed messages from discovery startup delay list " + "(sender node left topology): " + waitList);
                    } finally {
                        lock.writeLock().unlock();
                    }
                    break;
                default:
                    assert false : "Unexpected event: " + evt;
            }
        }
    };
    ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_JOINED, EVT_NODE_LEFT, EVT_NODE_FAILED);
    // Make sure that there are no stale messages due to window between communication
    // manager start and kernal start.
    // 1. Process wait list.
    Collection<Collection<DelayedMessage>> delayedMsgs = new ArrayList<>();
    lock.writeLock().lock();
    try {
        started = true;
        for (Entry<UUID, ConcurrentLinkedDeque8<DelayedMessage>> e : waitMap.entrySet()) {
            if (ctx.discovery().node(e.getKey()) != null) {
                ConcurrentLinkedDeque8<DelayedMessage> waitList = waitMap.remove(e.getKey());
                if (log.isDebugEnabled())
                    log.debug("Processing messages from discovery startup delay list: " + waitList);
                if (waitList != null)
                    delayedMsgs.add(waitList);
            }
        }
    } finally {
        lock.writeLock().unlock();
    }
    // After write lock released.
    if (!delayedMsgs.isEmpty()) {
        for (Collection<DelayedMessage> col : delayedMsgs) for (DelayedMessage msg : col) commLsnr.onMessage(msg.nodeId(), msg.message(), msg.callback());
    }
    // 2. Process messages sets.
    for (Map.Entry<Object, ConcurrentMap<UUID, GridCommunicationMessageSet>> e : msgSetMap.entrySet()) {
        ConcurrentMap<UUID, GridCommunicationMessageSet> map = e.getValue();
        for (GridCommunicationMessageSet set : map.values()) {
            if (ctx.discovery().node(set.nodeId()) == null) {
                // All map modifications should be synced for consistency.
                boolean rmv;
                synchronized (map) {
                    rmv = map.remove(set.nodeId(), set);
                }
                if (rmv) {
                    if (log.isDebugEnabled())
                        log.debug("Removed message set due to node leaving grid: " + set);
                    // Unregister timeout listener.
                    ctx.timeout().removeTimeoutObject(set);
                }
            }
        }
        boolean rmv;
        synchronized (map) {
            rmv = map.isEmpty();
        }
        if (rmv) {
            msgSetMap.remove(e.getKey(), map);
            // Node may still send stale messages for this topic
            // even after discovery notification is done.
            closedTopics.add(e.getKey());
        }
    }
}
Also used : ConcurrentLinkedDeque8(org.jsr166.ConcurrentLinkedDeque8) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) ConcurrentMap(java.util.concurrent.ConcurrentMap) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Entry(java.util.Map.Entry) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Collection(java.util.Collection) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 12 with GridLocalEventListener

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

the class GridDeploymentCommunication method sendResourceRequest.

/**
     * Sends request to the remote node and wait for response. If there is
     * no response until threshold time, method returns null.
     *
     *
     * @param rsrcName Resource name.
     * @param clsLdrId Class loader ID.
     * @param dstNode Remote node request should be sent to.
     * @param threshold Time in milliseconds when request is decided to
     *      be obsolete.
     * @return Either response value or {@code null} if timeout occurred.
     * @throws IgniteCheckedException Thrown if there is no connection with remote node.
     */
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter" })
GridDeploymentResponse sendResourceRequest(final String rsrcName, IgniteUuid clsLdrId, final ClusterNode dstNode, long threshold) throws IgniteCheckedException {
    assert rsrcName != null;
    assert dstNode != null;
    assert clsLdrId != null;
    Collection<UUID> nodeIds = activeReqNodeIds.get();
    if (nodeIds != null && nodeIds.contains(dstNode.id())) {
        if (log.isDebugEnabled())
            log.debug("Node attempts to load resource from one of the requesters " + "[rsrcName=" + rsrcName + ", dstNodeId=" + dstNode.id() + ", requesters=" + nodeIds + ']');
        GridDeploymentResponse fake = new GridDeploymentResponse();
        fake.success(false);
        fake.errorMessage("Node attempts to load resource from one of the requesters " + "[rsrcName=" + rsrcName + ", dstNodeId=" + dstNode.id() + ", requesters=" + nodeIds + ']');
        return fake;
    }
    Object resTopic = TOPIC_CLASSLOAD.topic(IgniteUuid.fromUuid(ctx.localNodeId()));
    GridDeploymentRequest req = new GridDeploymentRequest(resTopic, clsLdrId, rsrcName, false);
    // Send node IDs chain with request.
    req.nodeIds(nodeIds);
    final Object qryMux = new Object();
    final GridTuple<GridDeploymentResponse> res = new GridTuple<>();
    GridLocalEventListener discoLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent;
            assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
            DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
            UUID nodeId = discoEvt.eventNode().id();
            if (!nodeId.equals(dstNode.id()))
                // Not a destination node.
                return;
            GridDeploymentResponse fake = new GridDeploymentResponse();
            String errMsg = "Originating node left grid (resource will not be peer loaded) " + "[nodeId=" + dstNode.id() + ", rsrc=" + rsrcName + ']';
            U.warn(log, errMsg);
            fake.success(false);
            fake.errorMessage(errMsg);
            // because originating node has left grid.
            synchronized (qryMux) {
                res.set(fake);
                qryMux.notifyAll();
            }
        }
    };
    GridMessageListener resLsnr = new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            assert nodeId != null;
            assert msg != null;
            synchronized (qryMux) {
                if (!(msg instanceof GridDeploymentResponse)) {
                    U.error(log, "Received unknown peer class loading response [node=" + nodeId + ", msg=" + msg + ']');
                } else
                    res.set((GridDeploymentResponse) msg);
                qryMux.notifyAll();
            }
        }
    };
    try {
        ctx.io().addMessageListener(resTopic, resLsnr);
        // The destination node has potentially left grid here but in this case
        // Communication manager will throw the exception while sending message.
        ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        long start = U.currentTimeMillis();
        if (req.responseTopic() != null && !ctx.localNodeId().equals(dstNode.id()))
            req.responseTopicBytes(U.marshal(marsh, req.responseTopic()));
        ctx.io().sendToGridTopic(dstNode, TOPIC_CLASSLOAD, req, GridIoPolicy.P2P_POOL);
        if (log.isDebugEnabled())
            log.debug("Sent peer class loading request [node=" + dstNode.id() + ", req=" + req + ']');
        synchronized (qryMux) {
            try {
                long timeout = threshold - start;
                if (log.isDebugEnabled()) {
                    log.debug("Waiting for peer response from node [node=" + dstNode.id() + ", timeout=" + timeout + ']');
                }
                while (res.get() == null && timeout > 0) {
                    qryMux.wait(timeout);
                    timeout = threshold - U.currentTimeMillis();
                }
            } catch (InterruptedException e) {
                // Interrupt again to get it in the users code.
                Thread.currentThread().interrupt();
                throw new IgniteCheckedException("Got interrupted while waiting for response from node: " + dstNode.id(), e);
            }
        }
        if (res.get() == null) {
            U.warn(log, "Failed to receive peer response from node within duration [node=" + dstNode.id() + ", duration=" + (U.currentTimeMillis() - start) + ']');
        } else if (log.isDebugEnabled())
            log.debug("Received peer loading response [node=" + dstNode.id() + ", res=" + res.get() + ']');
        return res.get();
    } finally {
        ctx.event().removeLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
        ctx.io().removeMessageListener(resTopic, resLsnr);
    }
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID)

Example 13 with GridLocalEventListener

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

the class GridDeploymentPerLoaderStore method start.

/** {@inheritDoc} */
@Override
public void start() throws IgniteCheckedException {
    ctxLdr = U.detectClassLoader(getClass());
    discoLsnr = new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            assert evt instanceof DiscoveryEvent;
            UUID nodeId = ((DiscoveryEvent) evt).eventNode().id();
            if (evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED) {
                Collection<IsolatedDeployment> rmv = new LinkedList<>();
                synchronized (mux) {
                    for (Iterator<IsolatedDeployment> iter = cache.values().iterator(); iter.hasNext(); ) {
                        IsolatedDeployment dep = iter.next();
                        if (dep.senderNodeId().equals(nodeId)) {
                            dep.undeploy();
                            iter.remove();
                            rmv.add(dep);
                        }
                    }
                }
                for (IsolatedDeployment dep : rmv) dep.recordUndeployed();
            }
        }
    };
    ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);
    if (log.isDebugEnabled())
        log.debug(startInfo());
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) Iterator(java.util.Iterator) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) DeploymentEvent(org.apache.ignite.events.DeploymentEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Collection(java.util.Collection) UUID(java.util.UUID)

Example 14 with GridLocalEventListener

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

the class GridCacheDeploymentManager method start0.

/** {@inheritDoc} */
@Override
public void start0() throws IgniteCheckedException {
    globalLdr = new CacheClassLoader(cctx.gridConfig().getClassLoader());
    depEnabled = cctx.gridDeploy().enabled();
    if (depEnabled) {
        discoLsnr = new GridLocalEventListener() {

            @Override
            public void onEvent(Event evt) {
                assert evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT : "Unexpected event: " + evt;
                UUID id = ((DiscoveryEvent) evt).eventNode().id();
                if (log.isDebugEnabled())
                    log.debug("Processing node departure: " + id);
                for (Map.Entry<IgniteUuid, CachedDeploymentInfo<K, V>> entry : deps.entrySet()) {
                    CachedDeploymentInfo<K, V> d = entry.getValue();
                    if (log.isDebugEnabled())
                        log.debug("Examining cached info: " + d);
                    if (d.senderId().equals(id) || d.removeParticipant(id)) {
                        deps.remove(entry.getKey(), d);
                        if (log.isDebugEnabled())
                            log.debug("Removed cached info [d=" + d + ", deps=" + deps + ']');
                    }
                }
                allParticipants.remove(id);
            }
        };
        cctx.gridEvents().addLocalEventListener(discoLsnr, EVT_NODE_LEFT, EVT_NODE_FAILED);
    }
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) UUID(java.util.UUID)

Example 15 with GridLocalEventListener

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

the class AbstractDiscoveryTest method testDiscovery.

/**
     * @throws Exception If failed.
     */
public void testDiscovery() throws Exception {
    GridLocalEventListener discoLsnr = new DiscoveryListener();
    getSpiContext().addLocalEventListener(discoLsnr);
    Pinger pinger = null;
    if (isPingerStart()) {
        pinger = new Pinger();
        pinger.start();
    }
    JOptionPane.showMessageDialog(null, "Press OK to end test.");
    if (pinger != null)
        pinger.interrupt();
}
Also used : GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)

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