Search in sources :

Example 66 with BrokenBarrierException

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

the class BPlusTreeSelfTest method testPutSizeLivelock.

/**
 * The test forces {@link BPlusTree#size} method to run into a livelock: during single run
 * the method is picking up new pages which are concurrently added to the tree until the new pages are not added
 * anymore. Test verifies that despite livelock condition a size from a valid range is returned.
 *
 * NB: This test has to be changed with the integration of IGNITE-3478.
 *
 * @throws Exception if test failed
 */
public void testPutSizeLivelock() throws Exception {
    MAX_PER_PAGE = 5;
    CNT = 800;
    final int SLIDING_WINDOW_SIZE = 16;
    final boolean DEBUG_PRINT = false;
    final TestTree tree = createTestTree(false);
    final AtomicLong curRmvKey = new AtomicLong(0);
    final AtomicLong curPutKey = new AtomicLong(SLIDING_WINDOW_SIZE);
    for (long i = curRmvKey.get(); i < curPutKey.get(); ++i) assertNull(tree.put(i));
    final int hwThreads = Runtime.getRuntime().availableProcessors();
    final int putRmvThreadCnt = Math.max(1, hwThreads / 2);
    final int sizeThreadCnt = hwThreads - putRmvThreadCnt;
    final CyclicBarrier putRmvOpBarrier = new CyclicBarrier(putRmvThreadCnt, new Runnable() {

        @Override
        public void run() {
            if (DEBUG_PRINT) {
                try {
                    X.println("===BARRIER=== size=" + tree.size() + " [" + tree.findFirst() + ".." + tree.findLast() + "]");
                } catch (IgniteCheckedException e) {
                // ignore
                }
            }
        }
    });
    final int loopCnt = CNT / hwThreads;
    IgniteInternalFuture<?> putRmvFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < loopCnt && !stop.get(); ++i) {
                int order;
                try {
                    order = putRmvOpBarrier.await();
                } catch (BrokenBarrierException e) {
                    // barrier reset() has been called: terminate
                    break;
                }
                Long putVal = curPutKey.getAndIncrement();
                if ((i & 0xff) == 0)
                    X.println(order + ": --> put(" + putVal + ")");
                assertNull(tree.put(putVal));
                Long rmvVal = curRmvKey.getAndIncrement();
                if ((i & 0xff) == 0)
                    X.println(order + ": --> rmv(" + rmvVal + ")");
                assertEquals(rmvVal, tree.remove(rmvVal));
                assertNull(tree.findOne(rmvVal));
            }
            return null;
        }
    }, putRmvThreadCnt, "put-remove");
    IgniteInternalFuture<?> sizeFut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final List<Long> treeContents = new ArrayList<>(SLIDING_WINDOW_SIZE * 2);
            final BPlusTree.TreeRowClosure<Long, Long> rowDumper = new BPlusTree.TreeRowClosure<Long, Long>() {

                @Override
                public boolean apply(BPlusTree<Long, Long> tree, BPlusIO<Long> io, long pageAddr, int idx) throws IgniteCheckedException {
                    treeContents.add(io.getLookupRow(tree, pageAddr, idx));
                    final long endMs = System.currentTimeMillis() + 10;
                    final long endPutKey = curPutKey.get() + MAX_PER_PAGE;
                    while (System.currentTimeMillis() < endMs && curPutKey.get() < endPutKey) Thread.yield();
                    return true;
                }
            };
            while (!stop.get()) {
                treeContents.clear();
                long treeSize = tree.size(rowDumper);
                long curPutVal = curPutKey.get();
                X.println(" ======> size=" + treeSize + "; last-put-value=" + curPutVal);
                if (treeSize < SLIDING_WINDOW_SIZE || treeSize > curPutVal)
                    fail("Tree size is not in bounds [" + SLIDING_WINDOW_SIZE + ".." + curPutVal + "]:" + treeSize + "; contents=" + treeContents);
            }
            return null;
        }
    }, sizeThreadCnt, "size");
    asyncRunFut = new GridCompoundFuture<>();
    asyncRunFut.add((IgniteInternalFuture) putRmvFut);
    asyncRunFut.add((IgniteInternalFuture) sizeFut);
    asyncRunFut.markInitialized();
    try {
        putRmvFut.get(getTestTimeout(), TimeUnit.MILLISECONDS);
    } finally {
        stop.set(true);
        putRmvOpBarrier.reset();
        asyncRunFut.get();
    }
    tree.validateTree();
    assertNoLocks();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReuseList(org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList) List(java.util.List) ArrayList(java.util.ArrayList) BPlusTree(org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree)

