Java Code Examples for java.util.concurrent.atomic.AtomicLong.addAndGet()

The following are Jave code examples for showing how to use addAndGet() of the java.util.concurrent.atomic.AtomicLong class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: circus-train   File: S3S3Copier.java   View Source Code Vote up 6 votes
private Metrics gatherAllCopyResults() {
  AtomicLong bytesReplicated = new AtomicLong(0);
  registerRunningMetrics(bytesReplicated);
  for (Copy copyJob : copyJobs) {
    try {
      copyJob.waitForCompletion();
      long alreadyReplicated = bytesReplicated.addAndGet(copyJob.getProgress().getTotalBytesToTransfer());
      if (totalBytesToReplicate > 0) {
        LOG.info("Replicating...': {}% complete",
            String.format("%.0f", (alreadyReplicated / (double) totalBytesToReplicate) * 100.0));
      }
    } catch (InterruptedException e) {
      throw new CircusTrainException(e);
    }
  }
  ImmutableMap<String, Long> metrics = ImmutableMap.of(S3S3CopierMetrics.Metrics.TOTAL_BYTES_TO_REPLICATE.name(),
      totalBytesToReplicate);
  return new S3S3CopierMetrics(metrics, bytesReplicated.get());
}
 
Example 2
Project: aliyun-tablestore-hbase-client   File: OTSAdapter.java   View Source Code Vote up 6 votes
private void doPut(String tableName, final List<com.alicloud.tablestore.adaptor.struct.OPut> puts) throws IOException {
  ArrayList<com.alicloud.tablestore.adaptor.struct.OPut> tableWriteBuffer = getTableWriteBuffer(tableName);
  AtomicLong currentBufferSize = getTableCurrentBufferSize(tableName);
  boolean autoFlush = getAutoFlush(tableName);
  long writeBufferSize = getWriteBufferSize(tableName);

  List<com.alicloud.tablestore.adaptor.struct.OPut> flushPuts = null;
  synchronized (tableWriteBuffer) {
    for (com.alicloud.tablestore.adaptor.struct.OPut put : puts) {
      tableWriteBuffer.add(put);
      currentBufferSize.addAndGet(put.getWritableSize());
    }
    if (autoFlush || currentBufferSize.get() > writeBufferSize) {
      flushPuts = new ArrayList<com.alicloud.tablestore.adaptor.struct.OPut>(tableWriteBuffer);
      tableWriteBuffer.clear();
      currentBufferSize.set(0);
    }
  }
  if (flushPuts != null && !flushPuts.isEmpty()) {
    doCommits(tableName, flushPuts);
  }
}
 
Example 3
Project: aliyun-tablestore-hbase-client   File: OTSAdapter.java   View Source Code Vote up 6 votes
private void doCommits(String tableName, final List<com.alicloud.tablestore.adaptor.struct.OPut> puts) throws IOException {
  boolean flushSuccessfully = false;
  try {
    otsProxy.putMultiple(tableName, puts);
    flushSuccessfully = true;
  } finally {
    if (!flushSuccessfully && !getClearBufferOnFail(tableName)) {
      ArrayList<com.alicloud.tablestore.adaptor.struct.OPut> tableWriteBuffer = getTableWriteBuffer(tableName);
      synchronized (tableWriteBuffer) {
        AtomicLong currentBufferSize = getTableCurrentBufferSize(tableName);
        for (com.alicloud.tablestore.adaptor.struct.OPut put : puts) {
          tableWriteBuffer.add(put);
          currentBufferSize.addAndGet(put.getWritableSize());
        }
      }
    }
  }
}
 
Example 4
Project: angel   File: TaskContext.java   View Source Code Vote up 5 votes
/**
 * Increment the counter
 * @param counterName counter name
 * @param updateValue increment value
 */
public void updateCounter(String counterName, int updateValue) {
  AtomicLong counter = metrics.get(counterName);
  if(counter == null) {
    counter = metrics.putIfAbsent(counterName, new AtomicLong(0));
    if(counter == null) {
      counter = metrics.get(counterName);
    }
  }
  counter.addAndGet(updateValue);
}
 
