Search in sources :

Example 1 with IgniteCallable

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

the class IgniteComputeProducer method doCall.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void doCall(final Exchange exchange, final AsyncCallback callback, IgniteCompute compute) throws Exception {
    Object job = exchange.getIn().getBody();
    IgniteReducer<Object, Object> reducer = exchange.getIn().getHeader(IgniteConstants.IGNITE_COMPUTE_REDUCER, IgniteReducer.class);
    if (Collection.class.isAssignableFrom(job.getClass())) {
        Collection<?> col = (Collection<?>) job;
        TypeConverter tc = exchange.getContext().getTypeConverter();
        Collection<IgniteCallable<?>> callables = new ArrayList<>(col.size());
        for (Object o : col) {
            callables.add(tc.mandatoryConvertTo(IgniteCallable.class, o));
        }
        if (reducer != null) {
            compute.call((Collection) callables, reducer);
        } else {
            compute.call((Collection) callables);
        }
    } else if (IgniteCallable.class.isAssignableFrom(job.getClass())) {
        compute.call((IgniteCallable<Object>) job);
    } else {
        throw new RuntimeCamelException(String.format("Ignite Compute endpoint with CALL executionType is only " + "supported for IgniteCallable payloads, or collections of them. The payload type was: %s.", job.getClass().getName()));
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ArrayList(java.util.ArrayList) Collection(java.util.Collection) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 2 with IgniteCallable

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

the class GridCacheQueueApiSelfAbstractTest method testAffinityCall.

/**
     * @throws Exception If failed.
     */
public void testAffinityCall() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    try (final IgniteQueue<Integer> queue1 = grid(0).queue("Queue1", 0, colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                queue1.affinityCall(new IgniteCallable<Object>() {

                    @Override
                    public Object call() {
                        return null;
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityCall() for non-collocated queue: " + queue1.name() + ". This operation is supported only for collocated queues.");
    }
    colCfg.setCollocated(true);
    try (final IgniteQueue<Integer> queue2 = grid(0).queue("Queue2", 0, colCfg)) {
        queue2.add(100);
        Integer res = queue2.affinityCall(new IgniteCallable<Integer>() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public Integer call() {
                assertTrue(ignite.cachex("datastructures_0").affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Queue2"));
                return queue2.take();
            }
        });
        assertEquals(100, res.intValue());
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteException(org.apache.ignite.IgniteException)

Example 3 with IgniteCallable

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

the class GridMultipleJobsSelfTest method runTest.

/**
     * @param jobsNum Number of jobs.
     * @param threadNum Number of threads.
     * @param jobCls Job class.
     * @throws Exception If failed.
     */
private void runTest(final int jobsNum, int threadNum, final Class<? extends IgniteCallable<Boolean>> jobCls) throws Exception {
    final Ignite ignite1 = grid(1);
    final CountDownLatch latch = new CountDownLatch(jobsNum);
    final AtomicInteger jobsCnt = new AtomicInteger();
    final AtomicInteger resCnt = new AtomicInteger();
    GridTestUtils.runMultiThreaded(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (true) {
                int cnt = jobsCnt.incrementAndGet();
                if (cnt > jobsNum)
                    break;
                IgniteCallable<Boolean> job;
                try {
                    job = jobCls.newInstance();
                } catch (Exception e) {
                    throw new IgniteCheckedException("Could not instantiate a job.", e);
                }
                IgniteFuture<Boolean> fut = ignite1.compute().callAsync(job);
                if (cnt % LOG_MOD == 0)
                    X.println("Submitted jobs: " + cnt);
                fut.listen(new CIX1<IgniteFuture<Boolean>>() {

                    @Override
                    public void applyx(IgniteFuture<Boolean> f) {
                        try {
                            assert f.get();
                        } finally {
                            latch.countDown();
                            long cnt = resCnt.incrementAndGet();
                            if (cnt % LOG_MOD == 0)
                                X.println("Results count: " + cnt);
                        }
                    }
                });
            }
        }
    }, threadNum, "TEST-THREAD");
    latch.await();
}
Also used : CIX1(org.apache.ignite.internal.util.typedef.CIX1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 4 with IgniteCallable

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

the class GridClosureProcessorSelfTest method testReducerError.

/**
     * @throws Exception If failed.
     */
public void testReducerError() throws Exception {
    final Ignite g = grid(0);
    final Collection<IgniteCallable<Integer>> jobs = new ArrayList<>();
    for (int i = 0; i < g.cluster().nodes().size(); i++) {
        jobs.add(new IgniteCallable<Integer>() {

            @Override
            public Integer call() throws Exception {
                throw new RuntimeException("Test exception.");
            }
        });
    }
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            g.compute().call(jobs, new IgniteReducer<Integer, Object>() {

                @Override
                public boolean collect(@Nullable Integer e) {
                    fail("Expects failed jobs never call 'collect' method.");
                    return true;
                }

                @Override
                public Object reduce() {
                    return null;
                }
            });
            return null;
        }
    }, IgniteException.class, null);
}
Also used : IgniteReducer(org.apache.ignite.lang.IgniteReducer) ArrayList(java.util.ArrayList) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCallable(org.apache.ignite.lang.IgniteCallable) Ignite(org.apache.ignite.Ignite) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with IgniteCallable

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

the class IgniteTxPessimisticOriginatingNodeFailureAbstractSelfTest method testTxOriginatingNodeFails.

/**
     * @param keys Keys to update.
     * @param fullFailure Flag indicating whether to simulate rollback state.
     * @throws Exception If failed.
     */
protected void testTxOriginatingNodeFails(Collection<Integer> keys, final boolean fullFailure) throws Exception {
    assertFalse(keys.isEmpty());
    final Collection<IgniteKernal> grids = new ArrayList<>();
    ClusterNode txNode = grid(originatingNode()).localNode();
    for (int i = 1; i < gridCount(); i++) grids.add((IgniteKernal) grid(i));
    failingNodeId = grid(0).localNode().id();
    final Map<Integer, String> map = new HashMap<>();
    final String initVal = "initialValue";
    for (Integer key : keys) {
        grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
        map.put(key, String.valueOf(key));
    }
    Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
    info("Node being checked: " + grid(1).localNode().id());
    for (Integer key : keys) {
        Collection<ClusterNode> nodes = new ArrayList<>();
        nodes.addAll(grid(1).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key));
        nodes.remove(txNode);
        nodeMap.put(key, nodes);
    }
    info("Starting tx [values=" + map + ", topVer=" + ((IgniteKernal) grid(1)).context().discovery().topologyVersion() + ']');
    if (fullFailure)
        ignoreMessages(ignoreMessageClasses(), F.asList(grid(1).localNode().id()));
    final IgniteEx originatingNodeGrid = grid(originatingNode());
    GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            IgniteCache<Integer, String> cache = originatingNodeGrid.cache(DEFAULT_CACHE_NAME);
            assertNotNull(cache);
            Transaction tx = originatingNodeGrid.transactions().txStart();
            assertEquals(PESSIMISTIC, tx.concurrency());
            try {
                cache.putAll(map);
                info("Before commitAsync");
                IgniteFuture<?> fut = tx.commitAsync();
                info("Got future for commitAsync().");
                fut.get(3, TimeUnit.SECONDS);
            } catch (IgniteFutureTimeoutException ignored) {
                info("Failed to wait for commit future completion [fullFailure=" + fullFailure + ']');
            }
            return null;
        }
    }).get();
    info(">>> Stopping originating node " + txNode);
    G.stop(grid(originatingNode()).name(), true);
    ignoreMessages(Collections.<Class<?>>emptyList(), Collections.<UUID>emptyList());
    info(">>> Stopped originating node: " + txNode.id());
    boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (IgniteKernal g : grids) {
                GridCacheAdapter<?, ?> cache = g.internalCache(DEFAULT_CACHE_NAME);
                IgniteTxManager txMgr = cache.isNear() ? ((GridNearCacheAdapter) cache).dht().context().tm() : cache.context().tm();
                int txNum = txMgr.idMapSize();
                if (txNum != 0)
                    return false;
            }
            return true;
        }
    }, 10000);
    assertTrue(txFinished);
    info("Transactions finished.");
    for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
        final Integer key = e.getKey();
        final String val = map.get(key);
        assertFalse(e.getValue().isEmpty());
        for (ClusterNode node : e.getValue()) {
            final UUID checkNodeId = node.id();
            compute(G.ignite(checkNodeId).cluster().forNode(node)).call(new IgniteCallable<Void>() {

                /** */
                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    assertNotNull(cache);
                    assertEquals("Failed to check entry value on node: " + checkNodeId, fullFailure ? initVal : val, cache.localPeek(key));
                    return null;
                }
            });
        }
    }
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        for (Ignite g : G.allGrids()) assertEquals(fullFailure ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(e.getKey()));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IgniteCallable (org.apache.ignite.lang.IgniteCallable)21 Ignite (org.apache.ignite.Ignite)13 IgniteException (org.apache.ignite.IgniteException)12 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)7 Callable (java.util.concurrent.Callable)6 IgniteEx (org.apache.ignite.internal.IgniteEx)6 Collection (java.util.Collection)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 Nullable (org.jetbrains.annotations.Nullable)4 UUID (java.util.UUID)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 IgniteCompute (org.apache.ignite.IgniteCompute)3 IgniteLogger (org.apache.ignite.IgniteLogger)3 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 LoggerResource (org.apache.ignite.resources.LoggerResource)3 HashMap (java.util.HashMap)2