Search in sources :

Example 6 with TransactionSerializationException

use of org.apache.ignite.transactions.TransactionSerializationException in project ignite by apache.

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatePartition.

/**
 * @param atomicityMode Cache atomicity mode.
 * @param cacheGrp {@code True} if test cache multiple caches in the same group.
 * @throws Exception If failed.
 */
private void concurrentUpdatePartition(CacheAtomicityMode atomicityMode, boolean cacheGrp) throws Exception {
    Ignite srv = startGrid(0);
    Ignite client = startClientGrid(1);
    List<AtomicInteger> cntrs = new ArrayList<>();
    List<String> caches = new ArrayList<>();
    if (cacheGrp) {
        for (int i = 0; i < 3; i++) {
            CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME + i);
            ccfg.setGroupName("testGroup");
            ccfg.setWriteSynchronizationMode(FULL_SYNC);
            ccfg.setAtomicityMode(atomicityMode);
            IgniteCache<Object, Object> cache = client.createCache(ccfg);
            caches.add(cache.getName());
            cntrs.add(startListener(cache).get1());
        }
    } else {
        CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
        ccfg.setWriteSynchronizationMode(FULL_SYNC);
        ccfg.setAtomicityMode(atomicityMode);
        IgniteCache<Object, Object> cache = client.createCache(ccfg);
        caches.add(cache.getName());
        cntrs.add(startListener(cache).get1());
    }
    Affinity<Integer> aff = srv.affinity(caches.get(0));
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = SF.applyLB(10, 4);
    final int UPDATES = SF.applyLB(1000, 100);
    final List<IgniteCache<Object, Object>> srvCaches = new ArrayList<>();
    for (String cacheName : caches) srvCaches.add(srv.cache(cacheName));
    for (int i = 0; i < SF.applyLB(15, 5); i++) {
        log.info("Iteration: " + i);
        GridTestUtils.runMultiThreaded(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) {
                    for (int c = 0; c < srvCaches.size(); c++) {
                        if (atomicityMode == ATOMIC)
                            srvCaches.get(c).put(keys.get(rnd.nextInt(KEYS)), i);
                        else {
                            IgniteCache<Object, Object> cache0 = srvCaches.get(c);
                            IgniteTransactions txs = cache0.unwrap(Ignite.class).transactions();
                            boolean committed = false;
                            while (!committed) {
                                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                    cache0.put(keys.get(rnd.nextInt(KEYS)), i);
                                    tx.commit();
                                    committed = true;
                                } catch (CacheException e) {
                                    assertTrue(e.getCause() instanceof TransactionSerializationException);
                                    assertEquals(atomicityMode, TRANSACTIONAL_SNAPSHOT);
                                }
                            }
                        }
                    }
                }
                return null;
            }
        }, THREADS, "update");
        for (final AtomicInteger evtCnt : cntrs) {
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    log.info("Events: " + evtCnt.get());
                    return evtCnt.get() >= THREADS * UPDATES;
                }
            }, 5000);
            assertEquals(THREADS * UPDATES, evtCnt.get());
            evtCnt.set(0);
        }
    }
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) CacheException(javax.cache.CacheException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 7 with TransactionSerializationException

use of org.apache.ignite.transactions.TransactionSerializationException in project ignite by apache.

the class CacheContinuousQueryOperationFromCallbackTest method doTest.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
@SuppressWarnings({ "TypeMayBeWeakened", "unchecked", "TooBroadScope" })
protected void doTest(final CacheConfiguration ccfg, boolean fromLsnr) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    assertEquals(0, filterCbCntr.get());
    try {
        List<Set<T2<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(NODES);
        List<Set<T2<QueryTestKey, QueryTestValue>>> evtsFromCallbacks = new ArrayList<>(NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final AtomicInteger cbCntr = new AtomicInteger(0);
        final int threadCnt = SYSTEM_POOL_SIZE * 2;
        for (int idx = 0; idx < NODES; idx++) {
            Set<T2<QueryTestKey, QueryTestValue>> evts = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            Set<T2<QueryTestKey, QueryTestValue>> evtsFromCb = Collections.newSetFromMap(new ConcurrentHashMap<T2<QueryTestKey, QueryTestValue>, Boolean>());
            IgniteCache<Object, Object> cache = grid(idx).getOrCreateCache(ccfg.getName());
            ContinuousQuery qry = new ContinuousQuery();
            qry.setLocalListener(new TestCacheAsyncEventListener(evts, evtsFromCb, fromLsnr ? cache : null, qryCntr, cbCntr));
            if (!fromLsnr)
                qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
            rcvdEvts.add(evts);
            evtsFromCallbacks.add(evtsFromCb);
            QueryCursor qryCursor = cache.query(qry);
            qries.add(qryCursor);
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS) - KEYS);
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() != ATOMIC && rnd.nextBoolean();
                    Transaction tx = null;
                    boolean committed = false;
                    while (!committed && !Thread.currentThread().isInterrupted()) {
                        try {
                            if (startTx)
                                tx = cache.unwrap(Ignite.class).transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
                            if ((cache.get(key) == null) || rnd.nextBoolean())
                                cache.invoke(key, new IncrementTestEntryProcessor());
                            else {
                                QueryTestValue val;
                                QueryTestValue newVal;
                                do {
                                    val = cache.get(key);
                                    newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                                } while (!cache.replace(key, val, newVal));
                            }
                            if (tx != null)
                                tx.commit();
                            committed = true;
                        } catch (Exception e) {
                            assertTrue(e.getCause() instanceof TransactionSerializationException);
                            assertEquals(ccfg.getAtomicityMode(), TRANSACTIONAL_SNAPSHOT);
                        } finally {
                            if (tx != null)
                                tx.close();
                        }
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(30, TimeUnit.SECONDS);
        assert GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * NODES;
            }
        }, getTestTimeout());
        for (Set<T2<QueryTestKey, QueryTestValue>> set : rcvdEvts) checkEvents(set, ITERATION_CNT * threadCnt, grid(0).cache(ccfg.getName()), false);
        if (fromLsnr) {
            final int expCnt = qryCntr.get() * NODES * KEYS_FROM_CALLBACK;
            boolean res = GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return cbCntr.get() >= expCnt;
                }
            }, getTestTimeout());
            assertTrue("Failed to wait events [exp=" + expCnt + ", act=" + cbCntr.get() + "]", res);
            assertEquals(expCnt, cbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, qryCntr.get() * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        } else {
            final int expInvkCnt = ITERATION_CNT * threadCnt * (ccfg.getCacheMode() != REPLICATED ? (ccfg.getBackups() + 1) : NODES - 1) * NODES;
            GridTestUtils.waitForCondition(new PA() {

                @Override
                public boolean apply() {
                    return filterCbCntr.get() >= expInvkCnt;
                }
            }, getTestTimeout());
            assertEquals(expInvkCnt, filterCbCntr.get());
            for (Set<T2<QueryTestKey, QueryTestValue>> set : evtsFromCallbacks) checkEvents(set, expInvkCnt * KEYS_FROM_CALLBACK, grid(0).cache(ccfg.getName()), true);
        }
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) QueryCursor(org.apache.ignite.cache.query.QueryCursor) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCache(org.apache.ignite.IgniteCache) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) EntryProcessorException(javax.cache.processor.EntryProcessorException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 8 with TransactionSerializationException

use of org.apache.ignite.transactions.TransactionSerializationException in project ignite by apache.

the class IgniteUtils method exceptionConverters.

/**
 * Gets map with converters to convert internal checked exceptions to public API unchecked exceptions.
 *
 * @return Exception converters.
 */
private static Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> exceptionConverters() {
    Map<Class<? extends IgniteCheckedException>, C1<IgniteCheckedException, IgniteException>> m = new HashMap<>();
    m.put(IgniteInterruptedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteInterruptedException(e.getMessage(), (InterruptedException) e.getCause());
        }
    });
    m.put(IgniteFutureCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteFutureCancelledException(e.getMessage(), e);
        }
    });
    m.put(IgniteFutureTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteFutureTimeoutException(e.getMessage(), e);
        }
    });
    m.put(ClusterGroupEmptyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ClusterGroupEmptyException(e.getMessage(), e);
        }
    });
    m.put(ClusterTopologyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            ClusterTopologyException topEx = new ClusterTopologyException(e.getMessage(), e);
            ClusterTopologyCheckedException checked = (ClusterTopologyCheckedException) e;
            if (checked.retryReadyFuture() != null)
                topEx.retryReadyFuture(new IgniteFutureImpl<>(checked.retryReadyFuture()));
            return topEx;
        }
    });
    m.put(IgniteDeploymentCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteDeploymentException(e.getMessage(), e);
        }
    });
    m.put(ComputeTaskTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ComputeTaskTimeoutException(e.getMessage(), e);
        }
    });
    m.put(ComputeTaskCancelledCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new ComputeTaskCancelledException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxRollbackCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionRollbackException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxHeuristicCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionHeuristicException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            if (e.getCause() instanceof TransactionDeadlockException)
                return new TransactionTimeoutException(e.getMessage(), e.getCause());
            return new TransactionTimeoutException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxOptimisticCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionOptimisticException(e.getMessage(), e);
        }
    });
    m.put(IgniteClientDisconnectedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new IgniteClientDisconnectedException(((IgniteClientDisconnectedCheckedException) e).reconnectFuture(), e.getMessage(), e);
        }
    });
    m.put(IgniteTxSerializationCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionSerializationException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxDuplicateKeyCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionDuplicateKeyException(e.getMessage(), e);
        }
    });
    m.put(IgniteTxAlreadyCompletedCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {

        @Override
        public IgniteException apply(IgniteCheckedException e) {
            return new TransactionAlreadyCompletedException(e.getMessage(), e);
        }
    });
    return m;
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IgniteDeploymentException(org.apache.ignite.IgniteDeploymentException) ClusterGroupEmptyException(org.apache.ignite.cluster.ClusterGroupEmptyException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) TransactionHeuristicException(org.apache.ignite.transactions.TransactionHeuristicException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) C1(org.apache.ignite.internal.util.typedef.C1) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ComputeTaskTimeoutException(org.apache.ignite.compute.ComputeTaskTimeoutException) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) ComputeTaskCancelledException(org.apache.ignite.compute.ComputeTaskCancelledException) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 9 with TransactionSerializationException