Example 5
Project: sstore-soft   File: MapReduceLoader.java   View Source Code Vote up 5 votes
/**
 * Load the tuples for the given table name
 * @param tableName
 */
protected void generateTableData(String tableName) {
    LOG.debug("Starting data generator for '" + tableName + "'");
    final AbstractTableGenerator generator = this.generators.get(tableName);
    assert(generator != null);
    long tableSize = generator.getTableSize();
    long batchSize = generator.getBatchSize();
    final AtomicLong table_ctr = this.table_sizes.get(tableName); 
    VoltTable table = generator.getVoltTable();
    
    LOG.info("Loading " + tableSize + " tuples for table '" + tableName + "'");
    while (generator.hasMore()) {
        generator.addRow();
        if (table.getRowCount() >= batchSize) {
            if (table_ctr.get() % 100000 == 0) LOG.info(String.format(tableName + ": loading %d rows (id %d of %d)", table.getRowCount(), generator.getCount(), tableSize));
            loadVoltTable(tableName, table);
            table_ctr.addAndGet(table.getRowCount());
            table.clearRowData();
        }
    } // WHILE
    if (table.getRowCount() > 0) {
        LOG.info(tableName + ": loading final " + table.getRowCount() + " rows.");
        loadVoltTable(tableName, table);
        this.table_sizes.get(tableName).addAndGet(table.getRowCount());
        table.clearRowData();
    }
    LOG.info(tableName + ": Inserted " + this.table_sizes.get(tableName) + " tuples");
}
 
Example 6
Project: elasticsearch_my   File: DeleteByQueryConcurrentTests.java   View Source Code Vote up 5 votes
public void testConcurrentDeleteByQueriesOnSameDocs() throws Throwable {
    final long docs = randomIntBetween(50, 100);

    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < docs; i++) {
        builders.add(client().prepareIndex("test", "doc", String.valueOf(i)).setSource("foo", "bar"));
    }
    indexRandom(true, true, true, builders);

    final Thread[] threads =  new Thread[scaledRandomIntBetween(2, 9)];

    final CountDownLatch start = new CountDownLatch(1);
    final MatchQueryBuilder query = matchQuery("foo", "bar");
    final AtomicLong deleted = new AtomicLong(0);

    for (int t = 0; t < threads.length; t++) {
        Runnable r = () -> {
            try {
                start.await();

                BulkByScrollResponse response = deleteByQuery().source("test").filter(query).refresh(true).get();
                // Some deletions might fail due to version conflict, but
                // what matters here is the total of successful deletions
                deleted.addAndGet(response.getDeleted());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        };
        threads[t] = new Thread(r);
        threads[t].start();
    }

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

    assertHitCount(client().prepareSearch("test").setSize(0).get(), 0L);
    assertThat(deleted.get(), equalTo(docs));
}
 
Example 7
Project: elasticsearch_my   File: PrioritizedRunnableTests.java   View Source Code Vote up 5 votes
public void testGetAgeInMillis() throws Exception {
    AtomicLong time = new AtomicLong();

    PrioritizedRunnable runnable = new PrioritizedRunnable(Priority.NORMAL, time::get) {
        @Override
        public void run() {
        }
    };
    assertEquals(0, runnable.getAgeInMillis());
    int milliseconds = randomIntBetween(1, 256);
    time.addAndGet(TimeUnit.NANOSECONDS.convert(milliseconds, TimeUnit.MILLISECONDS));
    assertEquals(milliseconds, runnable.getAgeInMillis());
}
 
Example 8
Project: datarouter   File: AtomicCounter.java   View Source Code Vote up 5 votes
public void merge(CountCollector other){
	for(Map.Entry<String,AtomicLong> otherEntry : MapTool.nullSafe(other.getCountByKey()).entrySet()){
		AtomicLong existingValue = countByKey.get(otherEntry.getKey());
		if(existingValue != null){
			existingValue.addAndGet(otherEntry.getValue().longValue());
		}else{
			countByKey.put(otherEntry.getKey(), new AtomicLong(otherEntry.getValue().longValue()));
		}
	}
}
 
