Search in sources :

Example 46 with CacheException

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

the class GridH2IndexBase method toSearchRowMessage.

/**
     * @param row Search row.
     * @return Row message.
     */
private GridH2RowMessage toSearchRowMessage(SearchRow row) {
    if (row == null)
        return null;
    List<GridH2ValueMessage> vals = new ArrayList<>(indexColumns.length);
    for (IndexColumn idxCol : indexColumns) {
        Value val = row.getValue(idxCol.column.getColumnId());
        if (val == null)
            break;
        try {
            vals.add(GridH2ValueMessageFactory.toMessage(val));
        } 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) Value(org.h2.value.Value) GridH2RowMessage(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowMessage) IndexColumn(org.h2.table.IndexColumn)

Example 47 with CacheException

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

the class GridH2IndexBase method toSearchRow.

/**
     * @param msg Row message.
     * @return Search row.
     */
private SearchRow toSearchRow(GridH2RowMessage msg) {
    if (msg == null)
        return null;
    GridKernalContext ctx = kernalContext();
    Value[] vals = new Value[getTable().getColumns().length];
    assert vals.length > 0;
    List<GridH2ValueMessage> msgVals = msg.values();
    for (int i = 0; i < indexColumns.length; i++) {
        if (i >= msgVals.size())
            continue;
        try {
            vals[indexColumns[i].column.getColumnId()] = msgVals.get(i).value(ctx);
        } catch (IgniteCheckedException e) {
            throw new CacheException(e);
        }
    }
    return database.createRow(vals, MEMORY_CALCULATE);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) GridKernalContext(org.apache.ignite.internal.GridKernalContext) Value(org.h2.value.Value) GridH2ValueMessage(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessage)

Example 48 with CacheException

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

the class CacheGetInsideLockChangingTopologyTest method testMultithreaded.

/**
     * @throws Exception If failed.
     */
public void testMultithreaded() throws Exception {
    fail("https://issues.apache.org/jira/browse/IGNITE-2204");
    final AtomicBoolean finished = new AtomicBoolean();
    final int NEW_NODE = SRVS + CLIENTS;
    final AtomicInteger stopIdx = new AtomicInteger();
    IgniteInternalFuture<?> restartFut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            int idx = stopIdx.getAndIncrement();
            int node = NEW_NODE + idx;
            while (!finished.get()) {
                log.info("Start node: " + node);
                startGrid(node);
                U.sleep(300);
                log.info("Stop node: " + node);
                stopGrid(node);
            }
            return null;
        }
    }, 2, "stop-thread");
    try {
        final long stopTime = System.currentTimeMillis() + 60_000;
        final AtomicInteger idx = new AtomicInteger();
        final int KEYS = 100_000;
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                int node = idx.getAndIncrement() % (SRVS + CLIENTS);
                Ignite ignite = ignite(node);
                IgniteCache<Integer, Integer> txCache1 = ignite.cache(TX_CACHE1);
                IgniteCache<Integer, Integer> txCache2 = ignite.cache(TX_CACHE2);
                IgniteCache<Integer, Integer> atomicCache = ignite.cache(ATOMIC_CACHE);
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (U.currentTimeMillis() < stopTime) {
                    Integer lockKey = rnd.nextInt(KEYS, KEYS + 1000);
                    Lock lock = txCache1.lock(lockKey);
                    try {
                        lock.lock();
                        try {
                            executeGet(txCache1);
                            executeGet(txCache2);
                            executeGet(atomicCache);
                        } finally {
                            lock.unlock();
                        }
                        try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
                            txCache1.put(lockKey, lockKey);
                            executeGet(txCache1);
                            executeGet(txCache2);
                            executeGet(atomicCache);
                            tx.commit();
                        }
                    } catch (IgniteException | CacheException e) {
                        log.info("Error: " + e);
                    }
                }
                return null;
            }

            private void executeGet(IgniteCache<Integer, Integer> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < 100; i++) cache.get(rnd.nextInt(KEYS));
                Set<Integer> keys = new HashSet<>();
                for (int i = 0; i < 100; i++) {
                    keys.add(rnd.nextInt(KEYS));
                    if (keys.size() == 20) {
                        cache.getAll(keys);
                        keys.clear();
                    }
                }
                cache.getAll(keys);
            }
        }, 10, "test-thread");
        finished.set(true);
        restartFut.get();
    } finally {
        finished.set(true);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IgniteCache(org.apache.ignite.IgniteCache) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) Lock(java.util.concurrent.locks.Lock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 49 with CacheException

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

the class CacheAsyncOperationsTest method async2.

/**
     *
     * @param cache Cache.
     */
private void async2(IgniteCache<Integer, Integer> cache) {
    cache.put(1, 1);
    latch = new CountDownLatch(1);
    IgniteFuture<?> fut1 = cache.putAsync(0, 0);
    IgniteFuture<?> fut2 = cache.putAsync(0, 0);
    IgniteFuture<?> fut3 = cache.getAndPutAsync(1, 2);
    IgniteFuture<?> fut4 = cache.putAsync(0, 0);
    assertFalse(fut1.isDone());
    assertFalse(fut2.isDone());
    assertFalse(fut3.isDone());
    assertFalse(fut4.isDone());
    latch.countDown();
    try {
        fut1.get();
        fail();
    } catch (CacheException e) {
        log.info("Expected error: " + e);
    }
    try {
        fut2.get();
        fail();
    } catch (CacheException e) {
        log.info("Expected error: " + e);
    }
    assertEquals(1, fut3.get());
    try {
        fut4.get();
        fail();
    } catch (CacheException e) {
        log.info("Expected error: " + e);
    }
    assertNull(cache.get(0));
    assertEquals((Integer) 2, cache.get(1));
}
Also used : CacheException(javax.cache.CacheException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 50 with CacheException

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

the class GridCachePartitionedTopologyChangeSelfTest method testExplicitLocks.

/**
     * @throws Exception If failed.
     */
public void testExplicitLocks() throws Exception {
    try {
        startGridsMultiThreaded(2);
        IgniteKernal[] nodes = new IgniteKernal[] { (IgniteKernal) grid(0), (IgniteKernal) grid(1) };
        Collection<IgniteInternalFuture> futs = new ArrayList<>();
        final CountDownLatch startLatch = new CountDownLatch(1);
        for (final IgniteKernal node : nodes) {
            List<Integer> parts = partitions(node, PARTITION_PRIMARY);
            Map<Integer, Integer> keyMap = keysFor(node, parts);
            for (final Integer key : keyMap.values()) {
                futs.add(multithreadedAsync(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Lock lock = node.cache(DEFAULT_CACHE_NAME).lock(key);
                            lock.lock();
                            try {
                                info(">>> Acquired explicit lock for key: " + key);
                                startLatch.await();
                                info(">>> Acquiring explicit lock for key: " + key * 10);
                                Lock lock10 = node.cache(DEFAULT_CACHE_NAME).lock(key * 10);
                                lock10.lock();
                                try {
                                    info(">>> Releasing locks [key1=" + key + ", key2=" + key * 10 + ']');
                                } finally {
                                    lock10.unlock();
                                }
                            } finally {
                                lock.unlock();
                            }
                        } catch (CacheException e) {
                            info(">>> Failed to perform lock [key=" + key + ", e=" + e + ']');
                        } catch (InterruptedException ignored) {
                            info(">>> Interrupted while waiting for start latch.");
                            Thread.currentThread().interrupt();
                        }
                    }
                }, 1));
            }
        }
        IgniteInternalFuture<?> startFut = multithreadedAsync(new Runnable() {

            @Override
            public void run() {
                try {
                    startGrid(2);
                    info(">>> Started grid2.");
                } catch (Exception e) {
                    info(">>> Failed to start grid: " + e);
                }
            }
        }, 1);
        U.sleep(5000);
        assertFalse(startFut.isDone());
        info(">>> Waiting for all locks to be released.");
        startLatch.countDown();
        for (IgniteInternalFuture fut : futs) fut.get(1000);
        startFut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CountDownLatch(java.util.concurrent.CountDownLatch) CacheException(javax.cache.CacheException) Lock(java.util.concurrent.locks.Lock)

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