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();
}
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);
}
}
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);
}
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();
}
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());
}
Aggregations