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

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

Introduction

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

Prototype

public final int get() 

Source Link

Document

Returns the current value, with memory effects as specified by VarHandle#getVolatile .

Usage

From source file:org.apache.hadoop.hbase.mapreduce.TestLoadIncrementalHFilesSplitRecovery.java

/**
 * This test exercises the path where there is a split after initial
 * validation but before the atomic bulk load call. We cannot use presplitting
 * to test this path, so we actually inject a split just before the atomic
 * region load.// www.jav a2 s  . c  o m
 */
@Test
public void testSplitWhileBulkLoadPhase() throws Exception {
    final String table = "splitWhileBulkloadPhase";
    setupTable(table, 10);
    populateTable(table, 1);
    assertExpectedTable(table, ROWCOUNT, 1);

    // Now let's cause trouble.  This will occur after checks and cause bulk
    // files to fail when attempt to atomically import.  This is recoverable.
    final AtomicInteger attemptedCalls = new AtomicInteger();
    LoadIncrementalHFiles lih2 = new LoadIncrementalHFiles(util.getConfiguration()) {

        protected void bulkLoadPhase(final HTable htable, final HConnection conn, ExecutorService pool,
                Deque<LoadQueueItem> queue, final Multimap<ByteBuffer, LoadQueueItem> regionGroups)
                throws IOException {
            int i = attemptedCalls.incrementAndGet();
            if (i == 1) {
                // On first attempt force a split.
                forceSplit(table);
            }

            super.bulkLoadPhase(htable, conn, pool, queue, regionGroups);
        }
    };

    // create HFiles for different column families
    HTable t = new HTable(util.getConfiguration(), Bytes.toBytes(table));
    Path bulk = buildBulkFiles(table, 2);
    lih2.doBulkLoad(bulk, t);

    // check that data was loaded
    // The three expected attempts are 1) failure because need to split, 2)
    // load of split top 3) load of split bottom
    assertEquals(attemptedCalls.get(), 3);
    assertExpectedTable(table, ROWCOUNT, 2);
}

From source file:com.smartitengineering.cms.spi.impl.content.GroovyGeneratorTest.java

@Test
public void testMultiGroovyRepGeneration() throws IOException {
    TypeRepresentationGenerator generator = new GroovyRepresentationGenerator();
    final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class);
    WorkspaceAPIImpl impl = new WorkspaceAPIImpl() {

        @Override/*  w  w  w .  j  av a2  s.c  om*/
        public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) {
            return template;
        }
    };
    impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.GROOVY, generator));
    final RepresentationProvider provider = new RepresentationProviderImpl();
    final WorkspaceAPI api = impl;
    registerBeanFactory(api);
    final Content content = mockery.mock(Content.class);
    final Field field = mockery.mock(Field.class);
    final FieldValue value = mockery.mock(FieldValue.class);
    final ContentType type = mockery.mock(ContentType.class);
    final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap");
    final RepresentationDef def = mockery.mock(RepresentationDef.class);
    final int threadCount = new Random().nextInt(100);
    logger.info("Number of parallel threads " + threadCount);
    mockery.checking(new Expectations() {

        {
            exactly(threadCount).of(template).getTemplateType();
            will(returnValue(TemplateType.GROOVY));
            exactly(threadCount).of(template).getTemplate();
            final byte[] toByteArray = IOUtils.toByteArray(getClass().getClassLoader()
                    .getResourceAsStream("scripts/groovy/GroovyTestRepresentationGenerator.groovy"));
            will(returnValue(toByteArray));
            exactly(threadCount).of(template).getName();
            will(returnValue(REP_NAME));
            for (int i = 0; i < threadCount; ++i) {
                exactly(1).of(value).getValue();
                will(returnValue(String.valueOf(i)));
            }
            exactly(threadCount).of(field).getValue();
            will(returnValue(value));
            exactly(threadCount).of(content).getField(with(Expectations.<String>anything()));
            will(returnValue(field));
            exactly(threadCount).of(content).getContentDefinition();
            will(returnValue(type));
            final ContentId contentId = mockery.mock(ContentId.class);
            exactly(2 * threadCount).of(content).getContentId();
            will(returnValue(contentId));
            final WorkspaceId wId = mockery.mock(WorkspaceId.class);
            exactly(threadCount).of(contentId).getWorkspaceId();
            will(returnValue(wId));
            exactly(2 * threadCount).of(type).getRepresentationDefs();
            will(returnValue(reps));
            exactly(2 * threadCount).of(reps).get(with(REP_NAME));
            will(returnValue(def));
            exactly(threadCount).of(def).getParameters();
            will(returnValue(Collections.emptyMap()));
            exactly(threadCount).of(def).getMIMEType();
            will(returnValue(GroovyGeneratorTest.MIME_TYPE));
            final ResourceUri rUri = mockery.mock(ResourceUri.class);
            exactly(threadCount).of(def).getResourceUri();
            will(returnValue(rUri));
            exactly(threadCount).of(rUri).getValue();
            will(returnValue("iUri"));
        }
    });
    final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount));
    final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount));
    final AtomicInteger integer = new AtomicInteger(0);
    Threads group = new Threads();
    for (int i = 0; i < threadCount; ++i) {
        group.addThread(new Thread(new Runnable() {

            public void run() {
                Representation representation = provider.getRepresentation(REP_NAME, type, content);
                Assert.assertNotNull(representation);
                Assert.assertEquals(REP_NAME, representation.getName());
                final String rep = StringUtils.newStringUtf8(representation.getRepresentation());
                list.add(rep);
                set.add(rep);
                Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType());
                integer.addAndGet(1);
            }
        }));
    }
    group.start();
    try {
        group.join();
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
    logger.info("Generated reps list: " + list);
    logger.info("Generated reps set: " + set);
    Assert.assertEquals(threadCount, integer.get());
    Assert.assertEquals(threadCount, list.size());
    Assert.assertEquals(threadCount, set.size());
}

