Java Code Examples for java.util.concurrent.CyclicBarrier#reset()

The following examples show how to use java.util.concurrent.CyclicBarrier#reset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CyclicBarrierTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * A reset before threads enter barrier does not throw
 * BrokenBarrierException
 */
public void testReset_NoBrokenBarrier() throws Exception {
    final CyclicBarrier c = new CyclicBarrier(3);
    c.reset();

    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws Exception {
            c.await();
        }});
    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws Exception {
            c.await();
        }});

    c.await();
    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example 2
Source File: CyclicBarrierTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A reset of an active barrier causes waiting threads to throw
 * BrokenBarrierException
 */
public void testReset_BrokenBarrier() throws InterruptedException {
    final CyclicBarrier c = new CyclicBarrier(3);
    final CountDownLatch pleaseReset = new CountDownLatch(2);
    Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
        public void realRun() throws Exception {
            pleaseReset.countDown();
            c.await();
        }};
    Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
        public void realRun() throws Exception {
            pleaseReset.countDown();
            c.await();
        }};

    t1.start();
    t2.start();
    await(pleaseReset);

    awaitNumberWaiting(c, 2);
    c.reset();
    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example 3
Source File: CyclicBarrierTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A reset before threads enter barrier does not throw
 * BrokenBarrierException
 */
public void testReset_NoBrokenBarrier() throws Exception {
    final CyclicBarrier c = new CyclicBarrier(3);
    c.reset();

    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws Exception {
            c.await();
        }});
    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws Exception {
            c.await();
        }});

    c.await();
    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example 4
Source File: JobRunnerTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static StepRunner waitingRunner(CyclicBarrier barrier) {
    return (step, id) -> {
        try {
            if (step.get() == deployTester) {
                barrier.await(); // Wake up the main thread, which waits for this step to be locked.
                barrier.reset();
                barrier.await(); // Then wait while holding the lock for this step, until the main thread wakes us up.
            }
        }
        catch (InterruptedException | BrokenBarrierException e) {
            throw new AssertionError(e);
        }
        return Optional.of(running);
    };
}
 
Example 5
Source File: CyclicBarrierTest.java    From algorithms with MIT License 5 votes vote down vote up
@Test
public void shouldReset() throws InterruptedException {
    CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
    AtomicInteger value = new AtomicInteger(0);
    Runnable runnable = () -> {
        try {
            cyclicBarrier.await();
            value.incrementAndGet();
            cyclicBarrier.await();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
    Thread thread1 = new Thread(runnable);
    Thread thread2 = new Thread(runnable);
    thread1.start();
    thread2.start();
    thread1.join();
    thread2.join();
    assertThat(value.get()).isEqualTo(2);
    cyclicBarrier.reset();
    Thread thread3 = new Thread(runnable);
    Thread thread4 = new Thread(runnable);
    thread3.start();
    thread4.start();
    thread3.join();
    thread4.join();
    assertThat(value.get()).isEqualTo(4);
}
 
Example 6
Source File: CyclicBarrierTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Reset of a barrier after interruption reinitializes it.
 */
public void testResetAfterInterrupt() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(3);
    for (int i = 0; i < 2; i++) {
        final CyclicBarrier start = new CyclicBarrier(3);
        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }};

        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }};

        t1.start();
        t2.start();
        start.await();
        t1.interrupt();
        awaitTermination(t1);
        awaitTermination(t2);
        assertTrue(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
        barrier.reset();
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
    }
}
 
Example 7
Source File: CyclicBarrierTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Reset of a non-broken barrier does not break barrier
 */
public void testResetWithoutBreakage() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(3);
    for (int i = 0; i < 3; i++) {
        final CyclicBarrier start = new CyclicBarrier(3);
        Thread t1 = newStartedThread(new CheckedRunnable() {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }});

        Thread t2 = newStartedThread(new CheckedRunnable() {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }});

        start.await();
        barrier.await();
        awaitTermination(t1);
        awaitTermination(t2);
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
        if (i == 1) barrier.reset();
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
    }
}
 
