Search in sources :

Example 1 with TransactionSerializationException

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

the class IgniteClientCacheStartFailoverTest method testRebalanceStateConcurrentStart.

/**
 * @throws Exception If failed.
 */
@Test
public void testRebalanceStateConcurrentStart() throws Exception {
    final int SRVS1 = 3;
    final int CLIENTS = 5;
    final int SRVS2 = 5;
    startGrids(SRVS1);
    Ignite srv0 = ignite(0);
    final int KEYS = 1000;
    final List<String> cacheNames = startCaches(srv0, KEYS);
    final List<Ignite> clients = new ArrayList<>();
    for (int i = 0; i < CLIENTS; i++) clients.add(startClientGrid(SRVS1 + i));
    final CyclicBarrier barrier = new CyclicBarrier(clients.size() + SRVS2);
    final AtomicInteger clientIdx = new AtomicInteger();
    final Set<Integer> keys = new HashSet<>();
    for (int i = 0; i < KEYS; i++) keys.add(i);
    IgniteInternalFuture<?> fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            barrier.await();
            Ignite client = clients.get(clientIdx.getAndIncrement());
            for (String cacheName : cacheNames) client.cache(cacheName);
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 10; i++) {
                for (String cacheName : cacheNames) {
                    IgniteCache<Object, Object> cache = client.cache(cacheName);
                    Map<Object, Object> map0 = cache.getAll(keys);
                    assertEquals("[cache=" + cacheName + ", expected=" + KEYS + ", actual=" + map0.size() + ']', KEYS, map0.size());
                    int key = rnd.nextInt(KEYS);
                    try {
                        cache.put(key, i);
                    } catch (CacheException e) {
                        log.error("It couldn't put a value [cache=" + cacheName + ", key=" + key + ", val=" + i + ']', e);
                        CacheConfiguration ccfg = cache.getConfiguration(CacheConfiguration.class);
                        TransactionSerializationException txEx = X.cause(e, TransactionSerializationException.class);
                        boolean notContains = !txEx.getMessage().contains("Cannot serialize transaction due to write conflict (transaction is marked for rollback)");
                        if (txEx == null || ccfg.getAtomicityMode() != TRANSACTIONAL_SNAPSHOT || notContains)
                            fail("Assert violated because exception was thrown [e=" + e.getMessage() + ']');
                    }
                }
            }
            return null;
        }
    }, clients.size(), "client-cache-start");
    final AtomicInteger srvIdx = new AtomicInteger(SRVS1 + CLIENTS);
    IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            barrier.await();
            startGrid(srvIdx.incrementAndGet());
            return null;
        }
    }, SRVS2, "node-start");
    fut1.get();
    fut2.get();
    final AffinityTopologyVersion topVer = new AffinityTopologyVersion(SRVS1 + SRVS2 + CLIENTS, 1);
    for (Ignite client : clients) {
        for (String cacheName : cacheNames) {
            final GridDhtPartitionTopology top = ((IgniteKernal) client).context().cache().internalCache(cacheName).context().topology();
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    return top.rebalanceFinished(topVer);
                }
            }, 5000);
            assertTrue(top.rebalanceFinished(topVer));
        }
    }
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) CacheException(javax.cache.CacheException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TreeMap(java.util.TreeMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 2 with TransactionSerializationException

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

the class CacheMvccSqlTxQueriesAbstractTest method testQueryInsertUpdateMultithread.

/**
 * @throws Exception If failed.
 */
@Test
public void testQueryInsertUpdateMultithread() throws Exception {
    ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT).setIndexedTypes(Integer.class, Integer.class);
    startGridsMultiThreaded(2);
    final Phaser phaser = new Phaser(2);
    final AtomicReference<Exception> ex = new AtomicReference<>();
    GridCompoundFuture fut = new GridCompoundFuture();
    fut.add(multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            IgniteEx node = grid(0);
            try {
                while (true) {
                    try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        tx.timeout(TX_TIMEOUT);
                        IgniteCache<Object, Object> cache0 = node.cache(DEFAULT_CACHE_NAME);
                        SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (1,1),(2,2),(3,3)");
                        try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
                            cur.getAll();
                        }
                        awaitPhase(phaser, 2);
                        qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (4,4),(5,5),(6,6)");
                        try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) {
                            cur.getAll();
                        }
                        tx.commit();
                        break;
                    } catch (CacheException e) {
                        MvccFeatureChecker.assertMvccWriteConflict(e);
                    }
                }
            } catch (Exception e) {
                onException(ex, e);
            }
        }
    }, 1));
    fut.add(multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            IgniteEx node = grid(1);
            try {
                phaser.arriveAndAwaitAdvance();
                while (true) {
                    try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        tx.timeout(TX_TIMEOUT);
                        IgniteCache<Integer, Integer> cache0 = node.cache(DEFAULT_CACHE_NAME);
                        cache0.invokeAllAsync(F.asSet(1, 2, 3, 4, 5, 6), new EntryProcessor<Integer, Integer, Void>() {

                            @Override
                            public Void process(MutableEntry<Integer, Integer> entry, Object... arguments) throws EntryProcessorException {
                                entry.setValue(entry.getValue() * 10);
                                return null;
                            }
                        });
                        phaser.arrive();
                        tx.commit();
                        break;
                    } catch (Exception e) {
                        assertTrue(e instanceof TransactionSerializationException);
                    }
                }
            } catch (Exception e) {
                onException(ex, e);
            }
        }
    }, 1));
    try {
        fut.markInitialized();
        fut.get(TX_TIMEOUT);
    } catch (IgniteCheckedException e) {
        onException(ex, e);
    } finally {
        phaser.forceTermination();
    }
    Exception ex0 = ex.get();
    if (ex0 != null)
        throw ex0;
    IgniteCache cache = grid(0).cache(DEFAULT_CACHE_NAME);
    assertEquals(10, cache.get(1));
    assertEquals(20, cache.get(2));
    assertEquals(30, cache.get(3));
    assertEquals(40, cache.get(4));
    assertEquals(50, cache.get(5));
    assertEquals(60, cache.get(6));
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteEx(org.apache.ignite.internal.IgniteEx) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) List(java.util.List) Phaser(java.util.concurrent.Phaser) Test(org.junit.Test)

