Search in sources :

Example 26 with CacheException

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

the class GridQueryProcessor method querySqlFieldsNoCache.

/**
     * Query SQL fields without strict dependency on concrete cache.
     *
     * @param qry Query.
     * @param keepBinary Keep binary flag.
     * @return Cursor.
     */
public FieldsQueryCursor<List<?>> querySqlFieldsNoCache(final SqlFieldsQuery qry, final boolean keepBinary) {
    checkxEnabled();
    validateSqlFieldsQuery(qry);
    if (qry.isLocal())
        throw new IgniteException("Local query is not supported without specific cache.");
    if (qry.getSchema() == null)
        qry.setSchema(QueryUtils.DFLT_SCHEMA);
    if (!busyLock.enterBusy())
        throw new IllegalStateException("Failed to execute query (grid is stopping).");
    try {
        IgniteOutClosureX<FieldsQueryCursor<List<?>>> clo = new IgniteOutClosureX<FieldsQueryCursor<List<?>>>() {

            @Override
            public FieldsQueryCursor<List<?>> applyx() throws IgniteCheckedException {
                GridQueryCancel cancel = new GridQueryCancel();
                return idx.queryDistributedSqlFields(qry.getSchema(), qry, keepBinary, cancel, null);
            }
        };
        return executeQuery(GridCacheQueryType.SQL_FIELDS, qry.getSql(), null, clo, true);
    } catch (IgniteCheckedException e) {
        throw new CacheException(e);
    } finally {
        busyLock.leaveBusy();
    }
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 27 with CacheException

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

the class PlatformUtils method unwrapQueryException.

/**
     * Unwrap query exception.
     *
     * @param err Initial error.
     * @return Unwrapped error.
     */
public static IgniteCheckedException unwrapQueryException(Throwable err) {
    assert err != null;
    Throwable parent = err;
    Throwable child = parent.getCause();
    while (true) {
        if (child == null || child == parent)
            break;
        if (child instanceof IgniteException || child instanceof IgniteCheckedException || child instanceof CacheException) {
            // Continue unwrapping.
            parent = child;
            child = parent.getCause();
            continue;
        }
        break;
    }
    // Specific exception found, but detailed message doesn't exist. Just pass exception name then.
    if (parent.getMessage() == null)
        return new IgniteCheckedException("Query execution failed due to exception: " + parent.getClass().getName(), err);
    return new IgniteCheckedException(parent.getMessage(), err);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException)

Example 28 with CacheException

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

the class IgniteCachePutAllRestartTest method testStopNode.

/**
     * @throws Exception If failed.
     */
public void testStopNode() throws Exception {
    startGrids(NODES);
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Thread.currentThread().setName("put-thread");
            IgniteCache<Integer, Integer> cache = ignite(0).cache(CACHE_NAME);
            Random rnd = new Random();
            int iter = 0;
            while (!stop.get()) {
                Map<Integer, Integer> map = new HashMap<>();
                for (int i = 0; i < 10; i++) map.put(rnd.nextInt(1000), i);
                try {
                    cache.putAll(map);
                } catch (CacheException e) {
                    log.info("Update failed: " + e);
                }
                iter++;
                if (iter % 1000 == 0)
                    log.info("Iteration: " + iter);
            }
            return null;
        }
    });
    try {
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        long endTime = System.currentTimeMillis() + 2 * 60_000;
        while (System.currentTimeMillis() < endTime) {
            int node = rnd.nextInt(1, NODES);
            stopGrid(node);
            startGrid(node);
        }
    } finally {
        stop.set(true);
    }
    fut.get();
}
Also used : CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) CacheException(javax.cache.CacheException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with CacheException

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

the class IgniteCachePutAllRestartTest method testStopOriginatingNode.

/**
     * @throws Exception If failed.
     */
public void testStopOriginatingNode() throws Exception {
    startGrids(NODES);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    long endTime = System.currentTimeMillis() + 2 * 60_000;
    while (System.currentTimeMillis() < endTime) {
        int node = rnd.nextInt(0, NODES);
        final Ignite ignite = ignite(node);
        info("Running iteration on the node [idx=" + node + ", nodeId=" + ignite.cluster().localNode().id() + ']');
        final IgniteCache<Integer, Integer> cache = ignite.cache(CACHE_NAME);
        IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Thread.currentThread().setName("put-thread");
                Random rnd = new Random();
                long endTime = System.currentTimeMillis() + 60_000;
                try {
                    int iter = 0;
                    while (System.currentTimeMillis() < endTime) {
                        Map<Integer, Integer> map = new HashMap<>();
                        for (int i = 0; i < 10; i++) map.put(rnd.nextInt(1000), i);
                        cache.putAll(map);
                        iter++;
                        log.info("Iteration: " + iter);
                    }
                    fail("Should fail.");
                } catch (CacheException | IllegalStateException e) {
                    log.info("Expected error: " + e);
                }
                return null;
            }
        });
        ignite.close();
        fut.get();
        startGrid(node);
    }
}
Also used : CacheException(javax.cache.CacheException) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with CacheException

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

the class CacheContinuousQueryFailoverAbstractSelfTest method checkBackupQueue.

