Search in sources :

Example 36 with CacheException

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

the class IgniteTxConfigCacheSelfTest method checkExplicitTxTimeout.

/**
     * Success if explicit tx fails.
     *
     * @param cache Cache name.
     * @param ignite Ignite instance.
     * @throws Exception If failed.
     */
protected void checkExplicitTxTimeout(final IgniteCache<Object, Object> cache, final Ignite ignite) throws Exception {
    try (final Transaction tx = ignite.transactions().txStart()) {
        assert tx != null;
        sleepForTxFailure();
        cache.put("key", "val");
        fail("Timeout exception must be thrown");
    } catch (CacheException e) {
        assert e.getCause() instanceof TransactionTimeoutException;
    }
    assert !cache.containsKey("key");
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) CacheException(javax.cache.CacheException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException)

Example 37 with CacheException

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

the class IgniteCachePutRetryAtomicSelfTest method testPutInsideTransaction.

/**
     * @throws Exception If failed.
     */
public void testPutInsideTransaction() throws Exception {
    ignite(0).createCache(cacheConfiguration(false, false));
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setName("tx-cache");
    ccfg.setAtomicityMode(TRANSACTIONAL);
    try (IgniteCache<Integer, Integer> txCache = ignite(0).getOrCreateCache(ccfg)) {
        final AtomicBoolean finished = new AtomicBoolean();
        IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                while (!finished.get()) {
                    stopGrid(3);
                    U.sleep(300);
                    startGrid(3);
                }
                return null;
            }
        });
        try {
            IgniteTransactions txs = ignite(0).transactions();
            IgniteCache<Object, Object> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
            long stopTime = System.currentTimeMillis() + 60_000;
            while (System.currentTimeMillis() < stopTime) {
                for (int i = 0; i < 10_000; i++) {
                    try {
                        try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            txCache.put(0, 0);
                            cache.put(i, i);
                            tx.commit();
                        }
                    } catch (IgniteException | CacheException e) {
                        log.info("Ignore exception: " + e);
                    }
                }
            }
            finished.set(true);
            fut.get();
        } finally {
            finished.set(true);
        }
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 38 with CacheException

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

the class IgniteFutureImplTest method testChainError.

/**
     * @throws Exception If failed.
     */
public void testChainError() throws Exception {
    {
        GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
        IgniteFutureImpl<String> fut = createFuture(fut0);
        final IgniteException err0 = new IgniteException("test error");
        final AtomicBoolean chainedPassed = new AtomicBoolean();
        IgniteFuture<Integer> chained = fut.chain(new C1<IgniteFuture<String>, Integer>() {

            @Override
            public Integer apply(IgniteFuture<String> fut) {
                try {
                    fut.get();
                    fail();
                    return -1;
                } catch (IgniteException | CacheException err) {
                    assertExpectedException(err, err0);
                    chainedPassed.set(true);
                    throw err;
                }
            }
        });
        final AtomicBoolean lsnrPassed = new AtomicBoolean();
        IgniteInClosure<? super IgniteFuture<Integer>> lsnr1 = new CI1<IgniteFuture<Integer>>() {

            @Override
            public void apply(IgniteFuture<Integer> fut) {
                try {
                    fut.get();
                    fail();
                } catch (IgniteException | CacheException err) {
                    assertExpectedException(err, err0);
                    lsnrPassed.set(true);
                }
            }
        };
        chained.listen(lsnr1);
        fut0.onDone(err0);
        assertTrue(chainedPassed.get());
        assertTrue(lsnrPassed.get());
        try {
            chained.get();
            fail();
        } catch (IgniteException | CacheException err) {
            assertExpectedException(err, err0);
        }
        try {
            fut.get();
            fail();
        } catch (IgniteException | CacheException err) {
            assertExpectedException(err, err0);
        }
    }
    {
        GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
        IgniteFutureImpl<String> fut = createFuture(fut0);
        final IgniteCheckedException err0 = new IgniteCheckedException("test error");
        final AtomicBoolean chainedPassed = new AtomicBoolean();
        IgniteFuture<Integer> chained = fut.chain(new C1<IgniteFuture<String>, Integer>() {

            @Override
            public Integer apply(IgniteFuture<String> fut) {
                try {
                    fut.get();
                    fail();
                    return -1;
                } catch (IgniteException | CacheException err) {
                    assertExpectedException(err, err0);
                    chainedPassed.set(true);
                    throw err;
                }
            }
        });
        final AtomicBoolean lsnrPassed = new AtomicBoolean();
        IgniteInClosure<? super IgniteFuture<Integer>> lsnr1 = new CI1<IgniteFuture<Integer>>() {

            @Override
            public void apply(IgniteFuture<Integer> fut) {
                try {
                    fut.get();
                    fail();
                } catch (IgniteException | CacheException err) {
                    assertExpectedException(err, err0);
                    lsnrPassed.set(true);
                }
            }
        };
        chained.listen(lsnr1);
        fut0.onDone(err0);
        assertTrue(chainedPassed.get());
        assertTrue(lsnrPassed.get());
        try {
            chained.get();
            fail();
        } catch (IgniteException | CacheException err) {
            assertExpectedException(err, err0);
        }
        try {
            fut.get();
            fail();
        } catch (IgniteException | CacheException err) {
            assertExpectedException(err, err0);
        }
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteFuture(org.apache.ignite.lang.IgniteFuture) C1(org.apache.ignite.internal.util.typedef.C1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure)

Example 39 with CacheException

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectCacheQueries.

/**
     * @throws Exception If failed.
     */
public void testReconnectCacheQueries() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            SqlQuery<Integer, Person> sqlQry = new SqlQuery<>(Person.class, "where id > 1");
            try {
                assertEquals(9999, cache.query(sqlQry).getAll().size());
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            try {
                SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select avg(p.id) from Person p");
                List<List<?>> res = cache.query(fieldsQry).getAll();
                assertEquals(1, res.size());
                Integer avg = (Integer) res.get(0).get(0);
                assertEquals(5_000, avg.intValue());
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            return null;
        }
    });
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List)

Example 40 with CacheException

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

the class IgniteClientReconnectQueriesTest method testReconnectQueryInProgress.

/**
     * @throws Exception If failed.
     */
public void testReconnectQueryInProgress() throws Exception {
    Ignite cln = grid(serverCount());
    assertTrue(cln.cluster().localNode().isClient());
    final Ignite srv = clientRouter(cln);
    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
    clnCache.put(1, new Person(1, "name1", "surname1"));
    clnCache.put(2, new Person(2, "name2", "surname2"));
    clnCache.put(3, new Person(3, "name3", "surname3"));
    blockMessage(GridQueryNextPageResponse.class);
    final SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "_key <> 0");
    qry.setPageSize(1);
    final QueryCursor<Cache.Entry<Integer, Person>> cur1 = clnCache.query(qry);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                cur1.getAll();
            } catch (CacheException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);
    assertNotDone(fut);
    unblockMessage();
    reconnectClientNode(cln, srv, null);
    assertTrue((Boolean) fut.get(2, SECONDS));
    QueryCursor<Cache.Entry<Integer, Person>> cur2 = clnCache.query(qry);
    assertEquals(3, cur2.getAll().size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) CacheException(javax.cache.CacheException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite)

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