Example 3 with TransactionSerializationException

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testMultiThreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultiThreaded() throws Exception {
    this.backups = 2;
    final int SRV_NODES = 3;
    startGridsMultiThreaded(SRV_NODES);
    Ignite qryClient = startClientGrid(SRV_NODES);
    final IgniteCache<Object, Object> cache = qryClient.cache(DEFAULT_CACHE_NAME);
    CacheEventListener1 lsnr = new CacheEventListener1(true);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = cache.query(qry);
    final int SRV_IDX = SRV_NODES - 1;
    List<Integer> keys = primaryKeys(ignite(SRV_IDX).cache(DEFAULT_CACHE_NAME), 10);
    final int THREADS = 10;
    for (int i = 0; i < keys.size(); i++) {
        log.info("Iteration: " + i);
        Ignite srv = ignite(SRV_IDX);
        TestCommunicationSpi spi = (TestCommunicationSpi) srv.configuration().getCommunicationSpi();
        spi.sndFirstOnly = new AtomicBoolean(false);
        final Integer key = keys.get(i);
        final AtomicInteger val = new AtomicInteger();
        CountDownLatch latch = new CountDownLatch(THREADS);
        lsnr.latch = latch;
        IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Integer val0 = val.getAndIncrement();
                boolean updated = false;
                while (!updated) {
                    try {
                        cache.put(key, val0);
                        updated = true;
                    } catch (CacheException e) {
                        assertTrue(e.getCause() instanceof TransactionSerializationException);
                        assertSame(atomicityMode(), CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT);
                    }
                }
                return null;
            }
        }, THREADS, "update-thread");
        fut.get();
        stopGrid(SRV_IDX);
        if (!latch.await(5, SECONDS))
            fail("Failed to wait for notifications [exp=" + THREADS + ", left=" + lsnr.latch.getCount() + ']');
        assertEquals(THREADS, lsnr.allEvts.size());
        Set<Integer> vals = new HashSet<>();
        boolean err = false;
        for (CacheEntryEvent<?, ?> evt : lsnr.allEvts) {
            assertEquals(key, evt.getKey());
            assertNotNull(evt.getValue());
            if (!vals.add((Integer) evt.getValue())) {
                err = true;
                log.info("Extra event: " + evt);
            }
        }
        for (int v = 0; v < THREADS; v++) {
            if (!vals.contains(v)) {
                err = true;
                log.info("Event for value not received: " + v);
            }
        }
        assertFalse("Invalid events, see log for details.", err);
        lsnr.allEvts.clear();
        startGrid(SRV_IDX);
    }
    cur.close();
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 4 with TransactionSerializationException

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

