Search in sources :

Example 51 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class ClusterGroupAbstractTest method testExecution.

/**
 * @throws Exception If test failed.
 */
public void testExecution() throws Exception {
    String name = "oneMoreGrid";
    Collection<IgniteBiTuple<Ignite, IgnitePredicate<Event>>> lsnrs = new LinkedList<>();
    try {
        final AtomicInteger cnt = new AtomicInteger();
        Ignite g = startGrid(name);
        IgnitePredicate<Event> lsnr;
        if (!IgniteCluster.class.isAssignableFrom(projection().getClass())) {
            g.events().localListen(lsnr = new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    assert evt.type() == EVT_JOB_STARTED;
                    assert false;
                    return true;
                }
            }, EVT_JOB_STARTED);
            lsnrs.add(F.t(g, lsnr));
        }
        for (ClusterNode node : prj.nodes()) {
            g = G.ignite(node.id());
            g.events().localListen(lsnr = new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event evt) {
                    assert evt.type() == EVT_JOB_STARTED;
                    synchronized (mux) {
                        cnt.incrementAndGet();
                        mux.notifyAll();
                    }
                    return true;
                }
            }, EVT_JOB_STARTED);
            lsnrs.add(F.t(g, lsnr));
        }
        run1(cnt);
        run2(cnt);
        call1(cnt);
        call2(cnt);
        call3(cnt);
        call4(cnt);
        call5(cnt);
        forkjoin1(cnt);
        forkjoin2(cnt);
        exec1(cnt);
        exec2(cnt);
        executorService(cnt);
        checkActiveFutures();
    } finally {
        for (IgniteBiTuple<Ignite, IgnitePredicate<Event>> t : lsnrs) t.get1().events().stopLocalListen(t.get2(), EVT_JOB_STARTED);
        stopGrid(name);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCluster(org.apache.ignite.IgniteCluster) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) LinkedList(java.util.LinkedList)

Example 52 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class AdaptiveLoadBalancingSpi method onContextInitialized0.

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

        @Override
        public void onEvent(Event evt) {
            switch(evt.type()) {
                case EVT_TASK_FINISHED:
                case EVT_TASK_FAILED:
                    {
                        TaskEvent taskEvt = (TaskEvent) evt;
                        taskTops.remove(taskEvt.taskSessionId());
                        if (log.isDebugEnabled())
                            log.debug("Removed task topology from topology cache for session: " + taskEvt.taskSessionId());
                        break;
                    }
                case EVT_JOB_MAPPED:
                    {
                        // We should keep topology and use cache in ComputeTask#map() method to
                        // avoid O(n*n/2) complexity, after that we can drop caches.
                        // Here we set mapped property and later cache will be ignored
                        JobEvent jobEvt = (JobEvent) evt;
                        IgniteBiTuple<Boolean, WeightedTopology> weightedTop = taskTops.get(jobEvt.taskSessionId());
                        if (weightedTop != null)
                            weightedTop.set1(true);
                        if (log.isDebugEnabled())
                            log.debug("Job has been mapped. Ignore cache for session: " + jobEvt.taskSessionId());
                        break;
                    }
                case EVT_NODE_METRICS_UPDATED:
                case EVT_NODE_FAILED:
                case EVT_NODE_JOINED:
                case EVT_NODE_LEFT:
                    {
                        DiscoveryEvent discoEvt = (DiscoveryEvent) evt;
                        rwLock.writeLock().lock();
                        try {
                            switch(evt.type()) {
                                case EVT_NODE_JOINED:
                                    {
                                        nodeJobs.put(discoEvt.eventNode().id(), new AtomicInteger(0));
                                        break;
                                    }
                                case EVT_NODE_LEFT:
                                case EVT_NODE_FAILED:
                                    {
                                        nodeJobs.remove(discoEvt.eventNode().id());
                                        break;
                                    }
                                case EVT_NODE_METRICS_UPDATED:
                                    {
                                        // Reset counter.
                                        nodeJobs.put(discoEvt.eventNode().id(), new AtomicInteger(0));
                                        break;
                                    }
                            }
                        } finally {
                            rwLock.writeLock().unlock();
                        }
                    }
            }
        }
    }, EVT_NODE_METRICS_UPDATED, EVT_NODE_FAILED, EVT_NODE_JOINED, EVT_NODE_LEFT, EVT_TASK_FINISHED, EVT_TASK_FAILED, EVT_JOB_MAPPED);
    // Put all known nodes.
    rwLock.writeLock().lock();
    try {
        for (ClusterNode node : getSpiContext().nodes()) nodeJobs.put(node.id(), new AtomicInteger(0));
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) JobEvent(org.apache.ignite.events.JobEvent) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskEvent(org.apache.ignite.events.TaskEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent)

Example 53 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridContinuousProcessor method registerHandler.

/**
 * @param nodeId Node ID.
 * @param routineId Consume ID.
 * @param hnd Handler.
 * @param bufSize Buffer size.
 * @param interval Time interval.
 * @param autoUnsubscribe Automatic unsubscribe flag.
 * @param loc Local registration flag.
 * @return Whether listener was actually registered.
 * @throws IgniteCheckedException In case of error.
 */
