List of usage examples for java.util.concurrent.atomic AtomicInteger incrementAndGet
public final int incrementAndGet()
From source file:org.apache.hadoop.hbase.master.procedure.MasterProcedureTestingUtility.java
private static int countMetaRegions(final HMaster master, final TableName tableName) throws IOException { final AtomicInteger actualRegCount = new AtomicInteger(0); final MetaTableAccessor.Visitor visitor = new MetaTableAccessor.Visitor() { @Override/*from w w w.j a va 2s . c o m*/ public boolean visit(Result rowResult) throws IOException { RegionLocations list = MetaTableAccessor.getRegionLocations(rowResult); if (list == null) { LOG.warn("No serialized HRegionInfo in " + rowResult); return true; } HRegionLocation l = list.getRegionLocation(); if (l == null) { return true; } if (!l.getRegionInfo().getTable().equals(tableName)) { return false; } if (l.getRegionInfo().isOffline() || l.getRegionInfo().isSplit()) return true; HRegionLocation[] locations = list.getRegionLocations(); for (HRegionLocation location : locations) { if (location == null) continue; ServerName serverName = location.getServerName(); // Make sure that regions are assigned to server if (serverName != null && serverName.getHostAndPort() != null) { actualRegCount.incrementAndGet(); } } return true; } }; MetaTableAccessor.scanMetaForTableRegions(master.getConnection(), visitor, tableName); return actualRegCount.get(); }
From source file:interactivespaces.activity.component.ActivityComponentContextTest.java
/** * Make a couple of threads start running and see if they properly stop * running when the context signals startup successful. *///from ww w .ja v a2s. co m @Test public void testStartupWaitWithTwoThreadsSuccess() throws Exception { final CountDownLatch startLatch = new CountDownLatch(2); final CountDownLatch stopLatch = new CountDownLatch(2); final AtomicInteger countAllowedHandlers = new AtomicInteger(0); Runnable runnable = new Runnable() { @Override public void run() { startLatch.countDown(); if (context.canHandlerRun()) { countAllowedHandlers.incrementAndGet(); } stopLatch.countDown(); } }; executor.execute(runnable); executor.execute(runnable); // Make sure they have both entered before starting the wait. Assert.assertTrue(startLatch.await(500, TimeUnit.MILLISECONDS)); context.endStartupPhase(true); // Make sure they have both entered before starting the wait. Assert.assertTrue(stopLatch.await(500, TimeUnit.MILLISECONDS)); // All handlers should have been allowed. Assert.assertEquals(2, countAllowedHandlers.get()); }
From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java
@Test public void shouldDecodeOneViewQueryResponse() throws Exception { String response = Resources.read("query_one.json", this.getClass()); HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "OK")); HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8)); ViewQueryRequest requestMock = mock(ViewQueryRequest.class); queue.add(requestMock);// w w w. j av a2 s . c o m channel.writeInbound(responseHeader, responseChunk); latch.await(1, TimeUnit.SECONDS); assertEquals(1, firedEvents.size()); ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0); assertTrue(inbound.status().isSuccess()); assertEquals(200, inbound.responseCode()); assertEquals("OK", inbound.responsePhrase()); final AtomicInteger calledRow = new AtomicInteger(); inbound.rows().toBlocking().forEach(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { calledRow.incrementAndGet(); try { Map found = mapper.readValue(byteBuf.toString(CharsetUtil.UTF_8), Map.class); assertEquals(3, found.size()); assertTrue(found.containsKey("id")); assertTrue(found.containsKey("key")); assertTrue(found.containsKey("value")); } catch (IOException e) { e.printStackTrace(); assertFalse(true); } } }); assertEquals(1, calledRow.get()); final AtomicInteger called = new AtomicInteger(); inbound.info().toBlocking().forEach(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { called.incrementAndGet(); assertEquals("{\"total_rows\":7303}", byteBuf.toString(CharsetUtil.UTF_8)); } }); assertEquals(1, called.get()); }
From source file:interactivespaces.activity.component.ActivityComponentContextTest.java
/** * Make a couple of threads start running and see if they properly stop * running when the context signals startup failure. *///from w w w . ja v a 2 s. c o m @Test public void testStartupWaitWithTwoThreadsFailure() throws Exception { final CountDownLatch startLatch = new CountDownLatch(2); final CountDownLatch stopLatch = new CountDownLatch(2); final AtomicInteger countAllowedHandlers = new AtomicInteger(0); Runnable runnable = new Runnable() { @Override public void run() { startLatch.countDown(); if (context.canHandlerRun()) { countAllowedHandlers.incrementAndGet(); } stopLatch.countDown(); } }; executor.execute(runnable); executor.execute(runnable); // Make sure they have both entered before starting the wait. Assert.assertTrue(startLatch.await(500, TimeUnit.MILLISECONDS)); context.endStartupPhase(false); // Make sure they have both entered before starting the wait. Assert.assertTrue(stopLatch.await(500, TimeUnit.MILLISECONDS)); // No handlers should have been allowed. Assert.assertEquals(0, countAllowedHandlers.get()); }
From source file:org.acmsl.queryj.api.handlers.AbstractTemplateWritingHandler.java
/** * Writes the templates./* www . jav a2s .co m*/ * @param templates the templates. * @param engineName the engine name. * @param parameters the parameters. * @param charset the file encoding. * @param templateGenerator the template generator. * @param threadCount the number of threads to use. * @param rootDir the root dir. * @return the futures for the concurrent threads. * @throws QueryJBuildException if the templates cannot be written. */ @NotNull @SuppressWarnings("unused") protected List<Future<?>> writeTemplatesMultithread2ndVersion(@Nullable final List<T> templates, @NotNull final String engineName, @NotNull final QueryJCommand parameters, @NotNull final Charset charset, @NotNull final TG templateGenerator, final int threadCount, @NotNull final File rootDir) throws QueryJBuildException { @NotNull final List<Future<?>> result; if (templates != null) { result = new ArrayList<>(templates.size()); @NotNull final ExecutorService threadPool = Executors.newFixedThreadPool(threadCount); @NotNull final CyclicBarrier round = new CyclicBarrier(threadCount); @NotNull AtomicInteger index = new AtomicInteger(0); int intIndex; @Nullable final Log t_Log = UniqueLogFactory.getLog(AbstractTemplateWritingHandler.class); for (@Nullable final T t_Template : templates) { if (t_Template != null) { intIndex = index.incrementAndGet(); if (intIndex <= threadCount) { if (t_Log != null) { t_Log.info("Starting a new thread " + intIndex + "/" + threadCount); } result.add(threadPool.submit((Runnable) buildGeneratorThread(t_Template, templateGenerator, retrieveOutputDir(t_Template.getTemplateContext(), rootDir, parameters), rootDir, charset, intIndex, round, parameters))); } else { if (t_Log != null) { t_Log.info("No threads available " + intIndex + "/" + threadCount); } index = new AtomicInteger(0); try { round.await(); } catch (@NotNull final InterruptedException interrupted) { if (t_Log != null) { t_Log.info("Thread pool interrupted while waiting", interrupted); } } catch (@NotNull final BrokenBarrierException brokenBarrier) { if (t_Log != null) { t_Log.info(BROKEN_BARRIER_LITERAL, brokenBarrier); } } if (t_Log != null) { t_Log.info("Resetting thread pool (shutdown? " + threadPool.isShutdown() + ")"); } round.reset(); } } } } else { result = new ArrayList<>(0); } return result; }
From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java
@Test public void shouldDecodeManyViewQueryResponse() throws Exception { String response = Resources.read("query_many.json", this.getClass()); HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "OK")); HttpContent responseChunk1 = new DefaultHttpContent( Unpooled.copiedBuffer(response.substring(0, 500), CharsetUtil.UTF_8)); HttpContent responseChunk2 = new DefaultHttpContent( Unpooled.copiedBuffer(response.substring(500, 1234), CharsetUtil.UTF_8)); HttpContent responseChunk3 = new DefaultLastHttpContent( Unpooled.copiedBuffer(response.substring(1234), CharsetUtil.UTF_8)); ViewQueryRequest requestMock = mock(ViewQueryRequest.class); queue.add(requestMock);// w ww . j a v a2 s . c o m channel.writeInbound(responseHeader, responseChunk1, responseChunk2, responseChunk3); latch.await(1, TimeUnit.SECONDS); assertEquals(1, firedEvents.size()); ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0); assertTrue(inbound.status().isSuccess()); final AtomicInteger calledRow = new AtomicInteger(); inbound.rows().toBlocking().forEach(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { calledRow.incrementAndGet(); try { Map found = mapper.readValue(byteBuf.toString(CharsetUtil.UTF_8), Map.class); assertEquals(3, found.size()); assertTrue(found.containsKey("id")); assertTrue(found.containsKey("key")); assertTrue(found.containsKey("value")); } catch (IOException e) { e.printStackTrace(); assertFalse(true); } } }); assertEquals(500, calledRow.get()); final AtomicInteger called = new AtomicInteger(); inbound.info().toBlocking().forEach(new Action1<ByteBuf>() { @Override public void call(ByteBuf byteBuf) { called.incrementAndGet(); assertEquals("{\"total_rows\":7303}", byteBuf.toString(CharsetUtil.UTF_8)); } }); assertEquals(1, called.get()); }
From source file:com.facebook.BatchRequestTests.java
@LargeTest public void testBatchCallbackIsCalled() { final AtomicInteger count = new AtomicInteger(); GraphRequest request1 = GraphRequest.newGraphPathRequest(null, "4", new GraphRequest.Callback() { @Override//from ww w.j a v a 2 s .co m public void onCompleted(GraphResponse response) { count.incrementAndGet(); } }); GraphRequest request2 = GraphRequest.newGraphPathRequest(null, "4", new GraphRequest.Callback() { @Override public void onCompleted(GraphResponse response) { count.incrementAndGet(); } }); GraphRequestBatch batch = new GraphRequestBatch(request1, request2); batch.addCallback(new GraphRequestBatch.Callback() { @Override public void onBatchCompleted(GraphRequestBatch batch) { count.incrementAndGet(); } }); batch.executeAndWait(); assertEquals(3, count.get()); }
From source file:org.apache.hadoop.hdfs.TestAutoEditRollWhenAvatarFailover.java
/** * Test if we can get block locations after killing primary avatar, * failing over to standby avatar (making it the new primary), * restarting a new standby avatar, killing the new primary avatar and * failing over to the restarted standby. * /*from ww w. j a v a 2s. c o m*/ * Write logs for a while to make sure automatic rolling are triggered. */ @Test public void testDoubleFailOverWithAutomaticRoll() throws Exception { setUp(false, "testDoubleFailOverWithAutomaticRoll"); // To make sure it's never the case that both primary and standby // issue rolling, we use a injection handler. final AtomicBoolean startKeepThread = new AtomicBoolean(true); final AtomicInteger countAutoRolled = new AtomicInteger(0); final AtomicBoolean needFail = new AtomicBoolean(false); final AtomicLong currentThreadId = new AtomicLong(-1); final Object waitFor10Rolls = new Object(); InjectionHandler.set(new InjectionHandler() { @Override protected void _processEvent(InjectionEventI event, Object... args) { if (event == InjectionEvent.FSEDIT_AFTER_AUTOMATIC_ROLL) { countAutoRolled.incrementAndGet(); if (countAutoRolled.get() >= 10) { synchronized (waitFor10Rolls) { waitFor10Rolls.notifyAll(); } } if (!startKeepThread.get()) { currentThreadId.set(-1); } else if (currentThreadId.get() == -1) { currentThreadId.set(Thread.currentThread().getId()); } else if (currentThreadId.get() != Thread.currentThread().getId()) { LOG.warn("[Thread " + Thread.currentThread().getId() + "] expected: " + currentThreadId); needFail.set(true); } LOG.info("[Thread " + Thread.currentThread().getId() + "] finish automatic log rolling, count " + countAutoRolled.get()); // Increase the rolling time a little bit once after 7 auto rolls if (countAutoRolled.get() % 7 == 3) { DFSTestUtil.waitNMilliSecond(75); } } } }); FileSystem fs = cluster.getFileSystem(); // Add some transactions during a period of time before failing over. long startTime = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { fs.setTimes(new Path("/"), 0, 0); DFSTestUtil.waitNMilliSecond(100); if (i % 10 == 0) { LOG.info("================== executed " + i + " queries"); } if (countAutoRolled.get() >= 10) { LOG.info("Automatic rolled 10 times."); long duration = System.currentTimeMillis() - startTime; TestCase.assertTrue("Automatic rolled 10 times in just " + duration + " msecs, which is too short", duration > 4500); break; } } TestCase.assertTrue("Only " + countAutoRolled + " automatic rolls triggered, which is lower than expected.", countAutoRolled.get() >= 10); // Tune the rolling timeout temporarily to avoid race conditions // only triggered in tests cluster.getPrimaryAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(5000); cluster.getStandbyAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(5000); LOG.info("================== killing primary 1"); cluster.killPrimary(); // Fail over and make sure after fail over, automatic edits roll still // will happen. countAutoRolled.set(0); startKeepThread.set(false); currentThreadId.set(-1); LOG.info("================== failing over 1"); cluster.failOver(); cluster.getPrimaryAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(1000); LOG.info("================== restarting standby"); cluster.restartStandby(); cluster.getStandbyAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(1000); LOG.info("================== Finish restarting standby"); // Wait for automatic rolling happens if there is no new transaction. startKeepThread.set(true); startTime = System.currentTimeMillis(); long waitDeadLine = startTime + 20000; synchronized (waitFor10Rolls) { while (System.currentTimeMillis() < waitDeadLine && countAutoRolled.get() < 10) { waitFor10Rolls.wait(waitDeadLine - System.currentTimeMillis()); } } TestCase.assertTrue("Only " + countAutoRolled + " automatic rolls triggered, which is lower than expected.", countAutoRolled.get() >= 10); long duration = System.currentTimeMillis() - startTime; TestCase.assertTrue("Automatic rolled 10 times in just " + duration + " msecs", duration > 9000); // failover back countAutoRolled.set(0); startKeepThread.set(false); currentThreadId.set(-1); cluster.getPrimaryAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(6000); cluster.getStandbyAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(6000); LOG.info("================== killing primary 2"); cluster.killPrimary(); LOG.info("================== failing over 2"); cluster.failOver(); cluster.getPrimaryAvatar(0).avatar.namesystem.getFSImage().getEditLog().setTimeoutRollEdits(1000); // Make sure after failover back, automatic rolling can still happen. startKeepThread.set(true); for (int i = 0; i < 100; i++) { fs.setTimes(new Path("/"), 0, 0); DFSTestUtil.waitNMilliSecond(200); if (i % 10 == 0) { LOG.info("================== executed " + i + " queries"); } if (countAutoRolled.get() > 10) { LOG.info("Automatic rolled 10 times."); duration = System.currentTimeMillis() - startTime; TestCase.assertTrue("Automatic rolled 10 times in just " + duration + " msecs, which is too short", duration > 9000); break; } } TestCase.assertTrue("Only " + countAutoRolled + " automatic rolls triggered, which is lower than expected.", countAutoRolled.get() >= 10); InjectionHandler.clear(); if (needFail.get()) { TestCase.fail("Automatic rolling doesn't happen in the same thread when should."); } }
From source file:io.fabric8.msg.jnatsd.TestProtocol.java
@Test public void testUnsubscribe() throws Exception { final Channel<Boolean> ch = new Channel<Boolean>(); final AtomicInteger count = new AtomicInteger(0); final int max = 20; connectionFactory.setReconnectAllowed(false); try (Connection c = connectionFactory.createConnection()) { try (final AsyncSubscription s = c.subscribeAsync("foo", new MessageHandler() { @Override/* w ww. j av a 2 s . com*/ public void onMessage(Message m) { count.incrementAndGet(); if (count.get() == max) { try { m.getSubscription().unsubscribe(); assertFalse(m.getSubscription().isValid()); } catch (Exception e) { fail("Unsubscribe failed with err: " + e.getMessage()); } ch.add(true); } } })) { for (int i = 0; i < max; i++) { c.publish("foo", null, (byte[]) null); } sleep(100); c.flush(); if (s.isValid()) { assertTrue("Test complete signal not received", ch.get(5, TimeUnit.SECONDS)); assertFalse(s.isValid()); } assertEquals(max, count.get()); } } }
From source file:org.apache.hadoop.hbase.master.procedure.TestWALProcedureStoreOnHDFS.java
@Test(timeout = 60000) public void testWalAbortOnLowReplicationWithQueuedWriters() throws Exception { assertEquals(3, UTIL.getDFSCluster().getDataNodes().size()); store.registerListener(new ProcedureStore.ProcedureStoreListener() { @Override//www . j a v a2s .c om public void postSync() { Threads.sleepWithoutInterrupt(2000); } @Override public void abortProcess() { } }); final AtomicInteger reCount = new AtomicInteger(0); Thread[] thread = new Thread[store.getNumThreads() * 2 + 1]; for (int i = 0; i < thread.length; ++i) { final long procId = i + 1; thread[i] = new Thread() { public void run() { try { LOG.debug("[S] INSERT " + procId); store.insert(new TestProcedure(procId, -1), null); LOG.debug("[E] INSERT " + procId); } catch (RuntimeException e) { reCount.incrementAndGet(); LOG.debug("[F] INSERT " + procId + ": " + e.getMessage()); } } }; thread[i].start(); } Thread.sleep(1000); LOG.info("Stop DataNode"); UTIL.getDFSCluster().stopDataNode(0); assertEquals(2, UTIL.getDFSCluster().getDataNodes().size()); for (int i = 0; i < thread.length; ++i) { thread[i].join(); } assertFalse(store.isRunning()); assertTrue(reCount.toString(), reCount.get() >= store.getNumThreads() && reCount.get() < thread.length); }