List of usage examples for java.util.concurrent.atomic AtomicInteger compareAndSet
public final boolean compareAndSet(int expectedValue, int newValue)
From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java
@SuppressWarnings("checkstyle:fallthrough") private void deleteAllLedgers(DeleteLedgerCallback callback, Object ctx) { List<LedgerInfo> ledgers = Lists.newArrayList(ManagedLedgerImpl.this.ledgers.values()); AtomicInteger ledgersToDelete = new AtomicInteger(ledgers.size()); if (ledgers.isEmpty()) { // No ledgers to delete, proceed with deleting metadata deleteMetadata(callback, ctx);// ww w . j a v a 2 s. c o m return; } for (LedgerInfo ls : ledgers) { if (log.isDebugEnabled()) { log.debug("[{}] Deleting ledger {}", name, ls); } bookKeeper.asyncDeleteLedger(ls.getLedgerId(), (rc, ctx1) -> { switch (rc) { case BKException.Code.NoSuchLedgerExistsException: log.warn("[{}] Ledger {} not found when deleting it", name, ls.getLedgerId()); // Continue anyway case BKException.Code.OK: if (ledgersToDelete.decrementAndGet() == 0) { // All ledgers deleted, now remove ML metadata deleteMetadata(callback, ctx); } break; default: // Handle error log.warn("[{}] Failed to delete ledger {} -- {}", name, ls.getLedgerId(), BKException.getMessage(rc)); int toDelete = ledgersToDelete.get(); if (toDelete != -1 && ledgersToDelete.compareAndSet(toDelete, -1)) { // Trigger callback only once callback.deleteLedgerFailed(createManagedLedgerException(rc), ctx); } } }, null); } }
From source file:org.apache.bookkeeper.replication.AuditorLedgerCheckerTest.java
private void addEntry(int numEntriesToWrite, LedgerHandle lh) throws InterruptedException, BKException { final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite); final AtomicInteger rc = new AtomicInteger(BKException.Code.OK); for (int i = 0; i < numEntriesToWrite; i++) { ByteBuffer entry = ByteBuffer.allocate(4); entry.putInt(rng.nextInt(Integer.MAX_VALUE)); entry.position(0);/*from w ww. j a v a 2 s . c o m*/ lh.asyncAddEntry(entry.array(), new AddCallback() { public void addComplete(int rc2, LedgerHandle lh, long entryId, Object ctx) { rc.compareAndSet(BKException.Code.OK, rc2); completeLatch.countDown(); } }, null); } completeLatch.await(); if (rc.get() != BKException.Code.OK) { throw BKException.create(rc.get()); } }
From source file:org.apache.druid.indexing.kafka.supervisor.KafkaSupervisor.java
/** * This method does two things -//from w w w .j a v a 2 s . c o m * 1. Makes sure the checkpoints information in the taskGroup is consistent with that of the tasks, if not kill * inconsistent tasks. * 2. truncates the checkpoints in the taskGroup corresponding to which segments have been published, so that any newly * created tasks for the taskGroup start indexing from after the latest published offsets. */ private void verifyAndMergeCheckpoints(final TaskGroup taskGroup) { final int groupId = taskGroup.groupId; final List<Pair<String, TreeMap<Integer, Map<Integer, Long>>>> taskSequences = new ArrayList<>(); final List<ListenableFuture<TreeMap<Integer, Map<Integer, Long>>>> futures = new ArrayList<>(); final List<String> taskIds = new ArrayList<>(); for (String taskId : taskGroup.taskIds()) { final ListenableFuture<TreeMap<Integer, Map<Integer, Long>>> checkpointsFuture = taskClient .getCheckpointsAsync(taskId, true); taskIds.add(taskId); futures.add(checkpointsFuture); } try { List<TreeMap<Integer, Map<Integer, Long>>> futuresResult = Futures.successfulAsList(futures) .get(futureTimeoutInSeconds, TimeUnit.SECONDS); for (int i = 0; i < futuresResult.size(); i++) { final TreeMap<Integer, Map<Integer, Long>> checkpoints = futuresResult.get(i); final String taskId = taskIds.get(i); if (checkpoints == null) { try { // catch the exception in failed futures futures.get(i).get(); } catch (Exception e) { log.error(e, "Problem while getting checkpoints for task [%s], killing the task", taskId); killTask(taskId); taskGroup.tasks.remove(taskId); } } else if (checkpoints.isEmpty()) { log.warn("Ignoring task [%s], as probably it is not started running yet", taskId); } else { taskSequences.add(new Pair<>(taskId, checkpoints)); } } } catch (Exception e) { throw new RuntimeException(e); } final KafkaDataSourceMetadata latestDataSourceMetadata = (KafkaDataSourceMetadata) indexerMetadataStorageCoordinator .getDataSourceMetadata(dataSource); final boolean hasValidOffsetsFromDb = latestDataSourceMetadata != null && latestDataSourceMetadata.getKafkaPartitions() != null && ioConfig.getTopic().equals(latestDataSourceMetadata.getKafkaPartitions().getTopic()); final Map<Integer, Long> latestOffsetsFromDb; if (hasValidOffsetsFromDb) { latestOffsetsFromDb = latestDataSourceMetadata.getKafkaPartitions().getPartitionOffsetMap(); } else { latestOffsetsFromDb = null; } // order tasks of this taskGroup by the latest sequenceId taskSequences.sort((o1, o2) -> o2.rhs.firstKey().compareTo(o1.rhs.firstKey())); final Set<String> tasksToKill = new HashSet<>(); final AtomicInteger earliestConsistentSequenceId = new AtomicInteger(-1); int taskIndex = 0; while (taskIndex < taskSequences.size()) { TreeMap<Integer, Map<Integer, Long>> taskCheckpoints = taskSequences.get(taskIndex).rhs; String taskId = taskSequences.get(taskIndex).lhs; if (earliestConsistentSequenceId.get() == -1) { // find the first replica task with earliest sequenceId consistent with datasource metadata in the metadata // store if (taskCheckpoints.entrySet().stream() .anyMatch(sequenceCheckpoint -> sequenceCheckpoint.getValue().entrySet().stream() .allMatch(partitionOffset -> Longs.compare(partitionOffset.getValue(), latestOffsetsFromDb == null ? partitionOffset.getValue() : latestOffsetsFromDb.getOrDefault(partitionOffset.getKey(), partitionOffset.getValue())) == 0) && earliestConsistentSequenceId.compareAndSet(-1, sequenceCheckpoint.getKey())) || (pendingCompletionTaskGroups.getOrDefault(groupId, EMPTY_LIST).size() > 0 && earliestConsistentSequenceId.compareAndSet(-1, taskCheckpoints.firstKey()))) { final SortedMap<Integer, Map<Integer, Long>> latestCheckpoints = new TreeMap<>( taskCheckpoints.tailMap(earliestConsistentSequenceId.get())); log.info("Setting taskGroup sequences to [%s] for group [%d]", latestCheckpoints, groupId); taskGroup.sequenceOffsets.clear(); taskGroup.sequenceOffsets.putAll(latestCheckpoints); } else { log.debug("Adding task [%s] to kill list, checkpoints[%s], latestoffsets from DB [%s]", taskId, taskCheckpoints, latestOffsetsFromDb); tasksToKill.add(taskId); } } else { // check consistency with taskGroup sequences if (taskCheckpoints.get(taskGroup.sequenceOffsets.firstKey()) == null || !(taskCheckpoints.get(taskGroup.sequenceOffsets.firstKey()) .equals(taskGroup.sequenceOffsets.firstEntry().getValue())) || taskCheckpoints.tailMap(taskGroup.sequenceOffsets.firstKey()) .size() != taskGroup.sequenceOffsets.size()) { log.debug("Adding task [%s] to kill list, checkpoints[%s], taskgroup checkpoints [%s]", taskId, taskCheckpoints, taskGroup.sequenceOffsets); tasksToKill.add(taskId); } } taskIndex++; } if ((tasksToKill.size() > 0 && tasksToKill.size() == taskGroup.tasks.size()) || (taskGroup.tasks.size() == 0 && pendingCompletionTaskGroups.getOrDefault(groupId, EMPTY_LIST).size() == 0)) { // killing all tasks or no task left in the group ? // clear state about the taskgroup so that get latest offset information is fetched from metadata store log.warn("Clearing task group [%d] information as no valid tasks left the group", groupId); taskGroups.remove(groupId); partitionGroups.get(groupId).replaceAll((partition, offset) -> NOT_SET); } taskSequences.stream().filter(taskIdSequences -> tasksToKill.contains(taskIdSequences.lhs)) .forEach(sequenceCheckpoint -> { log.warn( "Killing task [%s], as its checkpoints [%s] are not consistent with group checkpoints[%s] or latest " + "persisted offsets in metadata store [%s]", sequenceCheckpoint.lhs, sequenceCheckpoint.rhs, taskGroup.sequenceOffsets, latestOffsetsFromDb); killTask(sequenceCheckpoint.lhs); taskGroup.tasks.remove(sequenceCheckpoint.lhs); }); }
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);//ww w. j av a 2s. 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:org.apache.hadoop.mapred.LearningScheduler.java
private double[] getExepctedUtility(TaskTrackerStatus ttstatus, JobInProgress job, boolean isMap, NodeEnvironment env) {// w w w . j a va2s. c om double ret[] = new double[3]; JobStatistics jobstat = getJobStatistics(job, isMap); int utility = utilFunc.getUtility(this, job, isMap); // check if this job's tasks are not being assigned for some time // if thats the case, forcefull set successDist = 1 double successDist = 0; AtomicInteger asgn = assignments.get(job); if (asgn != null) { if (asgn.compareAndSet(MAX_ASGN_IGNORE_LIMIT, 0)) { successDist = 1; } } else { successDist = classifier.getSuccessDistance(jobstat, env); } LOG.debug(getJobName(job) + (isMap ? "_map" : "_reduce") + " Utility = " + utility + " Likelihood: " + successDist); ret[0] = successDist * utility; ret[1] = utility; ret[2] = successDist; return ret; }
From source file:org.apache.synapse.transport.fix.FIXIncomingMessageHandler.java
/** * This callback receives messages for the application. This is one of the * core entry points for the FIX application. Every application level * request will come through here. A new thread will be spawned from the * thread pool for each incoming message. * * @param message QuickFIX message/*from w w w .j a v a2 s .c o m*/ * @param sessionID QuickFIX session ID * @throws FieldNotFound * @throws IncorrectDataFormat * @throws IncorrectTagValue * @throws UnsupportedMessageType */ public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { if (log.isDebugEnabled()) { StringBuffer sb = new StringBuffer(); sb.append("Received FIX message from ") .append(message.getHeader().getField(new SenderCompID()).getValue()); sb.append("\nMessage Sequence Number: ") .append(message.getHeader().getField(new MsgSeqNum()).getValue()); sb.append("\nReceiver ID: ").append(message.getHeader().getField(new TargetCompID()).getValue()); log.debug(sb.toString()); if (log.isTraceEnabled()) { log.trace("Message: " + message.toString()); } } AtomicInteger atomicCounter = countersMap.get(sessionID); int counter = atomicCounter.incrementAndGet(); boolean rolled = atomicCounter.compareAndSet(FIXConstants.DEFAULT_COUNTER_UPPER_LIMIT, 0); if (rolled && log.isDebugEnabled()) { log.debug("Incoming request counter rolled over for the session: " + sessionID); } workerPool.execute(new FIXWorkerThread(message, sessionID, counter)); }
From source file:org.jenkinsci.remoting.protocol.ProtocolStackTest.java
@Test public void initSequence() throws IOException { Logger logger = Logger.getLogger(ProtocolStack.class.getName()); CapturingHandler handler = new CapturingHandler(); assertThat(logger.isLoggable(Level.FINEST), is(false)); Level oldLevel = logger.getLevel(); logger.addHandler(handler);//from w ww.j ava2 s . co m try { logger.setLevel(Level.FINEST); assertThat(logger.isLoggable(Level.FINEST), is(true)); final AtomicInteger state = new AtomicInteger(); ProtocolStack.on(new NetworkLayer(selector) { @Override protected void write(@NonNull ByteBuffer data) throws IOException { } @Override public void start() throws IOException { state.compareAndSet(0, 1); } @Override public void doCloseSend() throws IOException { } @Override public void doCloseRecv() { } @Override public boolean isSendOpen() { return true; } }).filter(new FilterLayer() { @Override public void start() throws IOException { state.compareAndSet(1, 2); } @Override public void onRecv(@NonNull ByteBuffer data) throws IOException { } @Override public void doSend(@NonNull ByteBuffer data) throws IOException { } }).filter(new FilterLayer() { @Override public void start() throws IOException { state.compareAndSet(2, 3); } @Override public void onRecv(@NonNull ByteBuffer data) throws IOException { } @Override public void doSend(@NonNull ByteBuffer data) throws IOException { } }).named("initSeq").build(new ApplicationLayer<Void>() { @Override public Void get() { return null; } @Override public void onRead(@NonNull ByteBuffer data) throws IOException { } @Override public void start() throws IOException { state.compareAndSet(3, 4); } @Override public void onReadClosed(IOException cause) throws IOException { } @Override public boolean isReadOpen() { return true; } }); assertThat("Init in sequence", state.get(), is(4)); assertThat(handler.logRecords, contains( allOf(hasProperty("message", is("[{0}] Initializing")), hasProperty("parameters", is(new Object[] { "initSeq" }))), allOf(hasProperty("message", is("[{0}] Starting")), hasProperty("parameters", is(new Object[] { "initSeq" }))), allOf(hasProperty("message", is("[{0}] Started")), hasProperty("parameters", is(new Object[] { "initSeq" }))))); } finally { logger.removeHandler(handler); logger.setLevel(oldLevel); } }
From source file:org.jenkinsci.remoting.protocol.ProtocolStackTest.java
@Test public void initSequenceFailure() throws IOException { Logger logger = Logger.getLogger(ProtocolStack.class.getName()); CapturingHandler handler = new CapturingHandler(); assertThat(logger.isLoggable(Level.FINEST), is(false)); Level oldLevel = logger.getLevel(); logger.addHandler(handler);//w w w. j av a 2s .co m try { logger.setLevel(Level.FINEST); assertThat(logger.isLoggable(Level.FINEST), is(true)); final AtomicInteger state = new AtomicInteger(); try { ProtocolStack.on(new NetworkLayer(selector) { @Override protected void write(@NonNull ByteBuffer data) throws IOException { } @Override public void start() throws IOException { state.compareAndSet(0, 1); } @Override public void doCloseSend() throws IOException { } @Override public void doCloseRecv() { } @Override public boolean isSendOpen() { return true; } }).filter(new FilterLayer() { @Override public void start() throws IOException { state.compareAndSet(1, 2); throw new IOException("boom"); } @Override public void onRecv(@NonNull ByteBuffer data) throws IOException { } @Override public void doSend(@NonNull ByteBuffer data) throws IOException { } }).filter(new FilterLayer() { @Override public void start() throws IOException { state.set(-2); } @Override public void onRecv(@NonNull ByteBuffer data) throws IOException { } @Override public void doSend(@NonNull ByteBuffer data) throws IOException { } @Override public void onRecvClosed(IOException cause) throws IOException { state.compareAndSet(2, 3); super.onRecvClosed(cause); } }).named("initSeq").build(new ApplicationLayer<Void>() { @Override public Void get() { return null; } @Override public void onRead(@NonNull ByteBuffer data) throws IOException { } @Override public void start() throws IOException { state.set(-3); } @Override public void onReadClosed(IOException cause) throws IOException { state.compareAndSet(3, 4); } @Override public boolean isReadOpen() { return true; } }); fail("Expecting IOException"); } catch (IOException e) { assertThat(e.getMessage(), is("boom")); } assertThat(handler.logRecords, contains( allOf(hasProperty("message", is("[{0}] Initializing")), hasProperty("parameters", is(new Object[] { "initSeq" }))), allOf(hasProperty("message", is("[{0}] Starting")), hasProperty("parameters", is(new Object[] { "initSeq" }))), allOf(hasProperty("message", is("[{0}] Start failure")), hasProperty("parameters", is(new Object[] { "initSeq" })), hasProperty("thrown", hasProperty("message", is("boom")))))); assertThat("Init in sequence", state.get(), is(4)); } finally { logger.removeHandler(handler); logger.setLevel(oldLevel); } }
From source file:org.jenkinsci.remoting.protocol.ProtocolStackTest.java
@Test public void stackCloseSequence() throws IOException { Logger logger = Logger.getLogger(ProtocolStack.class.getName()); CapturingHandler handler = new CapturingHandler(); assertThat(logger.isLoggable(Level.FINEST), is(false)); Level oldLevel = logger.getLevel(); logger.addHandler(handler);// ww w . j a v a2s .c o m try { logger.setLevel(Level.FINEST); assertThat(logger.isLoggable(Level.FINEST), is(true)); final AtomicInteger state = new AtomicInteger(); ProtocolStack.on(new NetworkLayer(selector) { @Override public void start() throws IOException { } @Override protected void write(@NonNull ByteBuffer data) throws IOException { } @Override public void doCloseRecv() { state.compareAndSet(3, 4); onRecvClosed(); } @Override public void doCloseSend() throws IOException { state.compareAndSet(2, 3); doCloseRecv(); } @Override public boolean isSendOpen() { return true; } }).filter(new FilterLayer() { @Override public void start() throws IOException { } @Override public void onRecv(@NonNull ByteBuffer data) throws IOException { } @Override public void doSend(@NonNull ByteBuffer data) throws IOException { } @Override public void doCloseSend() throws IOException { state.compareAndSet(1, 2); super.doCloseSend(); } @Override public void onRecvClosed(IOException cause) throws IOException { state.compareAndSet(4, 5); super.onRecvClosed(cause); } }).filter(new FilterLayer() { @Override public void start() throws IOException { } @Override public void onRecv(@NonNull ByteBuffer data) throws IOException { } @Override public void doSend(@NonNull ByteBuffer data) throws IOException { } @Override public void doCloseSend() throws IOException { state.compareAndSet(0, 1); super.doCloseSend(); } @Override public void onRecvClosed(IOException cause) throws IOException { state.compareAndSet(5, 6); super.onRecvClosed(cause); } }).named("closeSeq").build(new ApplicationLayer<Void>() { @Override public boolean isReadOpen() { return true; } @Override public void onRead(@NonNull ByteBuffer data) throws IOException { } @Override public Void get() { return null; } @Override public void start() throws IOException { } @Override public void onReadClosed(IOException cause) throws IOException { state.compareAndSet(6, 7); } }).close(); assertThat("Close in sequence", state.get(), is(7)); assertThat(handler.logRecords, contains( allOf(hasProperty("message", is("[{0}] Initializing")), hasProperty("parameters", is(new Object[] { "closeSeq" }))), allOf(hasProperty("message", is("[{0}] Starting")), hasProperty("parameters", is(new Object[] { "closeSeq" }))), allOf(hasProperty("message", is("[{0}] Started")), hasProperty("parameters", is(new Object[] { "closeSeq" }))), allOf(hasProperty("message", is("[{0}] Closing")), hasProperty("parameters", is(new Object[] { "closeSeq" }))), allOf(hasProperty("message", is("[{0}] Closed")), hasProperty("parameters", is(new Object[] { "closeSeq" }))))); } finally { logger.removeHandler(handler); logger.setLevel(oldLevel); } }