Search in sources :

Example 16 with CyclicBarrier

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

the class TestLeafQueue method testConcurrentAccess.

@Test
public void testConcurrentAccess() throws Exception {
    YarnConfiguration conf = new YarnConfiguration();
    MockRM rm = new MockRM();
    rm.init(conf);
    rm.start();
    final String queue = "default";
    final String user = "user";
    CapacityScheduler cs = (CapacityScheduler) rm.getResourceScheduler();
    final LeafQueue defaultQueue = (LeafQueue) cs.getQueue(queue);
    final List<FiCaSchedulerApp> listOfApps = createListOfApps(10000, user, defaultQueue);
    final CyclicBarrier cb = new CyclicBarrier(2);
    final List<ConcurrentModificationException> conException = new ArrayList<ConcurrentModificationException>();
    Thread submitAndRemove = new Thread(new Runnable() {

        @Override
        public void run() {
            for (FiCaSchedulerApp fiCaSchedulerApp : listOfApps) {
                defaultQueue.submitApplicationAttempt(fiCaSchedulerApp, user);
            }
            try {
                cb.await();
            } catch (Exception e) {
            // Ignore
            }
            for (FiCaSchedulerApp fiCaSchedulerApp : listOfApps) {
                defaultQueue.finishApplicationAttempt(fiCaSchedulerApp, queue);
            }
        }
    }, "SubmitAndRemoveApplicationAttempt Thread");
    Thread getAppsInQueue = new Thread(new Runnable() {

        List<ApplicationAttemptId> apps = new ArrayList<ApplicationAttemptId>();

        @Override
        public void run() {
            try {
                try {
                    cb.await();
                } catch (Exception e) {
                // Ignore
                }
                defaultQueue.collectSchedulerApplications(apps);
            } catch (ConcurrentModificationException e) {
                conException.add(e);
            }
        }
    }, "GetAppsInQueue Thread");
    submitAndRemove.start();
    getAppsInQueue.start();
    submitAndRemove.join();
    getAppsInQueue.join();
    assertTrue("ConcurrentModificationException is thrown", conException.isEmpty());
    rm.stop();
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) ArrayList(java.util.ArrayList) MockRM(org.apache.hadoop.yarn.server.resourcemanager.MockRM) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) FiCaSchedulerApp(org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.fica.FiCaSchedulerApp) Test(org.junit.Test)

Example 17 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project hive by apache.

the class TestSessionManagerMetrics method testThreadPoolMetrics.

/**
   * Tests metrics regarding async thread pool.
   *
   * The test does the following steps:
   * - Submit four tasks
   * - Wait with the metrics verification, until the first two tasks are running.
   * If, for some reason, the tasks are not started within a timeout period, make the test fail.
   * - Make the tasks wait until the metrics are checked.
   * - Verify the metrics. Both the EXEC_ASYNC_POOL_SIZE and EXEC_ASYNC_QUEUE_SIZE should be 2.
   * - Let the first two tasks complete, so the remaining two tasks can be removed from the queue and started.
   * - Wait until the remaining tasks are running.
   * Do the metrics check only if they are started to avoid the failures when the queue size was not 0.
   * If, for some reason, the tasks are not started within a timeout period, make the test fail.
   * - Verify the metrics.
   * The EXEC_ASYNC_POOL_SIZE should be 2 and the EXEC_ASYNC_QUEUE_SIZE should be 0.
   * - Let the remaining tasks complete.
   */
@Test
public void testThreadPoolMetrics() throws Exception {
    String errorMessage = null;
    CyclicBarrier ready = new CyclicBarrier(3);
    CyclicBarrier completed = new CyclicBarrier(3);
    try {
        sm.submitBackgroundOperation(new BarrierRunnable(ready, completed));
        sm.submitBackgroundOperation(new BarrierRunnable(ready, completed));
        sm.submitBackgroundOperation(new BarrierRunnable(ready, completed));
        sm.submitBackgroundOperation(new BarrierRunnable(ready, completed));
        errorMessage = String.format(FAIL_TO_START_MSG, "first");
        ready.await(BARRIER_AWAIT_TIMEOUT, TimeUnit.SECONDS);
        ready.reset();
        String json = metrics.dumpJson();
        MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_POOL_SIZE, 2);
        MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_QUEUE_SIZE, 2);
        errorMessage = String.format(FAIL_TO_COMPLETE_MSG, "first");
        completed.await(BARRIER_AWAIT_TIMEOUT, TimeUnit.SECONDS);
        completed.reset();
        errorMessage = String.format(FAIL_TO_START_MSG, "second");
        ready.await(BARRIER_AWAIT_TIMEOUT, TimeUnit.SECONDS);
        json = metrics.dumpJson();
        MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_POOL_SIZE, 2);
        MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_QUEUE_SIZE, 0);
        errorMessage = String.format(FAIL_TO_COMPLETE_MSG, "second");
        completed.await(BARRIER_AWAIT_TIMEOUT, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        Assert.fail(errorMessage);
    }
}
Also used : CyclicBarrier(java.util.concurrent.CyclicBarrier) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 18 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project hive by apache.

the class TestSessionManagerMetrics method testActiveSessionMetrics.

