Example usage for java.util.concurrent Semaphore availablePermits

List of usage examples for java.util.concurrent Semaphore availablePermits

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore availablePermits.

Prototype

public int availablePermits() 

Source Link

Document

Returns the current number of permits available in this semaphore.

Usage

From source file:co.paralleluniverse.photon.Photon.java

public static void main(final String[] args) throws InterruptedException, IOException {

    final Options options = new Options();
    options.addOption("rate", true, "Requests per second (default " + rateDefault + ")");
    options.addOption("duration", true,
            "Minimum test duration in seconds: will wait for <duration> * <rate> requests to terminate or, if progress check enabled, no progress after <duration> (default "
                    + durationDefault + ")");
    options.addOption("maxconnections", true,
            "Maximum number of open connections (default " + maxConnectionsDefault + ")");
    options.addOption("timeout", true,
            "Connection and read timeout in millis (default " + timeoutDefault + ")");
    options.addOption("print", true,
            "Print cycle in millis, 0 to disable intermediate statistics (default " + printCycleDefault + ")");
    options.addOption("check", true,
            "Progress check cycle in millis, 0 to disable progress check (default " + checkCycleDefault + ")");
    options.addOption("stats", false, "Print full statistics when finish (default false)");
    options.addOption("minmax", false, "Print min/mean/stddev/max stats when finish (default false)");
    options.addOption("name", true, "Test name to print in the statistics (default '" + testNameDefault + "')");
    options.addOption("help", false, "Print help");

    try {/*  ww  w. jav a 2  s.  com*/
        final CommandLine cmd = new BasicParser().parse(options, args);
        final String[] ar = cmd.getArgs();
        if (cmd.hasOption("help") || ar.length != 1)
            printUsageAndExit(options);

        final String url = ar[0];

        final int timeout = Integer.parseInt(cmd.getOptionValue("timeout", timeoutDefault));
        final int maxConnections = Integer
                .parseInt(cmd.getOptionValue("maxconnections", maxConnectionsDefault));
        final int duration = Integer.parseInt(cmd.getOptionValue("duration", durationDefault));
        final int printCycle = Integer.parseInt(cmd.getOptionValue("print", printCycleDefault));
        final int checkCycle = Integer.parseInt(cmd.getOptionValue("check", checkCycleDefault));
        final String testName = cmd.getOptionValue("name", testNameDefault);
        final int rate = Integer.parseInt(cmd.getOptionValue("rate", rateDefault));

        final MetricRegistry metrics = new MetricRegistry();
        final Meter requestMeter = metrics.meter("request");
        final Meter responseMeter = metrics.meter("response");
        final Meter errorsMeter = metrics.meter("errors");
        final Logger log = LoggerFactory.getLogger(Photon.class);
        final ConcurrentHashMap<String, AtomicInteger> errors = new ConcurrentHashMap<>();
        final HttpGet request = new HttpGet(url);
        final StripedTimeSeries<Long> sts = new StripedTimeSeries<>(30000, false);
        final StripedHistogram sh = new StripedHistogram(60000, 5);

        log.info("name: " + testName + " url:" + url + " rate:" + rate + " duration:" + duration
                + " maxconnections:" + maxConnections + ", " + "timeout:" + timeout);
        final DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(IOReactorConfig.custom()
                .setConnectTimeout(timeout).setIoThreadCount(10).setSoTimeout(timeout).build());

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            final List<ExceptionEvent> events = ioreactor.getAuditLog();
            if (events != null)
                events.stream().filter(event -> event != null).forEach(event -> {
                    System.err.println(
                            "Apache Async HTTP Client I/O Reactor Error Time: " + event.getTimestamp());
                    //noinspection ThrowableResultOfMethodCallIgnored
                    if (event.getCause() != null)
                        //noinspection ThrowableResultOfMethodCallIgnored
                        event.getCause().printStackTrace();
                });
            if (cmd.hasOption("stats"))
                printFinishStatistics(errorsMeter, sts, sh, testName);
            if (!errors.keySet().isEmpty())
                errors.entrySet().stream()
                        .forEach(p -> log.info(testName + " " + p.getKey() + " " + p.getValue() + "ms"));
            System.out.println(
                    testName + " responseTime(90%): " + sh.getHistogramData().getValueAtPercentile(90) + "ms");
            if (cmd.hasOption("minmax")) {
                final HistogramData hd = sh.getHistogramData();
                System.out.format("%s %8s%8s%8s%8s\n", testName, "min", "mean", "sd", "max");
                System.out.format("%s %8d%8.2f%8.2f%8d\n", testName, hd.getMinValue(), hd.getMean(),
                        hd.getStdDeviation(), hd.getMaxValue());
            }
        }));

        final PoolingNHttpClientConnectionManager mngr = new PoolingNHttpClientConnectionManager(ioreactor);
        mngr.setDefaultMaxPerRoute(maxConnections);
        mngr.setMaxTotal(maxConnections);
        final CloseableHttpAsyncClient ahc = HttpAsyncClientBuilder.create().setConnectionManager(mngr)
                .setDefaultRequestConfig(RequestConfig.custom().setLocalAddress(null).build()).build();
        try (final CloseableHttpClient client = new FiberHttpClient(ahc)) {
            final int num = duration * rate;

            final CountDownLatch cdl = new CountDownLatch(num);
            final Semaphore sem = new Semaphore(maxConnections);
            final RateLimiter rl = RateLimiter.create(rate);

            spawnStatisticsThread(printCycle, cdl, log, requestMeter, responseMeter, errorsMeter, testName);

            for (int i = 0; i < num; i++) {
                rl.acquire();
                if (sem.availablePermits() == 0)
                    log.debug("Maximum connections count reached, waiting...");
                sem.acquireUninterruptibly();

                new Fiber<Void>(() -> {
                    requestMeter.mark();
                    final long start = System.nanoTime();
                    try {
                        try (final CloseableHttpResponse ignored = client.execute(request)) {
                            responseMeter.mark();
                        } catch (final Throwable t) {
                            markError(errorsMeter, errors, t);
                        }
                    } catch (final Throwable t) {
                        markError(errorsMeter, errors, t);
                    } finally {
                        final long now = System.nanoTime();
                        final long millis = TimeUnit.NANOSECONDS.toMillis(now - start);
                        sts.record(start, millis);
                        sh.recordValue(millis);
                        sem.release();
                        cdl.countDown();
                    }
                }).start();
            }
            spawnProgressCheckThread(log, duration, checkCycle, cdl);
            cdl.await();
        }
    } catch (final ParseException ex) {
        System.err.println("Parsing failed.  Reason: " + ex.getMessage());
    }
}

