Search in sources :

Example 16 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridContinuousProcessor method addNotification.

/**
 * @param nodeId ID of the node that started routine.
 * @param routineId Routine ID.
 * @param obj Notification object.
 * @param orderedTopic Topic for ordered notifications. If {@code null}, non-ordered message will be sent.
 * @param sync If {@code true} then waits for event acknowledgment.
 * @param msg If {@code true} then sent data is message.
 * @throws IgniteCheckedException In case of error.
 */
public void addNotification(UUID nodeId, final UUID routineId, @Nullable Object obj, @Nullable Object orderedTopic, boolean sync, boolean msg) throws IgniteCheckedException {
    assert nodeId != null;
    assert routineId != null;
    assert !msg || (obj instanceof Message || obj instanceof Collection) : obj;
    assert !nodeId.equals(ctx.localNodeId());
    if (processorStopped)
        return;
    final RemoteRoutineInfo info = rmtInfos.get(routineId);
    if (info != null) {
        assert info.interval == 0 || !sync;
        if (sync) {
            SyncMessageAckFuture fut = new SyncMessageAckFuture(nodeId);
            IgniteUuid futId = IgniteUuid.randomUuid();
            syncMsgFuts.put(futId, fut);
            try {
                sendNotification(nodeId, routineId, futId, obj instanceof Collection ? (Collection) obj : F.asList(obj), null, msg, null);
                info.hnd.onBatchAcknowledged(routineId, info.add(obj), ctx);
            } catch (IgniteCheckedException e) {
                syncMsgFuts.remove(futId);
                throw e;
            }
            while (true) {
                try {
                    fut.get(100, TimeUnit.MILLISECONDS);
                    break;
                } catch (IgniteFutureTimeoutCheckedException ignored) {
                    // in case left/fail event processing failed, hanged or delayed.
                    if (!ctx.discovery().alive(nodeId)) {
                        SyncMessageAckFuture fut0 = syncMsgFuts.remove(futId);
                        if (fut0 != null) {
                            ClusterTopologyCheckedException err = new ClusterTopologyCheckedException("Node left grid after receiving, but before processing the message [node=" + nodeId + "]");
                            fut0.onDone(err);
                        }
                        break;
                    }
                    LT.warn(log, "Failed to wait for ack message. [node=" + nodeId + ", routine=" + routineId + "]");
                }
            }
            assert fut.isDone() : "Future in not finished [fut= " + fut + "]";
        } else {
            final GridContinuousBatch batch = info.add(obj);
            if (batch != null) {
                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, batch.collect(), orderedTopic, msg, ackC);
            }
        }
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Message(org.apache.ignite.plugin.extensions.communication.Message) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) Collection(java.util.Collection) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) CI1(org.apache.ignite.internal.util.typedef.CI1) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 17 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 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) {

                @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();
                    }
                }
            });
            checker.setUncaughtExceptionHandler(new OomExceptionHandler(ctx));
            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) OomExceptionHandler(org.apache.ignite.thread.OomExceptionHandler) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 18 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class GridTcpRestNioListener method onMessage.

/**
 * {@inheritDoc}
 */
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
    if (msg instanceof GridMemcachedMessage)
        memcachedLsnr.onMessage(ses, (GridMemcachedMessage) msg);
    else if (msg instanceof GridRedisMessage)
        redisLsnr.onMessage(ses, (GridRedisMessage) msg);
    else if (msg instanceof GridClientPingPacket)
        ses.send(msg);
    else if (msg instanceof GridClientHandshakeRequest) {
        GridClientHandshakeRequest hs = (GridClientHandshakeRequest) msg;
        short ver = hs.version();
        if (!SUPP_VERS.contains(ver)) {
            U.error(log, "Client protocol version is not supported [ses=" + ses + ", ver=" + ver + ", supported=" + SUPP_VERS + ']');
            onSessionClosed(ses);
        } else {
            byte marshId = hs.marshallerId();
            if (marshMapLatch.getCount() > 0) {
                try {
                    U.await(marshMapLatch);
                } catch (IgniteInterruptedCheckedException e) {
                    U.error(log, "Marshaller is not initialized.", e);
                    onSessionClosed(ses);
                    return;
                }
            }
            GridClientMarshaller marsh = marshMap.get(marshId);
            if (marsh == null) {
                U.error(log, "Client marshaller ID is invalid. Note that .NET and C++ clients " + "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']');
                onSessionClosed(ses);
            } else {
                ses.addMeta(MARSHALLER.ordinal(), marsh);
                ses.send(GridClientHandshakeResponse.OK);
            }
        }
    } else {
        final GridRestRequest req = createRestRequest(ses, msg);
        if (req != null) {
            IgniteInternalFuture<GridRestResponse> taskFut = hnd.handleAsync(req);
            if (isInterruptible(msg))
                addFutureToSession(ses, taskFut);
            taskFut.listen(new CI1<IgniteInternalFuture<GridRestResponse>>() {

                @Override
                public void apply(IgniteInternalFuture<GridRestResponse> fut) {
                    removeFutureFromSession(ses, taskFut);
                    GridClientResponse res = new GridClientResponse();
                    res.requestId(msg.requestId());
                    res.clientId(msg.clientId());
                    try {
                        GridRestResponse restRes = fut.get();
                        res.sessionToken(restRes.sessionTokenBytes());
                        res.successStatus(restRes.getSuccessStatus());
                        res.errorMessage(restRes.getError());
                        Object o = restRes.getResponse();
                        // In case of metrics a little adjustment is needed.
                        if (o instanceof GridCacheRestMetrics)
                            o = ((GridCacheRestMetrics) o).map();
                        res.result(o);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to process client request: " + msg, e);
                        res.successStatus(GridClientResponse.STATUS_FAILED);
                        res.errorMessage("Failed to process client request: " + e.getMessage());
                    }
                    GridNioFuture<?> sf = ses.send(res);
                    // Check if send failed.
                    sf.listen(new CI1<IgniteInternalFuture<?>>() {

                        @Override
                        public void apply(IgniteInternalFuture<?> fut) {
                            try {
                                fut.get();
                            } catch (IgniteCheckedException e) {
                                U.error(log, "Failed to process client request [ses=" + ses + ", msg=" + msg + ']', e);
                            }
                        }
                    });
                }
            });
        } else
            U.error(log, "Failed to process client request (unknown packet type) [ses=" + ses + ", msg=" + msg + ']');
    }
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) CI1(org.apache.ignite.internal.util.typedef.CI1) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) GridRedisMessage(org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridCacheRestMetrics(org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheRestMetrics) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest)

