Search in sources :

Example 31 with CacheException

use of javax.cache.CacheException 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 ConcurrentHashMap8<>();
    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 = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("restart-thread");
            while (!stop.get()) {
                U.sleep(random(KILL_DELAY.get1(), KILL_DELAY.get2()));
                killAndRestart(stop);
                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;
        }
    });
    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) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) 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 32 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class DataStreamerTimeoutTest method timeoutOnAddData.

/**
     *
     */
private int timeoutOnAddData() throws Exception {
    boolean thrown = false;
    int processed = 0;
    try {
        Ignite ignite = startGrid(1);
        try (IgniteDataStreamer ldr = ignite.dataStreamer(CACHE_NAME)) {
            ldr.timeout(TIMEOUT);
            ldr.receiver(new TestDataReceiver());
            ldr.perNodeBufferSize(1);
            ldr.perNodeParallelOperations(1);
            ((DataStreamerImpl) ldr).maxRemapCount(0);
            try {
                for (int i = 0; i < ENTRY_AMOUNT; i++) {
                    ldr.addData(i, i);
                    processed++;
                }
            } catch (IllegalStateException ignored) {
            // No-op.
            }
        } catch (CacheException | IgniteDataStreamerTimeoutException ignored) {
            thrown = true;
        }
    } finally {
        stopAllGrids();
    }
    assertTrue(thrown);
    return processed;
}
Also used : IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) IgniteDataStreamerTimeoutException(org.apache.ignite.IgniteDataStreamerTimeoutException) CacheException(javax.cache.CacheException) Ignite(org.apache.ignite.Ignite)

Example 33 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class DataStreamerImplSelfTest method testNoDataNodesOnClose.

/**
     * Test logging on {@code DataStreamer.addData()} method when cache have no data nodes
     *
     * @throws Exception If fail.
     */
public void testNoDataNodesOnClose() throws Exception {
    boolean failed = false;
    cnt = 0;
    noNodesFilter = true;
    try {
        Ignite ignite = startGrid(1);
        try (IgniteDataStreamer<Integer, String> streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
            streamer.addData(1, "1");
        } catch (CacheException ignored) {
            failed = true;
        }
    } finally {
        noNodesFilter = false;
        assertTrue(failed);
    }
}
Also used : CacheException(javax.cache.CacheException) Ignite(org.apache.ignite.Ignite)

Example 34 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class GridStartStopSelfTest method testStopWhileInUse.

/**
     * @throws Exception If failed.
     */
public void testStopWhileInUse() throws Exception {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setConnectorConfiguration(null);
    cfg.setIgniteInstanceName(getTestIgniteInstanceName(0));
    CacheConfiguration cc = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cc.setAtomicityMode(TRANSACTIONAL);
    cfg.setCacheConfiguration(cc);
    final Ignite g0 = G.start(cfg);
    cfg = new IgniteConfiguration();
    cfg.setIgniteInstanceName(getTestIgniteInstanceName(1));
    cc = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cc.setAtomicityMode(TRANSACTIONAL);
    cfg.setCacheConfiguration(cc);
    final CountDownLatch latch = new CountDownLatch(1);
    Ignite g1 = G.start(cfg);
    Thread stopper = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try (Transaction ignored = g0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    g0.cache(DEFAULT_CACHE_NAME).get(1);
                    latch.countDown();
                    Thread.sleep(500);
                    info("Before stop.");
                    G.stop(getTestIgniteInstanceName(1), true);
                }
            } catch (Exception e) {
                error("Error.", e);
            }
        }
    });
    stopper.start();
    assert latch.await(1, SECONDS);
    info("Before remove.");
    try {
        g1.cache(DEFAULT_CACHE_NAME).remove(1);
    } catch (CacheException ignore) {
    // No-op.
    }
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CacheException(javax.cache.CacheException)

Example 35 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class GridCacheStopSelfTest method testStop.

/**
     * @param startTx If {@code true} starts transactions.
     * @throws Exception If failed.
     */
private void testStop(final boolean startTx) throws Exception {
    for (int i = 0; i < 10; i++) {
        startGrid(0);
        final int PUT_THREADS = 50;
        final CountDownLatch stopLatch = new CountDownLatch(1);
        final CountDownLatch readyLatch = new CountDownLatch(PUT_THREADS);
        final IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
        assertNotNull(cache);
        CacheConfiguration ccfg = cache.getConfiguration(CacheConfiguration.class);
        assertEquals(atomic ? ATOMIC : TRANSACTIONAL, ccfg.getAtomicityMode());
        assertEquals(replicated ? REPLICATED : PARTITIONED, ccfg.getCacheMode());
        Collection<IgniteInternalFuture<?>> putFuts = new ArrayList<>();
        for (int j = 0; j < PUT_THREADS; j++) {
            final int key = j;
            putFuts.add(GridTestUtils.runAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        if (startTx) {
                            TransactionConcurrency concurrency = key % 2 == 0 ? OPTIMISTIC : PESSIMISTIC;
                            try (Transaction tx = grid(0).transactions().txStart(concurrency, REPEATABLE_READ)) {
                                cache.put(key, key);
                                readyLatch.countDown();
                                stopLatch.await();
                                tx.commit();
                            }
                        } else {
                            readyLatch.countDown();
                            stopLatch.await();
                            cache.put(key, key);
                        }
                    } catch (CacheException | IgniteException | IllegalStateException e) {
                        log.info("Ignore error: " + e);
                    }
                    return null;
                }
            }, "cache-thread"));
        }
        readyLatch.await();
        stopLatch.countDown();
        stopGrid(0);
        for (IgniteInternalFuture<?> fut : putFuts) fut.get();
        try {
            cache.put(1, 1);
        } catch (IllegalStateException e) {
            if (!e.getMessage().startsWith(EXPECTED_MSG))
                e.printStackTrace();
            assertTrue("Unexpected error message: " + e.getMessage(), e.getMessage().startsWith(EXPECTED_MSG));
        }
    }
}
Also used : CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Callable(java.util.concurrent.Callable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheException (javax.cache.CacheException)144 Ignite (org.apache.ignite.Ignite)42 IgniteException (org.apache.ignite.IgniteException)36 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 Transaction (org.apache.ignite.transactions.Transaction)25 ArrayList (java.util.ArrayList)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 IgniteCache (org.apache.ignite.IgniteCache)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 List (java.util.List)16 Map (java.util.Map)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)15 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)13 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)13 HashMap (java.util.HashMap)12 IgniteTransactions (org.apache.ignite.IgniteTransactions)12 CyclicBarrier (java.util.concurrent.CyclicBarrier)11 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)10 Cache (javax.cache.Cache)9