Example 67 with BrokenBarrierException

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

the class GridNioSelfTest method testConcurrentConnects.

/**
 * @throws Exception If failed.
 */
public void testConcurrentConnects() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_CNT);
    final AtomicReference<Exception> err = new AtomicReference<>();
    final GridNioServer<?> srvr = startServer(new GridBufferedParser(true, ByteOrder.nativeOrder()), new EchoListener());
    try {
        IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

            @SuppressWarnings("BusyWait")
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 100 && !Thread.currentThread().isInterrupted(); i++) {
                        TestClient client = null;
                        try {
                            client = createClient(U.getLocalHost(), srvr.port(), U.getLocalHost());
                            MessageWithId msg = new MessageWithId(idProvider.getAndIncrement());
                            byte[] data = serializeMessage(msg);
                            for (int j = 0; j < 10; j++) client.sendMessage(data, data.length);
                            for (int j = 0; j < 10; j++) {
                                byte[] res = client.receiveMessage();
                                if (!Arrays.equals(data, res)) {
                                    info("Invalid response received.");
                                    err.compareAndSet(null, new IgniteCheckedException("Invalid response received."));
                                    barrier.reset();
                                    return;
                                }
                            }
                        } catch (IgniteCheckedException e) {
                            info("Encountered unexpected exception: " + e);
                            err.compareAndSet(null, e);
                            // Break the barrier.
                            barrier.reset();
                            break;
                        } catch (IOException e) {
                            info("Encountered IO exception: " + e);
                            err.compareAndSet(null, e);
                            // Break the barrier.
                            barrier.reset();
                            break;
                        } finally {
                            if (client != null)
                                client.close();
                        }
                        if ("conn-tester-1".equals(Thread.currentThread().getName()) && i % 10 == 0 && i > 0)
                            info("Run " + i + " iterations.");
                        barrier.await();
                        Thread.sleep(100);
                    }
                } catch (InterruptedException ignored) {
                    barrier.reset();
                    info("Test thread was interrupted (will exit).");
                } catch (BrokenBarrierException ignored) {
                    info("Barrier was broken (will exit).");
                }
            }
        }, THREAD_CNT, "conn-tester");
        fut.get();
        if (err.get() != null)
            throw err.get();
    } finally {
        srvr.stop();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) BindException(java.net.BindException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 68 with BrokenBarrierException

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

the class GridTreeBenchmark method doTest.

/**
 * @param tree Tree.
 * @param data Data.
 * @param warmup Warmup.
 * @return Time.
 * @throws BrokenBarrierException If failed.
 * @throws InterruptedException If failed.
 */
private static long doTest(final ConcurrentNavigableMap<UUID, UUID> tree, final UUID[] data, boolean warmup) throws BrokenBarrierException, InterruptedException {
    Thread[] ths = new Thread[THREADS];
    final CyclicBarrier barrier = new CyclicBarrier(THREADS + 1);
    final AtomicInteger cnt = new AtomicInteger();
    for (int i = 0; i < ths.length; i++) {
        ths[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                int off = cnt.getAndIncrement() * ITERATIONS;
                try {
                    barrier.await();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
                for (int i = 0; i < ITERATIONS; i++) {
                    UUID id = data[off + i];
                    id = tree.put(id, id);
                    assert id == null;
                }
            }
        });
        ths[i].start();
    }
    barrier.await();
    long start = System.currentTimeMillis();
    for (Thread t : ths) t.join();
    long time = System.currentTimeMillis() - start;
    if (!warmup) {
        System.out.println(tree.getClass().getSimpleName() + "  " + time + " ms");
        return time;
    }
    return 0;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 69 with BrokenBarrierException

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

the class GridTreeBenchmark method doTestMaps.

/**
 * @throws BrokenBarrierException If failed.
 * @throws InterruptedException If failed.
 */
private static void doTestMaps() throws BrokenBarrierException, InterruptedException {
    final UUID[] data = generate();
    @SuppressWarnings("unchecked") final Map<UUID, UUID>[] maps = new Map[4];
    for (int i = 0; i < maps.length; i++) maps[i] = new SnapTreeMap<>();
    final Thread[] ths = new Thread[THREADS];
    final CyclicBarrier barrier = new CyclicBarrier(THREADS + 1);
    final AtomicInteger cnt = new AtomicInteger();
    for (int i = 0; i < ths.length; i++) {
        ths[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                int idx = cnt.getAndIncrement();
                int off = idx * ITERATIONS;
                Map<UUID, UUID> map = maps[idx % maps.length];
                try {
                    barrier.await();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
                for (int i = 0; i < ITERATIONS; i++) {
                    UUID id = data[off + i];
                    id = map.put(id, id);
                    assert id == null;
                }
            }
        });
        ths[i].start();
    }
    System.out.println("Sleep");
    Thread.sleep(10000);
    System.out.println("Go");
    barrier.await();
    long start = System.currentTimeMillis();
    for (Thread t : ths) t.join();
    long time = System.currentTimeMillis() - start;
    System.out.println(time);
}
Also used : SnapTreeMap(org.apache.ignite.internal.util.snaptree.SnapTreeMap) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) SnapTreeMap(org.apache.ignite.internal.util.snaptree.SnapTreeMap) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap)