Example 8
Source File: CyclicBarrierTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reset of a non-broken barrier does not break barrier
 */
public void testResetWithoutBreakage() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(3);
    for (int i = 0; i < 3; i++) {
        final CyclicBarrier start = new CyclicBarrier(3);
        Thread t1 = newStartedThread(new CheckedRunnable() {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }});

        Thread t2 = newStartedThread(new CheckedRunnable() {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }});

        start.await();
        barrier.await();
        awaitTermination(t1);
        awaitTermination(t2);
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
        if (i == 1) barrier.reset();
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
    }
}
 
Example 9
Source File: CyclicBarrierTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reset of a barrier after interruption reinitializes it.
 */
public void testResetAfterInterrupt() throws Exception {
    final CyclicBarrier barrier = new CyclicBarrier(3);
    for (int i = 0; i < 2; i++) {
        final CyclicBarrier start = new CyclicBarrier(3);
        Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }};

        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }};

        t1.start();
        t2.start();
        start.await();
        t1.interrupt();
        awaitTermination(t1);
        awaitTermination(t2);
        assertTrue(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
        barrier.reset();
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
    }
}
 
Example 10
Source File: CyclicBarrierTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reset of a barrier after a failed command reinitializes it.
 */
public void testResetAfterCommandException() throws Exception {
    final CyclicBarrier barrier =
        new CyclicBarrier(3, new Runnable() {
                public void run() {
                    throw new NullPointerException(); }});
    for (int i = 0; i < 2; i++) {
        final CyclicBarrier start = new CyclicBarrier(3);
        Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }};

        Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
            public void realRun() throws Exception {
                start.await();
                barrier.await();
            }};

        t1.start();
        t2.start();
        start.await();
        awaitNumberWaiting(barrier, 2);
        try {
            barrier.await();
            shouldThrow();
        } catch (NullPointerException success) {}
        awaitTermination(t1);
        awaitTermination(t2);
        assertTrue(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
        barrier.reset();
        assertFalse(barrier.isBroken());
        assertEquals(0, barrier.getNumberWaiting());
    }
}
 
Example 11
Source File: LightwaveRfBindingFunctionalTest.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private Answer<DatagramPacket> sendMessageOnceUnlatched(final String data, final CyclicBarrier latch) {
    return new Answer<DatagramPacket>() {
        @Override
        public DatagramPacket answer(InvocationOnMock invocation)
                throws InterruptedException, BrokenBarrierException {
            Object[] args = invocation.getArguments();
            ((DatagramPacket) args[0]).setData(data.getBytes());
            latch.await();
            latch.reset();
            return null;
        }
    };
}
 