From source file:com.uber.hoodie.func.TestBoundedInMemoryQueue.java

private boolean isQueueFull(Semaphore rateLimiter) {
    return (rateLimiter.availablePermits() == 0 && rateLimiter.hasQueuedThreads());
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testCancelSingleTask() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });//from ww w .ja  v a2s.com
    Task task = getSleepIncrementTask();
    workerPool.assignTask(task);
    Thread.sleep(10);
    workerPool.abortTask(task.clusterName);
    Thread.sleep(100);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testAssignSameClusterConflict() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });//w ww  .j a  v a2 s .  c  o  m
    Task task = getSleepIncrementTask();
    workerPool.assignTask(task);
    Thread.sleep(2000);
    Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue());
    workerPool.assignTask(task);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
    Thread.sleep(100);
    Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue());
    Thread.sleep(1000);
}

From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java

@Test
public void testNoPendingTask() throws Exception {
    // Assuming there is no task in the queue in the test.
    PowerMockito.when(taskQueue.dequeueTask(anyString())).thenReturn(null);
    Semaphore idleWorkersSemaphore = new Semaphore(1);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, taskQueue);
    TaskDispatcher dispatcher = new TaskDispatcher(1, idleWorkersSemaphore, workerPool, taskQueue);
    dispatcher.start();//from   w w w . j av a2  s  .  c o m
    Thread.sleep(1000);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
    Thread.sleep(1000);
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
    dispatcher.stop();
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testAssignSingleTask() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });/*from ww w. ja  v  a  2  s.co m*/
    workerPool.assignTask(getSleepIncrementTask());
    Thread.sleep(2000);
    Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue());
    workerPool.assignTask(getSleepIncrementTask());
    Thread.sleep(2000);
    Assert.assertEquals(2, SleepIncrementTask.executionCounter.intValue());
    Assert.assertEquals(2, idleWorkersSemaphore.availablePermits());
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testAssignSameCluster() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });// w ww  .j  ava2  s .c  om
    Task task = getSleepIncrementTask();
    workerPool.assignTask(task);
    Thread.sleep(2000);
    Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue());
    workerPool.assignTask(task);
    Thread.sleep(2000);
    Assert.assertEquals(2, (int) SleepIncrementTask.executionCounter);
    Assert.assertEquals(2, idleWorkersSemaphore.availablePermits());
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testAssignMultiTask() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });/*from  w ww. j a  v a2s . co m*/
    workerPool.assignTask(getSleepIncrementTask());
    workerPool.assignTask(getSleepIncrementTask());
    workerPool.assignTask(getSleepIncrementTask());
    Thread.sleep(1500);
    // Only expect 2 to finish because the pool size is 2
    Assert.assertEquals(2, SleepIncrementTask.executionCounter.intValue());
    Assert.assertEquals(2, idleWorkersSemaphore.availablePermits());
    Thread.sleep(1000);
}

