Search in sources :

Example 86 with ThreadLocalRandom

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

the class CacheContinuousQueryOperationP2PTest method testContinuousQuery.

/**
 * @param ccfg Cache configuration.
 * @param isClient Client.
 * @throws Exception If failed.
 */
protected void testContinuousQuery(CacheConfiguration<Object, Object> ccfg, boolean isClient) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    QueryCursor<?> cur = null;
    final Class<Factory<CacheEntryEventFilter>> evtFilterFactory = (Class<Factory<CacheEntryEventFilter>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    final CountDownLatch latch = new CountDownLatch(10);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    TestLocalListener localLsnr = new TestLocalListener() {

        @Override
        public void onEvent(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) throws CacheEntryListenerException {
            for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
                latch.countDown();
                log.info("Received event: " + evt);
            }
        }
    };
    MutableCacheEntryListenerConfiguration<Integer, Integer> lsnrCfg = new MutableCacheEntryListenerConfiguration<>(new FactoryBuilder.SingletonFactory<>(localLsnr), (Factory<? extends CacheEntryEventFilter<? super Integer, ? super Integer>>) (Object) evtFilterFactory.newInstance(), true, true);
    qry.setLocalListener(localLsnr);
    qry.setRemoteFilterFactory((Factory<? extends CacheEntryEventFilter<Integer, Integer>>) (Object) evtFilterFactory.newInstance());
    IgniteCache<Integer, Integer> cache = null;
    try {
        if (isClient)
            cache = grid(NODES - 1).cache(ccfg.getName());
        else
            cache = grid(rnd.nextInt(NODES - 1)).cache(ccfg.getName());
        cur = cache.query(qry);
        cache.registerCacheEntryListener(lsnrCfg);
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertTrue(latch.await(3, TimeUnit.SECONDS));
    } finally {
        if (cur != null)
            cur.close();
        if (cache != null)
            cache.deregisterCacheEntryListener(lsnrCfg);
    }
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 87 with ThreadLocalRandom

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

the class GridCacheClientNodeBinaryObjectMetadataMultinodeTest method testClientMetadataInitialization.

/**
 * @throws Exception If failed.
 */
public void testClientMetadataInitialization() throws Exception {
    startGrids(2);
    final AtomicBoolean stop = new AtomicBoolean();
    final ConcurrentHashSet<String> allTypes = new ConcurrentHashSet<>();
    IgniteInternalFuture<?> fut;
    try {
        // Update binary metadata concurrently with client nodes start.
        fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                IgniteBinary binaries = ignite(0).binary();
                IgniteCache<Object, Object> cache = ignite(0).cache(DEFAULT_CACHE_NAME).withKeepBinary();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < 1000; i++) {
                    log.info("Iteration: " + i);
                    String type = "binary-type-" + i;
                    allTypes.add(type);
                    for (int f = 0; f < 10; f++) {
                        BinaryObjectBuilder builder = binaries.builder(type);
                        String fieldName = "f" + f;
                        builder.setField(fieldName, i);
                        cache.put(rnd.nextInt(0, 100_000), builder.build());
                        if (f % 100 == 0)
                            log.info("Put iteration: " + f);
                    }
                    if (stop.get())
                        break;
                }
                return null;
            }
        }, 5, "update-thread");
    } finally {
        stop.set(true);
    }
    client = true;
    startGridsMultiThreaded(2, 5);
    fut.get();
    assertFalse(allTypes.isEmpty());
    log.info("Expected binary types: " + allTypes.size());
    assertEquals(7, ignite(0).cluster().nodes().size());
    for (int i = 0; i < 7; i++) {
        log.info("Check metadata on node: " + i);
        boolean client = i > 1;
        assertEquals((Object) client, ignite(i).configuration().isClientMode());
        IgniteBinary binaries = ignite(i).binary();
        Collection<BinaryType> metaCol = binaries.types();
        assertEquals(allTypes.size(), metaCol.size());
        Set<String> names = new HashSet<>();
        for (BinaryType meta : metaCol) {
            info("Binary type: " + meta);
            assertTrue(names.add(meta.typeName()));
            assertNull(meta.affinityKeyFieldName());
            assertEquals(10, meta.fieldNames().size());
        }
        assertEquals(allTypes.size(), names.size());
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) Callable(java.util.concurrent.Callable) IgniteBinary(org.apache.ignite.IgniteBinary) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) HashSet(java.util.HashSet)

Example 88 with ThreadLocalRandom

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

the class IgnitePdsTransactionsHangTest method testTransactionsDontHang.

/**
 * Copied from customers benchmark.
 *
 * @throws Exception If failed.
 */