Example 12
Source File: SimpleProducerConsumerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "batch")
public void testConcurrentConsumerReceiveWhileReconnect(int batchMessageDelayMs) throws Exception {
    final int recvQueueSize = 100;
    final int numConsumersThreads = 10;

    String subName = UUID.randomUUID().toString();
    final Consumer<byte[]> consumer = pulsarClient.newConsumer()
            .topic("persistent://my-property/my-ns/my-topic7").subscriptionName(subName)
            .startMessageIdInclusive()
            .receiverQueueSize(recvQueueSize).subscribe();
    ExecutorService executor = Executors.newCachedThreadPool();

    final CyclicBarrier barrier = new CyclicBarrier(numConsumersThreads + 1);
    for (int i = 0; i < numConsumersThreads; i++) {
        executor.submit((Callable<Void>) () -> {
            barrier.await();
            consumer.receive();
            return null;
        });
    }

    barrier.await();
    // there will be 10 threads calling receive() from the same consumer and will block
    Thread.sleep(100);

    // we restart the broker to reconnect
    restartBroker();
    Thread.sleep(2000);

    // publish 100 messages so that the consumers blocked on receive() will now get the messages
    ProducerBuilder<byte[]> producerBuilder = pulsarClient.newProducer()
            .topic("persistent://my-property/my-ns/my-topic7");

    if (batchMessageDelayMs != 0) {
        producerBuilder.batchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
        producerBuilder.batchingMaxMessages(5);
        producerBuilder.enableBatching(true);
    }
    Producer<byte[]> producer = producerBuilder.create();
    for (int i = 0; i < recvQueueSize; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Thread.sleep(500);

    ConsumerImpl<byte[]> consumerImpl = (ConsumerImpl<byte[]>) consumer;
    // The available permits should be 10 and num messages in the queue should be 90
    Assert.assertEquals(consumerImpl.getAvailablePermits(), numConsumersThreads);
    Assert.assertEquals(consumerImpl.numMessagesInQueue(), recvQueueSize - numConsumersThreads);

    barrier.reset();
    for (int i = 0; i < numConsumersThreads; i++) {
        executor.submit((Callable<Void>) () -> {
            barrier.await();
            consumer.receive();
            return null;
        });
    }
    barrier.await();
    Thread.sleep(100);

    // The available permits should be 20 and num messages in the queue should be 80
    Assert.assertEquals(consumerImpl.getAvailablePermits(), numConsumersThreads * 2);
    Assert.assertEquals(consumerImpl.numMessagesInQueue(), recvQueueSize - (numConsumersThreads * 2));

    // clear the queue
    while (true) {
        Message<byte[]> msg = consumer.receive(1, TimeUnit.SECONDS);
        if (msg == null) {
            break;
        }
    }

    // The available permits should be 0 and num messages in the queue should be 0
    Assert.assertEquals(consumerImpl.getAvailablePermits(), 0);
    Assert.assertEquals(consumerImpl.numMessagesInQueue(), 0);

    barrier.reset();
    for (int i = 0; i < numConsumersThreads; i++) {
        executor.submit((Callable<Void>) () -> {
            barrier.await();
            consumer.receive();
            return null;
        });
    }
    barrier.await();
    // we again make 10 threads call receive() and get blocked
    Thread.sleep(100);

    restartBroker();
    Thread.sleep(2000);

    // The available permits should be 10 and num messages in the queue should be 90
    Assert.assertEquals(consumerImpl.getAvailablePermits(), numConsumersThreads);
    Assert.assertEquals(consumerImpl.numMessagesInQueue(), recvQueueSize - numConsumersThreads);
    consumer.close();
}
 
Example 13
Source File: DelayedClientTransportTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void reprocess_newStreamRacesWithReprocess() throws Exception {
  final CyclicBarrier barrier = new CyclicBarrier(2);
  // In both phases, we only expect the first pickSubchannel() call to block on the barrier.
  final AtomicBoolean nextPickShouldWait = new AtomicBoolean(true);
  ///////// Phase 1: reprocess() twice with the same picker
  SubchannelPicker picker = mock(SubchannelPicker.class);

  doAnswer(new Answer<PickResult>() {
      @Override
      @SuppressWarnings("CatchAndPrintStackTrace")
      public PickResult answer(InvocationOnMock invocation) throws Throwable {
        if (nextPickShouldWait.compareAndSet(true, false)) {
          try {
            barrier.await();
            return PickResult.withNoResult();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        return PickResult.withNoResult();
      }
  }).when(picker).pickSubchannel(any(PickSubchannelArgs.class));

  // Because there is no pending stream yet, it will do nothing but save the picker.
  delayedTransport.reprocess(picker);
  verify(picker, never()).pickSubchannel(any(PickSubchannelArgs.class));

  Thread sideThread = new Thread("sideThread") {
      @Override
      public void run() {
        // Will call pickSubchannel and wait on barrier
        delayedTransport.newStream(method, headers, callOptions);
      }
    };
  sideThread.start();

  PickSubchannelArgsImpl args = new PickSubchannelArgsImpl(method, headers, callOptions);
  PickSubchannelArgsImpl args2 = new PickSubchannelArgsImpl(method, headers2, callOptions);

  // Is called from sideThread
  verify(picker, timeout(5000)).pickSubchannel(args);

  // Because stream has not been buffered (it's still stuck in newStream()), this will do nothing,
  // but incrementing the picker version.
  delayedTransport.reprocess(picker);
  verify(picker).pickSubchannel(args);

  // Now let the stuck newStream() through
  barrier.await(5, TimeUnit.SECONDS);

  sideThread.join(5000);
  assertFalse("sideThread should've exited", sideThread.isAlive());
  // newStream() detects that there has been a new picker while it's stuck, thus will pick again.
  verify(picker, times(2)).pickSubchannel(args);

  barrier.reset();
  nextPickShouldWait.set(true);

  ////////// Phase 2: reprocess() with a different picker
  // Create the second stream
  Thread sideThread2 = new Thread("sideThread2") {
      @Override
      public void run() {
        // Will call pickSubchannel and wait on barrier
        delayedTransport.newStream(method, headers2, callOptions);
      }
    };
  sideThread2.start();
  // The second stream will see the first picker
  verify(picker, timeout(5000)).pickSubchannel(args2);
  // While the first stream won't use the first picker any more.
  verify(picker, times(2)).pickSubchannel(args);

  // Now use a different picker
  SubchannelPicker picker2 = mock(SubchannelPicker.class);
  when(picker2.pickSubchannel(any(PickSubchannelArgs.class)))
      .thenReturn(PickResult.withNoResult());
  delayedTransport.reprocess(picker2);
  // The pending first stream uses the new picker
  verify(picker2).pickSubchannel(args);
  // The second stream is still pending in creation, doesn't use the new picker.
  verify(picker2, never()).pickSubchannel(args2);

  // Now let the second stream finish creation
  barrier.await(5, TimeUnit.SECONDS);

  sideThread2.join(5000);
  assertFalse("sideThread2 should've exited", sideThread2.isAlive());
  // The second stream should see the new picker
  verify(picker2, timeout(5000)).pickSubchannel(args2);

  // Wrapping up
  verify(picker, times(2)).pickSubchannel(args);
  verify(picker).pickSubchannel(args2);
  verify(picker2).pickSubchannel(args);
  verify(picker2).pickSubchannel(args);
}
 
Example 14
Source File: ShardConsumerTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
private void awaitAndResetBarrier(CyclicBarrier barrier) throws Exception {
    barrier.await();
    barrier.reset();
}
 
Example 15
Source File: Test7194254.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final int NUMBER_OF_JAVA_PRIORITIES =
            Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
    final CyclicBarrier barrier =
            new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);

    for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
        final int priority = p;
        new Thread("Priority=" + p) {
            {
                setPriority(priority);
            }
            public void run() {
                try {
                    barrier.await(); // 1st
                    barrier.await(); // 2nd
                } catch (Exception exc) {
                    // ignore
                }
            }
        }.start();
    }
    barrier.await(); // 1st

    int matches = 0;
    List<String> failed = new ArrayList<>();
    try {
        String pid = getPid();
        String jstack = System.getProperty("java.home") + "/../bin/jstack";
        Process process = new ProcessBuilder(jstack, pid)
                .redirectErrorStream(true).start();
        Pattern pattern = Pattern.compile(
                "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while((line = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    matches += 1;
                    String expected = matcher.group(1);
                    String actual = matcher.group(2);
                    if (!expected.equals(actual)) {
                        failed.add(line);
                    }
                }
            }
        }
        barrier.await(); // 2nd
    } finally {
        barrier.reset();
    }

    if (matches != NUMBER_OF_JAVA_PRIORITIES) {
        throw new AssertionError("matches: expected " +
                NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
    }
    if (!failed.isEmpty()) {
        throw new AssertionError(failed.size() + ":" + failed);
    }
    System.out.println("Test passes.");
}
 
