Search in sources :

Example 51 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)

Example 52 with BrokenBarrierException

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

the class TestSimpleRequestController method testWaitForMaximumCurrentTasks.

@Test
public void testWaitForMaximumCurrentTasks() throws Exception {
    final AtomicInteger max = new AtomicInteger(0);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    SimpleRequestController controller = new SimpleRequestController(HBaseConfiguration.create());
    final AtomicLong tasks = controller.tasksInProgress;
    Runnable runnable = () -> {
        try {
            barrier.await();
            controller.waitForMaximumCurrentTasks(max.get(), 123, 1, null);
        } catch (InterruptedIOException e) {
            Assert.fail(e.getMessage());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    };
    // First test that our runnable thread only exits when tasks is zero.
    Thread t = new Thread(runnable);
    t.start();
    barrier.await();
    t.join();
    // Now assert we stay running if max == zero and tasks is > 0.
    barrier.reset();
    tasks.set(1000000);
    t = new Thread(runnable);
    t.start();
    barrier.await();
    while (tasks.get() > 0) {
        assertTrue(t.isAlive());
        tasks.set(tasks.get() - 1);
    }
    t.join();
}
Also used : InterruptedIOException(java.io.InterruptedIOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 53 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project Smack by igniterealtime.

the class PacketWriterTest method shouldBlockAndUnblockTest.

/**
     * Make sure that stanza(/packet) writer does block once the queue reaches
     * {@link PacketWriter#QUEUE_SIZE} and that
     * {@link PacketWriter#sendStanza(org.jivesoftware.smack.tcp.packet.Packet)} does unblock after the
     * interrupt.
     * 
     * @throws InterruptedException
     * @throws BrokenBarrierException
     * @throws NotConnectedException 
     * @throws XmppStringprepException 
     */
@SuppressWarnings("javadoc")
@Test
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException, XmppStringprepException {
    XMPPTCPConnection connection = new XMPPTCPConnection("user", "pass", "example.org");
    final PacketWriter pw = connection.new PacketWriter();
    connection.packetWriter = pw;
    connection.packetReader = connection.new PacketReader();
    connection.setWriter(new BlockingStringWriter());
    pw.init();
    for (int i = 0; i < XMPPTCPConnection.PacketWriter.QUEUE_SIZE; i++) {
        pw.sendStreamElement(new Message());
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    shutdown = false;
    prematureUnblocked = false;
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                barrier.await();
                pw.sendStreamElement(new Message());
                // should only return after the pw was interrupted
                if (!shutdown) {
                    prematureUnblocked = true;
                }
            } catch (Exception e) {
            }
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
            }
        }
    });
    t.start();
    // This barrier is not strictly necessary, but may increases the chances that the threat
    // will block before we call shutdown. Otherwise we may get false positives (which is still
    // better then false negatives).
    barrier.await();
    // Not really cool, but may increases the chances for 't' to block in sendStanza.
    Thread.sleep(250);
    // Set to true for testing purposes, so that shutdown() won't wait packet writer
    pw.shutdownDone.reportSuccess();
    // Shutdown the packetwriter
    pw.shutdown(false);
    shutdown = true;
    barrier.await();
    if (prematureUnblocked) {
        fail("Should not unblock before the thread got shutdown");
    }
    synchronized (t) {
        t.notify();
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) PacketWriter(org.jivesoftware.smack.tcp.XMPPTCPConnection.PacketWriter) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 54 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project redisson by redisson.

the class RedissonSemaphoreTest method testConcurrency_MultiInstance_10_permits.

@Test
public void testConcurrency_MultiInstance_10_permits() throws InterruptedException {
    Assume.assumeFalse(RedissonRuntimeEnvironment.isTravis);
    int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    final AtomicInteger checkPermits = new AtomicInteger(s.availablePermits());
    final CyclicBarrier barrier = new CyclicBarrier(s.availablePermits());
    testMultiInstanceConcurrencySequentiallyLaunched(iterations, r -> {
        RSemaphore s1 = r.getSemaphore("test");
        try {
            s1.acquire();
            barrier.await();
            if (checkPermits.decrementAndGet() > 0) {
                assertThat(s1.availablePermits()).isEqualTo(0);
                assertThat(s1.tryAcquire()).isFalse();
            } else {
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });
    System.out.println(lockedCounter.get());
    assertThat(lockedCounter.get()).isLessThan(iterations);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RSemaphore(org.redisson.api.RSemaphore) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 55 with BrokenBarrierException

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

the class TestNodeManagerResync method testContainerResourceIncreaseIsSynchronizedWithRMResync.

@SuppressWarnings("unchecked")
@Test(timeout = 60000)
public void testContainerResourceIncreaseIsSynchronizedWithRMResync() throws IOException, InterruptedException, YarnException {
    NodeManager nm = new TestNodeManager4();
    YarnConfiguration conf = createNMConfig();
    conf.setBoolean(YarnConfiguration.RM_WORK_PRESERVING_RECOVERY_ENABLED, true);
    nm.init(conf);
    nm.start();
    // Start a container and make sure it is in RUNNING state
    ((TestNodeManager4) nm).startContainer();
    // Simulate a container resource increase in a separate thread
    ((TestNodeManager4) nm).increaseContainersResource();
    // Simulate RM restart by sending a RESYNC event
    LOG.info("Sending out RESYNC event");
    nm.getNMDispatcher().getEventHandler().handle(new NodeManagerEvent(NodeManagerEventType.RESYNC));
    try {
        syncBarrier.await();
    } catch (BrokenBarrierException e) {
        e.printStackTrace();
    }
    Assert.assertFalse(assertionFailedInThread.get());
    nm.stop();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) BaseContainerManagerTest(org.apache.hadoop.yarn.server.nodemanager.containermanager.BaseContainerManagerTest) 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