Example 9
Project: s-store   File: MapReduceLoader.java   View Source Code Vote up 5 votes
/**
 * Load the tuples for the given table name
 * @param tableName
 */
protected void generateTableData(String tableName) {
    LOG.debug("Starting data generator for '" + tableName + "'");
    final AbstractTableGenerator generator = this.generators.get(tableName);
    assert(generator != null);
    long tableSize = generator.getTableSize();
    long batchSize = generator.getBatchSize();
    final AtomicLong table_ctr = this.table_sizes.get(tableName); 
    VoltTable table = generator.getVoltTable();
    
    LOG.info("Loading " + tableSize + " tuples for table '" + tableName + "'");
    while (generator.hasMore()) {
        generator.addRow();
        if (table.getRowCount() >= batchSize) {
            if (table_ctr.get() % 100000 == 0) LOG.info(String.format(tableName + ": loading %d rows (id %d of %d)", table.getRowCount(), generator.getCount(), tableSize));
            loadVoltTable(tableName, table);
            table_ctr.addAndGet(table.getRowCount());
            table.clearRowData();
        }
    } // WHILE
    if (table.getRowCount() > 0) {
        LOG.info(tableName + ": loading final " + table.getRowCount() + " rows.");
        loadVoltTable(tableName, table);
        this.table_sizes.get(tableName).addAndGet(table.getRowCount());
        table.clearRowData();
    }
    LOG.info(tableName + ": Inserted " + this.table_sizes.get(tableName) + " tuples");
}
 
Example 10
Project: boohee_v5.6   File: OperatorObserveOn.java   View Source Code Vote up 5 votes
void pollQueue() {
    int emitted = 0;
    AtomicLong localRequested = this.requested;
    AtomicLong localCounter = this.counter;
    do {
        localCounter.set(1);
        long produced = 0;
        long r = localRequested.get();
        while (!this.child.isUnsubscribed()) {
            if (this.finished) {
                Throwable error = this.error;
                if (error != null) {
                    this.queue.clear();
                    this.child.onError(error);
                    return;
                } else if (this.queue.isEmpty()) {
                    this.child.onCompleted();
                    return;
                }
            }
            if (r > 0) {
                Object o = this.queue.poll();
                if (o != null) {
                    this.child.onNext(this.on.getValue(o));
                    r--;
                    emitted++;
                    produced++;
                }
            }
            if (produced > 0 && localRequested.get() != Long.MAX_VALUE) {
                localRequested.addAndGet(-produced);
            }
        }
        return;
    } while (localCounter.decrementAndGet() > 0);
    if (emitted > 0) {
        request((long) emitted);
    }
}
 
Example 11
Project: ditb   File: ServerSideScanMetrics.java   View Source Code Vote up 5 votes
/**
 * @param counterName
 * @param delta
 */
public void addToCounter(String counterName, long delta) {
  AtomicLong c = this.counters.get(counterName);
  if (c != null) {
    c.addAndGet(delta);
  }
}
 