Example 16
Source File: Test7194254.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final int NUMBER_OF_JAVA_PRIORITIES =
            Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
    final CyclicBarrier barrier =
            new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);

    for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
        final int priority = p;
        new Thread("Priority=" + p) {
            {
                setPriority(priority);
            }
            public void run() {
                try {
                    barrier.await(); // 1st
                    barrier.await(); // 2nd
                } catch (Exception exc) {
                    // ignore
                }
            }
        }.start();
    }
    barrier.await(); // 1st

    int matches = 0;
    List<String> failed = new ArrayList<>();
    try {
        String pid = getPid();
        String jstack = System.getProperty("java.home") + "/../bin/jstack";
        Process process = new ProcessBuilder(jstack, pid)
                .redirectErrorStream(true).start();
        Pattern pattern = Pattern.compile(
                "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while((line = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    matches += 1;
                    String expected = matcher.group(1);
                    String actual = matcher.group(2);
                    if (!expected.equals(actual)) {
                        failed.add(line);
                    }
                }
            }
        }
        barrier.await(); // 2nd
    } finally {
        barrier.reset();
    }

    if (matches != NUMBER_OF_JAVA_PRIORITIES) {
        throw new AssertionError("matches: expected " +
                NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
    }
    if (!failed.isEmpty()) {
        throw new AssertionError(failed.size() + ":" + failed);
    }
    System.out.println("Test passes.");
}
 
