List of usage examples for java.util.concurrent.atomic AtomicLong AtomicLong
public AtomicLong()
From source file:com.qwazr.cluster.manager.ClusterManager.java
private ClusterManager(ExecutorService executor, String publicAddress, Set<String> myGroups) throws IOException, URISyntaxException { myAddress = ClusterNode.toAddress(publicAddress); if (logger.isInfoEnabled()) logger.info("Server: " + myAddress + " Groups: " + ArrayUtils.prettyPrint(myGroups)); this.myGroups = myGroups; this.executor = executor; // Load the configuration String masters_env = System.getenv("QWAZR_MASTERS"); // No configuration file ? Okay, we are a simple node if (StringUtils.isEmpty(masters_env)) { clusterMasterArray = null;//from w w w.java 2 s.c o m clusterNodeMap = null; clusterClient = null; checkTimeMap = null; lastTimeCheck = null; isMaster = false; isCluster = false; if (logger.isInfoEnabled()) logger.info("No QWAZR_MASTERS environment variable. This node is not part of a cluster."); return; } // Store the last time a master checked the node checkTimeMap = new ConcurrentHashMap<>(); lastTimeCheck = new AtomicLong(); // Build the master list and check if I am a master boolean isMaster = false; HashSet<String> masterSet = new HashSet<>(); int i = 0; String[] masters = StringUtils.split(masters_env, ','); for (String master : masters) { String masterAddress = ClusterNode.toAddress(master.trim()); logger.info("Add a master: " + masterAddress); masterSet.add(masterAddress); if (masterAddress == myAddress) { isMaster = true; if (logger.isInfoEnabled()) logger.info("I am a master!"); } } isCluster = true; clusterMasterArray = masterSet.toArray(new String[masterSet.size()]); clusterClient = new ClusterMultiClient(executor, clusterMasterArray, 60000); this.isMaster = isMaster; if (!isMaster) { clusterNodeMap = null; return; } // We load the cluster node map clusterNodeMap = new ClusterNodeMap(); }
From source file:com.alibaba.druid.benckmark.pool.Case2.java
private void p0(final DataSource dataSource, String name, int threadCount) throws Exception { final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(threadCount); final AtomicLong blockedStat = new AtomicLong(); final AtomicLong waitedStat = new AtomicLong(); for (int i = 0; i < threadCount; ++i) { Thread thread = new Thread() { public void run() { try { startLatch.await();/*from ww w . ja va 2s . c o m*/ long threadId = Thread.currentThread().getId(); long startBlockedCount, startWaitedCount; { ThreadInfo threadInfo = ManagementFactory.getThreadMXBean().getThreadInfo(threadId); startBlockedCount = threadInfo.getBlockedCount(); startWaitedCount = threadInfo.getWaitedCount(); } for (int i = 0; i < LOOP_COUNT; ++i) { Connection conn = dataSource.getConnection(); conn.close(); } ThreadInfo threadInfo = ManagementFactory.getThreadMXBean().getThreadInfo(threadId); long blockedCount = threadInfo.getBlockedCount() - startBlockedCount; long waitedCount = threadInfo.getWaitedCount() - startWaitedCount; blockedStat.addAndGet(blockedCount); waitedStat.addAndGet(waitedCount); } catch (Exception ex) { ex.printStackTrace(); } endLatch.countDown(); } }; thread.start(); } long startMillis = System.currentTimeMillis(); long startYGC = TestUtil.getYoungGC(); long startFullGC = TestUtil.getFullGC(); startLatch.countDown(); endLatch.await(); long millis = System.currentTimeMillis() - startMillis; long ygc = TestUtil.getYoungGC() - startYGC; long fullGC = TestUtil.getFullGC() - startFullGC; System.out.println("thread " + threadCount + " " + name + " millis : " + NumberFormat.getInstance().format(millis) + ", YGC " + ygc + " FGC " + fullGC + " blockedCount " + blockedStat.get() + " waitedCount " + waitedStat.get()); }
From source file:com.btoddb.fastpersitentqueue.flume.FpqChannelTest.java
@Test public void testThreading() throws Exception { final int numEntries = 1000; final int numPushers = 4; final int numPoppers = 4; final int entrySize = 1000; channel.setMaxTransactionSize(2000); final int popBatchSize = 100; channel.setMaxMemorySegmentSizeInBytes(10000000); channel.setMaxJournalFileSize(10000000); channel.setMaxJournalDurationInMs(30000); channel.setFlushPeriodInMs(1000);//from www .j ava 2s .co m channel.setNumberOfFlushWorkers(4); final Random pushRand = new Random(1000L); final Random popRand = new Random(1000000L); final AtomicInteger pusherFinishCount = new AtomicInteger(); final AtomicInteger numPops = new AtomicInteger(); final AtomicLong counter = new AtomicLong(); final AtomicLong pushSum = new AtomicLong(); final AtomicLong popSum = new AtomicLong(); channel.start(); ExecutorService execSrvc = Executors.newFixedThreadPool(numPushers + numPoppers); Set<Future> futures = new HashSet<Future>(); // start pushing for (int i = 0; i < numPushers; i++) { Future future = execSrvc.submit(new Runnable() { @Override public void run() { for (int i = 0; i < numEntries; i++) { try { long x = counter.getAndIncrement(); pushSum.addAndGet(x); ByteBuffer bb = ByteBuffer.wrap(new byte[entrySize]); bb.putLong(x); Transaction tx = channel.getTransaction(); tx.begin(); MyEvent event1 = new MyEvent(); event1.addHeader("x", String.valueOf(x)).setBody(new byte[numEntries - 8]); // take out size of long channel.put(event1); tx.commit(); tx.close(); Thread.sleep(pushRand.nextInt(5)); } catch (Exception e) { e.printStackTrace(); } } pusherFinishCount.incrementAndGet(); } }); futures.add(future); } // start popping for (int i = 0; i < numPoppers; i++) { Future future = execSrvc.submit(new Runnable() { @Override public void run() { while (pusherFinishCount.get() < numPushers || !channel.isEmpty()) { try { Transaction tx = channel.getTransaction(); tx.begin(); Event event; int count = popBatchSize; while (null != (event = channel.take()) && count-- > 0) { popSum.addAndGet(Long.valueOf(event.getHeaders().get("x"))); numPops.incrementAndGet(); } tx.commit(); tx.close(); Thread.sleep(popRand.nextInt(10)); } catch (Exception e) { e.printStackTrace(); } } } }); futures.add(future); } boolean finished = false; while (!finished) { try { for (Future f : futures) { f.get(); } finished = true; } catch (InterruptedException e) { // ignore Thread.interrupted(); } } assertThat(numPops.get(), is(numEntries * numPushers)); assertThat(channel.isEmpty(), is(true)); assertThat(pushSum.get(), is(popSum.get())); }
From source file:com.mfizz.observer.core.ServiceObserver.java
public ServiceObserver(ServiceObserverConfiguration serviceConfig, ExecutorService executor, Class<D> dataClass, Class<A> aggregateClass, Class<S> summaryClass, ContentParser<D> contentParser, ObserverNamingStrategy ons) {/*w w w . j av a 2 s. c o m*/ this.serviceConfig = serviceConfig; this.executor = executor; this.observers = new ConcurrentHashMap<String, Observer<D>>(); this.dataClass = dataClass; this.aggregateClass = aggregateClass; this.summaryClass = summaryClass; this.contentParser = contentParser; if (ons == null) { this.ons = ObserverNamingStrategy.DEFAULT; } else { this.ons = ons; } this.snapshotAllTimer = Metrics.newTimer( new MetricName("com.mfizz.observer.ServiceObservers", serviceConfig.getName(), "snapshot-all-time"), TimeUnit.MILLISECONDS, TimeUnit.MILLISECONDS); this.snapshotAllAttemptedCounter = new AtomicLong(); this.snapshotAllCompletedCounter = new AtomicLong(); this.lastSnapshotAllResult = new AtomicReference<SnapshotAllResult>(); //this.lastSnapshotAllTimestamp = new AtomicLong(0); //this.lastSnapshotsCompletedCounter = new AtomicInteger(); //this.lastSnapshotsFailedCounter = new AtomicInteger(); this.groups = new TreeSet<String>(); this.summary = new ConcurrentHashMap<String, SummaryGroup<S>>(); this.snapshots = new ConcurrentHashMap<String, TimeSeries<ObserveAggregateSnapshot<A>>>(); // always make sure "current" is added to set of vars to track // "current" always tracked for all this.serviceConfig.getPeriods().add(SummaryPeriod.parse("current")); // // create high performance http client that can be reused by all observers // SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); // increase max total connection to 200 cm.setMaxTotal(200); // increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); this.httpclient = new DefaultHttpClient(cm); this.httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { // always return false -- we never want a request retried return false; } }); this.httpclient.getParams() .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer((int) serviceConfig.getSocketTimeout())) .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer((int) serviceConfig.getConnectionTimeout())) .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) .setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false); }
From source file:org.apache.activemq.leveldb.test.ReplicatedLevelDBBrokerTest.java
@Test @Ignore//w w w. j a v a2 s.com public void testReplicationQuorumLoss() throws Throwable { System.out.println("======================================"); System.out.println(" Start 2 ActiveMQ nodes."); System.out.println("======================================"); startBrokerAsync(createBrokerNode("node-1", port)); startBrokerAsync(createBrokerNode("node-2", port)); BrokerService master = waitForNextMaster(); System.out.println("======================================"); System.out.println(" Start the producer and consumer"); System.out.println("======================================"); final AtomicBoolean stopClients = new AtomicBoolean(false); final ArrayBlockingQueue<String> errors = new ArrayBlockingQueue<String>(100); final AtomicLong receivedCounter = new AtomicLong(); final AtomicLong sentCounter = new AtomicLong(); Thread producer = startFailoverClient("producer", new Client() { @Override public void execute(Connection connection) throws Exception { Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageProducer producer = session.createProducer(session.createQueue("test")); long actual = 0; while (!stopClients.get()) { TextMessage msg = session.createTextMessage("Hello World"); msg.setLongProperty("id", actual++); producer.send(msg); sentCounter.incrementAndGet(); } } }); Thread consumer = startFailoverClient("consumer", new Client() { @Override public void execute(Connection connection) throws Exception { connection.start(); Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(session.createQueue("test")); long expected = 0; while (!stopClients.get()) { Message msg = consumer.receive(200); if (msg != null) { long actual = msg.getLongProperty("id"); if (actual != expected) { errors.offer("Received got unexpected msg id: " + actual + ", expected: " + expected); } msg.acknowledge(); expected = actual + 1; receivedCounter.incrementAndGet(); } } } }); try { assertCounterMakesProgress(sentCounter, 10, TimeUnit.SECONDS); assertCounterMakesProgress(receivedCounter, 5, TimeUnit.SECONDS); assertNull(errors.poll()); System.out.println("======================================"); System.out.println(" Master should stop once the quorum is lost."); System.out.println("======================================"); ArrayList<BrokerService> stopped = stopSlaves();// stopping the slaves should kill the quorum. assertStopsWithin(master, 10, TimeUnit.SECONDS); assertNull(errors.poll()); // clients should not see an error since they are failover clients. stopped.add(master); System.out.println("======================================"); System.out.println(" Restart the slave. Clients should make progress again.."); System.out.println("======================================"); startBrokersAsync(createBrokerNodes(stopped)); assertCounterMakesProgress(sentCounter, 10, TimeUnit.SECONDS); assertCounterMakesProgress(receivedCounter, 5, TimeUnit.SECONDS); assertNull(errors.poll()); } catch (Throwable e) { e.printStackTrace(); throw e; } finally { // Wait for the clients to stop.. stopClients.set(true); producer.join(); consumer.join(); } }
From source file:org.apache.aurora.scheduler.http.api.security.ApiSecurityIT.java
@Before public void setUp() { ini = new Ini(); Ini.Section users = ini.addSection(IniRealm.USERS_SECTION_NAME); users.put(ROOT.getUserName(), COMMA_JOINER.join(ROOT.getPassword(), ADMIN_ROLE)); users.put(WFARNER.getUserName(), COMMA_JOINER.join(WFARNER.getPassword(), ENG_ROLE)); users.put(UNPRIVILEGED.getUserName(), UNPRIVILEGED.getPassword()); users.put(BACKUP_SERVICE.getUserName(), COMMA_JOINER.join(BACKUP_SERVICE.getPassword(), BACKUP_ROLE)); users.put(DEPLOY_SERVICE.getUserName(), COMMA_JOINER.join(DEPLOY_SERVICE.getPassword(), DEPLOY_ROLE)); Ini.Section roles = ini.addSection(IniRealm.ROLES_SECTION_NAME); roles.put(ADMIN_ROLE, "*"); roles.put(ENG_ROLE, "thrift.AuroraSchedulerManager:*"); roles.put(BACKUP_ROLE, "thrift.AuroraAdmin:listBackups"); roles.put(DEPLOY_ROLE, "thrift.AuroraSchedulerManager:killTasks:" + ADS_STAGING_JOB.getRole() + ":" + ADS_STAGING_JOB.getEnvironment() + ":" + ADS_STAGING_JOB.getName()); auroraAdmin = createMock(AnnotatedAuroraAdmin.class); statsProvider = createMock(StatsProvider.class); expect(statsProvider.makeCounter(anyString())).andStubReturn(new AtomicLong()); }
From source file:com.espertech.esper.core.EPRuntimeImpl.java
/** * Constructor.//from w w w . j a v a 2 s. c o m * @param services - references to services */ public EPRuntimeImpl(final EPServicesContext services) { this.services = services; this.threadWorkQueue = new ThreadWorkQueue(); isLatchStatementInsertStream = this.services.getEngineSettingsService().getEngineSettings().getThreading() .isInsertIntoDispatchPreserveOrder(); isUsingExternalClocking = !this.services.getEngineSettingsService().getEngineSettings().getThreading() .isInternalTimerEnabled(); isSubselectPreeval = services.getEngineSettingsService().getEngineSettings().getExpression() .isSelfSubselectPreeval(); isPrioritized = services.getEngineSettingsService().getEngineSettings().getExecution().isPrioritized(); routedInternal = new AtomicLong(); routedExternal = new AtomicLong(); engineFilterAndDispatchTimeContext = new ExprEvaluatorContext() { private ExpressionResultCacheService expressionResultCacheService = new ExpressionResultCacheService(); public TimeProvider getTimeProvider() { return services.getSchedulingService(); } public ExpressionResultCacheService getExpressionResultCacheService() { return expressionResultCacheService; } }; matchesPerStmtThreadLocal = new ThreadLocal<Map<EPStatementHandle, ArrayDeque<FilterHandleCallback>>>() { protected synchronized Map<EPStatementHandle, ArrayDeque<FilterHandleCallback>> initialValue() { if (isPrioritized) { return new TreeMap<EPStatementHandle, ArrayDeque<FilterHandleCallback>>( new Comparator<EPStatementHandle>() { public int compare(EPStatementHandle o1, EPStatementHandle o2) { if (o1 == o2) { return 0; } if (o1.equals(o2)) { return 0; } return o1.getPriority() >= o2.getPriority() ? -1 : 1; } }); } else { return new HashMap<EPStatementHandle, ArrayDeque<FilterHandleCallback>>(10000); } } }; schedulePerStmtThreadLocal = new ThreadLocal<Map<EPStatementHandle, Object>>() { protected synchronized Map<EPStatementHandle, Object> initialValue() { if (isPrioritized) { return new TreeMap<EPStatementHandle, Object>(new Comparator<EPStatementHandle>() { public int compare(EPStatementHandle o1, EPStatementHandle o2) { if (o1 == o2) { return 0; } if (o1.equals(o2)) { return 0; } return o1.getPriority() >= o2.getPriority() ? -1 : 1; } }); } else { return new HashMap<EPStatementHandle, Object>(10000); } } }; services.getThreadingService().initThreading(services, this); }
From source file:org.apache.hadoop.hbase.client.TestClientPushback.java
@Test(timeout = 60000) public void testClientTracksServerPushback() throws Exception { Configuration conf = UTIL.getConfiguration(); ClusterConnection conn = (ClusterConnection) ConnectionFactory.createConnection(conf); HTable table = (HTable) conn.getTable(tableName); HRegionServer rs = UTIL.getHBaseCluster().getRegionServer(0); Region region = rs.getOnlineRegions(tableName).get(0); LOG.debug("Writing some data to " + tableName); // write some data Put p = new Put(Bytes.toBytes("row")); p.addColumn(family, qualifier, Bytes.toBytes("value1")); table.put(p);/*ww w. j a va 2 s . c om*/ // get the current load on RS. Hopefully memstore isn't flushed since we wrote the the data int load = (int) ((((HRegion) region).addAndGetGlobalMemstoreSize(0) * 100) / flushSizeBytes); LOG.debug("Done writing some data to " + tableName); // get the stats for the region hosting our table ClientBackoffPolicy backoffPolicy = conn.getBackoffPolicy(); assertTrue("Backoff policy is not correctly configured", backoffPolicy instanceof ExponentialClientBackoffPolicy); ServerStatisticTracker stats = conn.getStatisticsTracker(); assertNotNull("No stats configured for the client!", stats); // get the names so we can query the stats ServerName server = rs.getServerName(); byte[] regionName = region.getRegionInfo().getRegionName(); // check to see we found some load on the memstore ServerStatistics serverStats = stats.getServerStatsForTesting(server); ServerStatistics.RegionStatistics regionStats = serverStats.getStatsForRegion(regionName); assertEquals("We did not find some load on the memstore", load, regionStats.getMemstoreLoadPercent()); // check that the load reported produces a nonzero delay long backoffTime = backoffPolicy.getBackoffTime(server, regionName, serverStats); assertNotEquals("Reported load does not produce a backoff", backoffTime, 0); LOG.debug("Backoff calculated for " + region.getRegionInfo().getRegionNameAsString() + " @ " + server + " is " + backoffTime); // Reach into the connection and submit work directly to AsyncProcess so we can // monitor how long the submission was delayed via a callback List<Row> ops = new ArrayList<Row>(1); ops.add(p); final CountDownLatch latch = new CountDownLatch(1); final AtomicLong endTime = new AtomicLong(); long startTime = EnvironmentEdgeManager.currentTime(); table.mutator.ap.submit(tableName, ops, true, new Batch.Callback<Result>() { @Override public void update(byte[] region, byte[] row, Result result) { endTime.set(EnvironmentEdgeManager.currentTime()); latch.countDown(); } }, true); // Currently the ExponentialClientBackoffPolicy under these test conditions // produces a backoffTime of 151 milliseconds. This is long enough so the // wait and related checks below are reasonable. Revisit if the backoff // time reported by above debug logging has significantly deviated. latch.await(backoffTime * 2, TimeUnit.MILLISECONDS); assertNotEquals("AsyncProcess did not submit the work time", endTime.get(), 0); assertTrue("AsyncProcess did not delay long enough", endTime.get() - startTime >= backoffTime); }
From source file:com.espertech.esper.core.service.EPRuntimeImpl.java
/** * Constructor.//from ww w . j av a2s .c o m * @param services - references to services */ public EPRuntimeImpl(final EPServicesContext services) { this.services = services; this.threadWorkQueue = new ThreadWorkQueue(); isLatchStatementInsertStream = this.services.getEngineSettingsService().getEngineSettings().getThreading() .isInsertIntoDispatchPreserveOrder(); isUsingExternalClocking = !this.services.getEngineSettingsService().getEngineSettings().getThreading() .isInternalTimerEnabled(); isSubselectPreeval = services.getEngineSettingsService().getEngineSettings().getExpression() .isSelfSubselectPreeval(); isPrioritized = services.getEngineSettingsService().getEngineSettings().getExecution().isPrioritized(); routedInternal = new AtomicLong(); routedExternal = new AtomicLong(); engineFilterAndDispatchTimeContext = new ExprEvaluatorContext() { private ExpressionResultCacheService expressionResultCacheService = services .getExpressionResultCacheSharable(); public TimeProvider getTimeProvider() { return services.getSchedulingService(); } public ExpressionResultCacheService getExpressionResultCacheService() { return expressionResultCacheService; } public int getAgentInstanceId() { return -1; } public EventBean getContextProperties() { return null; } public AgentInstanceScriptContext getAgentInstanceScriptContext() { return null; } public String getStatementName() { return null; } public String getEngineURI() { return null; } public String getStatementId() { return null; } public StatementAgentInstanceLock getAgentInstanceLock() { return null; } }; initThreadLocals(); services.getThreadingService().initThreading(services, this); }
From source file:com.alibaba.rocketmq.tools.command.message.PrintMessageByQueueCommand.java
private static void calculateByTag(final List<MessageExt> msgs, final Map<String, AtomicLong> tagCalmap, final boolean calByTag) { if (!calByTag) return;//from w ww.j av a 2 s .c om for (MessageExt msg : msgs) { String tag = msg.getTags(); if (StringUtils.isNotBlank(tag)) { AtomicLong count = tagCalmap.get(tag); if (count == null) { count = new AtomicLong(); tagCalmap.put(tag, count); } count.incrementAndGet(); } } }