From source file:org.alfresco.tools.RenameUser.java

@Override
protected int execute() throws ToolException {
    // Used for ability to be final and have a set
    final AtomicInteger status = new AtomicInteger(0);

    BatchProcessWorker<User> worker = new BatchProcessWorkerAdaptor<User>() {
        public void process(final User user) throws Throwable {
            RunAsWork<Void> runAsWork = new RunAsWork<Void>() {
                @Override/*from  ww w.  j  av  a2  s .  c  om*/
                public Void doWork() throws Exception {
                    try {
                        renameUser(user.getOldUsername(), user.getNewUsername());
                    } catch (Throwable t) {
                        status.set(handleError(t));
                    }
                    return null;
                }
            };
            AuthenticationUtil.runAs(runAsWork, context.getUsername());
        }
    };

    // Use 2 threads, 20 User objects per transaction. Log every 100 entries.
    BatchProcessor<User> processor = new BatchProcessor<User>("HomeFolderProviderSynchronizer",
            getServiceRegistry().getTransactionService().getRetryingTransactionHelper(),
            new WorkProvider(context), 2, 20, null, logger, 100);
    processor.process(worker, true);

    return status.get();
}

From source file:org.apache.hadoop.hbase.client.TestHCM.java

private void testConnectionClose(boolean allowsInterrupt) throws Exception {
    String tableName = "HCM-testConnectionClose" + allowsInterrupt;
    TEST_UTIL.createTable(tableName.getBytes(), FAM_NAM).close();

    boolean previousBalance = TEST_UTIL.getHBaseAdmin().setBalancerRunning(false, true);

    Configuration c2 = new Configuration(TEST_UTIL.getConfiguration());
    // We want to work on a separate connection.
    c2.set(HConstants.HBASE_CLIENT_INSTANCE_ID, String.valueOf(-1));
    c2.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 100); // retry a lot
    c2.setInt(HConstants.HBASE_CLIENT_PAUSE, 0); // don't wait between retries.
    c2.setInt(RpcClient.FAILED_SERVER_EXPIRY_KEY, 0); // Server do not really expire
    c2.setBoolean(RpcClient.ALLOWS_INTERRUPTS, allowsInterrupt);

    final HTable table = new HTable(c2, tableName.getBytes());

    Put put = new Put(ROW);
    put.add(FAM_NAM, ROW, ROW);/*from   w w w.ja  va 2  s .  c  o m*/
    table.put(put);

    // 4 steps: ready=0; doGets=1; mustStop=2; stopped=3
    final AtomicInteger step = new AtomicInteger(0);

    final AtomicReference<Throwable> failed = new AtomicReference<Throwable>(null);
    Thread t = new Thread("testConnectionCloseThread") {
        public void run() {
            int done = 0;
            try {
                step.set(1);
                while (step.get() == 1) {
                    Get get = new Get(ROW);
                    table.get(get);
                    done++;
                    if (done % 100 == 0)
                        LOG.info("done=" + done);
                }
            } catch (Throwable t) {
                failed.set(t);
                LOG.error(t);
            }
            step.set(3);
        }
    };
    t.start();
    TEST_UTIL.waitFor(20000, new Waiter.Predicate<Exception>() {
        @Override
        public boolean evaluate() throws Exception {
            return step.get() == 1;
        }
    });

    ServerName sn = table.getRegionLocation(ROW).getServerName();
    ConnectionManager.HConnectionImplementation conn = (ConnectionManager.HConnectionImplementation) table
            .getConnection();
    RpcClient rpcClient = conn.getRpcClient();

    LOG.info("Going to cancel connections. connection=" + conn.toString() + ", sn=" + sn);
    for (int i = 0; i < 5000; i++) {
        rpcClient.cancelConnections(sn.getHostname(), sn.getPort());
        Thread.sleep(5);
    }

    step.compareAndSet(1, 2);
    // The test may fail here if the thread doing the gets is stuck. The way to find
    //  out what's happening is to look for the thread named 'testConnectionCloseThread'
    TEST_UTIL.waitFor(40000, new Waiter.Predicate<Exception>() {
        @Override
        public boolean evaluate() throws Exception {
            return step.get() == 3;
        }
    });

    table.close();
    Assert.assertTrue("Unexpected exception is " + failed.get(), failed.get() == null);
    TEST_UTIL.getHBaseAdmin().setBalancerRunning(previousBalance, true);
}