Example 17
Source File: Test7194254.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final int NUMBER_OF_JAVA_PRIORITIES =
            Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
    final CyclicBarrier barrier =
            new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);

    for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
        final int priority = p;
        new Thread("Priority=" + p) {
            {
                setPriority(priority);
            }
            public void run() {
                try {
                    barrier.await(); // 1st
                    barrier.await(); // 2nd
                } catch (Exception exc) {
                    // ignore
                }
            }
        }.start();
    }
    barrier.await(); // 1st

    int matches = 0;
    List<String> failed = new ArrayList<>();
    try {
        String pid = getPid();
        String jstack = System.getProperty("java.home") + "/../bin/jstack";
        Process process = new ProcessBuilder(jstack, pid)
                .redirectErrorStream(true).start();
        Pattern pattern = Pattern.compile(
                "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while((line = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    matches += 1;
                    String expected = matcher.group(1);
                    String actual = matcher.group(2);
                    if (!expected.equals(actual)) {
                        failed.add(line);
                    }
                }
            }
        }
        barrier.await(); // 2nd
    } finally {
        barrier.reset();
    }

    if (matches != NUMBER_OF_JAVA_PRIORITIES) {
        throw new AssertionError("matches: expected " +
                NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
    }
    if (!failed.isEmpty()) {
        throw new AssertionError(failed.size() + ":" + failed);
    }
    System.out.println("Test passes.");
}
 
