Search in sources :

Example 46 with BrokenBarrierException

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 47 with BrokenBarrierException

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;
}
Also used : Arrays(java.util.Arrays) CyclicBarrier(java.util.concurrent.CyclicBarrier) Tombstone(org.hibernate.cache.infinispan.util.Tombstone) TimeoutException(java.util.concurrent.TimeoutException) Assert.assertTrue(org.junit.Assert.assertTrue) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) TestForIssue(org.hibernate.testing.TestForIssue) List(java.util.List) Future(java.util.concurrent.Future) Assert.assertFalse(org.junit.Assert.assertFalse) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) Assert.assertEquals(org.junit.Assert.assertEquals) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor)

Example 48 with BrokenBarrierException

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]);
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SSLException(javax.net.ssl.SSLException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.Test)

Example 49 with BrokenBarrierException

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();
}
Also used : RMContext(org.apache.hadoop.yarn.server.resourcemanager.RMContext) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) DelegationTokenIdentifier(org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier) InetSocketAddress(java.net.InetSocketAddress) Text(org.apache.hadoop.io.Text) ByteBuffer(java.nio.ByteBuffer) DataInputByteBuffer(org.apache.hadoop.io.DataInputByteBuffer) CyclicBarrier(java.util.concurrent.CyclicBarrier) ClientRMService(org.apache.hadoop.yarn.server.resourcemanager.ClientRMService) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Credentials(org.apache.hadoop.security.Credentials) Test(org.junit.Test)

Example 50 with BrokenBarrierException

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");
}
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)

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