Search in sources :

Example 56 with IgniteFuture

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

the class GridCacheReferenceCleanupSelfTest method severalAsyncOpsCallable.

/**
 * Crates callable for several async ops test.
 *
 * @return Callable.
 * @throws Exception If failed.
 */
private Callable<Collection<WeakReference<Object>>> severalAsyncOpsCallable() throws Exception {
    return new Callable<Collection<WeakReference<Object>>>() {

        @Override
        public Collection<WeakReference<Object>> call() throws Exception {
            Collection<WeakReference<Object>> refs = new ArrayList<>();
            Ignite g = startGrid();
            try {
                IgniteCache<Integer, TestValue> cache = g.cache(DEFAULT_CACHE_NAME);
                refs.add(new WeakReference<Object>(cacheContext(cache)));
                Collection<IgniteFuture<?>> futs = new ArrayList<>(1000);
                for (int i = 0; i < 1000; i++) {
                    TestValue val = new TestValue(i);
                    refs.add(new WeakReference<Object>(val));
                    futs.add(cache.putIfAbsentAsync(0, val));
                }
                for (IgniteFuture<?> fut : futs) fut.get();
            } finally {
                G.stop(g.name(), cancel);
            }
            return refs;
        }
    };
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Callable(java.util.concurrent.Callable) WeakReference(java.lang.ref.WeakReference) Ignite(org.apache.ignite.Ignite)

Example 57 with IgniteFuture

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

the class PlatformTransactions method processInStreamOutLong.

/**
 * {@inheritDoc}
 */
@Override
public long processInStreamOutLong(int type, BinaryRawReaderEx reader) throws IgniteCheckedException {
    long txId = reader.readLong();
    IgniteFuture fut0;
    switch(type) {
        case OP_COMMIT_ASYNC:
            fut0 = tx(txId).commitAsync();
            break;
        case OP_ROLLBACK_ASYNC:
            fut0 = tx(txId).rollbackAsync();
            break;
        default:
            return super.processInStreamOutLong(type, reader);
    }
    // Future result is the tx itself, we do not want to return it to the platform.
    IgniteFuture fut = fut0.chain(new C1<IgniteFuture, Object>() {

        private static final long serialVersionUID = 0L;

        @Override
        public Object apply(IgniteFuture fut) {
            return null;
        }
    });
    readAndListenFuture(reader, fut);
    return TRUE;
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Example 58 with IgniteFuture

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

the class PlatformDotNetEntityFrameworkCacheExtension method startBackgroundCleanup.

/**
 * Starts the background cleanup of old cache entries.
 *
 * @param grid Grid.
 * @param metaCache Meta cache.
 * @param dataCacheName Data cache name.
 * @param currentVersions Current versions.
 */
private void startBackgroundCleanup(Ignite grid, final Cache<CleanupNodeId, UUID> metaCache, final String dataCacheName, final Map<String, EntryProcessorResult<Long>> currentVersions) {
    if (cleanupFlags.containsKey(dataCacheName))
        // Current node already performs cleanup.
        return;
    if (!trySetGlobalCleanupFlag(grid, metaCache))
        return;
    cleanupFlags.put(dataCacheName, true);
    final ClusterGroup dataNodes = grid.cluster().forDataNodes(dataCacheName);
    IgniteFuture f = grid.compute(dataNodes).broadcastAsync(new RemoveOldEntriesRunnable(dataCacheName, currentVersions));
    f.listen(new CleanupCompletionListener(metaCache, dataCacheName));
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Example 59 with IgniteFuture

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

the class PlatformTargetProxyImpl method inStreamOutListenableAsync.

/**
 * Performs asyncronous operation.
 *
 * @param type Type.
 * @param memPtr Stream pointer.
 * @return Listenable.
 * @throws Exception On error.
 */
private PlatformListenable inStreamOutListenableAsync(int type, long memPtr) throws Exception {
    try (PlatformMemory mem = platformCtx.memory().get(memPtr)) {
        BinaryRawReaderEx reader = platformCtx.reader(mem);
        long futId = reader.readLong();
        int futTyp = reader.readInt();
        final PlatformAsyncResult res = target.processInStreamAsync(type, reader);
        if (res == null)
            throw new IgniteException("PlatformTarget.processInStreamAsync should not return null.");
        IgniteFuture fut = res.future();
        if (fut == null)
            throw new IgniteException("PlatformAsyncResult.future() should not return null.");
        return PlatformFutureUtils.listen(platformCtx, fut, futId, futTyp, new PlatformFutureUtils.Writer() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void write(BinaryRawWriterEx writer, Object obj, Throwable err) {
                res.write(writer, obj);
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public boolean canWrite(Object obj, Throwable err) {
                return err == null;
            }
        }, target);
    } catch (Exception e) {
        throw target.convertException(e);
    }
}
Also used : PlatformFutureUtils(org.apache.ignite.internal.processors.platform.utils.PlatformFutureUtils) IgniteFuture(org.apache.ignite.lang.IgniteFuture) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) IgniteException(org.apache.ignite.IgniteException) IgniteException(org.apache.ignite.IgniteException) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Example 60 with IgniteFuture

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

the class ComputeAsyncExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println("Compute asynchronous example started.");
        // Enable asynchronous mode.
        IgniteCompute compute = ignite.compute().withAsync();
        Collection<IgniteFuture<?>> futs = new ArrayList<>();
        // Iterate through all words in the sentence and create runnable jobs.
        for (final String word : "Print words using runnable".split(" ")) {
            // Execute runnable on some node.
            compute.run(() -> {
                System.out.println();
                System.out.println(">>> Printing '" + word + "' on this node from ignite job.");
            });
            futs.add(compute.future());
        }
        // Wait for completion of all futures.
        futs.forEach(IgniteFuture::get);
        System.out.println();
        System.out.println(">>> Finished printing words using runnable execution.");
        System.out.println(">>> Check all nodes for output (this node is also part of the cluster).");
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)76 Ignite (org.apache.ignite.Ignite)36 ArrayList (java.util.ArrayList)28 IgniteException (org.apache.ignite.IgniteException)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteCompute (org.apache.ignite.IgniteCompute)9 CI1 (org.apache.ignite.internal.util.typedef.CI1)9 List (java.util.List)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)7 UUID (java.util.UUID)6 Collection (java.util.Collection)5 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4