Example usage for java.util.concurrent.atomic AtomicInteger incrementAndGet

List of usage examples for java.util.concurrent.atomic AtomicInteger incrementAndGet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger incrementAndGet.

Prototype

public final int incrementAndGet() 

Source Link

Document

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd .

Usage

From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphoreCluster.java

@Test
public void testCluster() throws Exception {
    final int QTY = 20;
    final int OPERATION_TIME_MS = 1000;
    final String PATH = "/foo/bar/lock";

    ExecutorService executorService = Executors.newFixedThreadPool(QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    final Timing timing = new Timing();
    TestingCluster cluster = new TestingCluster(3);
    List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
    try {/*from  w w w  .j a v  a2 s  . c  o  m*/
        cluster.start();

        final AtomicInteger opCount = new AtomicInteger(0);
        for (int i = 0; i < QTY; ++i) {
            SemaphoreClient semaphoreClient = new SemaphoreClient(cluster.getConnectString(), PATH,
                    new Callable<Void>() {
                        @Override
                        public Void call() throws Exception {
                            opCount.incrementAndGet();
                            Thread.sleep(OPERATION_TIME_MS);
                            return null;
                        }
                    });
            completionService.submit(semaphoreClient);
            semaphoreClients.add(semaphoreClient);
        }

        timing.forWaiting().sleepABit();

        Assert.assertNotNull(SemaphoreClient.getActiveClient());

        final CountDownLatch latch = new CountDownLatch(1);
        CuratorFramework client = CuratorFrameworkFactory.newClient(cluster.getConnectString(),
                timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
        ConnectionStateListener listener = new ConnectionStateListener() {
            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.start();
        try {
            client.getZookeeperClient().blockUntilConnectedOrTimedOut();

            cluster.stop();

            latch.await();
        } finally {
            IOUtils.closeQuietly(client);
        }

        long startTicks = System.currentTimeMillis();
        for (;;) {
            int thisOpCount = opCount.get();
            Thread.sleep(2 * OPERATION_TIME_MS);
            if (thisOpCount == opCount.get()) {
                break; // checking that the op count isn't increasing
            }
            Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
        }

        int thisOpCount = opCount.get();

        Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
        cluster = new TestingCluster(iterator.next(), iterator.next());
        cluster.start();
        timing.forWaiting().sleepABit();

        startTicks = System.currentTimeMillis();
        for (;;) {
            Thread.sleep(2 * OPERATION_TIME_MS);
            if (opCount.get() > thisOpCount) {
                break; // checking that semaphore has started working again
            }
            Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
        }
    } finally {
        for (SemaphoreClient semaphoreClient : semaphoreClients) {
            IOUtils.closeQuietly(semaphoreClient);
        }
        IOUtils.closeQuietly(cluster);
        executorService.shutdownNow();
    }
}

From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static void load(CommandLine cmd, final Map<String, Double> stats) {
    String username = "dba";
    String password = "dba";
    String host = "localhost";
    int port = 1111;
    DetailLevel detailLevel = null;/*from   w  w w  .j  av  a 2 s  .c  om*/
    if (cmd.hasOption("detailLevel")) {
        try {
            detailLevel = Enum.valueOf(DetailLevel.class, cmd.getOptionValue("detailLevel"));
        } catch (IllegalArgumentException e) {
            LOG.fatal("Invalid argument value for detailLevel option", e);
            System.exit(1);
        }
    }
    final DetailLevel f_detailLevel = detailLevel;

    if (cmd.hasOption("username")) {
        username = cmd.getOptionValue("username");
    }
    if (cmd.hasOption("password")) {
        password = cmd.getOptionValue("password");
    }
    if (cmd.hasOption("host")) {
        host = cmd.getOptionValue("host");
    }
    if (cmd.hasOption("port")) {
        try {
            port = Integer.parseInt(cmd.getOptionValue("port"));
        } catch (NumberFormatException e) {
            LOG.fatal("Invalid port number: " + cmd.getOptionValue("port"));
            System.exit(1);
        }
    }

    final VirtuosoDaoFactory factory = new VirtuosoDaoFactory(host, port, username, password);
    ExecutorService pool = getThreadPool(cmd);

    final ProgressMonitor monitor = getProgressMonitor();
    final Pdb2RdfInputIterator i = processInput(cmd);
    final int inputSize = i.size();
    final AtomicInteger progressCount = new AtomicInteger();

    if (monitor != null) {
        monitor.setProgress(0, inputSize);
    }

    while (i.hasNext()) {
        final InputSource input = i.next();
        pool.execute(new Runnable() {
            public void run() {
                PdbXmlParser parser = new PdbXmlParser();
                UriBuilder uriBuilder = new UriBuilder();
                PdbRdfModel model = null;
                try {
                    model = new VirtPdbRdfModel(factory, Bio2RdfPdbUriPattern.PDB_GRAPH, uriBuilder,
                            factory.getTripleStoreDao());
                    if (f_detailLevel != null) {
                        parser.parse(input, model, f_detailLevel);
                    } else {
                        parser.parse(input, model);
                    }
                    if (stats != null) {
                        updateStats(stats, model);
                    }
                    if (monitor != null) {
                        monitor.setProgress(progressCount.incrementAndGet(), inputSize);
                    }

                } catch (Exception e) {
                    LOG.error("Uanble to parse input for pdb=" + (model != null ? model.getPdbId() : "null"),
                            e);
                }
            }
        });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            pool.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:org.apache.sshd.PortForwardingLoadTest.java

@Test
public void testLocalForwardingPayload() throws Exception {
    final int NUM_ITERATIONS = 100;
    final String PAYLOAD_TMP = "This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. ";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 1000; i++) {
        sb.append(PAYLOAD_TMP);/*from  www  .  j  a va 2  s.  c  om*/
    }
    final String PAYLOAD = sb.toString();
    Session session = createSession();
    final ServerSocket ss = new ServerSocket(0);
    int forwardedPort = ss.getLocalPort();
    int sinkPort = getFreePort();
    session.setPortForwardingL(sinkPort, "localhost", forwardedPort);
    final AtomicInteger conCount = new AtomicInteger(0);

    new Thread() {
        public void run() {
            try {
                for (int i = 0; i < NUM_ITERATIONS; ++i) {
                    Socket s = ss.accept();
                    conCount.incrementAndGet();
                    InputStream is = s.getInputStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buf = new byte[8192];
                    int l;
                    while (baos.size() < PAYLOAD.length() && (l = is.read(buf)) > 0) {
                        baos.write(buf, 0, l);
                    }
                    if (!PAYLOAD.equals(baos.toString())) {
                        assertEquals(PAYLOAD, baos.toString());
                    }
                    is = new ByteArrayInputStream(baos.toByteArray());
                    OutputStream os = s.getOutputStream();
                    while ((l = is.read(buf)) > 0) {
                        os.write(buf, 0, l);
                    }
                    s.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    Thread.sleep(50);

    for (int i = 0; i < NUM_ITERATIONS; i++) {
        Socket s = null;
        try {
            LoggerFactory.getLogger(getClass()).info("Iteration {}", i);
            s = new Socket("localhost", sinkPort);
            s.getOutputStream().write(PAYLOAD.getBytes());
            s.getOutputStream().flush();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[8192];
            int l;
            while (baos.size() < PAYLOAD.length() && (l = s.getInputStream().read(buf)) > 0) {
                baos.write(buf, 0, l);
            }
            assertEquals(PAYLOAD, baos.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }
    session.delPortForwardingL(sinkPort);
}

From source file:org.jtheque.file.FileServiceTest.java

@Test
public void importers() {
    final AtomicInteger counter = new AtomicInteger(0);

    fileService.registerImporter("no-module", new Importer() {
        @Override/*from  www  .  j  av a2s  . c o  m*/
        public boolean canImportFrom(String fileType) {
            return "xml".equals(fileType);
        }

        @Override
        public void importFrom(String path) throws FileException {
            assertEquals("path", path);

            counter.incrementAndGet();
        }
    });

    try {
        fileService.importDatas("no-module", "xml", "path");
    } catch (FileException e) {
        fail("Exception during the export");
    }

    assertEquals(1, counter.get());
}

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();/*from   w  w  w.j a v  a 2s. co  m*/
    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:org.apache.sshd.PortForwardingLoadTest.java

@Test
public void testRemoteForwardingPayload() throws Exception {
    final int NUM_ITERATIONS = 100;
    final String PAYLOAD = "This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. This is significantly longer Test Data. This is significantly "
            + "longer Test Data. ";
    Session session = createSession();//from w  w w. ja  va2s . com
    final ServerSocket ss = new ServerSocket(0);
    int forwardedPort = ss.getLocalPort();
    int sinkPort = getFreePort();
    session.setPortForwardingR(sinkPort, "localhost", forwardedPort);
    final boolean started[] = new boolean[1];
    started[0] = false;
    final AtomicInteger conCount = new AtomicInteger(0);

    new Thread() {
        public void run() {
            started[0] = true;
            try {
                for (int i = 0; i < NUM_ITERATIONS; ++i) {
                    Socket s = ss.accept();
                    conCount.incrementAndGet();
                    s.getOutputStream().write(PAYLOAD.getBytes());
                    s.getOutputStream().flush();
                    s.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    Thread.sleep(50);
    Assert.assertTrue("Server not started", started[0]);

    final boolean lenOK[] = new boolean[NUM_ITERATIONS];
    final boolean dataOK[] = new boolean[NUM_ITERATIONS];
    for (int i = 0; i < NUM_ITERATIONS; i++) {
        final int ii = i;
        Socket s = null;
        try {
            s = new Socket("localhost", sinkPort);
            byte b1[] = new byte[PAYLOAD.length() / 2];
            byte b2[] = new byte[PAYLOAD.length()];
            int read1 = s.getInputStream().read(b1);
            Thread.sleep(50);
            int read2 = s.getInputStream().read(b2);
            lenOK[ii] = PAYLOAD.length() == read1 + read2;
            dataOK[ii] = PAYLOAD.equals(new String(b1, 0, read1) + new String(b2, 0, read2));
            if (!lenOK[ii] || !dataOK[ii]) {
                throw new Exception("Bad data");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }
    int ok = 0;
    for (int i = 0; i < NUM_ITERATIONS; i++) {
        ok += lenOK[i] ? 1 : 0;
    }
    Thread.sleep(50);
    for (int i = 0; i < NUM_ITERATIONS; i++) {
        Assert.assertTrue(lenOK[i]);
        Assert.assertTrue(dataOK[i]);
    }
    session.delPortForwardingR(forwardedPort);
}

From source file:org.apache.hadoop.hbase.master.procedure.TestMasterProcedureSchedulerConcurrency.java

/**
 * Verify that "write" operations for a single table are serialized,
 * but different tables can be executed in parallel.
 *//*from  w ww  . j av  a2s.  c o  m*/
@Test(timeout = 60000)
public void testConcurrentWriteOps() throws Exception {
    final TestTableProcSet procSet = new TestTableProcSet(queue);

    final int NUM_ITEMS = 10;
    final int NUM_TABLES = 4;
    final AtomicInteger opsCount = new AtomicInteger(0);
    for (int i = 0; i < NUM_TABLES; ++i) {
        TableName tableName = TableName.valueOf(String.format("testtb-%04d", i));
        for (int j = 1; j < NUM_ITEMS; ++j) {
            procSet.addBack(new TestTableProcedure(i * 100 + j, tableName,
                    TableProcedureInterface.TableOperationType.EDIT));
            opsCount.incrementAndGet();
        }
    }
    assertEquals(opsCount.get(), queue.size());

    final Thread[] threads = new Thread[NUM_TABLES * 2];
    final HashSet<TableName> concurrentTables = new HashSet<TableName>();
    final ArrayList<String> failures = new ArrayList<String>();
    final AtomicInteger concurrentCount = new AtomicInteger(0);
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread() {
            @Override
            public void run() {
                while (opsCount.get() > 0) {
                    try {
                        Procedure proc = procSet.acquire();
                        if (proc == null) {
                            queue.signalAll();
                            if (opsCount.get() > 0) {
                                continue;
                            }
                            break;
                        }

                        TableName tableId = procSet.getTableName(proc);
                        synchronized (concurrentTables) {
                            assertTrue("unexpected concurrency on " + tableId, concurrentTables.add(tableId));
                        }
                        assertTrue(opsCount.decrementAndGet() >= 0);
                        try {
                            long procId = proc.getProcId();
                            int concurrent = concurrentCount.incrementAndGet();
                            assertTrue("inc-concurrent=" + concurrent + " 1 <= concurrent <= " + NUM_TABLES,
                                    concurrent >= 1 && concurrent <= NUM_TABLES);
                            LOG.debug("[S] tableId=" + tableId + " procId=" + procId + " concurrent="
                                    + concurrent);
                            Thread.sleep(2000);
                            concurrent = concurrentCount.decrementAndGet();
                            LOG.debug("[E] tableId=" + tableId + " procId=" + procId + " concurrent="
                                    + concurrent);
                            assertTrue("dec-concurrent=" + concurrent, concurrent < NUM_TABLES);
                        } finally {
                            synchronized (concurrentTables) {
                                assertTrue(concurrentTables.remove(tableId));
                            }
                            procSet.release(proc);
                        }
                    } catch (Throwable e) {
                        LOG.error("Failed " + e.getMessage(), e);
                        synchronized (failures) {
                            failures.add(e.getMessage());
                        }
                    } finally {
                        queue.signalAll();
                    }
                }
            }
        };
        threads[i].start();
    }
    for (int i = 0; i < threads.length; ++i) {
        threads[i].join();
    }
    assertTrue(failures.toString(), failures.isEmpty());
    assertEquals(0, opsCount.get());
    assertEquals(0, queue.size());

    for (int i = 1; i <= NUM_TABLES; ++i) {
        final TableName table = TableName.valueOf(String.format("testtb-%04d", i));
        final TestTableProcedure dummyProc = new TestTableProcedure(100, table,
                TableProcedureInterface.TableOperationType.DELETE);
        assertTrue("queue should be deleted, table=" + table, queue.markTableAsDeleted(table, dummyProc));
    }
}

From source file:com.joyent.manta.client.MantaClientIT.java

@Test
public final void testList() throws IOException {
    final String pathPrefix = String.format("%s/%s", testPathPrefix, UUID.randomUUID());
    mantaClient.putDirectory(pathPrefix, null);

    mantaClient.put(String.format("%s/%s", pathPrefix, UUID.randomUUID()), "");
    mantaClient.put(String.format("%s/%s", pathPrefix, UUID.randomUUID()), "");
    final String subDir = pathPrefix + "/" + UUID.randomUUID().toString();
    mantaClient.putDirectory(subDir, null);
    mantaClient.put(String.format("%s/%s", subDir, UUID.randomUUID()), "");
    final Stream<MantaObject> objs = mantaClient.listObjects(pathPrefix);

    final AtomicInteger count = new AtomicInteger(0);
    objs.forEach(obj -> {/*from w  w  w .  j  a va 2  s . co m*/
        count.incrementAndGet();
        Assert.assertTrue(obj.getPath().startsWith(testPathPrefix));
    });

    Assert.assertEquals(3, count.get());
}

From source file:com.twitter.distributedlog.service.balancer.ClusterBalancer.java

public void balance(int rebalanceWaterMark, double rebalanceTolerancePercentage, int rebalanceConcurrency,
        Optional<String> source, Optional<RateLimiter> rebalanceRateLimiter) {
    Map<SocketAddress, Set<String>> distribution = monitor.getStreamOwnershipDistribution();
    if (distribution.size() <= 1) {
        return;//from w  ww.  j  a  va2  s . c  om
    }
    SocketAddress sourceAddr = null;
    if (source.isPresent()) {
        sourceAddr = DLSocketAddress.parseSocketAddress(source.get());
        logger.info("Balancer source is {}", sourceAddr);
        if (!distribution.containsKey(sourceAddr)) {
            return;
        }
    }
    // Get the list of hosts ordered by number of streams in DESC order
    List<Host> hosts = new ArrayList<Host>(distribution.size());
    for (Map.Entry<SocketAddress, Set<String>> entry : distribution.entrySet()) {
        Host host = new Host(entry.getKey(), entry.getValue(), clientBuilder);
        hosts.add(host);
    }
    Collections.sort(hosts, new HostComparator());
    try {

        // find the host to move streams from.
        int hostIdxMoveFrom = -1;
        if (null != sourceAddr) {
            for (Host host : hosts) {
                ++hostIdxMoveFrom;
                if (sourceAddr.equals(host.address)) {
                    break;
                }
            }
        }

        // compute the average load.
        int totalStream = 0;
        for (Host host : hosts) {
            totalStream += host.streams.size();
        }
        double averageLoad;
        if (hostIdxMoveFrom >= 0) {
            averageLoad = ((double) totalStream / (hosts.size() - 1));
        } else {
            averageLoad = ((double) totalStream / hosts.size());
        }

        int moveFromLowWaterMark;
        int moveToHighWaterMark = Math.max(1,
                (int) (averageLoad + averageLoad * rebalanceTolerancePercentage / 100.0f));

        if (hostIdxMoveFrom >= 0) {
            moveFromLowWaterMark = Math.max(0, rebalanceWaterMark);
            moveStreams(hosts, new AtomicInteger(hostIdxMoveFrom), moveFromLowWaterMark,
                    new AtomicInteger(hosts.size() - 1), moveToHighWaterMark, rebalanceRateLimiter);
            moveRemainingStreamsFromSource(hosts.get(hostIdxMoveFrom), hosts, rebalanceRateLimiter);
        } else {
            moveFromLowWaterMark = Math.max((int) Math.ceil(averageLoad), rebalanceWaterMark);
            AtomicInteger moveFrom = new AtomicInteger(0);
            AtomicInteger moveTo = new AtomicInteger(hosts.size() - 1);
            while (moveFrom.get() < moveTo.get()) {
                moveStreams(hosts, moveFrom, moveFromLowWaterMark, moveTo, moveToHighWaterMark,
                        rebalanceRateLimiter);
                moveFrom.incrementAndGet();
            }
        }
    } finally {
        for (Host host : hosts) {
            host.close();
        }
    }
}

From source file:org.apache.calcite.test.CalciteAssert.java

static Function<RelNode, Void> checkRel(final String expected, final AtomicInteger counter) {
    return new Function<RelNode, Void>() {
        public Void apply(RelNode relNode) {
            if (counter != null) {
                counter.incrementAndGet();
            }/* w  ww.j  a  v  a 2s .c  o  m*/
            String s = Util.toLinux(RelOptUtil.toString(relNode));
            assertThat(s, containsString(expected));
            return null;
        }
    };
}