List of usage examples for java.util.concurrent.atomic AtomicInteger AtomicInteger
public AtomicInteger(int initialValue)
From source file:fi.luontola.cqrshotel.framework.EventStoreContract.java
@Test public void concurrent_writers_to_different_streams() throws Exception { final int BATCH_SIZE = 10; final int ITERATIONS = 100; long initialPosition = eventStore.getCurrentPosition(); AtomicInteger taskIdSeq = new AtomicInteger(0); repeatInParallel(ITERATIONS, () -> { UUID streamId = UUID.randomUUID(); int taskId = taskIdSeq.incrementAndGet(); List<Event> batch = createBatch(BATCH_SIZE, taskId); eventStore.saveEvents(streamId, batch, EventStore.BEGINNING); }, createRuntimeInvariantChecker(BATCH_SIZE)); List<Event> allEvents = eventStore.getAllEvents(initialPosition); assertThat("number of saved events", allEvents.size(), is(BATCH_SIZE * ITERATIONS)); assertAtomicBatches(BATCH_SIZE, allEvents); }
From source file:mazewar.Mazewar.java
/** * The place where all the pieces are put together. */// ww w .ja va2s. co m public Mazewar(String zkServer, int zkPort, int port, String name, String game, boolean robot) { super("ECE419 Mazewar"); consolePrintLn("ECE419 Mazewar started!"); /* Set up parent */ ZK_PARENT += game; // Throw up a dialog to get the GUIClient name. if (name != null) { clientId = name; } else { clientId = JOptionPane.showInputDialog("Enter your name"); } if ((clientId == null) || (clientId.length() == 0)) { Mazewar.quit(); } /* Connect to ZooKeeper and get sequencer details */ List<ClientNode> nodeList = null; try { zkWatcher = new ZkWatcher(); zkConnected = new CountDownLatch(1); zooKeeper = new ZooKeeper(zkServer + ":" + zkPort, ZK_TIMEOUT, new Watcher() { @Override public void process(WatchedEvent event) { /* Release Lock if ZooKeeper is connected */ if (event.getState() == SyncConnected) { zkConnected.countDown(); } else { System.err.println("Could not connect to ZooKeeper!"); System.exit(0); } } }); zkConnected.await(); /* Successfully connected, now create our node on ZooKeeper */ zooKeeper.create(Joiner.on('/').join(ZK_PARENT, clientId), Joiner.on(':').join(InetAddress.getLocalHost().getHostAddress(), port).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); /* Get Seed from Parent */ mazeSeed = Long.parseLong(new String(zooKeeper.getData(ZK_PARENT, false, null))); /* Initialize Sequence Number */ sequenceNumber = new AtomicInteger(zooKeeper.exists(ZK_PARENT, false).getVersion()); /* Get list of nodes */ nodeList = ClientNode.sortList(zooKeeper.getChildren(ZK_PARENT, false)); } catch (Exception e) { e.printStackTrace(); System.exit(1); } // Create the maze maze = new MazeImpl(new Point(mazeWidth, mazeHeight), mazeSeed); assert (maze != null); // Have the ScoreTableModel listen to the maze to find // out how to adjust scores. ScoreTableModel scoreModel = new ScoreTableModel(); assert (scoreModel != null); maze.addMazeListener(scoreModel); /* Initialize packet queue */ packetQueue = new ArrayBlockingQueue<MazePacket>(QUEUE_SIZE); sequencedQueue = new PriorityBlockingQueue<MazePacket>(QUEUE_SIZE, new Comparator<MazePacket>() { @Override public int compare(MazePacket o1, MazePacket o2) { return o1.sequenceNumber.compareTo(o2.sequenceNumber); } }); /* Inject Event Bus into Client */ Client.setEventBus(eventBus); /* Initialize ZMQ Context */ context = ZMQ.context(2); /* Set up publisher */ publisher = context.socket(ZMQ.PUB); publisher.bind("tcp://*:" + port); System.out.println("ZeroMQ Publisher Bound On: " + port); try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } /* Set up subscriber */ subscriber = context.socket(ZMQ.SUB); subscriber.subscribe(ArrayUtils.EMPTY_BYTE_ARRAY); clients = new ConcurrentHashMap<String, Client>(); try { for (ClientNode client : nodeList) { if (client.getName().equals(clientId)) { clientPath = ZK_PARENT + "/" + client.getPath(); guiClient = robot ? new RobotClient(clientId) : new GUIClient(clientId); clients.put(clientId, guiClient); maze.addClient(guiClient); eventBus.register(guiClient); subscriber.connect("tcp://" + new String(zooKeeper.getData(clientPath, false, null))); } else { addRemoteClient(client); } } } catch (Exception e) { e.printStackTrace(); } checkNotNull(guiClient, "Should have received our clientId in CLIENTS list!"); // Create the GUIClient and connect it to the KeyListener queue this.addKeyListener(guiClient); this.isRobot = robot; // Use braces to force constructors not to be called at the beginning of the // constructor. /*{ maze.addClient(new RobotClient("Norby")); maze.addClient(new RobotClient("Robbie")); maze.addClient(new RobotClient("Clango")); maze.addClient(new RobotClient("Marvin")); }*/ // Create the panel that will display the maze. overheadPanel = new OverheadMazePanel(maze, guiClient); assert (overheadPanel != null); maze.addMazeListener(overheadPanel); // Don't allow editing the console from the GUI console.setEditable(false); console.setFocusable(false); console.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder())); // Allow the console to scroll by putting it in a scrollpane JScrollPane consoleScrollPane = new JScrollPane(console); assert (consoleScrollPane != null); consoleScrollPane .setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Console")); // Create the score table scoreTable = new JTable(scoreModel); assert (scoreTable != null); scoreTable.setFocusable(false); scoreTable.setRowSelectionAllowed(false); // Allow the score table to scroll too. JScrollPane scoreScrollPane = new JScrollPane(scoreTable); assert (scoreScrollPane != null); scoreScrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Scores")); // Create the layout manager GridBagLayout layout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); getContentPane().setLayout(layout); // Define the constraints on the components. c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 3.0; c.gridwidth = GridBagConstraints.REMAINDER; layout.setConstraints(overheadPanel, c); c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 2.0; c.weighty = 1.0; layout.setConstraints(consoleScrollPane, c); c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; layout.setConstraints(scoreScrollPane, c); // Add the components getContentPane().add(overheadPanel); getContentPane().add(consoleScrollPane); getContentPane().add(scoreScrollPane); // Pack everything neatly. pack(); // Let the magic begin. setVisible(true); overheadPanel.repaint(); this.requestFocusInWindow(); }
From source file:com.aol.advertising.qiao.util.cache.PersistentMap.java
public void init() throws Exception { this.dbConfig = database.getConfig(); this.dbEnvironment = database.getEnvironment(); this.databaseName = database.getDatabaseName(); this.isTransactional = dbConfig.getTransactional(); this.cacheMode = dbConfig.getCacheMode(); if (highWaterMark > 0 && lowWaterMark == 0) this.lowWaterMark = (int) Math.round(0.9 * highWaterMark); this.itemCount = new AtomicInteger(super.size()); // init count _openSecondaryIndexes();/*ww w .j a va 2s . c om*/ if (timer == null) timer = new ScheduledThreadPoolExecutor(1); enableReaping(reapingInitDelaySecs, reapingIntervalSecs); }
From source file:cz.cuni.mff.ufal.AllBitstreamZipArchiveReader.java
/** Creates unique filename based on map of counters of already used filenames */ protected String createUniqueFilename(String filename, ConcurrentMap<String, AtomicInteger> usedFilenames) { String uniqueFilename = filename; usedFilenames.putIfAbsent(filename, new AtomicInteger(0)); int occurence = usedFilenames.get(filename).incrementAndGet(); if (occurence > 1) { uniqueFilename = addSuffixToFilename(filename, String.valueOf(occurence)); }// w ww .j a v a 2s. co m return uniqueFilename; }
From source file:com.mgmtp.jfunk.core.JFunk.java
private ExecutorService createExecutorService() { return new FixedSizeThreadExecutor(min(threadCount, scripts.size()), new ThreadFactory() { private final AtomicInteger threadNumber = new AtomicInteger(1); @Override/* ww w . j a va2 s . c o m*/ public Thread newThread(final Runnable r) { int id = threadNumber.getAndIncrement(); String threadName = StringUtils.leftPad(String.valueOf(id), 2, "0"); Thread th = new Thread(r); th.setName(threadName); return th; } }); }
From source file:io.pravega.client.state.impl.SynchronizerTest.java
@Test(timeout = 20000) public void testCompaction() throws EndOfSegmentException { String streamName = "streamName"; String scope = "scope"; MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory(); @Cleanup//from w w w . jav a 2 s. c o m MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory); StateSynchronizer<RevisionedImpl> sync = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build()); AtomicInteger callCount = new AtomicInteger(0); sync.initialize(new RegularUpdate()); sync.updateState(state -> { callCount.incrementAndGet(); return Collections.singletonList(new RegularUpdate()); }); assertEquals(1, callCount.get()); sync.updateState(state -> { callCount.incrementAndGet(); return Collections.singletonList(new RegularUpdate()); }); assertEquals(2, callCount.get()); sync.compact(state -> { callCount.incrementAndGet(); return new RegularUpdate(); }); assertEquals(3, callCount.get()); sync.updateState(s -> { callCount.incrementAndGet(); return Collections.singletonList(new RegularUpdate()); }); assertEquals(5, callCount.get()); sync.compact(state -> { callCount.incrementAndGet(); return new RegularUpdate(); }); assertEquals(6, callCount.get()); }
From source file:com.smoketurner.pipeline.application.core.MessageProcessor.java
/** * Stream an {@link S3Object} object and process each line with the * processor.//from ww w . j a va 2s. c o m * * @param object * S3Object to download and process * @return number of events processed * @throws IOException * if unable to stream the object */ private int streamObject(@Nonnull final S3Object object) throws IOException { final AtomicInteger eventCount = new AtomicInteger(0); try (S3ObjectInputStream input = object.getObjectContent()) { final BufferedReader reader; if (AmazonS3Downloader.isGZipped(object)) { reader = new BufferedReader( new InputStreamReader(new StreamingGZIPInputStream(input), StandardCharsets.UTF_8)); } else { reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); } // failed will be true if we did not successfully broadcast all // of the events because of no consumers final boolean failed = reader.lines().peek(event -> eventCount.incrementAndGet()) .anyMatch(broadcaster::test); if (failed) { // abort the current S3 download input.abort(); LOGGER.error("Partial events broadcast ({} sent) from key: {}/{}", eventCount.get(), object.getBucketName(), object.getKey()); throw new IOException("aborting download"); } } return eventCount.get(); }
From source file:org.apache.hadoop.gateway.rm.dispatch.RMHaDispatchTest.java
@Test public void testConnectivityFailover() throws Exception { String serviceName = "RESOURCEMANAGER"; HaDescriptor descriptor = HaDescriptorFactory.createDescriptor(); descriptor.addServiceConfig(//from w ww. j a v a 2 s . c o m HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null)); HaProvider provider = new DefaultHaProvider(descriptor); URI uri1 = new URI("http://passive-host"); URI uri2 = new URI("http://other-host"); URI uri3 = new URI("http://active-host"); ArrayList<String> urlList = new ArrayList<>(); urlList.add(uri1.toString()); urlList.add(uri2.toString()); urlList.add(uri3.toString()); provider.addHaService(serviceName, urlList); FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class); ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class); EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes(); EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)) .andReturn(provider).anyTimes(); BasicHttpResponse inboundResponse = EasyMock.createNiceMock(BasicHttpResponse.class); EasyMock.expect(inboundResponse.getStatusLine()).andReturn(getStatusLine()).anyTimes(); EasyMock.expect(inboundResponse.getEntity()).andReturn(getResponseEntity()).anyTimes(); EasyMock.expect(inboundResponse.getFirstHeader(LOCATION)).andReturn(getFirstHeader(uri3.toString())) .anyTimes(); BasicHttpParams params = new BasicHttpParams(); HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class); EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes(); EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes(); EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes(); HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once(); EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)) .once(); EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)) .once(); HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class); EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() { @Override public ServletOutputStream answer() throws Throwable { return new ServletOutputStream() { @Override public void write(int b) throws IOException { throw new IOException("unreachable-host"); } @Override public void setWriteListener(WriteListener arg0) { } @Override public boolean isReady() { return false; } }; } }).once(); Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName)); EasyMock.replay(filterConfig, servletContext, inboundResponse, outboundRequest, inboundRequest, outboundResponse); RMHaDispatch dispatch = new RMHaDispatch(); dispatch.setHttpClient(new DefaultHttpClient()); dispatch.setHaProvider(provider); dispatch.init(); long startTime = System.currentTimeMillis(); try { dispatch.setInboundResponse(inboundResponse); dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse); } catch (IOException e) { //this is expected after the failover limit is reached } Assert.assertEquals(uri3.toString(), dispatch.getUriFromInbound(inboundRequest, inboundResponse, null).toString()); long elapsedTime = System.currentTimeMillis() - startTime; Assert.assertEquals(uri3.toString(), provider.getActiveURL(serviceName)); //test to make sure the sleep took place Assert.assertTrue(elapsedTime > 1000); }
From source file:com.ict.dtube.broker.BrokerStartup.java
public static BrokerController main0(String[] args) { System.setProperty(RemotingCommand.RemotingVersionKey, Integer.toString(MQVersion.CurrentVersion)); // Socket???//www. j ava2s . c o m if (null == System.getProperty(NettySystemConfig.SystemPropertySocketSndbufSize)) { NettySystemConfig.SocketSndbufSize = 131072; } // Socket? if (null == System.getProperty(NettySystemConfig.SystemPropertySocketRcvbufSize)) { NettySystemConfig.SocketRcvbufSize = 131072; } try { // ? Options options = ServerUtil.buildCommandlineOptions(new Options()); commandLine = ServerUtil.parseCmdLine("mqbroker", args, buildCommandlineOptions(options), new PosixParser()); if (null == commandLine) { System.exit(-1); return null; } // ?? final BrokerConfig brokerConfig = new BrokerConfig(); final NettyServerConfig nettyServerConfig = new NettyServerConfig(); final NettyClientConfig nettyClientConfig = new NettyClientConfig(); nettyServerConfig.setListenPort(10911); final MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); // slave if (BrokerRole.SLAVE == messageStoreConfig.getBrokerRole()) { int ratio = messageStoreConfig.getAccessMessageInMemoryMaxRatio() - 10; messageStoreConfig.setAccessMessageInMemoryMaxRatio(ratio); } // ?? if (commandLine.hasOption('p')) { MixAll.printObjectProperties(null, brokerConfig); MixAll.printObjectProperties(null, nettyServerConfig); MixAll.printObjectProperties(null, nettyClientConfig); MixAll.printObjectProperties(null, messageStoreConfig); System.exit(0); } else if (commandLine.hasOption('m')) { MixAll.printObjectProperties(null, brokerConfig, true); MixAll.printObjectProperties(null, nettyServerConfig, true); MixAll.printObjectProperties(null, nettyClientConfig, true); MixAll.printObjectProperties(null, messageStoreConfig, true); System.exit(0); } // ? if (commandLine.hasOption('c')) { String file = commandLine.getOptionValue('c'); if (file != null) { configFile = file; InputStream in = new BufferedInputStream(new FileInputStream(file)); properties = new Properties(); properties.load(in); MixAll.properties2Object(properties, brokerConfig); MixAll.properties2Object(properties, nettyServerConfig); MixAll.properties2Object(properties, nettyClientConfig); MixAll.properties2Object(properties, messageStoreConfig); BrokerPathConfigHelper.setBrokerConfigPath(file); System.out.println("load config properties file OK, " + file); } } MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), brokerConfig); if (null == brokerConfig.getDtubeHome()) { System.out.println("Please set the " + MixAll.DTUBE_HOME_ENV + " variable in your environment to match the location of the Dtube installation"); System.exit(-2); } // Name Server?? IP:PORT String namesrvAddr = brokerConfig.getNamesrvAddr(); if (null != namesrvAddr) { try { String[] addrArray = namesrvAddr.split(";"); if (addrArray != null) { for (String addr : addrArray) { RemotingUtil.string2SocketAddress(addr); } } } catch (Exception e) { System.out.printf( "The Name Server Address[%s] illegal, please set it as follows, \"127.0.0.1:9876;192.168.0.1:9876\"\n", namesrvAddr); System.exit(-3); } } // BrokerId? switch (messageStoreConfig.getBrokerRole()) { case ASYNC_MASTER: case SYNC_MASTER: // Master Id0 brokerConfig.setBrokerId(MixAll.MASTER_ID); break; case SLAVE: if (brokerConfig.getBrokerId() <= 0) { System.out.println("Slave's brokerId must be > 0"); System.exit(-3); } break; default: break; } // Master?Slave???+1 messageStoreConfig.setHaListenPort(nettyServerConfig.getListenPort() + 1); // ?Logback LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure(brokerConfig.getDtubeHome() + "/conf/logback_broker.xml"); final Logger log = LoggerFactory.getLogger(LoggerName.BrokerLoggerName); // ??? MixAll.printObjectProperties(log, brokerConfig); MixAll.printObjectProperties(log, nettyServerConfig); MixAll.printObjectProperties(log, nettyClientConfig); MixAll.printObjectProperties(log, messageStoreConfig); // ?? final BrokerController controller = new BrokerController(// brokerConfig, // nettyServerConfig, // nettyClientConfig, // messageStoreConfig); boolean initResult = controller.initialize(); if (!initResult) { controller.shutdown(); System.exit(-3); } Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { private volatile boolean hasShutdown = false; private AtomicInteger shutdownTimes = new AtomicInteger(0); @Override public void run() { synchronized (this) { log.info("shutdown hook was invoked, " + this.shutdownTimes.incrementAndGet()); if (!this.hasShutdown) { this.hasShutdown = true; long begineTime = System.currentTimeMillis(); controller.shutdown(); long consumingTimeTotal = System.currentTimeMillis() - begineTime; log.info("shutdown hook over, consuming time total(ms): " + consumingTimeTotal); } } } }, "ShutdownHook")); // ?? controller.start(); String tip = "The broker[" + controller.getBrokerConfig().getBrokerName() + ", " + controller.getBrokerAddr() + "] boot success."; if (null != brokerConfig.getNamesrvAddr()) { tip += " and name server is " + brokerConfig.getNamesrvAddr(); } log.info(tip); System.out.println(tip); return controller; } catch (Throwable e) { e.printStackTrace(); System.exit(-1); } return null; }
From source file:com.olacabs.fabric.compute.sources.kafka.impl.KafkaReaderLeaderElector.java
private void peerCountChange(boolean force) { final String memberPath = memberPathPrefix(); List<String> members = null; try {/*from w w w. j a va 2 s . co m*/ members = curatorFramework.getChildren().forPath(memberPath); LOGGER.debug("Members: " + members); if (Sets.symmetricDifference(knownMembers, Sets.newHashSet(members)).isEmpty() && !force) { LOGGER.debug("No membership changes detected"); return; } //.intersection(knownMembers, Sets.newHashSet(members)) } catch (Exception e) { if (e instanceof KeeperException.NodeExistsException) { LOGGER.info("Looks like this topology/topic combination is being used for the first time"); } else { LOGGER.error("Error checking for node on ZK: ", e); } } if (null == members) { LOGGER.error("No members found .. how did i come here? ZK issue?"); return; } if (null == leaderSelector || !leaderSelector.hasLeadership()) { LOGGER.debug("I'm not the leader coordinator"); return; } knownMembers = Sets.newHashSet(members); final List<String> finalMembers = members; AtomicInteger counter = new AtomicInteger(0); readers.keySet().forEach(partition -> { String selectedReader = finalMembers.get(counter.getAndIncrement() % finalMembers.size()); LOGGER.info("[{}:{}:{}] Selected reader: {}", topology, topic, partition, selectedReader); final String communicatorPath = communicatorPath(partition); try { if (null == curatorFramework.checkExists().creatingParentContainersIfNeeded() .forPath(communicatorPath)) { curatorFramework.create().creatingParentContainersIfNeeded().forPath(communicatorPath); LOGGER.info("[{}:{}:{}] Created communicator", topology, topic, partition); } curatorFramework.setData().forPath(communicatorPath, selectedReader.getBytes()); LOGGER.error("Set reader at {} to {}", communicatorPath, selectedReader); } catch (Exception e) { LOGGER.error("Error setting reader value at {} to {}", communicatorPath, selectedReader, e); } }); }