Search in sources :

Example 26 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException 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 27 with BrokenBarrierException

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

the class IgniteOffheapReadWriteLockSelfTest method checkTagIdUpdate.

/**
     * @throws Exception if failed.
     */
private void checkTagIdUpdate(final boolean waitBeforeSwitch) throws Exception {
    final int numPairs = 100;
    final Pair[] data = new Pair[numPairs];
    for (int i = 0; i < numPairs; i++) data[i] = new Pair();
    final OffheapReadWriteLock lock = new OffheapReadWriteLock(16);
    final long ptr = GridUnsafe.allocateMemory(OffheapReadWriteLock.LOCK_SIZE);
    lock.init(ptr, TAG_0);
    final AtomicInteger reads = new AtomicInteger();
    final AtomicInteger writes = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean(false);
    final int threadCnt = 32;
    final CyclicBarrier barr = new CyclicBarrier(threadCnt);
    IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        /** {@inheritDoc} */
        @Override
        public Object call() throws Exception {
            try {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                int tag = TAG_0;
                long lastSwitch = System.currentTimeMillis();
                while (true) {
                    boolean write = rnd.nextInt(10) < 2;
                    boolean locked;
                    boolean switched = false;
                    if (write) {
                        locked = lock.writeLock(ptr, tag);
                        if (locked) {
                            try {
                                assertTrue(lock.isWriteLocked(ptr));
                                assertFalse(lock.isReadLocked(ptr));
                                int idx = rnd.nextInt(numPairs);
                                int delta = rnd.nextInt(100_000);
                                data[idx].a += delta;
                                data[idx].b -= delta;
                            } finally {
                                switched = System.currentTimeMillis() - lastSwitch > 1_000 || !waitBeforeSwitch;
                                if (switched && waitBeforeSwitch)
                                    info("Switching...");
                                int tag1 = (tag + (switched ? 1 : 0)) & 0xFFFF;
                                if (tag1 == 0)
                                    tag1 = 1;
                                lock.writeUnlock(ptr, tag1);
                            }
                            writes.incrementAndGet();
                        }
                    } else {
                        locked = lock.readLock(ptr, tag);
                        if (locked) {
                            try {
                                assert locked;
                                assertFalse(lock.isWriteLocked(ptr));
                                assertTrue(lock.isReadLocked(ptr));
                                for (int i1 = 0; i1 < data.length; i1++) {
                                    Pair pair = data[i1];
                                    assertEquals("Failed check for index: " + i1, pair.a, -pair.b);
                                }
                            } finally {
                                lock.readUnlock(ptr);
                            }
                            reads.incrementAndGet();
                        }
                    }
                    if (!locked || switched) {
                        try {
                            barr.await();
                        } catch (BrokenBarrierException ignore) {
                            // Done.
                            return null;
                        }
                        tag = (tag + 1) & 0xFFFF;
                        if (tag == 0)
                            tag = 1;
                        if (waitBeforeSwitch || (!waitBeforeSwitch && tag == 1))
                            info("Switch to a new tag: " + tag);
                        if (done.get()) {
                            barr.reset();
                            return null;
                        }
                        lastSwitch = System.currentTimeMillis();
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return null;
        }
    }, threadCnt, "tester");
    for (int i = 0; i < 30; i++) {
        Thread.sleep(1_000);
        info("Reads: " + reads.getAndSet(0) + ", writes=" + writes.getAndSet(0));
    }
    done.set(true);
    fut.get();
    validate(data);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) OffheapReadWriteLock(org.apache.ignite.internal.util.OffheapReadWriteLock) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 28 with BrokenBarrierException

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

the class GridTcpCommunicationSpiMultithreadedSelfTest method testFlowSend.

/**
     * @throws Exception If failed.
     */
public void testFlowSend() throws Exception {
    reject = true;
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_CNT);
    final Random rnd = new Random();
    final ClusterNode from = randomNode(rnd);
    ClusterNode tmp;
    do {
        tmp = randomNode(rnd);
    } while (tmp.id().equals(from.id()));
    final ClusterNode to = tmp;
    final int iterationCnt = 1000;
    final AtomicInteger threadId = new AtomicInteger();
    final int interval = 50;
    IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

        /** {@inheritDoc} */
        @Override
        public void run() {
            try {
                // Only first thread will print messages.
                int id = threadId.getAndIncrement();
                for (int i = 0; i < iterationCnt; i++) {
                    if (id == 0 && (i % 50) == 0)
                        info(">>> Running iteration " + i);
                    try {
                        for (ClusterNode node : nodes) {
                            Message msg = new GridTestMessage(from.id(), msgId.getAndIncrement(), 0);
                            spis.get(from.id()).sendMessage(node, msg);
                        }
                    } catch (IgniteException e) {
                        log.warning(">>> Oops, unable to send message (safe to ignore).", e);
                    }
                    barrier.await();
                }
            } catch (InterruptedException ignored) {
                Thread.currentThread().interrupt();
            } catch (BrokenBarrierException e) {
                info("Wait on barrier failed: " + e);
                Thread.currentThread().interrupt();
            }
        }
    }, THREAD_CNT, "message-sender");
    final AtomicBoolean run = new AtomicBoolean(true);
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Runnable() {

        @Override
        public void run() {
            try {
                while (run.get() && !Thread.currentThread().isInterrupted()) {
                    U.sleep(interval * 3 / 2);
                    ((TcpCommunicationSpi) spis.get(from.id())).onNodeLeft(to.id());
                }
            } catch (IgniteInterruptedCheckedException ignored) {
                Thread.currentThread().interrupt();
            }
        }
    }, 1);
    fut.get();
    run.set(false);
    fut2.get();
    // Wait when all messages are acknowledged to do not break next tests' logic.
    for (CommunicationSpi<Message> spi : spis.values()) {
        GridNioServer srv = U.field(spi, "nioSrvr");
        Collection<? extends GridNioSession> sessions = GridTestUtils.getFieldValue(srv, "sessions");
        for (GridNioSession ses : sessions) {
            final GridNioRecoveryDescriptor snd = ses.outRecoveryDescriptor();
            if (snd != null) {
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return snd.messagesRequests().isEmpty();
                    }
                }, 10_000);
                assertEquals("Unexpected messages: " + snd.messagesRequests(), 0, snd.messagesRequests().size());
            }
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNioServer(org.apache.ignite.internal.util.nio.GridNioServer) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridNioRecoveryDescriptor(org.apache.ignite.internal.util.nio.GridNioRecoveryDescriptor)