Example 12
Project: RxJava3-preview   File: QueueDrainHelper.java   View Source Code Vote up 4 votes
/**
     * Drains the queue based on the outstanding requests in post-completed mode (only!).
     *
     * @param n the current request amount
     * @param actual the target Subscriber to send events to
     * @param queue the queue to drain if in the post-complete state
     * @param state holds the request amount and the post-completed flag
     * @param isCancelled a supplier that returns true if the drain has been cancelled
     * @return true if the queue was completely drained or the drain process was cancelled
     */
    static <T> boolean postCompleteDrain(long n,
                                         Subscriber<? super T> actual,
                                         Queue<T> queue,
                                         AtomicLong state,
                                         BooleanSupplier isCancelled) {

// TODO enable fast-path
//        if (n == -1 || n == Long.MAX_VALUE) {
//            for (;;) {
//                if (isCancelled.getAsBoolean()) {
//                    break;
//                }
//
//                T v = queue.poll();
//
//                if (v == null) {
//                    actual.onComplete();
//                    break;
//                }
//
//                actual.onNext(v);
//            }
//
//            return true;
//        }

        long e = n & COMPLETED_MASK;

        for (; ; ) {

            while (e != n) {
                if (isCancelled(isCancelled)) {
                    return true;
                }

                T t = queue.poll();

                if (t == null) {
                    actual.onComplete();
                    return true;
                }

                actual.onNext(t);
                e++;
            }

            if (isCancelled(isCancelled)) {
                return true;
            }

            if (queue.isEmpty()) {
                actual.onComplete();
                return true;
            }

            n = state.get();

            if (n == e) {

                n = state.addAndGet(-(e & REQUESTED_MASK));

                if ((n & REQUESTED_MASK) == 0L) {
                    return false;
                }

                e = n & COMPLETED_MASK;
            }
        }

    }
 
Example 13
Project: boohee_v5.6   File: OperatorScan.java   View Source Code Vote up 4 votes
void emitLoop() {
    Subscriber<? super R> child = this.child;
    Queue<Object> queue = this.queue;
    NotificationLite<R> nl = NotificationLite.instance();
    AtomicLong requested = this.requested;
    long r = requested.get();
    while (true) {
        boolean max = r == Long.MAX_VALUE;
        if (!checkTerminated(this.done, queue.isEmpty(), child)) {
            long e = 0;
            while (r != 0) {
                boolean d = this.done;
                Object o = queue.poll();
                boolean empty = o == null;
                if (!checkTerminated(d, empty, child)) {
                    if (empty) {
                        break;
                    }
                    R v = nl.getValue(o);
                    try {
                        child.onNext(v);
                        r--;
                        e--;
                    } catch (Throwable ex) {
                        Exceptions.throwIfFatal(ex);
                        child.onError(OnErrorThrowable.addValueAsLastCause(ex, v));
                        return;
                    }
                }
                return;
            }
            if (!(e == 0 || max)) {
                r = requested.addAndGet(e);
            }
            synchronized (this) {
                if (this.missed) {
                    this.missed = false;
                } else {
                    this.emitting = false;
                    return;
                }
            }
        }
        return;
    }
}
 
Example 14
Project: hadoop   File: InstrumentationService.java   View Source Code Vote up 4 votes
@Override
public void incr(String group, String name, long count) {
  AtomicLong counter = getToAdd(group, name, AtomicLong.class, counterLock, counters);
  counter.addAndGet(count);
}
 
Example 15
Project: ditb   File: BucketAllocator.java   View Source Code Vote up 4 votes
/**
 * Rebuild the allocator's data structures from a persisted map.
 * @param availableSpace capacity of cache
 * @param map A map stores the block key and BucketEntry(block's meta data
 *          like offset, length)
 * @param realCacheSize cached data size statistics for bucket cache
 * @throws BucketAllocatorException
 */