use of org.apache.ignite.transactions.TransactionSerializationException in project ignite by apache.

the class CacheContinuousQueryFailoverAbstractSelfTest method testMultiThreadedFailover.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultiThreadedFailover() throws Exception {
    this.backups = 2;
    final int SRV_NODES = 4;
    startGridsMultiThreaded(SRV_NODES);
    final Ignite qryCln = startClientGrid(SRV_NODES);
    final IgniteCache<Object, Object> qryClnCache = qryCln.cache(DEFAULT_CACHE_NAME);
    final CacheEventListener2 lsnr = asyncCallback() ? new CacheEventAsyncListener2() : new CacheEventListener2();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = qryClnCache.query(qry);
    final AtomicBoolean stop = new AtomicBoolean();
    final int THREAD = 4;
    final int PARTS = THREAD;
    final List<List<T3<Object, Object, Object>>> expEvts = new ArrayList<>(THREAD + 5);
    for (int i = 0; i < THREAD; i++) expEvts.add(i, new ArrayList<T3<Object, Object, Object>>());
    final AtomicReference<CyclicBarrier> checkBarrier = new AtomicReference<>();
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    IgniteInternalFuture<?> restartFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                while (!stop.get() && !err) {
                    final int idx = rnd.nextInt(SRV_NODES);
                    log.info("Stop node: " + idx);
                    stopGrid(idx);
                    Thread.sleep(300);
                    GridTestUtils.waitForCondition(new PA() {

                        @Override
                        public boolean apply() {
                            return qryCln.cluster().nodes().size() == SRV_NODES;
                        }
                    }, 5000L);
                    try {
                        log.info("Start node: " + idx);
                        startGrid(idx);
                        Thread.sleep(300);
                        GridTestUtils.waitForCondition(new PA() {

                            @Override
                            public boolean apply() {
                                return qryCln.cluster().nodes().size() == SRV_NODES + 1;
                            }
                        }, 5000L);
                    } catch (Exception e) {
                        log.warning("Failed to stop nodes.", e);
                    }
                    CyclicBarrier bar = new CyclicBarrier(THREAD + 1, /* plus start/stop thread */
                    new Runnable() {

                        @Override
                        public void run() {
                            try {
                                int size0 = 0;
                                for (List<T3<Object, Object, Object>> evt : expEvts) size0 += evt.size();
                                final int size = size0;
                                GridTestUtils.waitForCondition(new PA() {

                                    @Override
                                    public boolean apply() {
                                        return lsnr.size() >= size;
                                    }
                                }, 10_000L);
                                List<T3<Object, Object, Object>> expEvts0 = new ArrayList<>();
                                for (List<T3<Object, Object, Object>> evt : expEvts) expEvts0.addAll(evt);
                                checkEvents(expEvts0, lsnr, false, false);
                                for (List<T3<Object, Object, Object>> evt : expEvts) evt.clear();
                            } catch (Exception e) {
                                log.error("Failed.", e);
                                err = true;
                                stop.set(true);
                            } finally {
                                checkBarrier.set(null);
                            }
                        }
                    });
                    assertTrue(checkBarrier.compareAndSet(null, bar));
                    if (!stop.get() && !err)
                        bar.await(1, MINUTES);
                }
            } catch (Throwable e) {
                log.error("Unexpected error: " + e, e);
                err = true;
                throw e;
            }
            return null;
        }
    });
    final long stopTime = System.currentTimeMillis() + 60_000;
    final AtomicInteger valCntr = new AtomicInteger(0);
    final AtomicInteger threadSeq = new AtomicInteger(0);
    GridTestUtils.runMultiThreaded(new Runnable() {

        @Override
        public void run() {
            try {
                final ThreadLocalRandom rnd = ThreadLocalRandom.current();
                final int threadId = threadSeq.getAndIncrement();
                log.error("Thread id: " + threadId);
                while (System.currentTimeMillis() < stopTime && !stop.get() && !err) {
                    Integer key = rnd.nextInt(PARTS);
                    Integer val = valCntr.incrementAndGet();
                    Integer prevVal = null;
                    boolean updated = false;
                    while (!updated) {
                        try {
                            prevVal = (Integer) qryClnCache.getAndPut(key, val);
                            updated = true;
                        } catch (CacheException e) {
                            assertTrue(e.getCause() instanceof TransactionSerializationException);
                            assertSame(atomicityMode(), CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT);
                        }
                    }
                    expEvts.get(threadId).add(new T3<>((Object) key, (Object) val, (Object) prevVal));
                    CyclicBarrier bar = checkBarrier.get();
                    if (bar != null)
                        bar.await(1, MINUTES);
                }
            } catch (Exception e) {
                log.error("Failed.", e);
                err = true;
                stop.set(true);
            } finally {
                stop.set(true);
            }
        }
    }, THREAD, "update-thread");
    restartFut.get();
    List<T3<Object, Object, Object>> expEvts0 = new ArrayList<>();
    for (List<T3<Object, Object, Object>> evt : expEvts) {
        expEvts0.addAll(evt);
        evt.clear();
    }
    if (!expEvts0.isEmpty())
        checkEvents(expEvts0, lsnr, true);
    cur.close();
    assertFalse("Unexpected error during test, see log for details.", err);
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) T3(org.apache.ignite.internal.util.typedef.T3) AtomicReference(java.util.concurrent.atomic.AtomicReference) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) CacheException(javax.cache.CacheException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PA(org.apache.ignite.internal.util.typedef.PA) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 10 with TransactionSerializationException

use of org.apache.ignite.transactions.TransactionSerializationException in project ignite by apache.

the class CacheContinuousQueryOrderingEventTest method doOrderingTest.

/**
 * @param ccfg Cache configuration.
 * @param async Async filter.
 * @throws Exception If failed.
 */
protected void doOrderingTest(final CacheConfiguration ccfg, final boolean async) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    try {
        List<BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(LISTENER_CNT * NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final int threadCnt = 20;
        for (int idx = 0; idx < NODES; idx++) {
            for (int i = 0; i < LISTENER_CNT; i++) {
                BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue = new ArrayBlockingQueue<>(ITERATION_CNT * threadCnt);
                ContinuousQuery qry = new ContinuousQuery();
                if (async) {
                    qry.setLocalListener(new TestCacheAsyncEventListener(queue, qryCntr));
                    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
                } else {
                    qry.setLocalListener(new TestCacheEventListener(queue, qryCntr));
                    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilter(ccfg.getName())));
                }
                rcvdEvts.add(queue);
                IgniteCache<Object, Object> cache = grid(idx).cache(ccfg.getName());
                QueryCursor qryCursor = cache.query(qry);
                qries.add(qryCursor);
            }
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
                    boolean startTx = atomicityMode(cache) != ATOMIC && rnd.nextBoolean();
                    Transaction tx = null;
                    boolean committed = false;
                    while (!committed && !Thread.currentThread().isInterrupted()) {
                        try {
                            if (startTx)
                                tx = cache.unwrap(Ignite.class).transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
                            if ((cache.get(key) == null) || rnd.nextBoolean()) {
                                cache.invoke(key, new CacheEntryProcessor<QueryTestKey, QueryTestValue, Object>() {

                                    @Override
                                    public Object process(MutableEntry<QueryTestKey, QueryTestValue> entry, Object... arguments) throws EntryProcessorException {
                                        if (entry.exists())
                                            entry.setValue(new QueryTestValue(entry.getValue().val1 + 1));
                                        else
                                            entry.setValue(new QueryTestValue(0));
                                        return null;
                                    }
                                });
                            } else {
                                QueryTestValue val;
                                QueryTestValue newVal;
                                do {
                                    val = cache.get(key);
                                    newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                                } while (!cache.replace(key, val, newVal));
                            }
                            if (tx != null)
                                tx.commit();
                            committed = true;
                        } catch (CacheException e) {
                            assertTrue(e.getCause() instanceof TransactionSerializationException);
                            assertEquals(atomicityMode(cache), TRANSACTIONAL_SNAPSHOT);
                        } finally {
                            if (tx != null)
                                tx.close();
                        }
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(15, TimeUnit.SECONDS);
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * LISTENER_CNT * NODES;
            }
        }, 1000L);
        for (BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue : rcvdEvts) checkEvents(queue, ITERATION_CNT * threadCnt);
        assertFalse("Ordering invocations of filter broken.", fail);
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Aggregations

TransactionSerializationException (org.apache.ignite.transactions.TransactionSerializationException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Ignite (org.apache.ignite.Ignite)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)7 CacheException (javax.cache.CacheException)7 IgniteCache (org.apache.ignite.IgniteCache)7 ArrayList (java.util.ArrayList)6 EntryProcessorException (javax.cache.processor.EntryProcessorException)5 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)5 Transaction (org.apache.ignite.transactions.Transaction)5 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteException (org.apache.ignite.IgniteException)4 QueryCursor (org.apache.ignite.cache.query.QueryCursor)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ClusterTopologyException (org.apache.ignite.cluster.ClusterTopologyException)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2