the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInListener.

/**
 * @param ccfg Cache configuration.
 * @param asyncFltr Async filter.
 * @param asyncLsnr Async listener.
 * @param jcacheApi Use JCache api for registration entry update listener.
 * @throws Exception If failed.
 */
private void testNonDeadLockInListener(CacheConfiguration ccfg, final boolean asyncFltr, boolean asyncLsnr, boolean jcacheApi) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    try {
        for (int i = 0; i < ITERATION_CNT; i++) {
            log.info("Start iteration: " + i);
            int nodeIdx = i % NODES;
            final String cacheName = ccfg.getName();
            final IgniteCache cache = grid(nodeIdx).cache(cacheName);
            final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
            final QueryTestValue val0 = new QueryTestValue(1);
            final QueryTestValue newVal = new QueryTestValue(2);
            final CountDownLatch latch = new CountDownLatch(1);
            final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    if (asyncFltr) {
                        assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
                        assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
                    }
                }
            };
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
                    QueryTestValue val = e.getValue();
                    if (val == null)
                        return;
                    else if (val.equals(newVal)) {
                        evtFromLsnrLatch.countDown();
                        return;
                    } else if (!val.equals(val0))
                        return;
                    // several ms to wait - mvcc coordinator need some time to register tx as finished.
                    if (ccfg.getAtomicityMode() == TRANSACTIONAL_SNAPSHOT) {
                        Object v = null;
                        while (v == null && !Thread.currentThread().isInterrupted()) {
                            v = cache0.get(key);
                            if (v == null)
                                doSleep(50);
                        }
                    }
                    try {
                        assertEquals(val, val0);
                        if (atomicityMode(cache0) != ATOMIC) {
                            boolean committed = false;
                            while (!committed && !Thread.currentThread().isInterrupted()) {
                                try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                    cache0.put(key, newVal);
                                    tx.commit();
                                    committed = true;
                                } catch (Exception ex) {
                                    assertTrue(ex.getCause() instanceof TransactionSerializationException);
                                    assertEquals(atomicityMode(cache0), TRANSACTIONAL_SNAPSHOT);
                                }
                            }
                        } else
                            cache0.put(key, newVal);
                        latch.countDown();
                    } catch (Exception exp) {
                        log.error("Failed: ", exp);
                        throw new IgniteException(exp);
                    }
                }
            };
            QueryCursor qry = null;
            MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
            CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
            CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFltr ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
            if (jcacheApi) {
                lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
                cache.registerCacheEntryListener(lsnrCfg);
            } else {
                ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
                conQry.setLocalListener(locLsnr);
                conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
                qry = cache.query(conQry);
            }
            try {
                if (rnd.nextBoolean())
                    cache.put(key, val0);
                else {
                    cache.invoke(key, new CacheEntryProcessor() {

                        @Override
                        public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
                            entry.setValue(val0);
                            return null;
                        }
                    });
                }
                assertTrue("Failed to waiting event.", U.await(latch, 3, SECONDS));
                assertEquals(cache.get(key), new QueryTestValue(2));
                assertTrue("Failed to waiting event from listener.", U.await(latch, 3, SECONDS));
            } finally {
                if (qry != null)
                    qry.close();
                if (lsnrCfg != null)
                    cache.deregisterCacheEntryListener(lsnrCfg);
            }
            log.info("Iteration finished: " + i);
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) EntryProcessorException(javax.cache.processor.EntryProcessorException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) Transaction(org.apache.ignite.transactions.Transaction) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Example 5 with TransactionSerializationException

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

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.