From source file:com.thoughtworks.go.agent.bootstrapper.AgentBootstrapperTest.java

@Test
public void shouldNotDieWhenInvocationOfLauncherRaisesException_butCreationOfLauncherWentThrough()
        throws InterruptedException {
    final Semaphore waitForLauncherInvocation = new Semaphore(1);
    waitForLauncherInvocation.acquire();
    final AgentBootstrapper bootstrapper = new AgentBootstrapper() {
        @Override//from   w w w  .  j a va  2  s . c  o m
        AgentLauncherCreator getLauncherCreator() {
            return new AgentLauncherCreator() {
                public AgentLauncher createLauncher() {
                    return new AgentLauncher() {
                        public int launch(AgentLaunchDescriptor descriptor) {
                            try {
                                throw new RuntimeException("fail!!! i say.");
                            } finally {
                                if (waitForLauncherInvocation.availablePermits() == 0) {
                                    waitForLauncherInvocation.release();
                                }
                            }
                        }

                    };
                }

                @Override
                public void close() {
                }
            };
        }
    };

    final AgentBootstrapper spyBootstrapper = stubJVMExit(bootstrapper);

    Thread stopLoopThd = new Thread(new Runnable() {
        public void run() {
            try {
                waitForLauncherInvocation.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ReflectionUtil.setField(spyBootstrapper, "loop", false);
        }
    });
    stopLoopThd.start();
    try {
        spyBootstrapper.go(true,
                new AgentBootstrapperArgs(new URL("http://" + "ghost-name" + ":" + 3518 + "/go"), null,
                        AgentBootstrapperArgs.SslMode.NONE));
        stopLoopThd.join();
    } catch (Exception e) {
        fail("should not have propagated exception thrown while invoking the launcher");
    }
}

From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java

@Test
public void testSingleTaskLifeCycle() throws Exception {
    // Assuming there is only one task in the queue
    PowerMockito.when(taskQueue.dequeueTask(anyString())).thenReturn(getSleepIncrementTaskFromQueue())
            .thenReturn(null);//from   w  ww  . j a v  a 2  s .c o m
    Semaphore idleWorkersSemaphore = new Semaphore(1);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, taskQueue);
    TaskDispatcher dispatcher = new TaskDispatcher(2, idleWorkersSemaphore, workerPool, taskQueue);
    dispatcher.start();
    // Wait for first task to be done
    synchronized (SleepIncrementTask.notifyObject) {
        SleepIncrementTask.notifyObject.wait();
    }
    verify(taskQueue, atLeastOnce()).dequeueTask(anyString());
    Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue());
    Assert.assertEquals(1, idleWorkersSemaphore.availablePermits());
    dispatcher.stop();
}