Search in sources :

Example 11 with IgniteFuture

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

the class CacheRebalancingSelfTest method testRebalanceFuture.

/**
 * @throws Exception If failed.
 */
public void testRebalanceFuture() throws Exception {
    IgniteEx ig0 = startGrid(0);
    startGrid(1);
    IgniteCache<Object, Object> cache = ig0.cache(DEFAULT_CACHE_NAME);
    IgniteFuture fut1 = cache.rebalance();
    fut1.get();
    startGrid(2);
    IgniteFuture fut2 = cache.rebalance();
    assert internalFuture(fut2) != internalFuture(fut1);
    fut2.get();
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Example 12 with IgniteFuture

use of org.apache.ignite.lang.IgniteFuture 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 13 with IgniteFuture

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

the class DataStreamProcessorSelfTest method checkLoaderMultithreaded.

/**
 * Tests loader in multithreaded environment with various count of grids started.
 *
 * @param nodesCntNoCache How many nodes should be started without cache.
 * @param nodesCntCache How many nodes should be started with cache.
 * @throws Exception If failed.
 */
protected void checkLoaderMultithreaded(int nodesCntNoCache, int nodesCntCache) throws Exception {
    try {
        // Start all required nodes.
        int idx = 1;
        useCache = true;
        for (int i = 0; i < nodesCntCache; i++) startGrid(idx++);
        useCache = false;
        for (int i = 0; i < nodesCntNoCache; i++) startGrid(idx++);
        Ignite g1 = grid(idx - 1);
        // Get and configure loader.
        final IgniteDataStreamer<Integer, Integer> ldr = g1.dataStreamer(DEFAULT_CACHE_NAME);
        ldr.receiver(DataStreamerCacheUpdaters.<Integer, Integer>individual());
        ldr.perNodeBufferSize(2);
        // Define count of puts.
        final AtomicInteger idxGen = new AtomicInteger();
        final AtomicBoolean done = new AtomicBoolean();
        try {
            final int totalPutCnt = 50000;
            IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    Collection<IgniteFuture<?>> futs = new ArrayList<>();
                    while (!done.get()) {
                        int idx = idxGen.getAndIncrement();
                        if (idx >= totalPutCnt) {
                            info(">>> Stopping producer thread since maximum count of puts reached.");
                            break;
                        }
                        futs.add(ldr.addData(idx, idx));
                    }
                    ldr.flush();
                    for (IgniteFuture<?> fut : futs) fut.get();
                    return null;
                }
            }, 5, "producer");
            IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    while (!done.get()) {
                        ldr.flush();
                        U.sleep(100);
                    }
                    return null;
                }
            }, 1, "flusher");
            // Define index of node being restarted.
            final int restartNodeIdx = nodesCntCache + nodesCntNoCache + 1;
            IgniteInternalFuture<?> fut3 = multithreadedAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        for (int i = 0; i < 5; i++) {
                            Ignite g = startGrid(restartNodeIdx);
                            UUID id = g.cluster().localNode().id();
                            info(">>>>>>> Started node: " + id);
                            U.sleep(1000);
                            stopGrid(getTestIgniteInstanceName(restartNodeIdx), true);
                            info(">>>>>>> Stopped node: " + id);
                        }
                    } finally {
                        done.set(true);
                        info("Start stop thread finished.");
                    }
                    return null;
                }
            }, 1, "start-stop-thread");
            fut1.get();
            fut2.get();
            fut3.get();
        } finally {
            ldr.close(false);
        }
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 14 with IgniteFuture

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

the class DataStreamerImplSelfTest method testAllOperationFinishedBeforeFutureCompletion.

/**
 * @throws Exception If failed.
 */
public void testAllOperationFinishedBeforeFutureCompletion() throws Exception {
    cnt = 0;
    Ignite ignite = startGrids(MAX_CACHE_COUNT);
    final IgniteCache cache = ignite.cache(DEFAULT_CACHE_NAME);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ex = new AtomicReference<>();
    Collection<Map.Entry> entries = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) entries.add(new IgniteBiTuple<>(i, "" + i));
    IgniteDataStreamer ldr = ignite.dataStreamer(DEFAULT_CACHE_NAME);
    ldr.addData(entries).listen(new IgniteInClosure<IgniteFuture<?>>() {

        @Override
        public void apply(IgniteFuture<?> future) {
            try {
                future.get();
                for (int i = 0; i < 100; i++) assertEquals("" + i, cache.get(i));
            } catch (Throwable e) {
                ex.set(e);
            }
            latch.countDown();
        }
    });
    ldr.tryFlush();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    Throwable e = ex.get();
    if (e != null) {
        if (e instanceof Error)
            throw (Error) e;
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new RuntimeException(e);
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) Ignite(org.apache.ignite.Ignite)

Example 15 with IgniteFuture

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

the class DataStreamerImplSelfTest method testNoDataNodesOnFlush.

/**
 * Test logging on {@code DataStreamer.addData()} method when cache have no data nodes
 *
 * @throws Exception If fail.
 */
public void testNoDataNodesOnFlush() throws Exception {
    boolean failed = false;
    cnt = 0;
    noNodesFilter = true;
    try {
        Ignite ignite = startGrid(1);
        IgniteFuture fut = null;
        try (IgniteDataStreamer<Integer, String> streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
            fut = streamer.addData(1, "1");
            streamer.flush();
        } catch (IllegalStateException ignored) {
            try {
                fut.get();
                fail("DataStreamer ignores failed streaming.");
            } catch (CacheServerNotFoundException ignored2) {
            // No-op.
            }
            failed = true;
        }
    } finally {
        noNodesFilter = false;
        assertTrue(failed);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite)

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