Search in sources :

Example 96 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteClientReconnectFailoverTest method testReconnectStreamerApi.

/**
     * @throws Exception If failed.
     */
public void testReconnectStreamerApi() throws Exception {
    final Ignite client = grid(serverCount());
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            stream(ATOMIC_CACHE);
            stream(TX_CACHE);
            return null;
        }

        private void stream(String cacheName) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            try (IgniteDataStreamer<Integer, Integer> streamer = client.dataStreamer(cacheName)) {
                streamer.allowOverwrite(true);
                streamer.perNodeBufferSize(10);
                for (int i = 0; i < 100; i++) streamer.addData(rnd.nextInt(100_000), 0);
            }
        }
    });
}
Also used : IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException)

Example 97 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteClientReconnectFailoverTest method testReconnectAtomicCache.

/**
     * @throws Exception If failed.
     */
public void testReconnectAtomicCache() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Integer> cache = client.cache(ATOMIC_CACHE);
    assertNotNull(cache);
    assertEquals(ATOMIC, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            TreeMap<Integer, Integer> map = new TreeMap<>();
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 10; i++) {
                Integer key = rnd.nextInt(0, 100_000);
                cache.put(key, key);
                assertEquals(key, cache.get(key));
                map.put(key, key);
            }
            cache.putAll(map);
            Map<Integer, Integer> res = cache.getAll(map.keySet());
            assertEquals(map, res);
            return null;
        }
    });
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException)

Example 98 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class IgniteClientReconnectFailoverTest method testReconnectTxCache.

/**
     * @throws Exception If failed.
     */
public void testReconnectTxCache() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Integer> cache = client.cache(TX_CACHE);
    assertNotNull(cache);
    assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
    final IgniteTransactions txs = client.transactions();
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                TreeMap<Integer, Integer> map = new TreeMap<>();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < 5; i++) {
                    Integer key = rnd.nextInt(0, 100_000);
                    cache.put(key, key);
                    assertEquals(key, cache.get(key));
                    map.put(key, key);
                }
                for (TransactionConcurrency txConcurrency : TransactionConcurrency.values()) {
                    try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
                        for (Map.Entry<Integer, Integer> e : map.entrySet()) {
                            cache.put(e.getKey(), e.getValue());
                            assertNotNull(cache.get(e.getKey()));
                        }
                        tx.commit();
                    }
                }
                cache.putAll(map);
                Map<Integer, Integer> res = cache.getAll(map.keySet());
                assertEquals(map, res);
            } catch (IgniteClientDisconnectedException e) {
                throw e;
            } catch (IgniteException e) {
                log.info("Ignore error: " + e);
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            return null;
        }
    });
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteTransactions(org.apache.ignite.IgniteTransactions) TreeMap(java.util.TreeMap) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 99 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class BinaryFieldExtractionSelfTest method testPrimitiveMarshalling.

/**
     * @throws Exception If failed.
     */
public void testPrimitiveMarshalling() throws Exception {
    BinaryMarshaller marsh = createMarshaller();
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    TestObject obj = new TestObject(0);
    BinaryObjectImpl binObj = toBinary(obj, marsh);
    BinaryFieldEx[] fields = new BinaryFieldEx[] { (BinaryFieldEx) binObj.type().field("bVal"), (BinaryFieldEx) binObj.type().field("cVal"), (BinaryFieldEx) binObj.type().field("sVal"), (BinaryFieldEx) binObj.type().field("iVal"), (BinaryFieldEx) binObj.type().field("lVal"), (BinaryFieldEx) binObj.type().field("fVal"), (BinaryFieldEx) binObj.type().field("dVal") };
    ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
    for (int i = 0; i < 100; i++) {
        TestObject to = new TestObject(rnd.nextLong());
        BinaryObjectImpl bObj = toBinary(to, marsh);
        for (BinaryFieldEx field : fields) field.writeField(bObj, buf);
        buf.flip();
        for (BinaryFieldEx field : fields) assertEquals(field.value(bObj), field.readField(buf));
        buf.flip();
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ByteBuffer(java.nio.ByteBuffer)

Example 100 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class CachePutIfAbsentTest method testTxConflictGetAndPutIfAbsent.

/**
     * @throws Exception If failed.
     */
public void testTxConflictGetAndPutIfAbsent() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 10; i++) {
                Integer key = rnd.nextInt(10_000);
                cache.put(key, 2);
                for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
                    for (TransactionIsolation isolation : TransactionIsolation.values()) {
                        try (Transaction tx = txs.txStart(concurrency, isolation)) {
                            Object old = cache.getAndPutIfAbsent(key, 3);
                            assertEquals(2, old);
                            Object val = cache.get(key);
                            assertEquals(2, val);
                            tx.commit();
                        }
                        assertEquals((Integer) 2, cache.get(key));
                    }
                }
            }
        } finally {
            ignite0.destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Transaction(org.apache.ignite.transactions.Transaction) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) IgniteTransactions(org.apache.ignite.IgniteTransactions)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)186 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)49 Ignite (org.apache.ignite.Ignite)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)45 IgniteCache (org.apache.ignite.IgniteCache)33 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)26 IgniteException (org.apache.ignite.IgniteException)26 Transaction (org.apache.ignite.transactions.Transaction)24 CacheException (javax.cache.CacheException)21 HashMap (java.util.HashMap)16 Map (java.util.Map)15 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 TreeMap (java.util.TreeMap)11 Callable (java.util.concurrent.Callable)11 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 LongAdder (java.util.concurrent.atomic.LongAdder)9 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)9