Example 29 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project android by JetBrains.

the class RenderSecurityManagerTest method testThread2.

@Test
public void testThread2() throws Exception {
    // Check that when a new thread is created simultaneously from an unrelated
    // thread during rendering, that new thread does not pick up the security manager.
    //
    final CyclicBarrier barrier1 = new CyclicBarrier(2);
    final CyclicBarrier barrier2 = new CyclicBarrier(2);
    final CyclicBarrier barrier3 = new CyclicBarrier(4);
    final CyclicBarrier barrier4 = new CyclicBarrier(4);
    final CyclicBarrier barrier5 = new CyclicBarrier(4);
    final CyclicBarrier barrier6 = new CyclicBarrier(4);
    // First the threads reach barrier1. Then from barrier1 to barrier2, thread1
    // installs the security manager. Then from barrier2 to barrier3, thread2
    // checks that it does not have any security restrictions, and creates thread3.
    // Thread1 will ensure that the security manager is working there, and it will
    // create thread4. Then after barrier3 (where thread3 and thread4 are now also
    // participating) thread3 will ensure that it too has no security restrictions,
    // and thread4 will ensure that it does. At barrier4 the security manager gets
    // uninstalled, and at barrier5 all threads will check that there are no more
    // restrictions. At barrier6 all threads are done.
    final Thread thread1 = new Thread("render") {

        @Override
        public void run() {
            try {
                barrier1.await();
                assertNull(RenderSecurityManager.getCurrent());
                RenderSecurityManager manager = new RenderSecurityManager(null, null);
                manager.setActive(true, myCredential);
                barrier2.await();
                Thread thread4 = new Thread() {

                    @Override
                    public void run() {
                        try {
                            barrier3.await();
                            try {
                                System.getProperties();
                                fail("Should have thrown security exception");
                            } catch (SecurityException e) {
                            // pass
                            }
                            barrier4.await();
                            barrier5.await();
                            assertNull(RenderSecurityManager.getCurrent());
                            assertNull(System.getSecurityManager());
                            barrier6.await();
                        } catch (InterruptedException e) {
                            fail(e.toString());
                        } catch (BrokenBarrierException e) {
                            fail(e.toString());
                        }
                    }
                };
                thread4.start();
                try {
                    System.getProperties();
                    fail("Should have thrown security exception");
                } catch (SecurityException e) {
                // expected
                }
                barrier3.await();
                barrier4.await();
                manager.dispose(myCredential);
                assertNull(RenderSecurityManager.getCurrent());
                assertNull(System.getSecurityManager());
                barrier5.await();
                barrier6.await();
            } catch (InterruptedException e) {
                fail(e.toString());
            } catch (BrokenBarrierException e) {
                fail(e.toString());
            }
        }
    };
    final Thread thread2 = new Thread("unrelated") {

        @Override
        public void run() {
            try {
                barrier1.await();
                assertNull(RenderSecurityManager.getCurrent());
                barrier2.await();
                assertNull(RenderSecurityManager.getCurrent());
                assertNotNull(System.getSecurityManager());
                try {
                    System.getProperties();
                } catch (SecurityException e) {
                    fail("Should not have been affected by security manager");
                }
                Thread thread3 = new Thread() {

                    @Override
                    public void run() {
                        try {
                            barrier3.await();
                            try {
                                System.getProperties();
                            } catch (SecurityException e) {
                                fail("Should not have been affected by security manager");
                            }
                            barrier4.await();
                            barrier5.await();
                            assertNull(RenderSecurityManager.getCurrent());
                            assertNull(System.getSecurityManager());
                            barrier6.await();
                        } catch (InterruptedException e) {
                            fail(e.toString());
                        } catch (BrokenBarrierException e) {
                            fail(e.toString());
                        }
                    }
                };
                thread3.start();
                barrier3.await();
                barrier4.await();
                barrier5.await();
                assertNull(RenderSecurityManager.getCurrent());
                assertNull(System.getSecurityManager());
                barrier6.await();
            } catch (InterruptedException e) {
                fail(e.toString());
            } catch (BrokenBarrierException e) {
                fail(e.toString());
            }
        }
    };
    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 30 with BrokenBarrierException

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

the class TestClientRMService method testConcurrentAppSubmit.

@Test(timeout = 4000)
public void testConcurrentAppSubmit() throws IOException, InterruptedException, BrokenBarrierException, YarnException {
    YarnScheduler yarnScheduler = mockYarnScheduler();
    RMContext rmContext = mock(RMContext.class);
    mockRMContext(yarnScheduler, rmContext);
    RMStateStore stateStore = mock(RMStateStore.class);
    when(rmContext.getStateStore()).thenReturn(stateStore);
    RMAppManager appManager = new RMAppManager(rmContext, yarnScheduler, null, mock(ApplicationACLsManager.class), new Configuration());
    final ApplicationId appId1 = getApplicationId(100);
    final ApplicationId appId2 = getApplicationId(101);
    final SubmitApplicationRequest submitRequest1 = mockSubmitAppRequest(appId1, null, null);
    final SubmitApplicationRequest submitRequest2 = mockSubmitAppRequest(appId2, null, null);
    final CyclicBarrier startBarrier = new CyclicBarrier(2);
    final CyclicBarrier endBarrier = new CyclicBarrier(2);
    EventHandler<Event> eventHandler = new EventHandler<Event>() {

        @Override
        public void handle(Event rawEvent) {
            if (rawEvent instanceof RMAppEvent) {
                RMAppEvent event = (RMAppEvent) rawEvent;
                if (event.getApplicationId().equals(appId1)) {
                    try {
                        startBarrier.await();
                        endBarrier.await();
                    } catch (BrokenBarrierException e) {
                        LOG.warn("Broken Barrier", e);
                    } catch (InterruptedException e) {
                        LOG.warn("Interrupted while awaiting barriers", e);
                    }
                }
            }
        }
    };
    when(rmContext.getDispatcher().getEventHandler()).thenReturn(eventHandler);
    doReturn(mock(RMTimelineCollectorManager.class)).when(rmContext).getRMTimelineCollectorManager();
    final ClientRMService rmService = new ClientRMService(rmContext, yarnScheduler, appManager, null, null, null);
    rmService.init(new Configuration());
    // submit an app and wait for it to block while in app submission
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                rmService.submitApplication(submitRequest1);
            } catch (YarnException | IOException e) {
            }
        }
    };
    t.start();
    // submit another app, so go through while the first app is blocked
    startBarrier.await();
    rmService.submitApplication(submitRequest2);
    endBarrier.await();
    t.join();
}
Also used : RMStateStore(org.apache.hadoop.yarn.server.resourcemanager.recovery.RMStateStore) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CapacitySchedulerConfiguration(org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) EventHandler(org.apache.hadoop.yarn.event.EventHandler) IOException(java.io.IOException) SubmitApplicationRequest(org.apache.hadoop.yarn.api.protocolrecords.SubmitApplicationRequest) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) CyclicBarrier(java.util.concurrent.CyclicBarrier) RMAppEvent(org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppEvent) ApplicationACLsManager(org.apache.hadoop.yarn.server.security.ApplicationACLsManager) RMTimelineCollectorManager(org.apache.hadoop.yarn.server.resourcemanager.timelineservice.RMTimelineCollectorManager) YarnScheduler(org.apache.hadoop.yarn.server.resourcemanager.scheduler.YarnScheduler) Event(org.apache.hadoop.yarn.event.Event) RMAppEvent(org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppEvent) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Test(org.junit.Test)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)62 CyclicBarrier (java.util.concurrent.CyclicBarrier)53 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 List (java.util.List)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 TimeoutException (java.util.concurrent.TimeoutException)8 Random (java.util.Random)7 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6 TimeUnit (java.util.concurrent.TimeUnit)6