Example 19 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class IgniteCacheDynamicStopSelfTest method checkStopStartCacheWithDataLoader.

/**
 * @param allowOverwrite Allow overwrite flag for streamer.
 * @throws Exception If failed.
 */
public void checkStopStartCacheWithDataLoader(final boolean allowOverwrite) throws Exception {
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(CacheMode.PARTITIONED);
    ignite(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                try (IgniteDataStreamer<Integer, Integer> str = ignite(0).dataStreamer(DEFAULT_CACHE_NAME)) {
                    str.allowOverwrite(allowOverwrite);
                    int i = 0;
                    while (!stop.get()) {
                        try {
                            str.addData(i % 10_000, i).listen(new CI1<IgniteFuture<?>>() {

                                @Override
                                public void apply(IgniteFuture<?> f) {
                                    try {
                                        f.get();
                                    } catch (CacheException ignore) {
                                    // This may be debugged.
                                    }
                                }
                            });
                        } catch (IllegalStateException ignored) {
                            break;
                        }
                        if (i > 0 && i % 10000 == 0)
                            info("Added: " + i);
                        i++;
                    }
                } catch (IllegalStateException | CacheException ignored) {
                // This may be debugged.
                }
            }
            return null;
        }
    });
    try {
        Thread.sleep(500);
        ignite(0).destroyCache(DEFAULT_CACHE_NAME);
        Thread.sleep(500);
        ignite(0).createCache(ccfg);
        Thread.sleep(1000);
    } finally {
        stop.set(true);
    }
    fut.get();
    int cnt = 0;
    for (Cache.Entry<Object, Object> ignored : ignite(0).cache(DEFAULT_CACHE_NAME)) cnt++;
    info(">>> cnt=" + cnt);
    ignite(0).destroyCache(DEFAULT_CACHE_NAME);
}
Also used : CacheException(javax.cache.CacheException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CI1(org.apache.ignite.internal.util.typedef.CI1) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Cache(javax.cache.Cache)

Example 20 with CI1

use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.

the class IgniteFutureImplTest method testChainAsync.

/**
 * @throws Exception If failed.
 */
@Test
public void testChainAsync() throws Exception {
    GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
    IgniteFuture<String> fut = createFuture(fut0);
    C1<IgniteFuture<String>, Integer> chainClos = new C1<IgniteFuture<String>, Integer>() {

        @Override
        public Integer apply(IgniteFuture<String> fut) {
            assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName());
            return Integer.valueOf(fut.get());
        }
    };
    IgniteFuture<Integer> chained1 = fut.chainAsync(chainClos, customExec);
    assertFalse(chained1.isDone());
    final CountDownLatch latch = new CountDownLatch(1);
    /**
     */
    class TestClosure implements CI1<IgniteFuture<Integer>> {

        private final CountDownLatch latch;

        /**
         */
        private TestClosure(CountDownLatch latch) {
            this.latch = latch;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void apply(IgniteFuture<Integer> fut) {
            assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName());
            assertEquals(10, (int) fut.get());
            latch.countDown();
        }
    }
    chained1.listen(new TestClosure(latch));
    fut0.onDone("10");
    // Chained future will be completed asynchronously.
    chained1.get(100, TimeUnit.MILLISECONDS);
    assertTrue(chained1.isDone());
    assertEquals(10, (int) chained1.get());
    assert latch.await(100, TimeUnit.MILLISECONDS);
    assertTrue(fut.isDone());
    assertEquals("10", fut.get());
    // Test finished future
    GridFinishedFuture<String> ffut0 = new GridFinishedFuture<>("10");
    CountDownLatch latch1 = new CountDownLatch(1);
    IgniteFuture<Integer> chained2 = createFuture(ffut0).chainAsync(chainClos, customExec);
    chained2.listen(new TestClosure(latch1));
    chained2.get(100, TimeUnit.MILLISECONDS);
    assertTrue(chained2.isDone());
    assertEquals(10, (int) chained2.get());
    assert latch1.await(100, TimeUnit.MILLISECONDS);
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) CI1(org.apache.ignite.internal.util.typedef.CI1) CountDownLatch(java.util.concurrent.CountDownLatch) C1(org.apache.ignite.internal.util.typedef.C1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

CI1 (org.apache.ignite.internal.util.typedef.CI1)32 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)11 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 Test (org.junit.Test)11 IgniteFuture (org.apache.ignite.lang.IgniteFuture)9 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IgniteException (org.apache.ignite.IgniteException)7 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 Collection (java.util.Collection)6 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)6 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)6 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Nullable (org.jetbrains.annotations.Nullable)6 List (java.util.List)5 UUID (java.util.UUID)5 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)5 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)5