Search in sources :

Example 1 with GridStripedLock

use of org.apache.ignite.internal.util.GridStripedLock in project ignite by apache.

the class BPlusTreeSelfTest method doTestRandomPutRemoveMultithreaded.

/**
     * @param canGetRow Can get row from inner page.
     * @throws Exception If failed.
     */
private void doTestRandomPutRemoveMultithreaded(boolean canGetRow) throws Exception {
    final TestTree tree = createTestTree(canGetRow);
    final Map<Long, Long> map = new ConcurrentHashMap8<>();
    final int loops = reuseList == null ? 20_000 : 60_000;
    final GridStripedLock lock = new GridStripedLock(256);
    final String[] ops = { "put", "rmv", "inv_put", "inv_rmv" };
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < loops; i++) {
                final Long x = (long) DataStructure.randomInt(CNT);
                final int op = DataStructure.randomInt(4);
                if (i % 10000 == 0)
                    X.println(" --> " + ops[op] + "_" + i + "  " + x);
                Lock l = lock.getLock(x.longValue());
                l.lock();
                try {
                    if (op == 0) {
                        // Put.
                        assertEquals(map.put(x, x), tree.put(x));
                        assertNoLocks();
                    } else if (op == 1) {
                        // Remove.
                        if (map.remove(x) != null) {
                            assertEquals(x, tree.remove(x));
                            assertNoLocks();
                        }
                        assertNull(tree.remove(x));
                        assertNoLocks();
                    } else if (op == 2) {
                        tree.invoke(x, null, new IgniteTree.InvokeClosure<Long>() {

                            IgniteTree.OperationType opType;

                            @Override
                            public void call(@Nullable Long row) {
                                opType = PUT;
                                if (row != null)
                                    assertEquals(x, row);
                            }

                            @Override
                            public Long newRow() {
                                return x;
                            }

                            @Override
                            public IgniteTree.OperationType operationType() {
                                return opType;
                            }
                        });
                        map.put(x, x);
                    } else if (op == 3) {
                        tree.invoke(x, null, new IgniteTree.InvokeClosure<Long>() {

                            IgniteTree.OperationType opType;

                            @Override
                            public void call(@Nullable Long row) {
                                if (row != null) {
                                    assertEquals(x, row);
                                    opType = REMOVE;
                                } else
                                    opType = NOOP;
                            }

                            @Override
                            public Long newRow() {
                                return null;
                            }

                            @Override
                            public IgniteTree.OperationType operationType() {
                                return opType;
                            }
                        });
                        map.remove(x);
                    } else
                        fail();
                } finally {
                    l.unlock();
                }
            }
            return null;
        }
    }, Runtime.getRuntime().availableProcessors(), "put-remove");
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stop.get()) {
                Thread.sleep(5000);
                X.println(TestTree.printLocks());
            }
            return null;
        }
    }, 1, "printLocks");
    IgniteInternalFuture<?> fut3 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stop.get()) {
                int low = DataStructure.randomInt(CNT);
                int high = low + DataStructure.randomInt(CNT - low);
                GridCursor<Long> c = tree.find((long) low, (long) high);
                Long last = null;
                while (c.next()) {
                    // Correct bounds.
                    assertTrue(low + " <= " + c.get() + " <= " + high, c.get() >= low);
                    assertTrue(low + " <= " + c.get() + " <= " + high, c.get() <= high);
                    if (// No duplicates.
                    last != null)
                        assertTrue(low + " <= " + last + " < " + c.get() + " <= " + high, c.get() > last);
                    last = c.get();
                }
            }
            return null;
        }
    }, 4, "find");
    try {
        fut.get(getTestTimeout(), TimeUnit.MILLISECONDS);
    } finally {
        stop.set(true);
        fut2.get();
        fut3.get();
    }
    GridCursor<Long> cursor = tree.find(null, null);
    while (cursor.next()) {
        Long x = cursor.get();
        assert x != null;
        assertEquals(map.get(x), x);
    }
    info("size: " + map.size());
    assertEquals(map.size(), tree.size());
    tree.validateTree();
    assertNoLocks();
}
Also used : GridCursor(org.apache.ignite.internal.util.lang.GridCursor) GridStripedLock(org.apache.ignite.internal.util.GridStripedLock) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridStripedLock(org.apache.ignite.internal.util.GridStripedLock) Lock(java.util.concurrent.locks.Lock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteTree(org.apache.ignite.internal.util.IgniteTree) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GridStripedLock

use of org.apache.ignite.internal.util.GridStripedLock in project ignite by apache.

the class GridStripedLockSelfTest method beforeTest.

/** {@inheritDoc} */
@Override
protected void beforeTest() throws Exception {
    lock = new GridStripedLock(STRIPE_COUNT);
    barrier = new CyclicBarrier(THREAD_COUNT);
}
Also used : GridStripedLock(org.apache.ignite.internal.util.GridStripedLock) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

GridStripedLock (org.apache.ignite.internal.util.GridStripedLock)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Lock (java.util.concurrent.locks.Lock)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteTree (org.apache.ignite.internal.util.IgniteTree)1 GridCursor (org.apache.ignite.internal.util.lang.GridCursor)1 Nullable (org.jetbrains.annotations.Nullable)1 ConcurrentHashMap8 (org.jsr166.ConcurrentHashMap8)1