Search in sources :

Example 21 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class OptimizedMarshallerNodeFailoverTest method classCacheUpdateFailover.

/**
 * @param stopSrv If {@code true} restarts server node, otherwise client node.
 * @throws Exception If failed.
 */
private void classCacheUpdateFailover(boolean stopSrv) throws Exception {
    cache = true;
    startGridsMultiThreaded(2);
    cache = stopSrv;
    IgniteCache<Integer, Object> cache0 = ignite(0).cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 20; i++) {
        log.info("Iteration: " + i);
        Map<Integer, Object> map = new HashMap<>();
        for (int j = 0; j < 10_000; j++) map.put(j, create(i + 1));
        final Ignite ignite = startGrid(2);
        IgniteInternalFuture fut = GridTestUtils.runAsync(new Callable() {

            @Override
            public Object call() throws Exception {
                ignite.close();
                return null;
            }
        });
        cache0.putAll(map);
        fut.get();
    }
    cache = true;
    // Check can start one more cache node.
    Ignite ignite = startGrid(2);
    assertNotNull(ignite.cache(DEFAULT_CACHE_NAME));
}
Also used : HashMap(java.util.HashMap) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable)

Example 22 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridCacheFinishPartitionsSelfTest method runLock.

/**
 * @param key Key.
 * @param keyPart Key partition.
 * @param waitParts Partitions to wait.
 * @return Wait time.
 * @throws Exception If failed.
 */
