Search in sources :

Example 46 with IgniteFuture

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

the class GridCacheAsyncOperationsLimitSelfTest method testAsyncOps.

/**
 * @throws Exception If failed.
 */
public void testAsyncOps() throws Exception {
    final AtomicInteger cnt = new AtomicInteger();
    final GridAtomicInteger max = new GridAtomicInteger();
    for (int i = 0; i < 5000; i++) {
        final int i0 = i;
        cnt.incrementAndGet();
        jcache().putAsync("key" + i, i).listen(new CI1<IgniteFuture<?>>() {

            @Override
            public void apply(IgniteFuture<?> t) {
                cnt.decrementAndGet();
                max.setIfGreater(cnt.get());
                if (i0 > 0 && i0 % 100 == 0)
                    info("cnt: " + cnt.get());
            }
        });
        assertTrue("Maximum number of permits exceeded: " + max.get(), max.get() <= 51);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAtomicInteger(org.apache.ignite.internal.util.GridAtomicInteger) GridAtomicInteger(org.apache.ignite.internal.util.GridAtomicInteger) IgniteFuture(org.apache.ignite.lang.IgniteFuture)

Example 47 with IgniteFuture

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

the class IgniteCacheDynamicStopSelfTest method checkStopStartCacheWithDataLoader.

/**
 * @param allowOverwrite Allow overwrite flag for streamer.
 * @throws Exception If failed.
 */
public void checkStopStartCacheWithDataLoader(final boolean allowOverwrite) throws Exception {
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(CacheMode.PARTITIONED);
    ignite(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Object call() throws Exception {
            while (!stop.get()) {
                try (IgniteDataStreamer<Integer, Integer> str = ignite(0).dataStreamer(DEFAULT_CACHE_NAME)) {
                    str.allowOverwrite(allowOverwrite);
                    int i = 0;
                    while (!stop.get()) {
                        try {
                            str.addData(i % 10_000, i).listen(new CI1<IgniteFuture<?>>() {

                                @Override
                                public void apply(IgniteFuture<?> f) {
                                    try {
                                        f.get();
                                    } catch (CacheException ignore) {
                                    // This may be debugged.
                                    }
                                }
                            });
                        } catch (IllegalStateException ignored) {
                            break;
                        }
                        if (i > 0 && i % 10000 == 0)
                            info("Added: " + i);
                        i++;
                    }
                } catch (IllegalStateException | CacheException ignored) {
                // This may be debugged.
                }
            }
            return null;
        }
    });
    try {
        Thread.sleep(500);
        ignite(0).destroyCache(DEFAULT_CACHE_NAME);
        Thread.sleep(500);
        ignite(0).createCache(ccfg);
        Thread.sleep(1000);
    } finally {
        stop.set(true);
    }
    fut.get();
    int cnt = 0;
    for (Cache.Entry<Object, Object> ignored : ignite(0).cache(DEFAULT_CACHE_NAME)) cnt++;
    info(">>> cnt=" + cnt);
    ignite(0).destroyCache(DEFAULT_CACHE_NAME);
}
Also used : CacheException(javax.cache.CacheException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) CI1(org.apache.ignite.internal.util.typedef.CI1) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Cache(javax.cache.Cache)

Example 48 with IgniteFuture

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testReentrantLockFailsWhenServersLeft.

/**
 * @throws Exception If failed.
 */
public void testReentrantLockFailsWhenServersLeft(final boolean fair) throws Exception {
    client = true;
    Ignite client = startGrid(gridCount());
    Ignite server = grid(0);
    // Initialize lock.
    IgniteLock srvLock = server.reentrantLock("lock", true, fair, true);
    IgniteSemaphore semaphore = server.semaphore("sync", 0, true, true);
    IgniteFuture fut = client.compute().applyAsync(new IgniteClosure<Ignite, Object>() {

        @Override
        public Object apply(Ignite ignite) {
            final IgniteLock l = ignite.reentrantLock("lock", true, fair, true);
            l.lock();
            assertTrue(l.isHeldByCurrentThread());
            l.unlock();
            assertFalse(l.isHeldByCurrentThread());
            // Signal the server to go down.
            ignite.semaphore("sync", 0, true, true).release();
            boolean isExceptionThrown = false;
            try {
                // Wait for the server to go down.
                Thread.sleep(1000);
                l.lock();
                fail("Exception must be thrown.");
            } catch (InterruptedException ignored) {
                fail("Interrupted exception not expected here.");
            } catch (IgniteException ignored) {
                isExceptionThrown = true;
            } finally {
                assertTrue(isExceptionThrown);
                assertFalse(l.isHeldByCurrentThread());
            }
            return null;
        }
    }, client);
    // Wait for the lock on client to be acquired then released.
    semaphore.acquire();
    for (int i = 0; i < gridCount(); i++) stopGrid(i);
    fut.get();
    client.close();
}
Also used : IgniteException(org.apache.ignite.IgniteException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) IgniteLock(org.apache.ignite.IgniteLock) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException)

