Search in sources :

Example 21 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project camel by apache.

the class IgniteComputeProducer method doRun.

private void doRun(final Exchange exchange, final AsyncCallback callback, IgniteCompute compute) throws Exception {
    Object job = exchange.getIn().getBody();
    if (Collection.class.isAssignableFrom(job.getClass())) {
        Collection<?> col = (Collection<?>) job;
        TypeConverter tc = exchange.getContext().getTypeConverter();
        Collection<IgniteRunnable> runnables = new ArrayList<>(col.size());
        for (Object o : col) {
            runnables.add(tc.mandatoryConvertTo(IgniteRunnable.class, o));
        }
        compute.run(runnables);
    } else if (IgniteRunnable.class.isAssignableFrom(job.getClass())) {
        compute.run((IgniteRunnable) job);
    } else {
        throw new RuntimeCamelException(String.format("Ignite Compute endpoint with RUN executionType is only " + "supported for IgniteRunnable payloads, or collections of them. The payload type was: %s.", job.getClass().getName()));
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) ArrayList(java.util.ArrayList) Collection(java.util.Collection) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 22 with IgniteRunnable

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

the class GridIoManager method start.

/** {@inheritDoc} */
@SuppressWarnings("deprecation")
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    assertParameter(discoDelay > 0, "discoveryStartupDelay > 0");
    startSpi();
    getSpi().setListener(commLsnr = new CommunicationListener<Serializable>() {

        @Override
        public void onMessage(UUID nodeId, Serializable msg, IgniteRunnable msgC) {
            try {
                onMessage0(nodeId, (GridIoMessage) msg, msgC);
            } catch (ClassCastException ignored) {
                U.error(log, "Communication manager received message of unknown type (will ignore): " + msg.getClass().getName() + ". Most likely GridCommunicationSpi is being used directly, " + "which is illegal - make sure to send messages only via GridProjection API.");
            }
        }

        @Override
        public void onDisconnected(UUID nodeId) {
            for (GridDisconnectListener lsnr : disconnectLsnrs) lsnr.onNodeDisconnected(nodeId);
        }
    });
    ctx.addNodeAttribute(DIRECT_PROTO_VER_ATTR, DIRECT_PROTO_VER);
    MessageFormatter[] formatterExt = ctx.plugins().extensions(MessageFormatter.class);
    if (formatterExt != null && formatterExt.length > 0) {
        if (formatterExt.length > 1)
            throw new IgniteCheckedException("More than one MessageFormatter extension is defined. Check your " + "plugins configuration and make sure that only one of them provides custom message format.");
        formatter = formatterExt[0];
    } else {
        formatter = new MessageFormatter() {

            @Override
            public MessageWriter writer(UUID rmtNodeId) throws IgniteCheckedException {
                assert rmtNodeId != null;
                return new DirectMessageWriter(U.directProtocolVersion(ctx, rmtNodeId));
            }

            @Override
            public MessageReader reader(UUID rmtNodeId, MessageFactory msgFactory) throws IgniteCheckedException {
                assert rmtNodeId != null;
                return new DirectMessageReader(msgFactory, U.directProtocolVersion(ctx, rmtNodeId));
            }
        };
    }
    MessageFactory[] msgs = ctx.plugins().extensions(MessageFactory.class);
    if (msgs == null)
        msgs = EMPTY;
    List<MessageFactory> compMsgs = new ArrayList<>();
    for (IgniteComponentType compType : IgniteComponentType.values()) {
        MessageFactory f = compType.messageFactory();
        if (f != null)
            compMsgs.add(f);
    }
    if (!compMsgs.isEmpty())
        msgs = F.concat(msgs, compMsgs.toArray(new MessageFactory[compMsgs.size()]));
    msgFactory = new GridIoMessageFactory(msgs);
    if (log.isDebugEnabled())
        log.debug(startInfo());
    addMessageListener(GridTopic.TOPIC_IO_TEST, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            ClusterNode node = ctx.discovery().node(nodeId);
            if (node == null)
                return;
            IgniteIoTestMessage msg0 = (IgniteIoTestMessage) msg;
            msg0.senderNodeId(nodeId);
            if (msg0.request()) {
                IgniteIoTestMessage res = new IgniteIoTestMessage(msg0.id(), false, null);
                res.flags(msg0.flags());
                res.onRequestProcessed();
                res.copyDataFromRequest(msg0);
                try {
                    sendToGridTopic(node, GridTopic.TOPIC_IO_TEST, res, GridIoPolicy.SYSTEM_POOL);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to send IO test response [msg=" + msg0 + "]", e);
                }
            } else {
                IoTestFuture fut = ioTestMap().get(msg0.id());
                msg0.onResponseProcessed();
                if (fut == null)
                    U.warn(log, "Failed to find IO test future [msg=" + msg0 + ']');
                else
                    fut.onResponse(msg0);
            }
        }
    });
}
Also used : Serializable(java.io.Serializable) DirectMessageReader(org.apache.ignite.internal.direct.DirectMessageReader) ArrayList(java.util.ArrayList) DirectMessageReader(org.apache.ignite.internal.direct.DirectMessageReader) MessageReader(org.apache.ignite.plugin.extensions.communication.MessageReader) MessageFormatter(org.apache.ignite.plugin.extensions.communication.MessageFormatter) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteComponentType(org.apache.ignite.internal.IgniteComponentType) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) CommunicationListener(org.apache.ignite.spi.communication.CommunicationListener) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) MessageWriter(org.apache.ignite.plugin.extensions.communication.MessageWriter) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject)