private long runLock(String key, int keyPart, Collection<Integer> waitParts) throws Exception {
    GridCacheSharedContext<Object, Object> ctx = grid.context().cache().context();
    final AtomicLong end = new AtomicLong(0);
    final CountDownLatch latch = new CountDownLatch(1);
    IgniteCache<String, String> cache = grid.cache(DEFAULT_CACHE_NAME);
    Lock lock = cache.lock(key);
    lock.lock();
    long start;
    try {
        start = System.currentTimeMillis();
        info("Start time: " + start);
        IgniteInternalFuture<?> fut = ctx.partitionReleaseFuture(new AffinityTopologyVersion(GRID_CNT + 1));
        assert fut != null;
        fut.listen(new CI1<IgniteInternalFuture<?>>() {

            @Override
            public void apply(IgniteInternalFuture<?> e) {
                end.set(System.currentTimeMillis());
                latch.countDown();
                info("End time: " + end.get());
            }
        });
        assert !fut.isDone() : "Failed waiting for locks [keyPart=" + keyPart + ", waitParts=" + waitParts + ", done=" + fut.isDone() + ']';
    } finally {
        lock.unlock();
    }
    latch.await();
    return end.get() - start;
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Lock(java.util.concurrent.locks.Lock) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 23 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class CacheSerializableTransactionsTest method testNoOptimisticExceptionOnChangingTopology.

/**
 * @throws Exception If failed.
 */
public void testNoOptimisticExceptionOnChangingTopology() throws Exception {
    if (FAST)
        return;
    final AtomicBoolean finished = new AtomicBoolean();
    final List<String> cacheNames = new ArrayList<>();
    Ignite srv = ignite(1);
    try {
        {
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
            ccfg.setName("cache1");
            ccfg.setRebalanceMode(SYNC);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        {
            // Store enabled.
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, true, false);
            ccfg.setName("cache2");
            ccfg.setRebalanceMode(SYNC);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        {
            // Eviction.
            CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
            ccfg.setName("cache3");
            ccfg.setRebalanceMode(SYNC);
            LruEvictionPolicy plc = new LruEvictionPolicy();
            plc.setMaxSize(100);
            ccfg.setEvictionPolicy(plc);
            ccfg.setOnheapCacheEnabled(true);
            srv.createCache(ccfg);
            cacheNames.add(ccfg.getName());
        }
        IgniteInternalFuture<?> restartFut = restartFuture(finished, null);
        List<IgniteInternalFuture<?>> futs = new ArrayList<>();
        final int KEYS_PER_THREAD = 100;
        for (int i = 1; i < SRVS + CLIENTS; i++) {
            final Ignite node = ignite(i);
            final int minKey = i * KEYS_PER_THREAD;
            final int maxKey = minKey + KEYS_PER_THREAD;
            // Threads update non-intersecting keys, optimistic exception should not be thrown.
            futs.add(GridTestUtils.runAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        log.info("Started update thread [node=" + node.name() + ", minKey=" + minKey + ", maxKey=" + maxKey + ']');
                        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                        List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
                        for (String cacheName : cacheNames) caches.add(node.<Integer, Integer>cache(cacheName));
                        assertEquals(3, caches.size());
                        int iter = 0;
                        while (!finished.get()) {
                            int keyCnt = rnd.nextInt(1, 10);
                            final Set<Integer> keys = new LinkedHashSet<>();
                            while (keys.size() < keyCnt) keys.add(rnd.nextInt(minKey, maxKey));
                            for (final IgniteCache<Integer, Integer> cache : caches) {
                                doInTransaction(node, OPTIMISTIC, SERIALIZABLE, new Callable<Void>() {

                                    @Override
                                    public Void call() throws Exception {
                                        for (Integer key : keys) randomOperation(rnd, cache, key);
                                        return null;
                                    }
                                });
                            }
                            if (iter % 100 == 0)
                                log.info("Iteration: " + iter);
                            iter++;
                        }
                        return null;
                    } catch (Throwable e) {
                        log.error("Unexpected error: " + e, e);
                        throw e;
                    }
                }
            }, "update-thread-" + i));
        }
        U.sleep(60_000);
        finished.set(true);
        restartFut.get();
        for (IgniteInternalFuture<?> fut : futs) fut.get();
    } finally {
        finished.set(true);
        for (String cacheName : cacheNames) destroyCache(cacheName);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteException(org.apache.ignite.IgniteException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) LruEvictionPolicy(org.apache.ignite.cache.eviction.lru.LruEvictionPolicy)

Example 24 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class GridCacheAbstractRemoveFailureTest method putAndRemove.

/**
 * @param duration Test duration.
 * @param txConcurrency Transaction concurrency if test explicit transaction.
 * @param txIsolation Transaction isolation if test explicit transaction.
 * @throws Exception If failed.
 */
private void putAndRemove(long duration, final TransactionConcurrency txConcurrency, final TransactionIsolation txIsolation) throws Exception {
    assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
    grid(0).destroyCache(DEFAULT_CACHE_NAME);
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setCacheMode(cacheMode());
    if (cacheMode() == PARTITIONED)
        ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode());
    ccfg.setNearConfiguration(nearCache());
    final IgniteCache<Integer, Integer> sndCache0 = grid(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicLong cntr = new AtomicLong();
    final AtomicLong errCntr = new AtomicLong();
    // Expected values in cache.
    final Map<Integer, GridTuple<Integer>> expVals = new ConcurrentHashMap<>();
    final AtomicReference<CyclicBarrier> cmp = new AtomicReference<>();
    IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("update-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteTransactions txs = sndCache0.unwrap(Ignite.class).transactions();
            while (!stop.get()) {
                for (int i = 0; i < 100; i++) {
                    int key = rnd.nextInt(KEYS_CNT);
                    boolean put = rnd.nextInt(0, 100) > 10;
                    while (true) {
                        try {
                            if (put) {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.put(key, i);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.put(key, i);
                                if (!failed)
                                    expVals.put(key, F.t(i));
                            } else {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.remove(key);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.remove(key);
                                if (!failed)
                                    expVals.put(key, F.<Integer>t(null));
                            }
                            break;
                        } catch (CacheException e) {
                            if (put)
                                log.error("Put failed [key=" + key + ", val=" + i + ']', e);
                            else
                                log.error("Remove failed [key=" + key + ']', e);
                            errCntr.incrementAndGet();
                        }
                    }
                }
                cntr.addAndGet(100);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    IgniteInternalFuture killFut = createAndRunConcurrentAction(stop, cmp);
    try {
        long stopTime = duration + U.currentTimeMillis();
        long nextAssert = U.currentTimeMillis() + ASSERT_FREQ;
        while (U.currentTimeMillis() < stopTime) {
            long start = System.nanoTime();
            long ops = cntr.longValue();
            U.sleep(1000);
            long diff = cntr.longValue() - ops;
            double time = (System.nanoTime() - start) / 1_000_000_000d;
            long opsPerSecond = (long) (diff / time);
            log.info("Operations/second: " + opsPerSecond);
            if (U.currentTimeMillis() >= nextAssert) {
                CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            cmp.set(null);
                            log.info("Checking cache content.");
                            assertCacheContent(expVals);
                            log.info("Finished check cache content.");
                        } catch (Throwable e) {
                            log.error("Unexpected error: " + e, e);
                            throw e;
                        }
                    }
                });
                log.info("Start cache content check.");
                cmp.set(barrier);
                try {
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    U.dumpThreads(log);
                    fail("Failed to check cache content: " + e);
                }
                log.info("Cache content check done.");
                nextAssert = System.currentTimeMillis() + ASSERT_FREQ;
            }
        }
    } finally {
        stop.set(true);
    }
    killFut.get();
    updateFut.get();
    log.info("Test finished. Update errors: " + errCntr.get());
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 25 with IgniteInternalFuture