Example 70 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project knime-core by knime.

the class TestLoadAndExecManySimultaneously method testConcurrency.

@Test
public void testConcurrency() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(NR_CONCURRENT);
    Future<Void>[] futures = new Future[NR_CONCURRENT];
    final AtomicBoolean isDone = new AtomicBoolean();
    for (int i = 0; i < NR_CONCURRENT; i++) {
        final int index = i;
        futures[i] = m_executorService.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try {
                    OneInstanceWorkflowTest oneInstance = m_instances[index];
                    oneInstance.prepareWorkflowDirectory();
                    barrier.await(10, TimeUnit.SECONDS);
                    try {
                        oneInstance.loadWorkflow(InternalNodeContainerState.CONFIGURED);
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                        throw e;
                    }
                    barrier.await(120, TimeUnit.SECONDS);
                    oneInstance.executeWorkflow();
                    barrier.await(10, TimeUnit.SECONDS);
                    oneInstance.saveWorkflow();
                    barrier.await(10, TimeUnit.SECONDS);
                    oneInstance.discardWorkflow();
                    barrier.await(10, TimeUnit.SECONDS);
                    oneInstance.loadWorkflow(InternalNodeContainerState.EXECUTED);
                    barrier.await(10, TimeUnit.SECONDS);
                    oneInstance.discardWorkflow();
                    return null;
                } finally {
                    isDone.set(true);
                }
            }

            private void await(final int secondsTimeOut) throws InterruptedException, BrokenBarrierException, TimeoutException {
                barrier.await(secondsTimeOut, TimeUnit.SECONDS);
            }
        });
    }
    ExecutionException brokenBarrierExceptionWrapper = null;
    for (int i = 0; i < NR_CONCURRENT; i++) {
        try {
            futures[i].get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof BrokenBarrierException) {
                if (brokenBarrierExceptionWrapper == null) {
                    brokenBarrierExceptionWrapper = e;
                }
            } else {
                throw e;
            }
        }
    }
    if (brokenBarrierExceptionWrapper != null) {
        throw brokenBarrierExceptionWrapper;
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)70 CyclicBarrier (java.util.concurrent.CyclicBarrier)61 Test (org.junit.Test)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 ArrayList (java.util.ArrayList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)12 List (java.util.List)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 Random (java.util.Random)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 TimeoutException (java.util.concurrent.TimeoutException)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6