BucketAllocator(long availableSpace, int[] bucketSizes, Map<BlockCacheKey, BucketEntry> map,
    AtomicLong realCacheSize) throws BucketAllocatorException {
  this(availableSpace, bucketSizes);

  // each bucket has an offset, sizeindex. probably the buckets are too big
  // in our default state. so what we do is reconfigure them according to what
  // we've found. we can only reconfigure each bucket once; if more than once,
  // we know there's a bug, so we just log the info, throw, and start again...
  boolean[] reconfigured = new boolean[buckets.length];
  for (Map.Entry<BlockCacheKey, BucketEntry> entry : map.entrySet()) {
    long foundOffset = entry.getValue().offset();
    int foundLen = entry.getValue().getLength();
    int bucketSizeIndex = -1;
    for (int i = 0; i < bucketSizes.length; ++i) {
      if (foundLen <= bucketSizes[i]) {
        bucketSizeIndex = i;
        break;
      }
    }
    if (bucketSizeIndex == -1) {
      throw new BucketAllocatorException(
          "Can't match bucket size for the block with size " + foundLen);
    }
    int bucketNo = (int) (foundOffset / bucketCapacity);
    if (bucketNo < 0 || bucketNo >= buckets.length)
      throw new BucketAllocatorException("Can't find bucket " + bucketNo
          + ", total buckets=" + buckets.length
          + "; did you shrink the cache?");
    Bucket b = buckets[bucketNo];
    if (reconfigured[bucketNo]) {
      if (b.sizeIndex() != bucketSizeIndex)
        throw new BucketAllocatorException(
            "Inconsistent allocation in bucket map;");
    } else {
      if (!b.isCompletelyFree())
        throw new BucketAllocatorException("Reconfiguring bucket "
            + bucketNo + " but it's already allocated; corrupt data");
      // Need to remove the bucket from whichever list it's currently in at
      // the moment...
      BucketSizeInfo bsi = bucketSizeInfos[bucketSizeIndex];
      BucketSizeInfo oldbsi = bucketSizeInfos[b.sizeIndex()];
      oldbsi.removeBucket(b);
      bsi.instantiateBucket(b);
      reconfigured[bucketNo] = true;
    }
    realCacheSize.addAndGet(foundLen);
    buckets[bucketNo].addAllocation(foundOffset);
    usedSize += buckets[bucketNo].getItemAllocationSize();
    bucketSizeInfos[bucketSizeIndex].blockAllocated(b);
  }
}
 
Example 16
Project: ditb   File: TestWALProcedureStore.java   View Source Code Vote up 4 votes
@Test
public void testInsertUpdateDelete() throws Exception {
  final int NTHREAD = 2;

  procStore.stop(false);
  fs.delete(logDir, true);

  org.apache.hadoop.conf.Configuration conf =
    new org.apache.hadoop.conf.Configuration(htu.getConfiguration());
  conf.setBoolean("hbase.procedure.store.wal.use.hsync", false);
  conf.setInt("hbase.procedure.store.wal.periodic.roll.msec", 10000);
  conf.setInt("hbase.procedure.store.wal.roll.threshold", 128 * 1024);

  fs.mkdirs(logDir);
  procStore = ProcedureTestingUtility.createWalStore(conf, fs, logDir);
  procStore.start(NTHREAD);
  procStore.recoverLease();

  LoadCounter loader = new LoadCounter();
  procStore.load(loader);
  assertEquals(0, loader.getMaxProcId());
  assertEquals(0, loader.getLoadedCount());
  assertEquals(0, loader.getCorruptedCount());

  final long LAST_PROC_ID = 9999;
  final Thread[] thread = new Thread[NTHREAD];
  final AtomicLong procCounter = new AtomicLong((long)Math.round(Math.random() * 100));
  for (int i = 0; i < thread.length; ++i) {
    thread[i] = new Thread() {
      @Override
      public void run() {
        Random rand = new Random();
        TestProcedure proc;
        do {
          proc = new TestProcedure(procCounter.addAndGet(1));
          // Insert
          procStore.insert(proc, null);
          // Update
          for (int i = 0, nupdates = rand.nextInt(10); i <= nupdates; ++i) {
            try { Thread.sleep(0, rand.nextInt(15)); } catch (InterruptedException e) {}
            procStore.update(proc);
          }
          // Delete
          procStore.delete(proc.getProcId());
        } while (proc.getProcId() < LAST_PROC_ID);
      }
    };
    thread[i].start();
  }

  for (int i = 0; i < thread.length; ++i) {
    thread[i].join();
  }

  procStore.getStoreTracker().dump();
  assertTrue(procCounter.get() >= LAST_PROC_ID);
  assertTrue(procStore.getStoreTracker().isEmpty());
  assertEquals(1, procStore.getActiveLogs().size());
}