Example 23 with IgniteRunnable

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

the class GridDhtAtomicCache method updateAllAsyncInternal0.

/**
     * Executes local update after preloader fetched values.
     *
     * @param nodeId Node ID.
     * @param req Update request.
     * @param completionCb Completion callback.
     */
private void updateAllAsyncInternal0(UUID nodeId, GridNearAtomicAbstractUpdateRequest req, UpdateReplyClosure completionCb) {
    ClusterNode node = ctx.discovery().node(nodeId);
    if (node == null) {
        U.warn(msgLog, "Skip near update request, node originated update request left [" + "futId=" + req.futureId() + ", node=" + nodeId + ']');
        return;
    }
    GridNearAtomicUpdateResponse res = new GridNearAtomicUpdateResponse(ctx.cacheId(), nodeId, req.futureId(), req.partition(), false, ctx.deploymentEnabled());
    assert !req.returnValue() || (req.operation() == TRANSFORM || req.size() == 1);
    GridDhtAtomicAbstractUpdateFuture dhtFut = null;
    boolean remap = false;
    String taskName = ctx.kernalContext().task().resolveTaskName(req.taskNameHash());
    IgniteCacheExpiryPolicy expiry = null;
    try {
        // If batch store update is enabled, we need to lock all entries.
        // First, need to acquire locks on cache entries, then check filter.
        List<GridDhtCacheEntry> locked = null;
        Collection<IgniteBiTuple<GridDhtCacheEntry, GridCacheVersion>> deleted = null;
        try {
            GridDhtPartitionTopology top = topology();
            top.readLock();
            try {
                if (top.stopping()) {
                    res.addFailedKeys(req.keys(), new IgniteCheckedException("Failed to perform cache operation " + "(cache is stopped): " + name()));
                    completionCb.apply(req, res);
                    return;
                }
                // external transaction or explicit lock.
                if (req.topologyLocked() || !needRemap(req.topologyVersion(), top.topologyVersion())) {
                    ctx.shared().database().ensureFreeSpace(ctx.memoryPolicy());
                    locked = lockEntries(req, req.topologyVersion());
                    boolean hasNear = ctx.discovery().cacheNearNode(node, name());
                    // Assign next version for update inside entries lock.
                    GridCacheVersion ver = ctx.versions().next(top.topologyVersion());
                    if (hasNear)
                        res.nearVersion(ver);
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("Assigned update version [futId=" + req.futureId() + ", writeVer=" + ver + ']');
                    }
                    assert ver != null : "Got null version for update request: " + req;
                    boolean sndPrevVal = !top.rebalanceFinished(req.topologyVersion());
                    dhtFut = createDhtFuture(ver, req);
                    expiry = expiryPolicy(req.expiry());
                    GridCacheReturn retVal = null;
                    if (// Several keys ...
                    req.size() > 1 && writeThrough() && // and store is enabled ...
                    !req.skipStore() && // and this is not local store ...
                    !ctx.store().isLocal() && // (conflict resolver should be used for local store)
                    !// and no DR.
                    ctx.dr().receiveEnabled()) {
                        // This method can only be used when there are no replicated entries in the batch.
                        UpdateBatchResult updRes = updateWithBatch(node, hasNear, req, res, locked, ver, dhtFut, ctx.isDrEnabled(), taskName, expiry, sndPrevVal);
                        deleted = updRes.deleted();
                        dhtFut = updRes.dhtFuture();
                        if (req.operation() == TRANSFORM)
                            retVal = updRes.invokeResults();
                    } else {
                        UpdateSingleResult updRes = updateSingle(node, hasNear, req, res, locked, ver, dhtFut, ctx.isDrEnabled(), taskName, expiry, sndPrevVal);
                        retVal = updRes.returnValue();
                        deleted = updRes.deleted();
                        dhtFut = updRes.dhtFuture();
                    }
                    if (retVal == null)
                        retVal = new GridCacheReturn(ctx, node.isLocal(), true, null, true);
                    res.returnValue(retVal);
                    if (dhtFut != null) {
                        if (req.writeSynchronizationMode() == PRIMARY_SYNC && // To avoid deadlock disable back-pressure for sender data node.
                        !ctx.discovery().cacheAffinityNode(ctx.discovery().node(nodeId), ctx.name()) && !dhtFut.isDone()) {
                            final IgniteRunnable tracker = GridNioBackPressureControl.threadTracker();
                            if (tracker != null && tracker instanceof GridNioMessageTracker) {
                                ((GridNioMessageTracker) tracker).onMessageReceived();
                                dhtFut.listen(new IgniteInClosure<IgniteInternalFuture<Void>>() {

                                    @Override
                                    public void apply(IgniteInternalFuture<Void> fut) {
                                        ((GridNioMessageTracker) tracker).onMessageProcessed();
                                    }
                                });
                            }
                        }
                        ctx.mvcc().addAtomicFuture(dhtFut.id(), dhtFut);
                    }
                } else {
                    // Should remap all keys.
                    remap = true;
                    res.remapTopologyVersion(top.topologyVersion());
                }
            } finally {
                top.readUnlock();
            }
        } catch (GridCacheEntryRemovedException e) {
            assert false : "Entry should not become obsolete while holding lock.";
            e.printStackTrace();
        } finally {
            if (locked != null)
                unlockEntries(locked, req.topologyVersion());
            // Enqueue if necessary after locks release.
            if (deleted != null) {
                assert !deleted.isEmpty();
                assert ctx.deferredDelete() : this;
                for (IgniteBiTuple<GridDhtCacheEntry, GridCacheVersion> e : deleted) ctx.onDeferredDelete(e.get1(), e.get2());
            }
            // TODO fire events only after successful fsync
            if (ctx.shared().wal() != null)
                ctx.shared().wal().fsync(null);
        }
    } catch (GridDhtInvalidPartitionException ignore) {
        if (log.isDebugEnabled())
            log.debug("Caught invalid partition exception for cache entry (will remap update request): " + req);
        remap = true;
        res.remapTopologyVersion(ctx.topology().topologyVersion());
    } catch (Throwable e) {
        // At least RuntimeException can be thrown by the code above when GridCacheContext is cleaned and there is
        // an attempt to use cleaned resources.
        U.error(log, "Unexpected exception during cache update", e);
        res.addFailedKeys(req.keys(), e);
        completionCb.apply(req, res);
        if (e instanceof Error)
            throw (Error) e;
        return;
    }
    if (remap) {
        assert dhtFut == null;
        completionCb.apply(req, res);
    } else if (dhtFut != null)
        dhtFut.map(node, res.returnValue(), res, completionCb);
    if (req.writeSynchronizationMode() != FULL_ASYNC)
        req.cleanup(!node.isLocal());
    sendTtlUpdateRequest(expiry);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridNioMessageTracker(org.apache.ignite.internal.util.nio.GridNioMessageTracker) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)

