Search in sources :

Example 61 with IgniteFuture

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

the class CacheAsyncOperationsFailoverAbstractTest method putAllAsyncFailover.

/**
     * @param threads Number of threads.
     * @param opsPerThread Number of concurrent async operations per thread.
     * @throws Exception If failed.
     */
private void putAllAsyncFailover(final int threads, final int opsPerThread) throws Exception {
    log.info("Start test [threads=" + threads + ", opsPerThread=" + opsPerThread + ']');
    final AtomicBoolean finished = new AtomicBoolean();
    final long endTime = System.currentTimeMillis() + TEST_TIME;
    IgniteInternalFuture<Object> restartFut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Thread.currentThread().setName("restart-thread");
            while (!finished.get() && System.currentTimeMillis() < endTime) {
                startGrid(NODE_CNT);
                U.sleep(500);
                stopGrid(NODE_CNT);
            }
            return null;
        }
    });
    try {
        final IgniteCache<TestKey, TestValue> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
        GridTestUtils.runMultiThreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                int iter = 0;
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                long time;
                long lastInfo = 0;
                while ((time = System.currentTimeMillis()) < endTime) {
                    if (time - lastInfo > 5000)
                        log.info("Starting operations [iter=" + iter + ']');
                    List<IgniteFuture<?>> futs = new ArrayList<>(opsPerThread);
                    for (int i = 0; i < opsPerThread; i++) {
                        TreeMap<TestKey, TestValue> map = new TreeMap<>();
                        int keys = rnd.nextInt(1, 50);
                        for (int k = 0; k < keys; k++) map.put(new TestKey(rnd.nextInt(10_000)), new TestValue(iter));
                        IgniteFuture<?> fut = cache.putAllAsync(map);
                        assertNotNull(fut);
                        futs.add(fut);
                    }
                    if (time - lastInfo > 5000) {
                        log.info("Waiting for futures [iter=" + iter + ']');
                        lastInfo = time;
                    }
                    for (IgniteFuture<?> fut : futs) fut.get();
                    iter++;
                }
                return null;
            }
        }, threads, "update-thread");
        finished.set(true);
        restartFut.get();
    } finally {
        finished.set(true);
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) TreeMap(java.util.TreeMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) List(java.util.List)

Example 62 with IgniteFuture

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

the class IgniteBinaryMetadataUpdateChangingTopologySelfTest method testNoDeadlockOptimistic.

/**
     * @throws Exception If failed.
     */
public void testNoDeadlockOptimistic() throws Exception {
    int key1 = primaryKey(ignite(1).cache("cache"));
    int key2 = primaryKey(ignite(2).cache("cache"));
    TestCommunicationSpi spi = (TestCommunicationSpi) ignite(1).configuration().getCommunicationSpi();
    spi.blockMessages(GridNearTxPrepareResponse.class, ignite(0).cluster().localNode().id());
    IgniteCache<Object, Object> cache = ignite(0).cache("cache");
    IgniteFuture futPutAll = cache.putAllAsync(F.asMap(key1, "val1", key2, new TestValue1()));
    try {
        Thread.sleep(500);
        IgniteInternalFuture<Void> fut = GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                startGrid(4);
                return null;
            }
        });
        Thread.sleep(1000);
        spi.stopBlock();
        futPutAll.get();
        fut.get();
    } finally {
        stopGrid(4);
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException)

Example 63 with IgniteFuture

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

the class IgniteBinaryMetadataUpdateChangingTopologySelfTest method testNoDeadlockInvoke.

/**
     * @throws Exception If failed.
     */
public void testNoDeadlockInvoke() throws Exception {
    int key1 = primaryKey(ignite(1).cache("cache"));
    int key2 = primaryKey(ignite(2).cache("cache"));
    TestCommunicationSpi spi = (TestCommunicationSpi) ignite(1).configuration().getCommunicationSpi();
    spi.blockMessages(GridNearTxPrepareResponse.class, ignite(0).cluster().localNode().id());
    IgniteCache<Object, Object> cache = ignite(0).cache("cache");
    IgniteFuture futInvokeAll = cache.invokeAllAsync(F.asSet(key1, key2), new TestEntryProcessor());
    try {
        Thread.sleep(500);
        IgniteInternalFuture<Void> fut = GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                startGrid(4);
                return null;
            }
        });
        Thread.sleep(1000);
        spi.stopBlock();
        futInvokeAll.get();
        fut.get();
    } finally {
        stopGrid(4);
    }
}
Also used : IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException)

Example 64 with IgniteFuture

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

the class IgnteCacheClientWriteBehindStoreNonCoalescingTest method testNonCoalescingIncrementing.

/**
     * @throws Exception If failed.
     */
public void testNonCoalescingIncrementing() throws Exception {
    Ignite ignite = grid(0);
    IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
    assertEquals(cache.getConfiguration(CacheConfiguration.class).getCacheStoreFactory().getClass(), TestIncrementStoreFactory.class);
    Set<Integer> keys = new HashSet<>();
    for (int i = 0; i < 1000; i++) {
        keys.add(i);
        cache.put(i, i);
    }
    Collection<IgniteFuture<?>> futs = new ArrayList<>();
    for (int i = 0; i < 100; i++) futs.add(updateKeys(cache, keys));
    for (IgniteFuture<?> fut : futs) fut.get();
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet)

Example 65 with IgniteFuture

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

the class GridScheduleSelfTest method testScheduleCallable.

/**
     * @throws Exception If failed.
     */
public void testScheduleCallable() throws Exception {
    SchedulerFuture<Integer> fut = null;
    // 1 minute frequency.
    long freq = 60;
    // 2 seconds delay.
    long delay = 2;
    try {
        fut = grid(0).scheduler().scheduleLocal(new Callable<Integer>() {

            private int cnt;

            @Override
            public Integer call() {
                info(">>> EXECUTING SCHEDULED CALLABLE! <<<");
                return ++cnt;
            }
        }, "{1, 2} * * * * *");
        final AtomicInteger notifyCnt = new AtomicInteger();
        fut.listen(new CI1<IgniteFuture<?>>() {

            @Override
            public void apply(IgniteFuture<?> e) {
                notifyCnt.incrementAndGet();
            }
        });
        assert !fut.isDone();
        assert !fut.isCancelled();
        assert fut.last() == null;
        long timeTillRun = freq + delay;
        info("Going to wait for the 1st run: " + timeTillRun);
        assertEquals((Integer) 1, fut.get(timeTillRun, SECONDS));
        assertEquals((Integer) 1, fut.last());
        assert !fut.isDone();
        assert !fut.isCancelled();
        info("Going to wait for the 2nd run: " + timeTillRun);
        assertEquals((Integer) 2, fut.get(timeTillRun, SECONDS));
        assertEquals((Integer) 2, fut.last());
        assert fut.isDone();
        assert !fut.isCancelled();
    } finally {
        assert fut != null;
        fut.cancel();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Aggregations

IgniteFuture (org.apache.ignite.lang.IgniteFuture)65 Ignite (org.apache.ignite.Ignite)31 ArrayList (java.util.ArrayList)23 IgniteException (org.apache.ignite.IgniteException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CacheException (javax.cache.CacheException)10 IgniteCompute (org.apache.ignite.IgniteCompute)8 CI1 (org.apache.ignite.internal.util.typedef.CI1)7 UUID (java.util.UUID)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 List (java.util.List)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)5 Collection (java.util.Collection)4 LinkedList (java.util.LinkedList)4 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)4 HashMap (java.util.HashMap)3