From source file:com.adobe.acs.commons.workflow.process.impl.ReplicateWithOptionsWorkflowProcess.java

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
        throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final long start = System.currentTimeMillis();

    try {//ww  w.j av  a  2s .  c  om
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final String originalPayload = (String) workItem.getWorkflowData().getPayload();
        final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, originalPayload);
        final ProcessArgs processArgs = new ProcessArgs(metaDataMap);

        final AtomicInteger count = new AtomicInteger(0);

        // Anonymous inner class to facilitate counting of processed payloads
        final ResourceRunnable replicatorRunnable = new ResourceRunnable() {
            @Override
            public void run(final Resource resource) throws Exception {
                if (processArgs.isThrottle()) {
                    throttledTaskRunner.waitForLowCpuAndLowMemory();
                }
                replicator.replicate(resource.getResourceResolver().adaptTo(Session.class),
                        processArgs.getReplicationActionType(), resource.getPath(),
                        processArgs.getReplicationOptions(resource));
                count.incrementAndGet();
            }
        };

        final ContentVisitor visitor = new ContentVisitor(replicatorRunnable);

        for (final String payload : payloads) {
            final Resource resource = resourceResolver.getResource(payload);

            if (processArgs.isTraverseTree()) {
                // Traverse the tree
                visitor.accept(resource);
            } else {
                // Only execute on the provided payload
                replicatorRunnable.run(resource);
            }
        }

        log.info("Replicate with Options processed [ {} ] total payloads in {} ms", count.get(),
                System.currentTimeMillis() - start);
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}

From source file:com.evilisn.ScheduledTasks.java