Example 24 with IgniteRunnable

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

the class GridTaskExecutionContextSelfTest method testWithNoFailoverClosure.

/**
 * @throws Exception If failed.
 */
public void testWithNoFailoverClosure() throws Exception {
    final IgniteRunnable r = new GridAbsClosureX() {

        @Override
        public void applyx() {
            CNT.incrementAndGet();
            throw new ComputeExecutionRejectedException("Expected error.");
        }
    };
    final Ignite g = grid(0);
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            g.compute().withNoFailover().run(r);
            return null;
        }
    }, ComputeExecutionRejectedException.class, "Expected error.");
    assertEquals(1, CNT.get());
}
Also used : ComputeExecutionRejectedException(org.apache.ignite.compute.ComputeExecutionRejectedException) Ignite(org.apache.ignite.Ignite) GridAbsClosureX(org.apache.ignite.internal.util.lang.GridAbsClosureX) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeExecutionRejectedException(org.apache.ignite.compute.ComputeExecutionRejectedException)

Example 25 with IgniteRunnable

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

the class CacheConcurrentReadThroughTest method testConcurrentReadThrough.

/**
 * @throws Exception If failed.
 */
public void testConcurrentReadThrough() throws Exception {
    startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    assertTrue(client.configuration().isClientMode());
    for (int iter = 0; iter < 10; iter++) {
        CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        final String cacheName = "test-" + iter;
        ccfg.setName(cacheName);
        ccfg.setReadThrough(true);
        ccfg.setCacheStoreFactory(new TestStoreFactory());
        ccfg.setStatisticsEnabled(true);
        client.createCache(ccfg);
        final Integer key = 1;
        TestCacheStore.loadCnt.set(0);
        Collection<IgniteFuture<?>> futs = new ArrayList<>();
        for (int i = 0; i < SYS_THREADS * 3; i++) {
            futs.add(client.compute().runAsync(new IgniteRunnable() {

                @IgniteInstanceResource
                private transient Ignite ignite;

                @Override
                public void run() {
                    assertFalse(ignite.configuration().isClientMode());
                    Object v = ignite.<Integer, Integer>cache(cacheName).get(key);
                    if (v == null)
                        throw new IgniteException("Failed to get value");
                }
            }));
        }
        for (IgniteFuture<?> fut : futs) fut.get();
        int loadCnt = TestCacheStore.loadCnt.get();
        long misses = ignite(1).cache(cacheName).metrics().getCacheMisses();
        log.info("Iteration [iter=" + iter + ", loadCnt=" + loadCnt + ", misses=" + misses + ']');
        assertTrue("Unexpected loadCnt: " + loadCnt, loadCnt > 0 && loadCnt <= SYS_THREADS);
        assertTrue("Unexpected misses: " + misses, misses > 0 && misses <= SYS_THREADS);
        client.destroyCache(cacheName);
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)43 Ignite (org.apache.ignite.Ignite)16 IgniteException (org.apache.ignite.IgniteException)12 IgniteEx (org.apache.ignite.internal.IgniteEx)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteFuture (org.apache.ignite.lang.IgniteFuture)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Event (org.apache.ignite.events.Event)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Serializable (java.io.Serializable)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2