Search in sources :

Example 1 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class TaskCommandHandlerSelfTest method testManyTasksRun.

/**
     * @throws Exception If failed.
     */
public void testManyTasksRun() throws Exception {
    GridClientCompute compute = client.compute();
    for (int i = 0; i < 1000; i++) assertEquals(new Integer("executing".length()), compute.execute(TestTask.class.getName(), "executing"));
    GridClientFactory.stop(client.id(), true);
    IgniteKernal g = (IgniteKernal) grid(0);
    Map<GridRestCommand, GridRestCommandHandler> handlers = U.field(g.context().rest(), "handlers");
    GridTaskCommandHandler taskHnd = (GridTaskCommandHandler) F.find(handlers.values(), null, new P1<GridRestCommandHandler>() {

        @Override
        public boolean apply(GridRestCommandHandler e) {
            return e instanceof GridTaskCommandHandler;
        }
    });
    assertNotNull("GridTaskCommandHandler was not found", taskHnd);
    ConcurrentLinkedHashMap taskDesc = U.field(taskHnd, "taskDescs");
    assertTrue("Task result map size exceeded max value [mapSize=" + taskDesc.sizex() + ", " + "maxSize=" + MAX_TASK_RESULTS + ']', taskDesc.sizex() <= MAX_TASK_RESULTS);
}
Also used : GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) IgniteKernal(org.apache.ignite.internal.IgniteKernal) P1(org.apache.ignite.internal.util.typedef.P1) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) GridTaskCommandHandler(org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler)

Example 2 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class GridConcurrentLinkedHashMapMultiThreadedSelfTest method testInsertOrderIterator.

/**
     * @throws Exception If failed.
     */
public void testInsertOrderIterator() throws Exception {
    final AtomicBoolean run = new AtomicBoolean(true);
    info(">>> Test grid concurrent linked hash map iterator...");
    final Map<Integer, String> linkedMap = new ConcurrentLinkedHashMap<>();
    Set<Integer> original = new HashSet<>();
    final int keyCnt = 10000;
    for (int i = 0; i < keyCnt; i++) {
        linkedMap.put(i, "value" + i);
        original.add(i);
    }
    long start = System.currentTimeMillis();
    // Updater threads.
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            Random rnd = new Random();
            while (run.get()) {
                int key = rnd.nextInt(keyCnt);
                linkedMap.put(key, "value" + key);
            }
            return null;
        }
    }, 10, "updater");
    try {
        // Check that iterator always contains all the values.
        int iterCnt = 10000;
        for (int i = 0; i < iterCnt; i++) {
            Collection<Integer> cp = new HashSet<>(original);
            cp.removeAll(linkedMap.keySet());
            assertTrue("Keys disappeared from map: " + cp, cp.isEmpty());
        }
        info(">>> Iterator test complete [duration = " + (System.currentTimeMillis() - start) + ']');
    } finally {
        run.set(false);
        fut.get();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) Random(java.util.Random) Nullable(org.jetbrains.annotations.Nullable) HashSet(java.util.HashSet)

Example 3 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class GridCacheWriteBehindStoreSelfTest method testBatchApply.

/**
     * Tests that all values will be written to the underlying store
     * right in the same order as they were put into the store.
     *
     * @param writeCoalescing Write coalescing flag.
     * @throws Exception If failed.
     */
private void testBatchApply(boolean writeCoalescing) throws Exception {
    delegate = new GridCacheTestStore(new ConcurrentLinkedHashMap<Integer, String>());
    initStore(1, writeCoalescing);
    List<Integer> intList = new ArrayList<>(CACHE_SIZE);
    try {
        for (int i = 0; i < CACHE_SIZE; i++) {
            store.write(new CacheEntryImpl<>(i, "val" + i));
            intList.add(i);
        }
    } finally {
        shutdownStore();
    }
    Map<Integer, String> underlyingMap = delegate.getMap();
    assertTrue("Store map key set: " + underlyingMap.keySet(), F.eqOrdered(underlyingMap.keySet(), intList));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) ArrayList(java.util.ArrayList) GridCacheTestStore(org.apache.ignite.internal.processors.cache.GridCacheTestStore)

Example 4 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class GridConcurrentLinkedHashMapBenchmark method test.

/**
     * Test a generic access method on map.
     *
     * @param readOp Access method to test.
     * @param threadCnt Number of threads to run.
     * @param writeProportion Amount of writes from total number of iterations.
     */