Example 18
Source File: DelayedClientTransportTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void reprocess_newStreamRacesWithReprocess() throws Exception {
  final CyclicBarrier barrier = new CyclicBarrier(2);
  // In both phases, we only expect the first pickSubchannel() call to block on the barrier.
  final AtomicBoolean nextPickShouldWait = new AtomicBoolean(true);
  ///////// Phase 1: reprocess() twice with the same picker
  SubchannelPicker picker = mock(SubchannelPicker.class);

  doAnswer(new Answer<PickResult>() {
      @Override
      @SuppressWarnings("CatchAndPrintStackTrace")
      public PickResult answer(InvocationOnMock invocation) throws Throwable {
        if (nextPickShouldWait.compareAndSet(true, false)) {
          try {
            barrier.await();
            return PickResult.withNoResult();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        return PickResult.withNoResult();
      }
  }).when(picker).pickSubchannel(any(PickSubchannelArgs.class));

  // Because there is no pending stream yet, it will do nothing but save the picker.
  delayedTransport.reprocess(picker);
  verify(picker, never()).pickSubchannel(any(PickSubchannelArgs.class));

  Thread sideThread = new Thread("sideThread") {
      @Override
      public void run() {
        // Will call pickSubchannel and wait on barrier
        delayedTransport.newStream(method, headers, callOptions);
      }
    };
  sideThread.start();

  PickSubchannelArgsImpl args = new PickSubchannelArgsImpl(method, headers, callOptions);
  PickSubchannelArgsImpl args2 = new PickSubchannelArgsImpl(method, headers2, callOptions);

  // Is called from sideThread
  verify(picker, timeout(5000)).pickSubchannel(args);

  // Because stream has not been buffered (it's still stuck in newStream()), this will do nothing,
  // but incrementing the picker version.
  delayedTransport.reprocess(picker);
  verify(picker).pickSubchannel(args);

  // Now let the stuck newStream() through
  barrier.await(5, TimeUnit.SECONDS);

  sideThread.join(5000);
  assertFalse("sideThread should've exited", sideThread.isAlive());
  // newStream() detects that there has been a new picker while it's stuck, thus will pick again.
  verify(picker, times(2)).pickSubchannel(args);

  barrier.reset();
  nextPickShouldWait.set(true);

  ////////// Phase 2: reprocess() with a different picker
  // Create the second stream
  Thread sideThread2 = new Thread("sideThread2") {
      @Override
      public void run() {
        // Will call pickSubchannel and wait on barrier
        delayedTransport.newStream(method, headers2, callOptions);
      }
    };
  sideThread2.start();
  // The second stream will see the first picker
  verify(picker, timeout(5000)).pickSubchannel(args2);
  // While the first stream won't use the first picker any more.
  verify(picker, times(2)).pickSubchannel(args);

  // Now use a different picker
  SubchannelPicker picker2 = mock(SubchannelPicker.class);
  when(picker2.pickSubchannel(any(PickSubchannelArgs.class)))
      .thenReturn(PickResult.withNoResult());
  delayedTransport.reprocess(picker2);
  // The pending first stream uses the new picker
  verify(picker2).pickSubchannel(args);
  // The second stream is still pending in creation, doesn't use the new picker.
  verify(picker2, never()).pickSubchannel(args2);

  // Now let the second stream finish creation
  barrier.await(5, TimeUnit.SECONDS);

  sideThread2.join(5000);
  assertFalse("sideThread2 should've exited", sideThread2.isAlive());
  // The second stream should see the new picker
  verify(picker2, timeout(5000)).pickSubchannel(args2);

  // Wrapping up
  verify(picker, times(2)).pickSubchannel(args);
  verify(picker).pickSubchannel(args2);
  verify(picker2).pickSubchannel(args);
  verify(picker2).pickSubchannel(args);
}
 
Example 19
Source File: Test7194254.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final int NUMBER_OF_JAVA_PRIORITIES =
            Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
    final CyclicBarrier barrier =
            new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);

    for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
        final int priority = p;
        new Thread("Priority=" + p) {
            {
                setPriority(priority);
            }
            public void run() {
                try {
                    barrier.await(); // 1st
                    barrier.await(); // 2nd
                } catch (Exception exc) {
                    // ignore
                }
            }
        }.start();
    }
    barrier.await(); // 1st

    int matches = 0;
    List<String> failed = new ArrayList<>();
    try {
        String pid = getPid();
        String jstack = System.getProperty("java.home") + "/../bin/jstack";
        Process process = new ProcessBuilder(jstack, pid)
                .redirectErrorStream(true).start();
        Pattern pattern = Pattern.compile(
                "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;
            while((line = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    matches += 1;
                    String expected = matcher.group(1);
                    String actual = matcher.group(2);
                    if (!expected.equals(actual)) {
                        failed.add(line);
                    }
                }
            }
        }
        barrier.await(); // 2nd
    } finally {
        barrier.reset();
    }

    if (matches != NUMBER_OF_JAVA_PRIORITIES) {
        throw new AssertionError("matches: expected " +
                NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
    }
    if (!failed.isEmpty()) {
        throw new AssertionError(failed.size() + ":" + failed);
    }
    System.out.println("Test passes.");
}
 