/**
     * @param backups Number of backups.
     * @param updateFromClient If {@code true} executes cache update from client node.
     * @throws Exception If failed.
     */
private void checkBackupQueue(int backups, boolean updateFromClient) throws Exception {
    this.backups = atomicityMode() == CacheAtomicityMode.ATOMIC ? backups : backups < 2 ? 2 : backups;
    final int SRV_NODES = 4;
    startGridsMultiThreaded(SRV_NODES);
    client = true;
    Ignite qryClient = startGrid(SRV_NODES);
    client = false;
    IgniteCache<Object, Object> qryClientCache = qryClient.cache(DEFAULT_CACHE_NAME);
    Affinity<Object> aff = qryClient.affinity(DEFAULT_CACHE_NAME);
    CacheEventListener1 lsnr = asyncCallback() ? new CacheEventAsyncListener1(false) : new CacheEventListener1(false);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = qryClientCache.query(qry);
    int PARTS = 10;
    Map<Object, T2<Object, Object>> updates = new HashMap<>();
    List<T3<Object, Object, Object>> expEvts = new ArrayList<>();
    for (int i = 0; i < (atomicityMode() == CacheAtomicityMode.ATOMIC ? SRV_NODES - 1 : SRV_NODES - 2); i++) {
        log.info("Stop iteration: " + i);
        TestCommunicationSpi spi = (TestCommunicationSpi) ignite(i).configuration().getCommunicationSpi();
        Ignite ignite = ignite(i);
        IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
        List<Integer> keys = testKeys(cache, PARTS);
        CountDownLatch latch = new CountDownLatch(keys.size());
        lsnr.latch = latch;
        boolean first = true;
        for (Integer key : keys) {
            log.info("Put [node=" + ignite.name() + ", key=" + key + ", part=" + aff.partition(key) + ']');
            T2<Object, Object> t = updates.get(key);
            if (updateFromClient) {
                if (atomicityMode() == CacheAtomicityMode.TRANSACTIONAL) {
                    try (Transaction tx = qryClient.transactions().txStart()) {
                        qryClientCache.put(key, key);
                        tx.commit();
                    } catch (CacheException | ClusterTopologyException ignored) {
                        log.warning("Failed put. [Key=" + key + ", val=" + key + "]");
                        continue;
                    }
                } else
                    qryClientCache.put(key, key);
            } else {
                if (atomicityMode() == CacheAtomicityMode.TRANSACTIONAL) {
                    try (Transaction tx = ignite.transactions().txStart()) {
                        cache.put(key, key);
                        tx.commit();
                    } catch (CacheException | ClusterTopologyException ignored) {
                        log.warning("Failed put. [Key=" + key + ", val=" + key + "]");
                        continue;
                    }
                } else
                    cache.put(key, key);
            }
            if (t == null) {
                updates.put(key, new T2<>((Object) key, null));
                expEvts.add(new T3<>((Object) key, (Object) key, null));
            } else {
                updates.put(key, new T2<>((Object) key, (Object) key));
                expEvts.add(new T3<>((Object) key, (Object) key, (Object) key));
            }
            if (first) {
                spi.skipMsg = true;
                first = false;
            }
        }
        stopGrid(i);
        if (!latch.await(5, SECONDS)) {
            Set<Integer> keys0 = new HashSet<>(keys);
            keys0.removeAll(lsnr.keys);
            log.info("Missed events for keys: " + keys0);
            fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
        }
        checkEvents(expEvts, lsnr);
    }
    for (int i = 0; i < (atomicityMode() == CacheAtomicityMode.ATOMIC ? SRV_NODES - 1 : SRV_NODES - 2); i++) {
        log.info("Start iteration: " + i);
        Ignite ignite = startGrid(i);
        IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
        List<Integer> keys = testKeys(cache, PARTS);
        CountDownLatch latch = new CountDownLatch(keys.size());
        lsnr.latch = latch;
        for (Integer key : keys) {
            log.info("Put [node=" + ignite.name() + ", key=" + key + ", part=" + aff.partition(key) + ']');
            T2<Object, Object> t = updates.get(key);
            if (t == null) {
                updates.put(key, new T2<>((Object) key, null));
                expEvts.add(new T3<>((Object) key, (Object) key, null));
            } else {
                updates.put(key, new T2<>((Object) key, (Object) key));
                expEvts.add(new T3<>((Object) key, (Object) key, (Object) key));
            }
            if (updateFromClient)
                qryClientCache.put(key, key);
            else
                cache.put(key, key);
        }
        if (!latch.await(10, SECONDS)) {
            Set<Integer> keys0 = new HashSet<>(keys);
            keys0.removeAll(lsnr.keys);
            log.info("Missed events for keys: " + keys0);
            fail("Failed to wait for notifications [exp=" + keys.size() + ", left=" + lsnr.latch.getCount() + ']');
        }
        checkEvents(expEvts, lsnr);
    }
    cur.close();
    assertFalse("Unexpected error during test, see log for details.", err);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CacheException(javax.cache.CacheException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) T2(org.apache.ignite.internal.util.typedef.T2) T3(org.apache.ignite.internal.util.typedef.T3) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException)

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