@Scheduled(fixedRate = 10 * 60000)
public void scheduledCheckOCSP() throws InterruptedException {
    log.info("fetching certs");
    ArrayList<Cert> certs = (ArrayList<Cert>) getCerts();
    log.info(String.format("[%d] certs to be checked.", certs.size()));

    Date now = new Date();
    long startTime = now.getTime();
    System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "50");
    AtomicInteger c_REVOKED = new AtomicInteger();
    AtomicInteger c_GOOD = new AtomicInteger();
    AtomicInteger c_UNKNOWN = new AtomicInteger();
    AtomicInteger c_VALID = new AtomicInteger();
    AtomicInteger c_EXPIRED = new AtomicInteger();

    certs.parallelStream().forEach(o -> {
        try {//from   w  w w. j  a  v  a2s. c  om
            if (o.getClient_cert().getNotAfter().after(now)) {
                OCSP.RevocationStatus.CertStatus resp = OCSP.check(o.getClient_cert(), o.getIssuer_cert())
                        .getCertStatus();
                log.info(String.format("Serial Number [%20s]| OCSP Status:[%s]",
                        o.getClient_cert().getSerialNumber(), resp.toString()));
                c_VALID.getAndIncrement();
                if (resp == OCSP.RevocationStatus.CertStatus.GOOD)
                    c_GOOD.getAndIncrement();
                if (resp == OCSP.RevocationStatus.CertStatus.UNKNOWN)
                    c_UNKNOWN.getAndIncrement();
                if (resp == OCSP.RevocationStatus.CertStatus.REVOKED)
                    c_REVOKED.getAndIncrement();
            } else {
                //expired.
                c_EXPIRED.getAndIncrement();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertPathValidatorException e) {
            e.printStackTrace();
        }
    });
    long endTime = System.currentTimeMillis();
    log.info("ALL " + certs.size() + " certificates processed in " + (endTime - startTime) / 100
            + " seconds, with " + c_VALID.get() + " valid certs, " + c_EXPIRED + " expired certs, among which "
            + c_GOOD.get() + " is GOOD, " + c_REVOKED.get() + " is revoked, and " + c_UNKNOWN.get()
            + " is KNOWN.");
}

From source file:de.undercouch.bson4jackson.BsonParserTest.java

/**
 * Tests reading a very large string using multiple threads. Refers
 * issue #19. Does not fail reproducibly, but with very high probability.
 * You may have to run unit tests several times though to really rule out
 * multi-threading issues./*from w ww.java 2s. co m*/
 * @throws Exception if something went wrong
 * @author endasb
 */
@Test
public void parseBigStringInThreads() throws Exception {
    final BSONObject o = new BasicBSONObject();
    final AtomicInteger fails = new AtomicInteger(0);
    StringBuilder bigStr = new StringBuilder();
    for (int i = 0; i < 80000; i++) {
        bigStr.append("abc");
    }
    o.put("String", bigStr.toString());

    ArrayList<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < 50; i++) {
        threads.add(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<?, ?> data = parseBsonObject(o);
                    data = parseBsonObject(o);
                    assertNotNull(data);
                } catch (Exception e) {
                    fail("Threading issue " + fails.incrementAndGet());
                }
            }
        }));
    }
    for (Thread thread : threads) {
        thread.start();
    }

    for (Thread thread : threads) {
        thread.join();
    }

    assertEquals(0, fails.get());
}

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

@Test
public void testRelease1AtATime() throws Exception {
    final int CLIENT_QTY = 10;
    final int MAX = CLIENT_QTY / 2;
    final AtomicInteger maxLeases = new AtomicInteger(0);
    final AtomicInteger activeQty = new AtomicInteger(0);
    final AtomicInteger uses = new AtomicInteger(0);

    List<Future<Object>> futures = Lists.newArrayList();
    ExecutorService service = Executors.newFixedThreadPool(CLIENT_QTY);
    for (int i = 0; i < CLIENT_QTY; ++i) {
        Future<Object> f = service.submit(new Callable<Object>() {
            @Override/*from   w ww  .  j a v a 2s  .c om*/
            public Object call() throws Exception {
                CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
                        new RetryOneTime(1));
                client.start();
                try {
                    InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX);
                    Lease lease = semaphore.acquire(10, TimeUnit.SECONDS);
                    Assert.assertNotNull(lease);
                    uses.incrementAndGet();
                    try {
                        synchronized (maxLeases) {
                            int qty = activeQty.incrementAndGet();
                            if (qty > maxLeases.get()) {
                                maxLeases.set(qty);
                            }
                        }

                        Thread.sleep(500);
                    } finally {
                        activeQty.decrementAndGet();
                        lease.close();
                    }
                } finally {
                    client.close();
                }
                return null;
            }
        });
        futures.add(f);
    }

    for (Future<Object> f : futures) {
        f.get();
    }

    Assert.assertEquals(uses.get(), CLIENT_QTY);
    Assert.assertEquals(maxLeases.get(), MAX);
}

