Search in sources :

Example 41 with CacheException

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

the class IgniteCacheOffheapEvictQueryTest method testEvictAndRemove.

/**
     * @throws Exception If failed.
     */
public void testEvictAndRemove() throws Exception {
    final int KEYS_CNT = 3000;
    final int THREADS_CNT = 250;
    final IgniteCache<Integer, Integer> c = startGrid().cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < KEYS_CNT; i++) {
        c.put(i, i);
        if ((i & 1) == 0)
            c.localEvict(F.asList(i));
    }
    X.println("___ Cache loaded...");
    final CyclicBarrier b = new CyclicBarrier(THREADS_CNT, new Runnable() {

        @Override
        public void run() {
            X.println("___ go!");
        }
    });
    final AtomicInteger keys = new AtomicInteger(KEYS_CNT);
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            Random rnd = new GridRandom();
            try {
                b.await();
            } catch (InterruptedException e) {
                throw new IgniteInterruptedException(e);
            } catch (BrokenBarrierException e) {
                throw new IllegalStateException(e);
            }
            while (keys.get() > 0) {
                int k = rnd.nextInt(KEYS_CNT);
                try {
                    switch(rnd.nextInt(4)) {
                        case 0:
                            c.localEvict(F.asList(k));
                            break;
                        case 1:
                            c.get(k);
                            break;
                        case 2:
                            if (c.remove(k))
                                keys.decrementAndGet();
                            break;
                        case 3:
                            c.query(new SqlFieldsQuery("select _val from Integer where _key between ? and ?").setArgs(k, k + 20).setLocal(true)).getAll();
                            break;
                    }
                } catch (CacheException e) {
                    String msgStart = "Failed to get value for key:";
                    for (Throwable th = e; th != null; th = th.getCause()) {
                        String msg = th.getMessage();
                        if (msg != null && msg.startsWith(msgStart)) {
                            int dot = msg.indexOf('.', msgStart.length());
                            assertTrue(dot != -1);
                            final Integer failedKey = Integer.parseInt(msg.substring(msgStart.length(), dot).trim());
                            X.println("___ failed key: " + failedKey);
                            break;
                        }
                    }
                    LT.warn(log, e.getMessage());
                    return;
                }
            }
        }
    }, THREADS_CNT);
    try {
        fut.get(60_000);
        if (c.size(CachePeekMode.ALL) != 0)
            fail("Not all keys removed.");
        X.println("___ all keys removed");
    } catch (IgniteFutureTimeoutCheckedException ignored) {
        X.println("___ timeout");
        X.println("___ keys: " + keys.get());
        keys.set(0);
        fut.get();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CacheException(javax.cache.CacheException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)

Example 42 with CacheException

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

the class CacheSerializableTransactionsTest method incrementTx.

/**
     * @param nearCache If {@code true} near cache is enabled.
     * @param store If {@code true} cache store is enabled.
     * @param restart If {@code true} restarts one node.
     * @throws Exception If failed.
     */
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
    final Ignite srv = ignite(1);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
    final List<Ignite> clients = clients();
    final String cacheName = srv.createCache(ccfg).getName();
    final AtomicBoolean stop = new AtomicBoolean();
    try {
        final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
        for (Ignite client : clients) {
            if (nearCache)
                caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
            else
                caches.add(client.<Integer, Integer>cache(cacheName));
        }
        IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
        final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
        for (int i = 0; i < 30; i++) {
            final AtomicInteger cntr = new AtomicInteger();
            final Integer key = i;
            final AtomicInteger threadIdx = new AtomicInteger();
            final int THREADS = 10;
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = threadIdx.getAndIncrement() % caches.size();
                    IgniteCache<Integer, Integer> cache = caches.get(idx);
                    Ignite ignite = cache.unwrap(Ignite.class);
                    IgniteTransactions txs = ignite.transactions();
                    log.info("Started update thread: " + ignite.name());
                    barrier.await();
                    for (int i = 0; i < 1000; i++) {
                        if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
                            break;
                        try {
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Integer val = cache.get(key);
                                cache.put(key, val == null ? 1 : val + 1);
                                tx.commit();
                            }
                            cntr.incrementAndGet();
                        } catch (TransactionOptimisticException ignore) {
                        // Retry.
                        } catch (IgniteException | CacheException e) {
                            assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
                        }
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
            assertTrue(cntr.get() > 0);
            checkValue(key, cntr.get(), cacheName, restart);
            if (U.currentTimeMillis() > stopTime)
                break;
        }
        stop.set(true);
        if (restartFut != null)
            restartFut.get();
    } finally {
        stop.set(true);
        destroyCache(cacheName);
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 43 with CacheException

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

the class GridCacheBasicOpAbstractTest method testOptimisticTransaction.

/**
     *
     * @throws IgniteCheckedException If test fails.
     */
public void testOptimisticTransaction() throws Exception {
    CountDownLatch latch = new CountDownLatch(9);
    IgnitePredicate<Event> lsnr = new CacheEventListener(latch);
    try {
        IgniteCache<String, String> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
        IgniteCache<String, String> cache2 = ignite2.cache(DEFAULT_CACHE_NAME);
        IgniteCache<String, String> cache3 = ignite3.cache(DEFAULT_CACHE_NAME);
        ignite1.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
        ignite2.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
        ignite3.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
        Transaction tx = ignite1.transactions().txStart(OPTIMISTIC, READ_COMMITTED, 0, 0);
        try {
            cache1.put("tx1", "val1");
            cache1.put("tx2", "val2");
            cache1.put("tx3", "val3");
            assert cache2.get("tx1") == null;
            assert cache2.get("tx2") == null;
            assert cache2.get("tx3") == null;
            assert cache3.get("tx1") == null;
            assert cache3.get("tx2") == null;
            assert cache3.get("tx3") == null;
            tx.commit();
        } catch (CacheException e) {
            tx.rollback();
            throw e;
        }
        assert latch.await(5, SECONDS);
        String b1 = cache2.get("tx1");
        String b2 = cache2.get("tx2");
        String b3 = cache2.get("tx3");
        String c1 = cache3.get("tx1");
        String c2 = cache3.get("tx2");
        String c3 = cache3.get("tx3");
        assert b1 != null : "Invalid value: " + b1;
        assert b2 != null : "Invalid value: " + b2;
        assert b3 != null : "Invalid value: " + b3;
        assert c1 != null : "Invalid value: " + c1;
        assert c2 != null : "Invalid value: " + c2;
        assert c3 != null : "Invalid value: " + c3;
        assert "val1".equals(b1);
        assert "val2".equals(b2);
        assert "val3".equals(b3);
        assert "val1".equals(c1);
        assert "val2".equals(c2);
        assert "val3".equals(c3);
    } finally {
        ignite1.events().stopLocalListen(lsnr);
        ignite2.events().stopLocalListen(lsnr);
        ignite3.events().stopLocalListen(lsnr);
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 44 with CacheException

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

the class GridH2IndexBase method toRow.

/**
     * @param msg Message.
     * @return Row.
     */
private Row toRow(GridH2RowMessage msg) {
    if (msg == null)
        return null;
    GridKernalContext ctx = kernalContext();
    List<GridH2ValueMessage> vals = msg.values();
    assert !F.isEmpty(vals) : vals;
    Value[] vals0 = new Value[vals.size()];
    for (int i = 0; i < vals0.length; i++) {
        try {
            vals0[i] = vals.get(i).value(ctx);
        } catch (IgniteCheckedException e) {
            throw new CacheException(e);
        }
    }
    return database.createRow(vals0, MEMORY_CALCULATE);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridH2ValueMessage(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessage) Value(org.h2.value.Value)

Example 45 with CacheException

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

the class GridH2IndexBase method toRowMessage.

/**
     * @param row Row.
     * @return Row message.
     */
private GridH2RowMessage toRowMessage(Row row) {
    if (row == null)
        return null;
    int cols = row.getColumnCount();
    assert cols > 0 : cols;
    List<GridH2ValueMessage> vals = new ArrayList<>(cols);
    for (int i = 0; i < cols; i++) {
        try {
            vals.add(GridH2ValueMessageFactory.toMessage(row.getValue(i)));
        } catch (IgniteCheckedException e) {
            throw new CacheException(e);
        }
    }
    GridH2RowMessage res = new GridH2RowMessage();
    res.values(vals);
    return res;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) GridH2ValueMessage(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessage) GridH2RowMessage(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowMessage)

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