Example usage for java.util.concurrent Semaphore drainPermits

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

Introduction

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

Prototype

public int drainPermits() 

Source Link

Document

Acquires and returns all permits that are immediately available, or if negative permits are available, releases them.

Usage

From source file:com.netflix.curator.ensemble.exhibitor.TestExhibitorEnsembleProvider.java

@Test
public void testExhibitorFailures() throws Exception {
    final AtomicReference<String> backupConnectionString = new AtomicReference<String>("backup1:1");
    final AtomicReference<String> connectionString = new AtomicReference<String>(
            "count=1&port=2&server0=localhost");
    Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000,
            new Exhibitors.BackupConnectionStringProvider() {
                @Override/*w w  w.j a va  2 s.c o  m*/
                public String getBackupConnectionString() {
                    return backupConnectionString.get();
                }
            });
    ExhibitorRestClient mockRestClient = new ExhibitorRestClient() {
        @Override
        public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
            String localConnectionString = connectionString.get();
            if (localConnectionString == null) {
                throw new IOException();
            }
            return localConnectionString;
        }
    };

    final Semaphore semaphore = new Semaphore(0);
    ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10,
            new RetryOneTime(1)) {
        @Override
        protected void poll() {
            super.poll();
            semaphore.release();
        }
    };
    provider.pollForInitialEnsemble();
    try {
        provider.start();

        Assert.assertEquals(provider.getConnectionString(), "localhost:2");

        connectionString.set(null);
        semaphore.drainPermits();
        semaphore.acquire(); // wait for next poll
        Assert.assertEquals(provider.getConnectionString(), "backup1:1");

        backupConnectionString.set("backup2:2");
        semaphore.drainPermits();
        semaphore.acquire(); // wait for next poll
        Assert.assertEquals(provider.getConnectionString(), "backup2:2");

        connectionString.set("count=1&port=3&server0=localhost3");
        semaphore.drainPermits();
        semaphore.acquire(); // wait for next poll
        Assert.assertEquals(provider.getConnectionString(), "localhost3:3");
    } finally {
        IOUtils.closeQuietly(provider);
    }
}

From source file:com.netflix.curator.framework.imps.TestFrameworkEdges.java

@Test
public void testRetry() throws Exception {
    final int MAX_RETRIES = 3;
    final int serverPort = server.getPort();

    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000,
            new RetryOneTime(10));
    client.start();//w  w  w  . ja v a  2 s .c om
    try {
        final AtomicInteger retries = new AtomicInteger(0);
        final Semaphore semaphore = new Semaphore(0);
        client.getZookeeperClient().setRetryPolicy(new RetryPolicy() {
            @Override
            public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) {
                semaphore.release();
                if (retries.incrementAndGet() == MAX_RETRIES) {
                    try {
                        server = new TestingServer(serverPort);
                    } catch (Exception e) {
                        throw new Error(e);
                    }
                }
                return true;
            }
        });

        server.stop();

        // test foreground retry
        client.checkExists().forPath("/hey");
        Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, 10, TimeUnit.SECONDS));

        semaphore.drainPermits();
        retries.set(0);

        server.stop();

        // test background retry
        client.checkExists().inBackground().forPath("/hey");
        Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, 10, TimeUnit.SECONDS));
    } catch (Throwable e) {
        Assert.fail("Error", e);
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:com.netflix.curator.ensemble.exhibitor.TestExhibitorEnsembleProvider.java

@Test
public void testChanging() throws Exception {
    TestingServer secondServer = new TestingServer();
    try {/* www.j a v  a 2  s .  co  m*/
        String mainConnectionString = "count=1&port=" + server.getPort() + "&server0=localhost";
        String secondConnectionString = "count=1&port=" + secondServer.getPort() + "&server0=localhost";

        final Semaphore semaphore = new Semaphore(0);
        final AtomicReference<String> connectionString = new AtomicReference<String>(mainConnectionString);
        Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000,
                dummyConnectionStringProvider);
        ExhibitorRestClient mockRestClient = new ExhibitorRestClient() {
            @Override
            public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
                semaphore.release();
                return connectionString.get();
            }
        };
        ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo",
                10, new RetryOneTime(1));
        provider.pollForInitialEnsemble();

        Timing timing = new Timing().multiple(4);
        final CuratorZookeeperClient client = new CuratorZookeeperClient(provider, timing.session(),
                timing.connection(), null, new RetryOneTime(2));
        client.start();
        try {
            RetryLoop.callWithRetry(client, new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    client.getZooKeeper().create("/test", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
                            CreateMode.PERSISTENT);
                    return null;
                }
            });

            connectionString.set(secondConnectionString);
            semaphore.drainPermits();
            semaphore.acquire();

            server.stop(); // create situation where the current zookeeper gets a sys-disconnected

            Stat stat = RetryLoop.callWithRetry(client, new Callable<Stat>() {
                @Override
                public Stat call() throws Exception {
                    return client.getZooKeeper().exists("/test", false);
                }
            });
            Assert.assertNull(stat); // it's a different server so should be null
        } finally {
            client.close();
        }
    } finally {
        IOUtils.closeQuietly(secondServer);
    }
}

From source file:org.apache.kylin.storage.hbase.util.HbaseStreamingInput.java

public static void randomScan(String tableName) throws IOException {

    final Semaphore semaphore = new Semaphore(0);
    new Thread(new Runnable() {
        @Override// w  ww. j  av  a  2  s.c  o  m
        public void run() {
            scheduleJob(semaphore, 60000);//1 minutes a batch
        }
    }).start();

    while (true) {
        try {
            semaphore.acquire();
            int waiting = semaphore.drainPermits();
            if (waiting > 0) {
                logger.warn("Too many queries to handle! Blocking " + waiting + " sets of scan requests");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }

        Random r = new Random();
        Connection conn = getConnection();
        Table table = conn.getTable(TableName.valueOf(tableName));

        long leftBound = getFirstKeyTime(table);
        long rightBound = System.currentTimeMillis();

        for (int t = 0; t < 5; ++t) {
            long start = (long) (leftBound + r.nextDouble() * (rightBound - leftBound));
            long end = start + 600000;//a period of 10 minutes
            logger.info("A scan from " + formatTime(start) + " to " + formatTime(end));

            Scan scan = new Scan();
            scan.setStartRow(Bytes.toBytes(start));
            scan.setStopRow(Bytes.toBytes(end));
            scan.addFamily(CF);
            ResultScanner scanner = table.getScanner(scan);
            long hash = 0;
            int rowCount = 0;
            for (Result result : scanner) {
                Cell cell = result.getColumnLatestCell(CF, QN);
                byte[] value = cell.getValueArray();
                if (cell.getValueLength() != CELL_SIZE) {
                    logger.error("value size invalid!!!!!");
                }

                hash += Arrays.hashCode(Arrays.copyOfRange(value, cell.getValueOffset(),
                        cell.getValueLength() + cell.getValueOffset()));
                rowCount++;
            }
            scanner.close();
            logger.info("Scanned " + rowCount + " rows, the (meaningless) hash for the scan is " + hash);
        }
        table.close();
        conn.close();
    }
}