use of org.apache.ignite.internal.IgniteInternalFuture in project ignite by apache.

the class AuthenticationProcessorNodeRestartTest method testConcurrentAuthorize.

/**
 * @throws Exception If failed.
 */
public void testConcurrentAuthorize() throws Exception {
    final int testUsersCnt = 10;
    AuthorizationContext.context(actxDflt);
    for (int i = 0; i < testUsersCnt; ++i) grid(CLI_NODE).context().authentication().addUser("test" + i, "passwd_test" + i);
    final IgniteInternalFuture restartFut = GridTestUtils.runAsync(new Runnable() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < RESTARTS; ++i) {
                    int nodeIdx = RND.nextInt(NODES_COUNT - 1);
                    stopGrid(nodeIdx);
                    U.sleep(500);
                    startGrid(nodeIdx);
                    U.sleep(500);
                }
            } catch (Exception e) {
                e.printStackTrace(System.err);
                fail("Unexpected exception on server restart: " + e.getMessage());
            }
        }
    });
    final AtomicInteger usrCnt = new AtomicInteger();
    GridTestUtils.runMultiThreaded(new Runnable() {

        @Override
        public void run() {
            String user = "test" + usrCnt.getAndIncrement();
            try {
                while (!restartFut.isDone()) {
                    AuthorizationContext actx = grid(CLI_NODE).context().authentication().authenticate(user, "passwd_" + user);
                    assertNotNull(actx);
                }
            } catch (IgniteCheckedException e) {
                // Skip exception if server down.
                if (!e.getMessage().contains("Failed to send message (node may have left the grid or " + "TCP connection cannot be established due to firewall issues)")) {
                    e.printStackTrace();
                    fail("Unexpected exception: " + e.getMessage());
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail("Unexpected exception: " + e.getMessage());
            }
        }
    }, testUsersCnt, "user-op");
    restartFut.get();
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)245 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)114 Ignite (org.apache.ignite.Ignite)71 ArrayList (java.util.ArrayList)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)46 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)46 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 IgniteException (org.apache.ignite.IgniteException)33 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)29 UUID (java.util.UUID)28 IgniteCache (org.apache.ignite.IgniteCache)28 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 Callable (java.util.concurrent.Callable)27 HashMap (java.util.HashMap)25 Map (java.util.Map)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)19 CacheException (javax.cache.CacheException)16 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)16 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)16