From source file:com.alibaba.wasp.jdbc.TestAtomicOperation.java

/**
 * Test multi-threaded row mutations.//from ww w.j  a  v a  2s  .c o m
 */
@Test
public void testRowMutationMultiThreads() throws IOException {

    LOG.info("Starting test testRowMutationMultiThreads");

    // create 100 threads, each will alternate between adding and
    // removing a column
    int numThreads = 10;
    int opsPerThread = 50;
    AtomicOperation[] all = new AtomicOperation[numThreads];

    AtomicInteger failures = new AtomicInteger(0);
    // create all threads
    for (int i = 0; i < numThreads; i++) {
        try {
            all[i] = new AtomicOperation(entityGroup, opsPerThread, conn, failures) {
                @Override
                public void run() {
                    for (int i = 0; i < numOps; i++) {
                        try {
                            int lines = stmt.executeUpdate("insert into " + TABLE_NAME
                                    + " (column1,column2,column3) values(1,1,'wasptest')");
                            if (lines != 1) {
                                LOG.debug(r);
                                failures.incrementAndGet();
                                fail();
                            }
                        } catch (SQLException e) {
                            failures.incrementAndGet();
                        }
                    }
                }
            };
        } catch (SQLException e) {
        }
    }

    // run all threads
    for (int i = 0; i < numThreads; i++) {
        all[i].start();
    }

    // wait for all threads to finish
    for (int i = 0; i < numThreads; i++) {
        try {
            all[i].join();
        } catch (InterruptedException e) {
        }
    }
    System.out.println(failures.get());
    assertEquals(opsPerThread * numThreads - 1, failures.get());
}

From source file:org.keycloak.testsuite.admin.concurrency.ConcurrentLoginTest.java

@Test
public void concurrentCodeReuseShouldFail() throws Throwable {
    log.info("*********************************************");
    long start = System.currentTimeMillis();

    for (int i = 0; i < 10; i++) {
        OAuthClient oauth1 = new OAuthClient();
        oauth1.init(driver);/*  ww w  . j  a va 2s  . c  o  m*/
        oauth1.clientId("client0");

        OAuthClient.AuthorizationEndpointResponse resp = oauth1.doLogin("test-user@localhost", "password");
        String code = resp.getCode();
        Assert.assertNotNull(code);
        String codeURL = driver.getCurrentUrl();

        AtomicInteger codeToTokenSuccessCount = new AtomicInteger(0);
        AtomicInteger codeToTokenErrorsCount = new AtomicInteger(0);

        KeycloakRunnable codeToTokenTask = new KeycloakRunnable() {

            @Override
            public void run(int threadIndex, Keycloak keycloak, RealmResource realm) throws Throwable {
                log.infof("Trying to execute codeURL: %s, threadIndex: %d", codeURL, threadIndex);

                OAuthClient.AccessTokenResponse resp = oauth1.doAccessTokenRequest(code, "password");
                if (resp.getAccessToken() != null && resp.getError() == null) {
                    codeToTokenSuccessCount.incrementAndGet();
                } else if (resp.getAccessToken() == null && resp.getError() != null) {
                    codeToTokenErrorsCount.incrementAndGet();
                }
            }

        };

        run(DEFAULT_THREADS, DEFAULT_THREADS, codeToTokenTask);

        oauth1.openLogout();

        // Code should be successfully exchanged for the token at max once. In some cases (EG. Cross-DC) it may not be even successfully exchanged
        Assert.assertThat(codeToTokenSuccessCount.get(), Matchers.lessThanOrEqualTo(1));
        Assert.assertThat(codeToTokenErrorsCount.get(), Matchers.greaterThanOrEqualTo(DEFAULT_THREADS - 1));

        log.infof("Iteration %d passed successfully", i);
    }

    long end = System.currentTimeMillis() - start;
    log.info("concurrentCodeReuseShouldFail took " + (end / 1000) + "s");
    log.info("*********************************************");

}