Example 49 with IgniteFuture

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

the class CachePutAllFailoverAbstractTest method testPutAllFailover.

/**
 * @param test Test type
 * @throws Exception If failed.
 */
private void testPutAllFailover(final Test test) throws Exception {
    final AtomicBoolean finished = new AtomicBoolean();
    final long endTime = System.currentTimeMillis() + TEST_TIME;
    IgniteInternalFuture<Object> restartFut = createAndRunConcurrentAction(finished, endTime);
    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("Do putAll [iter=" + iter + ']');
                        lastInfo = time;
                    }
                    switch(test) {
                        case PUT_ALL:
                            {
                                TreeMap<TestKey, TestValue> map = new TreeMap<>();
                                for (int k = 0; k < 100; k++) map.put(new TestKey(rnd.nextInt(200)), new TestValue(iter));
                                cache.putAll(map);
                                break;
                            }
                        case PUT_ALL_ASYNC:
                            {
                                Collection<IgniteFuture<?>> futs = new ArrayList<>();
                                for (int i = 0; i < 10; i++) {
                                    TreeMap<TestKey, TestValue> map = new TreeMap<>();
                                    for (int k = 0; k < 100; k++) map.put(new TestKey(rnd.nextInt(200)), new TestValue(iter));
                                    IgniteFuture<?> fut = cache.putAllAsync(map);
                                    assertNotNull(fut);
                                    futs.add(fut);
                                }
                                for (IgniteFuture<?> fut : futs) fut.get();
                                break;
                            }
                        case PUT_ALL_PESSIMISTIC_TX:
                            {
                                final TreeMap<TestKey, TestValue> map = new TreeMap<>();
                                for (int k = 0; k < 100; k++) map.put(new TestKey(rnd.nextInt(200)), new TestValue(iter));
                                doInTransaction(ignite(0), new Callable<Object>() {

                                    @Override
                                    public Object call() throws Exception {
                                        for (TestKey key : map.keySet()) cache.get(key);
                                        cache.putAll(map);
                                        return null;
                                    }
                                });
                                break;
                            }
                        default:
                            assert false;
                    }
                    iter++;
                }
                return null;
            }
        }, 2, "update-thread");
        finished.set(true);
        restartFut.get();
    } finally {
        finished.set(true);
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteFuture(org.apache.ignite.lang.IgniteFuture) TreeMap(java.util.TreeMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 50 with IgniteFuture

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

the class GridCacheAbstractJobExecutionTest method checkTransactions.

/**
 * @param concur Concurrency.
 * @param isolation Isolation.
 * @param jobCnt Job count.
 * @throws Exception If fails.
 */
private void checkTransactions(final TransactionConcurrency concur, final TransactionIsolation isolation, final int jobCnt) throws Exception {
    info("Grid 0: " + grid(0).localNode().id());
    info("Grid 1: " + grid(1).localNode().id());
    info("Grid 2: " + grid(2).localNode().id());
    info("Grid 3: " + grid(3).localNode().id());
    Ignite ignite = grid(0);
    Collection<IgniteFuture<?>> futs = new LinkedList<>();
    final String key = "TestKey";
    info("Primary node for test key: " + grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key));
    for (int i = 0; i < jobCnt; i++) {
        futs.add(ignite.compute().applyAsync(new CX1<Integer, Void>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public Void applyx(final Integer i) {
                IgniteCache<String, int[]> cache = ignite.cache(DEFAULT_CACHE_NAME);
                try (Transaction tx = ignite.transactions().txStart(concur, isolation)) {
                    int[] arr = cache.get(key);
                    if (arr == null)
                        arr = new int[jobCnt];
                    arr[i] = 1;
                    cache.put(key, arr);
                    int c = cntr.getAndIncrement();
                    if (c % 50 == 0)
                        X.println("Executing transaction [i=" + i + ", c=" + c + ']');
                    tx.commit();
                }
                return null;
            }
        }, i));
    }
    for (IgniteFuture<?> fut : futs) // Wait for completion.
    fut.get();
    for (int i = 0; i < GRID_CNT; i++) {
        info("Running iteration: " + i);
        for (int g = 0; g < GRID_CNT; g++) {
            info("Will check grid: " + g);
            info("Value: " + grid(i).cache(DEFAULT_CACHE_NAME).localPeek(key));
        }
        IgniteCache<String, int[]> c = grid(i).cache(DEFAULT_CACHE_NAME);
        // which means that all previous transactions have committed.
        try (Transaction tx = grid(i).transactions().txStart(concur, isolation)) {
            int[] arr = c.get(key);
            assertNotNull(arr);
            assertEquals(jobCnt, arr.length);
            for (int j : arr) assertEquals(1, j);
            tx.commit();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CX1(org.apache.ignite.internal.util.typedef.CX1) LinkedList(java.util.LinkedList)

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