private boolean registerHandler(final UUID nodeId, final UUID routineId, final GridContinuousHandler hnd, int bufSize, final long interval, boolean autoUnsubscribe, boolean loc) throws IgniteCheckedException {
    assert nodeId != null;
    assert routineId != null;
    assert hnd != null;
    assert bufSize > 0;
    assert interval >= 0;
    final RemoteRoutineInfo info = new RemoteRoutineInfo(nodeId, hnd, bufSize, interval, autoUnsubscribe);
    boolean doRegister = loc;
    if (!doRegister) {
        stopLock.lock();
        try {
            doRegister = !stopped.remove(routineId) && rmtInfos.putIfAbsent(routineId, info) == null;
        } finally {
            stopLock.unlock();
        }
    }
    if (doRegister) {
        if (log.isDebugEnabled())
            log.debug("Register handler: [nodeId=" + nodeId + ", routineId=" + routineId + ", info=" + info + ']');
        if (interval > 0) {
            IgniteThread checker = new IgniteThread(new GridWorker(ctx.igniteInstanceName(), "continuous-buffer-checker", log) {

                @SuppressWarnings("ConstantConditions")
                @Override
                protected void body() {
                    long interval0 = interval;
                    while (!isCancelled()) {
                        try {
                            U.sleep(interval0);
                        } catch (IgniteInterruptedCheckedException ignored) {
                            break;
                        }
                        IgniteBiTuple<GridContinuousBatch, Long> t = info.checkInterval();
                        final GridContinuousBatch batch = t.get1();
                        if (batch != null && batch.size() > 0) {
                            try {
                                Collection<Object> toSnd = batch.collect();
                                boolean msg = toSnd.iterator().next() instanceof Message;
                                CI1<IgniteException> ackC = new CI1<IgniteException>() {

                                    @Override
                                    public void apply(IgniteException e) {
                                        if (e == null)
                                            info.hnd.onBatchAcknowledged(routineId, batch, ctx);
                                    }
                                };
                                sendNotification(nodeId, routineId, null, toSnd, hnd.orderedTopic(), msg, ackC);
                            } catch (ClusterTopologyCheckedException ignored) {
                                if (log.isDebugEnabled())
                                    log.debug("Failed to send notification to node (is node alive?): " + nodeId);
                            } catch (IgniteCheckedException e) {
                                U.error(log, "Failed to send notification to node: " + nodeId, e);
                            }
                        }
                        interval0 = t.get2();
                    }
                }
            });
            bufCheckThreads.put(routineId, checker);
            checker.start();
        }
        GridContinuousHandler.RegisterStatus status = hnd.register(nodeId, routineId, ctx);
        if (status == GridContinuousHandler.RegisterStatus.DELAYED) {
            info.markDelayedRegister();
            return false;
        } else
            return status == GridContinuousHandler.RegisterStatus.REGISTERED;
    }
    return false;
}
Also used : Message(org.apache.ignite.plugin.extensions.communication.Message) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CI1(org.apache.ignite.internal.util.typedef.CI1) GridWorker(org.apache.ignite.internal.util.worker.GridWorker) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Collection(java.util.Collection) IgniteThread(org.apache.ignite.thread.IgniteThread) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 54 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple 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, byte plc) {
            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)

Example 55 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method testCancelUpdate.

/**
 * @param key Key.
 * @param op Operation type.
 * @throws Exception If failed.
 */
private void testCancelUpdate(String key, Operation op) throws Exception {
    // Interceptor returns null to disabled update.
    CacheInterceptor retInterceptor = new NullPutInterceptor();
    interceptor.retInterceptor = retInterceptor;
    // Execute update when value is null, it should not change cache value.
    log.info("Update 1 " + op);
    update(0, op, key, 1, null);
    checkCacheValue(key, null);
    // Check values passed to interceptor.
    assertEquals(1, interceptor.beforePutMap.size());
    IgniteBiTuple t = interceptor.beforePutMap.get(key);
    assertEquals(null, t.get1());
    assertEquals(1, t.get2());
    // Disable interceptor and update cache.
    interceptor.reset();
    interceptor.disabled = true;
    clearCaches();
    jcache(0).put(key, 1);
    checkCacheValue(key, 1);
    // Execute update when value is not null, it should not change cache value.
    interceptor.disabled = false;
    interceptor.retInterceptor = retInterceptor;
    log.info("Update 2 " + op);
    update(0, op, key, 2, 1);
    checkCacheValue(key, 1);
    // Check values passed to interceptor.
    assertEquals(1, interceptor.beforePutMap.size());
    t = interceptor.beforePutMap.get(key);
    assertEquals(1, t.get1());
    assertEquals(2, t.get2());
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CacheInterceptor(org.apache.ignite.cache.CacheInterceptor)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)93 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)42 ArrayList (java.util.ArrayList)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 HashMap (java.util.HashMap)20 List (java.util.List)20 IgniteException (org.apache.ignite.IgniteException)20 IOException (java.io.IOException)18 Map (java.util.Map)16 UUID (java.util.UUID)16 ClusterNode (org.apache.ignite.cluster.ClusterNode)13 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 Collection (java.util.Collection)10 Collections (java.util.Collections)10 Iterator (java.util.Iterator)10 Random (java.util.Random)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 Collectors (java.util.stream.Collectors)9