public void testTransactionsDontHang() throws Exception {
    try {
        final Ignite g = startGrids(2);
        g.active(true);
        g.getOrCreateCache(getCacheConfiguration());
        ExecutorService threadPool = Executors.newFixedThreadPool(THREADS_CNT);
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(THREADS_CNT);
        final AtomicBoolean interrupt = new AtomicBoolean(false);
        final LongAdder operationCnt = new LongAdder();
        final IgniteCache<Long, TestEntity> cache = g.cache(CACHE_NAME);
        for (int i = 0; i < THREADS_CNT; i++) {
            threadPool.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        ThreadLocalRandom locRandom = ThreadLocalRandom.current();
                        cyclicBarrier.await();
                        while (!interrupt.get()) {
                            long randomKey = locRandom.nextLong(MAX_KEY_COUNT);
                            TestEntity entity = TestEntity.newTestEntity(locRandom);
                            try (Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                                cache.put(randomKey, entity);
                                tx.commit();
                            }
                            operationCnt.increment();
                        }
                    } catch (Throwable e) {
                        log.error("Unexpected exception:", e);
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        long stopTime = System.currentTimeMillis() + DURATION * 1000;
        long totalOperations = 0;
        int periods = 0;
        long max = Long.MIN_VALUE, min = Long.MAX_VALUE;
        while (System.currentTimeMillis() < stopTime) {
            U.sleep(1000);
            long sum = operationCnt.sumThenReset();
            periods++;
            if (periods > WARM_UP_PERIOD) {
                totalOperations += sum;
                max = Math.max(max, sum);
                min = Math.min(min, sum);
                log.info("Operation count: " + sum + " min=" + min + " max=" + max + " avg=" + totalOperations / (periods - WARM_UP_PERIOD));
            }
        }
        interrupt.set(true);
        threadPool.shutdown();
        log.info("Test complete");
        threadPool.awaitTermination(getTestTimeout(), TimeUnit.MILLISECONDS);
        IgniteTxManager tm = internalCache(cache).context().tm();
        assertEquals("There are still active transactions", 0, tm.activeTransactions().size());
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) Transaction(org.apache.ignite.transactions.Transaction) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Example 89 with ThreadLocalRandom

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

the class TrackingPageIOTest method testMerging.

/**
 */
public void testMerging() {
    ByteBuffer buf = ByteBuffer.allocateDirect(PAGE_SIZE);
    buf.order(ByteOrder.nativeOrder());
    ThreadLocalRandom rand = ThreadLocalRandom.current();
    int track = io.countOfPageToTrack(PAGE_SIZE);
    long basePageId = io.trackingPageFor(Math.max(rand.nextLong(Integer.MAX_VALUE - track), 0), PAGE_SIZE);
    assert basePageId >= 0;
    PageIO.setPageId(GridUnsafe.bufferAddress(buf), basePageId);
    TreeSet<Long> setIdx = new TreeSet<>();
    for (int i = 0; i < 4; i++) generateMarking(buf, track, basePageId, basePageId + rand.nextInt(1, track), setIdx, i, -1);
    TreeSet<Long> setIdx2 = new TreeSet<>();
    generateMarking(buf, track, basePageId, basePageId + rand.nextInt(1, track), setIdx2, 4, -1);
    assertEquals(setIdx2.size(), io.countOfChangedPage(buf, 4, PAGE_SIZE));
    assertEquals(setIdx.size(), io.countOfChangedPage(buf, 3, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertEquals("pageId = " + i, setIdx.contains(i), io.wasChanged(buf, i, 3, -1, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertEquals("pageId = " + i, setIdx2.contains(i), io.wasChanged(buf, i, 4, 3, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertFalse(io.wasChanged(buf, i, 5, 4, PAGE_SIZE));
}
Also used : TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ByteBuffer(java.nio.ByteBuffer)

Example 90 with ThreadLocalRandom

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

the class IgniteDataStreamerPerformanceTest method doTest.

/**
 * @throws Exception If failed.
 */
private void doTest() throws Exception {
    System.gc();
    System.gc();
    System.gc();
    try {
        useCache = true;
        startGridsMultiThreaded(GRID_CNT);
        useCache = false;
        Ignite ignite = startGrid();
        final IgniteDataStreamer<Integer, String> ldr = ignite.dataStreamer(DEFAULT_CACHE_NAME);
        ldr.perNodeBufferSize(8192);
        ldr.receiver(DataStreamerCacheUpdaters.<Integer, String>batchedSorted());
        ldr.autoFlushFrequency(0);
        final LongAdder cnt = new LongAdder();
        long start = U.currentTimeMillis();
        Thread t = new Thread(new Runnable() {

            @SuppressWarnings("BusyWait")
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException ignored) {
                        break;
                    }
                    info(">>> Adds/sec: " + cnt.sumThenReset() / 10);
                }
            }
        });
        t.setDaemon(true);
        t.start();
        // Runtime.getRuntime().availableProcessors();
        int threadNum = 2;
        multithreaded(new Callable<Object>() {

            @SuppressWarnings("InfiniteLoopStatement")
            @Override
            public Object call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (true) {
                    int i = rnd.nextInt(ENTRY_CNT);
                    ldr.addData(i, vals[rnd.nextInt(vals.length)]);
                    cnt.increment();
                }
            }
        }, threadNum, "loader");
        info("Closing loader...");
        ldr.close(false);
        long duration = U.currentTimeMillis() - start;
        info("Finished performance test. Duration: " + duration + "ms.");
    } finally {
        stopAllGrids();
    }
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)236 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)73 Ignite (org.apache.ignite.Ignite)65 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 IgniteCache (org.apache.ignite.IgniteCache)44 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)30 IgniteException (org.apache.ignite.IgniteException)27 Transaction (org.apache.ignite.transactions.Transaction)26 CacheException (javax.cache.CacheException)24 HashMap (java.util.HashMap)22 Map (java.util.Map)20 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)17 LongAdder (java.util.concurrent.atomic.LongAdder)15 TreeMap (java.util.TreeMap)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 Callable (java.util.concurrent.Callable)11 CountDownLatch (java.util.concurrent.CountDownLatch)11