use of java.util.concurrent.BrokenBarrierException in project elasticsearch by elastic.
the class LocalCheckpointTrackerTests method testWaitForOpsToComplete.
public void testWaitForOpsToComplete() throws BrokenBarrierException, InterruptedException {
final int seqNo = randomIntBetween(0, 32);
final CyclicBarrier barrier = new CyclicBarrier(2);
final AtomicBoolean complete = new AtomicBoolean();
final Thread thread = new Thread(() -> {
try {
// sychronize starting with the test thread
barrier.await();
tracker.waitForOpsToComplete(seqNo);
complete.set(true);
// synchronize with the test thread checking if we are no longer waiting
barrier.await();
} catch (BrokenBarrierException | InterruptedException e) {
throw new RuntimeException(e);
}
});
thread.start();
// synchronize starting with the waiting thread
barrier.await();
final List<Integer> elements = IntStream.rangeClosed(0, seqNo).boxed().collect(Collectors.toList());
Randomness.shuffle(elements);
for (int i = 0; i < elements.size() - 1; i++) {
tracker.markSeqNoAsCompleted(elements.get(i));
assertFalse(complete.get());
}
tracker.markSeqNoAsCompleted(elements.get(elements.size() - 1));
// synchronize with the waiting thread to mark that it is complete
barrier.await();
assertTrue(complete.get());
thread.join();
}
use of java.util.concurrent.BrokenBarrierException in project hibernate-orm by hibernate.
the class TombstoneTest method blockedPutFromLoad.
private Future<?> blockedPutFromLoad(CyclicBarrier putFromLoadBarrier) throws InterruptedException, BrokenBarrierException, TimeoutException {
BlockingInterceptor blockingInterceptor = new BlockingInterceptor(putFromLoadBarrier, PutKeyValueCommand.class, false, true);
entityCache.addInterceptor(blockingInterceptor, 0);
cleanup.add(() -> entityCache.removeInterceptor(BlockingInterceptor.class));
// the putFromLoad should be blocked in the interceptor
Future<?> putFromLoad = executor.submit(() -> withTxSessionApply(s -> {
assertEquals("Original item", s.load(Item.class, itemId).getDescription());
return null;
}));
putFromLoadBarrier.await(WAIT_TIMEOUT, TimeUnit.SECONDS);
blockingInterceptor.suspend(true);
return putFromLoad;
}
use of java.util.concurrent.BrokenBarrierException in project h2o-3 by h2oai.
the class SSLSocketChannelFactoryTest method shouldHandshake.
@Test
public void shouldHandshake() throws IOException, SSLContextException, BrokenBarrierException, InterruptedException {
SSLProperties props = new SSLProperties();
props.put("h2o_ssl_protocol", SecurityUtils.defaultTLSVersion());
props.put("h2o_ssl_jks_internal", getFile("src/test/resources/keystore.jks").getPath());
props.put("h2o_ssl_jks_password", "password");
props.put("h2o_ssl_jts", getFile("src/test/resources/cacerts.jks").getPath());
props.put("h2o_ssl_jts_password", "password");
final SSLSocketChannelFactory factory = new SSLSocketChannelFactory(props);
final CyclicBarrier barrier = new CyclicBarrier(2);
final CyclicBarrier testOne = new CyclicBarrier(2);
final CyclicBarrier testTwo = new CyclicBarrier(2);
final CyclicBarrier testThree = new CyclicBarrier(2);
final boolean[] hs = new boolean[] { true };
Thread client = new ClientThread(factory, testOne, testTwo, testThree, barrier);
client.setDaemon(false);
client.start();
try {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReceiveBufferSize(64 * 1024);
while (true) {
try {
serverSocketChannel.socket().bind(new InetSocketAddress(port));
break;
} catch (BindException e) {
port++;
}
}
barrier.await();
SocketChannel sock = serverSocketChannel.accept();
barrier.reset();
SSLSocketChannel wrappedChannel = (SSLSocketChannel) factory.wrapServerChannel(sock);
assertTrue(wrappedChannel.isHandshakeComplete());
// FIRST TEST: SSL -> SSL SMALL COMMUNICATION
ByteBuffer readBuffer = ByteBuffer.allocate(12);
while (readBuffer.hasRemaining()) {
wrappedChannel.read(readBuffer);
}
readBuffer.flip();
byte[] dst = new byte[12];
readBuffer.get(dst, 0, 12);
readBuffer.clear();
assertEquals("hello, world", new String(dst, "UTF-8"));
testOne.await();
// SECOND TEST: SSL -> SSL BIG COMMUNICATION
int read = 0;
byte[] dstBig = new byte[16];
ByteBuffer readBufferBig = ByteBuffer.allocate(1024);
while (read < 5 * 64 * 1024) {
while (readBufferBig.position() < 16) {
wrappedChannel.read(readBufferBig);
}
readBufferBig.flip();
readBufferBig.get(dstBig, 0, 16);
if (!readBufferBig.hasRemaining()) {
readBufferBig.clear();
} else {
readBufferBig.compact();
}
assertEquals("hello, world" + (read % 9) + "!!!", new String(dstBig, "UTF-8"));
read += 16;
}
testTwo.await();
// THIRD TEST: NON-SSL -> SSL COMMUNICATION
try {
while (readBuffer.hasRemaining()) {
wrappedChannel.read(readBuffer);
}
fail();
} catch (SSLException e) {
// PASSED
}
assertTrue(wrappedChannel.getEngine().isInboundDone());
testThree.await();
// FOURTH TEST: SSL -> NON-SSL COMMUNICATION
readBuffer.clear();
while (readBuffer.hasRemaining()) {
sock.read(readBuffer);
}
readBuffer.flip();
readBuffer.get(dst, 0, 12);
readBuffer.clear();
assertNotEquals("hello, world", new String(dst, "UTF-8"));
} catch (IOException | InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
barrier.await();
assertTrue("One of the handshakes failed!", hs[0]);
}
use of java.util.concurrent.BrokenBarrierException in project hadoop by apache.
the class TestDelegationTokenRenewer method testConcurrentAddApplication.
@Test(timeout = 20000)
public void testConcurrentAddApplication() throws IOException, InterruptedException, BrokenBarrierException {
final CyclicBarrier startBarrier = new CyclicBarrier(2);
final CyclicBarrier endBarrier = new CyclicBarrier(2);
// this token uses barriers to block during renew
final Credentials creds1 = new Credentials();
final Token<DelegationTokenIdentifier> token1 = mock(Token.class);
when(token1.getKind()).thenReturn(KIND);
DelegationTokenIdentifier dtId1 = new DelegationTokenIdentifier(new Text("user1"), new Text("renewer"), new Text("user1"));
when(token1.decodeIdentifier()).thenReturn(dtId1);
creds1.addToken(new Text("token"), token1);
doReturn(true).when(token1).isManaged();
doAnswer(new Answer<Long>() {
public Long answer(InvocationOnMock invocation) throws InterruptedException, BrokenBarrierException {
startBarrier.await();
endBarrier.await();
return Long.MAX_VALUE;
}
}).when(token1).renew(any(Configuration.class));
// this dummy token fakes renewing
final Credentials creds2 = new Credentials();
final Token<DelegationTokenIdentifier> token2 = mock(Token.class);
when(token2.getKind()).thenReturn(KIND);
when(token2.decodeIdentifier()).thenReturn(dtId1);
creds2.addToken(new Text("token"), token2);
doReturn(true).when(token2).isManaged();
doReturn(Long.MAX_VALUE).when(token2).renew(any(Configuration.class));
// fire up the renewer
final DelegationTokenRenewer dtr = createNewDelegationTokenRenewer(conf, counter);
RMContext mockContext = mock(RMContext.class);
when(mockContext.getSystemCredentialsForApps()).thenReturn(new ConcurrentHashMap<ApplicationId, ByteBuffer>());
ClientRMService mockClientRMService = mock(ClientRMService.class);
when(mockContext.getClientRMService()).thenReturn(mockClientRMService);
InetSocketAddress sockAddr = InetSocketAddress.createUnresolved("localhost", 1234);
when(mockClientRMService.getBindAddress()).thenReturn(sockAddr);
dtr.setRMContext(mockContext);
when(mockContext.getDelegationTokenRenewer()).thenReturn(dtr);
dtr.init(conf);
dtr.start();
// submit a job that blocks during renewal
Thread submitThread = new Thread() {
@Override
public void run() {
dtr.addApplicationAsync(mock(ApplicationId.class), creds1, false, "user", new Configuration());
}
};
submitThread.start();
// wait till 1st submit blocks, then submit another
startBarrier.await();
dtr.addApplicationAsync(mock(ApplicationId.class), creds2, false, "user", new Configuration());
// signal 1st to complete
endBarrier.await();
submitThread.join();
}
use of java.util.concurrent.BrokenBarrierException in project hive by apache.
the class TestSessionManagerMetrics method testActiveSessionTimeMetrics.
@Test
public void testActiveSessionTimeMetrics() throws Exception {
final CyclicBarrier ready = new CyclicBarrier(2);
CyclicBarrier completed = new CyclicBarrier(2);
String json = metrics.dumpJson();
MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, "NaN");
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);
long sessionActivateTime = System.currentTimeMillis();
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_AVG_ACTIVE_SESSION_TIME, (double) System.currentTimeMillis() - sessionActivateTime, 100d);
completed.await(2, TimeUnit.SECONDS);
ready.await(2, TimeUnit.SECONDS);
json = metrics.dumpJson();
MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, "NaN");
}
Aggregations