@SuppressWarnings({ "BusyWait" })
private static void test(C2<Integer, ConcurrentLinkedHashMap<Integer, Integer>, Integer> readOp, int threadCnt, double writeProportion) {
    assert writeProportion < 1;
    ConcurrentLinkedHashMap<Integer, Integer> map = new ConcurrentLinkedHashMap<>();
    CyclicBarrier barrier = new CyclicBarrier(threadCnt + 1);
    Collection<TestThread> threads = new ArrayList<>(threadCnt);
    for (int i = 0; i < threadCnt; i++) {
        TestThread thread = new TestThread(readOp, map, writeProportion, barrier);
        threads.add(thread);
        thread.start();
    }
    long start;
    try {
        // Wait threads warm-up.
        while (barrier.getNumberWaiting() != threadCnt) Thread.sleep(1);
        // Starting test and letting it run for 1 minute.
        barrier.await();
        start = System.currentTimeMillis();
        Thread.sleep(60000);
    } catch (InterruptedException ignored) {
        return;
    } catch (BrokenBarrierException e) {
        e.printStackTrace();
        return;
    }
    for (TestThread th : threads) th.interrupt();
    try {
        for (TestThread th : threads) th.join();
    } catch (InterruptedException ignored) {
        return;
    }
    long time = System.currentTimeMillis() - start;
    long iters = 0;
    for (TestThread th : threads) iters += th.iterations();
    System.out.printf("%8s, %8d, %12d, %12d, %12d, %8.3f, %8.2f\n", readOp.toString(), threadCnt, 1000 * iters / time, 1000 * iters / (time * threadCnt), iters, time / (double) 1000, writeProportion);
}
Also used : ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 5 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class GridConcurrentLinkedHashMapMultiThreadedSelfTest method testGetRemovePutIterator.

/**
     * Test multithreaded operations in concurrent linked hash map and iterator consistency.
     *
     * @throws Exception If failed.
     */
public void testGetRemovePutIterator() throws Exception {
    info(">>> Test grid concurrent linked hash map iterator...");
    final ConcurrentLinkedHashMap<Integer, String> linkedMap = new ConcurrentLinkedHashMap<>();
    Collection<Integer> original = new HashSet<>();
    final int keyCnt = 10000;
    for (int i = 0; i < keyCnt; i++) {
        linkedMap.put(i, "value" + i);
        original.add(i);
    }
    final AtomicBoolean run = new AtomicBoolean(true);
    long start = System.currentTimeMillis();
    // Updater threads.
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            Random rnd = new Random();
            while (run.get()) {
                int key = rnd.nextInt(keyCnt);
                linkedMap.get(key);
                linkedMap.remove(key);
                linkedMap.put(key, "value" + key);
            }
            info(">>> Exiting updater thread");
            return null;
        }
    }, 10, "updater");
    int iterCnt = 10000;
    for (int i = 0; i < iterCnt; i++) {
        Iterator<Integer> it = linkedMap.keySet().iterator();
        Collection<Integer> keys = new HashSet<>();
        // Since we have 10 running threads, iterator should show not less then keyCnt - 10 elements.
        while (it.hasNext()) {
            int key = it.next();
            assertFalse("Duplicate key: " + key, keys.contains(key));
            keys.add(key);
        }
        if (i % 500 == 0)
            info(">>> Run " + i + " iterations in " + (System.currentTimeMillis() - start) + "ms");
    }
    info(">>> Stopping updater threads");
    run.set(false);
    fut.get();
    info(">>> Updater threads stopped, will verify integrity of result map");
    Set<Integer> keys = linkedMap.keySet();
    original.removeAll(keys);
    assertTrue("Keys must be in map: " + original, original.isEmpty());
    info(">>> put get remove test complete [duration = " + (System.currentTimeMillis() - start) + ']');
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Nullable(org.jetbrains.annotations.Nullable) HashSet(java.util.HashSet)

Aggregations

ConcurrentLinkedHashMap (org.jsr166.ConcurrentLinkedHashMap)7 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HashSet (java.util.HashSet)3 Nullable (org.jetbrains.annotations.Nullable)3 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)1 GridCacheTestStore (org.apache.ignite.internal.processors.cache.GridCacheTestStore)1 GridRestCommandHandler (org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler)1 GridTaskCommandHandler (org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler)1 P1 (org.apache.ignite.internal.util.typedef.P1)1