Example 20
Source File: CheckpointFreeListTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Note: Test assumes that PDS size didn't change between the first checkpoint and after several node stops.
 * It's not true anymore with free-list caching since the only final free-list state is persisted on checkpoint.
 * Some changed, but currently empty buckets are not persisted and PDS size is smaller after the first checkpoint.
 * Test makes sense only with disabled caching.
 *
 * @throws Exception if fail.
 */
@Test
@WithSystemProperty(key = IgniteSystemProperties.IGNITE_PAGES_LIST_DISABLE_ONHEAP_CACHING, value = "true")
public void testRestoreFreeListCorrectlyAfterRandomStop() throws Exception {
    IgniteEx ignite0 = startGrid(0);
    ignite0.cluster().active(true);

    Random random = new Random();

    List<T2<Integer, byte[]>> cachedEntry = new ArrayList<>();

    IgniteCache<Integer, Object> cache = ignite0.cache(CACHE_NAME);

    for (int j = 0; j < CACHE_SIZE; j++) {
        byte[] val = new byte[random.nextInt(SF.apply(3072))];

        cache.put(j, val);

        cachedEntry.add(new T2<>(j, val));
    }

    Collections.shuffle(cachedEntry);

    //Remove half of entries.
    Collection<T2<Integer, byte[]>> entriesToRemove = cachedEntry.stream()
        .limit(cachedEntry.size() / 2)
        .collect(Collectors.toCollection(ConcurrentLinkedQueue::new));

    entriesToRemove.forEach(t2 -> cache.remove(t2.get1()));

    //During removing of entries free list grab a lot of free pages to itself so will do put/remove again for stabilization of free pages.
    entriesToRemove.forEach(t2 -> cache.put(t2.get1(), t2.get2()));

    entriesToRemove.forEach(t2 -> cache.remove(t2.get1()));

    forceCheckpoint();

    Path cacheFolder = Paths.get(U.defaultWorkDirectory(),
        DFLT_STORE_DIR,
        ignite0.name().replaceAll("\\.", "_"),
        CACHE_DIR_PREFIX + CACHE_NAME
    );

    Optional<Long> totalPartSizeBeforeStop = totalPartitionsSize(cacheFolder);

    CyclicBarrier nodeStartBarrier = new CyclicBarrier(2);

    int approximateIterationCount = SF.applyLB(10, 6);

    //Approximate count of entries to put per one iteration.
    int iterationDataCount = entriesToRemove.size() / approximateIterationCount;

    startAsyncPutThread(entriesToRemove, nodeStartBarrier);

    //Will stop node during put data several times.
    while (true) {
        stopGrid(0, true);

        ignite0 = startGrid(0);

        ignite0.cluster().active(true);

        if (entriesToRemove.isEmpty())
            break;

        //Notify put thread that node successfully started.
        nodeStartBarrier.await();
        nodeStartBarrier.reset();

        int awaitSize = entriesToRemove.size() - iterationDataCount;

        waitForCondition(() -> entriesToRemove.size() < awaitSize || entriesToRemove.size() == 0, 20000);
    }

    forceCheckpoint();

    Optional<Long> totalPartSizeAfterRestore = totalPartitionsSize(cacheFolder);

    //It allow that size after repeated put operations should be not more than on 15%(heuristic value) greater than before operations. In fact, it should not be multiplied in twice every time.
    long correctedRestoreSize = totalPartSizeAfterRestore.get() - (long)(totalPartSizeBeforeStop.get() * 0.15);

    assertTrue("Size after repeated put operations should be not more than on 15% greater. " +
            "Size before = " + totalPartSizeBeforeStop.get() + ", Size after = " + totalPartSizeAfterRestore.get(),
        totalPartSizeBeforeStop.get() > correctedRestoreSize);
}