/**
 * @param atomicityMode Cache atomicity mode.
 * @param cacheGrp {@code True} if test cache multiple caches in the same group.
 * @throws Exception If failed.
 */
private void concurrentUpdatesAndQueryStart(CacheAtomicityMode atomicityMode, boolean cacheGrp) throws Exception {
    Ignite srv = startGrid(0);
    Ignite client = startClientGrid(1);
    List<String> caches = new ArrayList<>();
    if (cacheGrp) {
        for (int i = 0; i < 3; i++) {
            CacheConfiguration<?, ?> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME + i);
            ccfg.setGroupName("testGroup");
            ccfg.setWriteSynchronizationMode(FULL_SYNC);
            ccfg.setAtomicityMode(atomicityMode);
            IgniteCache<?, ?> cache = client.createCache(ccfg);
            caches.add(cache.getName());
        }
    } else {
        CacheConfiguration<?, ?> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
        ccfg.setWriteSynchronizationMode(FULL_SYNC);
        ccfg.setAtomicityMode(atomicityMode);
        IgniteCache<?, ?> cache = client.createCache(ccfg);
        caches.add(cache.getName());
    }
    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 = 10;
    final int UPDATES = 1000;
    final List<IgniteCache<Object, Object>> srvCaches = new ArrayList<>();
    for (String cacheName : caches) srvCaches.add(srv.cache(cacheName));
    for (int i = 0; i < SF.applyLB(5, 2); i++) {
        log.info("Iteration: " + i);
        final AtomicBoolean stop = new AtomicBoolean();
        List<T2<AtomicInteger, QueryCursor>> qrys = new ArrayList<>();
        try {
            IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (!stop.get()) {
                        for (IgniteCache<Object, Object> srvCache : srvCaches) {
                            if (atomicityMode == ATOMIC)
                                srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
                            else {
                                IgniteTransactions txs = srvCache.unwrap(Ignite.class).transactions();
                                boolean committed = false;
                                while (!committed) {
                                    try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                        srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
                                        tx.commit();
                                        committed = true;
                                    } catch (CacheException e) {
                                        assertTrue(e.getCause() instanceof TransactionSerializationException);
                                        assertEquals(atomicityMode, TRANSACTIONAL_SNAPSHOT);
                                    }
                                }
                            }
                        }
                    }
                    return null;
                }
            }, THREADS, "update");
            U.sleep(1000);
            for (String cache : caches) for (int l = 0; l < 10; l++) qrys.add(startListener(client.cache(cache)));
            U.sleep(1000);
            stop.set(true);
            fut.get();
        } finally {
            stop.set(true);
        }
        GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) {
                    for (IgniteCache<Object, Object> srvCache : srvCaches) {
                        if (atomicityMode == ATOMIC)
                            srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                        else {
                            IgniteTransactions txs = srvCache.unwrap(Ignite.class).transactions();
                            boolean committed = false;
                            while (!committed) {
                                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                    srvCache.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 (T2<AtomicInteger, QueryCursor> qry : qrys) {
            final AtomicInteger evtCnt = qry.get1();
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    log.info("Events: " + evtCnt.get());
                    return evtCnt.get() >= THREADS * UPDATES;
                }
            }, 30000);
            assertEquals(THREADS * UPDATES, evtCnt.get());
            qry.get2().close();
        }
    }
}
Also used : TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteTransactions(org.apache.ignite.IgniteTransactions) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) T2(org.apache.ignite.internal.util.typedef.T2) QueryCursor(org.apache.ignite.cache.query.QueryCursor) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

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