@Test
public void testActiveSessionMetrics() throws Exception {
    final CyclicBarrier ready = new CyclicBarrier(2);
    CyclicBarrier completed = new CyclicBarrier(2);
    String json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_ACTIVE_SESSIONS, 0);
    SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>());
    final HiveSession session = sm.getSession(handle);
    OperationManager operationManager = mock(OperationManager.class);
    when(operationManager.newGetTablesOperation(session, "catalog", "schema", "table", null)).thenReturn(new BlockingOperation(session, OperationType.GET_TABLES, ready, completed));
    session.setOperationManager(operationManager);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                OperationHandle handle = session.getTables("catalog", "schema", "table", null);
                session.closeOperation(handle);
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    ready.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                // ignore
                }
            }
        }
    }).start();
    ready.await(2, TimeUnit.SECONDS);
    ready.reset();
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_ACTIVE_SESSIONS, 1);
    completed.await(2, TimeUnit.SECONDS);
    ready.await(2, TimeUnit.SECONDS);
    json = metrics.dumpJson();
    MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_ACTIVE_SESSIONS, 0);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) SessionHandle(org.apache.hive.service.cli.SessionHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) TimeoutException(java.util.concurrent.TimeoutException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) OperationManager(org.apache.hive.service.cli.operation.OperationManager) Test(org.junit.Test)

Example 19 with CyclicBarrier

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

the class TestRegionStates method testCanMakeProgressThoughMetaIsDown.

@Test(timeout = 10000)
public void testCanMakeProgressThoughMetaIsDown() throws IOException, InterruptedException, BrokenBarrierException {
    MasterServices server = mock(MasterServices.class);
    when(server.getServerName()).thenReturn(ServerName.valueOf("master,1,1"));
    Connection connection = mock(ClusterConnection.class);
    // Set up a table that gets 'stuck' when we try to fetch a row from the meta table.
    // It is stuck on a CyclicBarrier latch. We use CyclicBarrier because it will tell us when
    // thread is waiting on latch.
    Table metaTable = Mockito.mock(Table.class);
    final CyclicBarrier latch = new CyclicBarrier(2);
    when(metaTable.get((Get) Mockito.any())).thenAnswer(new Answer<Result>() {

        @Override
        public Result answer(InvocationOnMock invocation) throws Throwable {
            latch.await();
            throw new java.net.ConnectException("Connection refused");
        }
    });
    when(connection.getTable(TableName.META_TABLE_NAME)).thenReturn(metaTable);
    when(server.getConnection()).thenReturn((ClusterConnection) connection);
    Configuration configuration = mock(Configuration.class);
    when(server.getConfiguration()).thenReturn(configuration);
    TableStateManager tsm = mock(TableStateManager.class);
    ServerManager sm = mock(ServerManager.class);
    when(sm.isServerOnline(isA(ServerName.class))).thenReturn(true);
    RegionStateStore rss = mock(RegionStateStore.class);
    final RegionStates regionStates = new RegionStates(server, tsm, sm, rss);
    final ServerName sn = mockServer("one", 1);
    regionStates.updateRegionState(HRegionInfo.FIRST_META_REGIONINFO, State.SPLITTING_NEW, sn);
    Thread backgroundThread = new Thread("Get stuck setting server offline") {

        @Override
        public void run() {
            regionStates.serverOffline(sn);
        }
    };
    assertTrue(latch.getNumberWaiting() == 0);
    backgroundThread.start();
    while (latch.getNumberWaiting() == 0) ;
    // Verify I can do stuff with synchronized RegionStates methods, that I am not locked out.
    // Below is a call that is synchronized.  Can I do it and not block?
    regionStates.getRegionServerOfRegion(HRegionInfo.FIRST_META_REGIONINFO);
    // Done. Trip the barrier on the background thread.
    latch.await();
}
Also used : Table(org.apache.hadoop.hbase.client.Table) Configuration(org.apache.hadoop.conf.Configuration) ClusterConnection(org.apache.hadoop.hbase.client.ClusterConnection) Connection(org.apache.hadoop.hbase.client.Connection) CyclicBarrier(java.util.concurrent.CyclicBarrier) Result(org.apache.hadoop.hbase.client.Result) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServerName(org.apache.hadoop.hbase.ServerName) Test(org.junit.Test)

Example 20 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project druid by druid-io.

the class BytesBoundedLinkedQueueTest method testOfferAndPut.

@Test
public void testOfferAndPut() throws Exception {
    final BlockingQueue<TestObject> q = getQueue(10);
    try {
        q.offer(null);
        Assert.fail();
    } catch (NullPointerException success) {
    }
    final TestObject obj = new TestObject(2);
    while (q.remainingCapacity() > 0) {
        Assert.assertTrue(q.offer(obj, delayMS, TimeUnit.MILLISECONDS));
    }
    // queue full
    Assert.assertEquals(0, q.remainingCapacity());
    Assert.assertFalse(q.offer(obj, delayMS, TimeUnit.MILLISECONDS));
    Assert.assertFalse(q.offer(obj));
    final CyclicBarrier barrier = new CyclicBarrier(2);
    Future<Boolean> future = exec.submit(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            barrier.await();
            Assert.assertTrue(q.offer(obj, delayMS, TimeUnit.MILLISECONDS));
            Assert.assertEquals(q.remainingCapacity(), 0);
            barrier.await();
            q.put(obj);
            return true;
        }
    });
    barrier.await();
    q.take();
    barrier.await();
    q.take();
    Assert.assertTrue(future.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(java.util.concurrent.TimeoutException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)322 Test (org.junit.Test)112 CountDownLatch (java.util.concurrent.CountDownLatch)89 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)77 IOException (java.io.IOException)65 ArrayList (java.util.ArrayList)65 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)64 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)63 ExecutorService (java.util.concurrent.ExecutorService)39 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 Ignite (org.apache.ignite.Ignite)33 IgniteException (org.apache.ignite.IgniteException)32 Test (org.testng.annotations.Test)26 IgniteCache (org.apache.ignite.IgniteCache)25 List (java.util.List)24 TimeoutException (java.util.concurrent.TimeoutException)22 ExecutionException (java.util.concurrent.ExecutionException)21 Transaction (org.apache.ignite.transactions.Transaction)21 Callable (java.util.concurrent.Callable)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19