下面列出了java.util.concurrent.atomic.AtomicLong#set() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public double learnSequence(Sequence<T> sequence, AtomicLong nextRandom, double learningRate,
BatchSequences<T> batchSequences) {
Sequence<T> tempSequence = sequence;
if (sampling > 0)
tempSequence = applySubsampling(sequence, nextRandom);
int currentWindow = window;
if (variableWindows != null && variableWindows.length != 0) {
currentWindow = variableWindows[RandomUtils.nextInt(0, variableWindows.length)];
}
for (int i = 0; i < tempSequence.getElements().size(); i++) {
nextRandom.set(Math.abs(nextRandom.get() * 25214903917L + 11));
cbow(i, tempSequence.getElements(), (int) nextRandom.get() % currentWindow, nextRandom, learningRate,
currentWindow, batchSequences);
}
return 0;
}
@Override
public void updateOffset(MessageQueue mq, long offset, boolean increaseOnly) {
if (mq != null) {
AtomicLong offsetOld = this.offsetTable.get(mq);
if (null == offsetOld) {
offsetOld = this.offsetTable.putIfAbsent(mq, new AtomicLong(offset));
}
if (null != offsetOld) {
if (increaseOnly) {
MixAll.compareAndIncreaseOnly(offsetOld, offset);
} else {
offsetOld.set(offset);
}
}
}
}
private void increment(final long duration, final AtomicLong counter)
{
if (!active)
{
return;
}
if (counter.get() < Long.MAX_VALUE - duration)
{
counter.addAndGet(duration);
}
else
{
reset();
counter.set(duration);
}
}
@Override
public void updateOffset(MessageQueue mq, long offset, boolean increaseOnly) {
if (mq != null) {
AtomicLong offsetOld = this.offsetTable.get(mq);
if (null == offsetOld) {
offsetOld = this.offsetTable.putIfAbsent(mq, new AtomicLong(offset));
}
if (null != offsetOld) {
if (increaseOnly) {
MixAll.compareAndIncreaseOnly(offsetOld, offset);
} else {
offsetOld.set(offset);
}
}
}
}
@Override
public void updateOffset(MessageQueue mq, long offset, boolean increaseOnly) {
if (mq != null) {
AtomicLong offsetOld = this.offsetTable.get(mq);
if (null == offsetOld) {
offsetOld = this.offsetTable.putIfAbsent(mq, new AtomicLong(offset));
}
if (null != offsetOld) {
if (increaseOnly) {
MixAll.compareAndIncreaseOnly(offsetOld, offset);
} else {
offsetOld.set(offset);
}
}
}
}
/**
* floatValue returns current value.
*/
public void testFloatValue() {
AtomicLong ai = new AtomicLong();
assertEquals(0.0f, ai.floatValue());
for (long x : VALUES) {
ai.set(x);
assertEquals((float)x, ai.floatValue());
}
}
@Test
public void lastContactedTimestampUpdatedOnOutboundMessage() {
final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList());
assertThat(agent.streamDiscoveredPeers()).hasSize(0);
// Start a test peer and send a PING packet to the agent under test.
final MockPeerDiscoveryAgent testAgent = helper.startDiscoveryAgent();
final Packet ping = helper.createPingPacket(testAgent, agent);
helper.sendMessageBetweenAgents(testAgent, agent, ping);
assertThat(agent.streamDiscoveredPeers()).hasSize(1);
final AtomicLong lastContacted = new AtomicLong();
final AtomicLong lastSeen = new AtomicLong();
final AtomicLong firstDiscovered = new AtomicLong();
DiscoveryPeer peer = agent.streamDiscoveredPeers().iterator().next();
final long lc = peer.getLastContacted();
final long ls = peer.getLastSeen();
final long fd = peer.getFirstDiscovered();
assertThat(lc).isGreaterThan(0);
assertThat(ls).isGreaterThan(0);
assertThat(fd).isGreaterThan(0);
lastContacted.set(lc);
lastSeen.set(ls);
firstDiscovered.set(fd);
// Send another packet and ensure that timestamps are updated accordingly.
helper.sendMessageBetweenAgents(testAgent, agent, ping);
peer = agent.streamDiscoveredPeers().iterator().next();
assertThat(peer.getLastContacted()).isGreaterThan(lastContacted.get());
assertThat(peer.getLastSeen()).isGreaterThan(lastSeen.get());
assertThat(peer.getFirstDiscovered()).isEqualTo(firstDiscovered.get());
}
public static void main(String[] args){
DB db = DBMaker
//.newFileDB(new File("/mnt/big/db/aa"))
.newAppendFileDB(new File("/mnt/big/db/aa" + System.currentTimeMillis()))
.make();
Map map = db
.getTreeMap("map");
//.getHashMap("map");
long time = System.currentTimeMillis();
long max = (int) 1e8;
AtomicLong progress = new AtomicLong(0);
Utils.printProgress(progress);
while(progress.incrementAndGet()<max){
Long val = Utils.RANDOM.nextLong();
map.put(val, "test"+val);
}
progress.set(-1);
System.out.println("Closing");
db.close();
System.out.println(System.currentTimeMillis() - time);
}
@Test
public void testLengthAlwaysEqual() throws Exception {
Field f = UUIDGeneratorImpl.class.getDeclaredField("count");
f.setAccessible(true);
AtomicLong c = (AtomicLong)f.get(null);
c.set(0);
int referenceLength = new UUIDGeneratorImpl().getNextUUID().length();
int numUUIDsCreated = 0;
long lastVal = -1;
while (true) {
UUIDGenerator uuid = new UUIDGeneratorImpl();
++numUUIDsCreated;
assertTrue("Bad uuid: "+ uuid , uuid.getNextUUID().length() == referenceLength);
c.set(c.get() * 2);
long thisVal = Long.parseLong(uuid.getNextUUID().substring(uuid.getNextUUID().lastIndexOf("-")+1), 16);
if (thisVal < lastVal) {
if (c.get() < 0xFFFFFFFFFFL) {
Assert.fail("Cycled after "+numUUIDsCreated+" uuids. lastVal was "+lastVal+", thisVal is "+thisVal);
} else {
break;
}
} else {
lastVal = thisVal;
}
}
System.out.println("Length tested OK after "+ numUUIDsCreated+ "iterations");
}
@Test
void should_not_retry_for_put_post_patch_delete() {
InstanceExchangeFilterFunction filter = InstanceExchangeFilterFunctions.retry(1, emptyMap());
AtomicLong invocationCount = new AtomicLong(0L);
ExchangeFunction exchange = (r) -> Mono.fromSupplier(() -> {
invocationCount.incrementAndGet();
throw new IllegalStateException("Test");
});
ClientRequest patchRequest = ClientRequest.create(HttpMethod.PATCH, URI.create("/test")).build();
StepVerifier.create(filter.filter(INSTANCE, patchRequest, exchange))
.verifyError(IllegalStateException.class);
assertThat(invocationCount.get()).isEqualTo(1);
invocationCount.set(0L);
ClientRequest putRequest = ClientRequest.create(HttpMethod.PUT, URI.create("/test")).build();
StepVerifier.create(filter.filter(INSTANCE, putRequest, exchange)).verifyError(IllegalStateException.class);
assertThat(invocationCount.get()).isEqualTo(1);
invocationCount.set(0L);
ClientRequest postRequest = ClientRequest.create(HttpMethod.POST, URI.create("/test")).build();
StepVerifier.create(filter.filter(INSTANCE, postRequest, exchange))
.verifyError(IllegalStateException.class);
assertThat(invocationCount.get()).isEqualTo(1);
invocationCount.set(0L);
ClientRequest deleteRequest = ClientRequest.create(HttpMethod.DELETE, URI.create("/test")).build();
StepVerifier.create(filter.filter(INSTANCE, deleteRequest, exchange))
.verifyError(IllegalStateException.class);
assertThat(invocationCount.get()).isEqualTo(1);
}
@Test
public void removeAndAddRepeatedlyGauge() {
Registry r = new DefaultRegistry();
Id id = r.createId("test");
AtomicLong value = new AtomicLong();
for (int i = 0; i < 10; ++i) {
PolledMeter.using(r).withId(id).monitorValue(value);
value.set(i);
PolledMeter.update(r);
PolledMeter.remove(r, id);
}
Assertions.assertEquals(9.0, r.gauge("test").value(), 1e-12);
}
public void updatePullFromWhichNode(final MessageQueue mq, final long brokerId) {
AtomicLong suggest = this.pullFromWhichNodeTable.get(mq);
if (null == suggest) {
this.pullFromWhichNodeTable.put(mq, new AtomicLong(brokerId));
} else {
suggest.set(brokerId);
}
}
/**
* getAndIncrement returns previous value and increments
*/
public void testGetAndIncrement() {
AtomicLong ai = new AtomicLong(1);
assertEquals(1, ai.getAndIncrement());
assertEquals(2, ai.get());
ai.set(-2);
assertEquals(-2, ai.getAndIncrement());
assertEquals(-1, ai.getAndIncrement());
assertEquals(0, ai.getAndIncrement());
assertEquals(1, ai.get());
}
private static void updateNumberOfWaits(AtomicLong start, AtomicLong maxTime) {
Long now = System.currentTimeMillis();
Long startValue = start.get();
if (startValue != 0 && now - startValue > 1000) {
maxTime.incrementAndGet();
}
start.set(now);
}
/**
* Assert that the operation is complete and successful. Note that the future completion and callback invocation
* happens outside of the GetOperation, so those are not checked here. But at this point, the operation result should
* be ready.
* @param options The {@link GetBlobOptions} for the operation to check.
* @param readIntoFuture The future associated with the read on the {@link ReadableStreamChannel} result of the
* operation.
* @param asyncWritableChannel The {@link ByteBufferAsyncWritableChannel} to which bytes will be written by the
* operation.
* @param readableStreamChannel The {@link ReadableStreamChannel} that bytes are read from in the operation.
* @param readCompleteLatch The latch to count down once the read is completed.
* @param readCompleteResult This will contain the bytes written on return.
* @param readCompleteThrowable This will contain any exceptions encountered during the read.
*/
private void assertBlobReadSuccess(GetBlobOptions options, Future<Long> readIntoFuture,
ByteBufferAsyncWritableChannel asyncWritableChannel, ReadableStreamChannel readableStreamChannel,
CountDownLatch readCompleteLatch, AtomicLong readCompleteResult,
AtomicReference<Throwable> readCompleteThrowable) {
try {
ByteBuffer putContentBuf;
if (options != null && options.isRawMode()) {
putContentBuf = getBlobBuffer();
Assert.assertNotNull("Did not find server with blob: " + blobIdStr, putContentBuf);
} else {
putContentBuf = ByteBuffer.wrap(putContent);
// If a range is set, compare the result against the specified byte range.
if (options != null && options.getRange() != null) {
ByteRange range = options.getRange().toResolvedByteRange(blobSize);
putContentBuf = ByteBuffer.wrap(putContent, (int) range.getStartOffset(), (int) range.getRangeSize());
}
}
long written;
Assert.assertTrue("ReadyForPollCallback should have been invoked as readInto() was called",
mockNetworkClient.getAndClearWokenUpStatus());
// Compare byte by byte.
final int bytesToRead = putContentBuf.remaining();
int readBytes = 0;
do {
ByteBuffer buf = asyncWritableChannel.getNextChunk();
int bufLength = buf.remaining();
Assert.assertTrue("total content read should not be greater than length of put content",
readBytes + bufLength <= bytesToRead);
while (buf.hasRemaining()) {
Assert.assertEquals("Get and Put blob content should match", putContentBuf.get(), buf.get());
readBytes++;
}
asyncWritableChannel.resolveOldestChunk(null);
Assert.assertTrue("ReadyForPollCallback should have been invoked as writable channel callback was called",
mockNetworkClient.getAndClearWokenUpStatus());
} while (readBytes < bytesToRead);
written = readIntoFuture.get();
Assert.assertEquals("the returned length in the future should be the length of data written", (long) readBytes,
written);
Assert.assertNull("There should be no more data in the channel", asyncWritableChannel.getNextChunk(0));
readableStreamChannel.close();
readCompleteResult.set(written);
} catch (Throwable e) {
readCompleteThrowable.set(e);
} finally {
readCompleteLatch.countDown();
}
}
public static void main(String[] args) {
try {
// Set up Mobility connection...
final MobilityController mobilityController = MobilityRPC.newController();
final MobilitySession session = mobilityController.getSession(UUID.randomUUID());
final ConnectionId connectionId = new ConnectionId("127.0.0.1", 5739);
final AtomicLong numIterations = new AtomicLong();
final AtomicLong numObjectsSent = new AtomicLong();
final AtomicLong sumOfLatencyNanos = new AtomicLong();
class BenchmarkTask implements Callable<Collection<? extends Comparable>> {
@Override
public Collection<? extends Comparable> call() {
Collection<? extends Comparable> input = Util.createCollection(REQUEST_SIZE);
Collection<? extends Comparable> output = null;
long startTime = System.nanoTime();
for (int iterationNumber = 0; iterationNumber < NUM_REQUESTS_PER_THREAD; iterationNumber++) {
output = processRemotelyViaMobility(input, session, connectionId);
}
long timeTakenNanos = System.nanoTime() - startTime;
numIterations.addAndGet(NUM_REQUESTS_PER_THREAD);
numObjectsSent.addAndGet(REQUEST_SIZE * NUM_REQUESTS_PER_THREAD);
sumOfLatencyNanos.addAndGet(timeTakenNanos);
return output;
}
}
Future<Collection<? extends Comparable>> result = null;
// Warm up (run the test code but discard results)...
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++) {
result = executorService.submit(new BenchmarkTask());
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
// Run test...
executorService = Executors.newFixedThreadPool(NUM_THREADS);
numIterations.set(0);
numObjectsSent.set(0);
sumOfLatencyNanos.set(0);
for (int i = 0; i < NUM_THREADS; i++) {
result = executorService.submit(new BenchmarkTask());
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
mobilityController.destroy();
System.out.println("Finished. Final result was: " + ((result == null) ? null : result.get()));
System.out.println("Mobility Num Threads\tMobility Request Size\tMobility Requests per sec\tMobility Latency Per Request(ns)");
System.out.println(NUM_THREADS + "\t" + (((double)numObjectsSent.get()) / numIterations.get()) + "\t" + (numIterations.get() / (sumOfLatencyNanos.get() / 1000000000.0)) + "\t" + (((double) sumOfLatencyNanos.get()) / numIterations.get()));
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Test(timeOut = 60_000)
public void testInMemoryCommitTableCheckpoints(ITestContext context) throws Exception {
final byte[] row = Bytes.toBytes("test-sc");
final byte[] family = Bytes.toBytes(TEST_FAMILY);
final byte[] qualifier = Bytes.toBytes("testdata");
final byte[] qualifier2 = Bytes.toBytes("testdata2");
final byte[] data1 = Bytes.toBytes("testWrite-");
final CountDownLatch beforeCTRemove = new CountDownLatch(1);
final CountDownLatch afterCommit = new CountDownLatch(1);
final CountDownLatch writerDone = new CountDownLatch(1);
final AtomicLong startTimestamp = new AtomicLong(0);
final AtomicLong commitTimestamp = new AtomicLong(0);
PostCommitActions syncPostCommitter =
spy(new HBaseSyncPostCommitter(new NullMetricsProvider(), getCommitTable(context).getClient(), connection));
final AbstractTransactionManager tm = (AbstractTransactionManager) newTransactionManager(context, syncPostCommitter);
Table htable = connection.getTable(TableName.valueOf(TEST_TABLE));
SnapshotFilterImpl snapshotFilter = new SnapshotFilterImpl(new HTableAccessWrapper(htable, htable),
tm.getCommitTableClient());
final TTable table = new TTable(htable,snapshotFilter);
doAnswer(new Answer<ListenableFuture<Void>>() {
@Override
public ListenableFuture<Void> answer(InvocationOnMock invocation) throws Throwable {
afterCommit.countDown();
beforeCTRemove.await();
ListenableFuture<Void> result = (ListenableFuture<Void>) invocation.callRealMethod();
return result;
}
}).when(syncPostCommitter).removeCommitTableEntry(any(HBaseTransaction.class));
Thread writeThread = new Thread("WriteThread"){
@Override
public void run() {
try {
HBaseTransaction tx1 = (HBaseTransaction) tm.begin();
Put put = new Put(row);
put.addColumn(family, qualifier, data1);
startTimestamp.set(tx1.getStartTimestamp());
table.put(tx1, put);
tx1.checkpoint();
Put put2 = new Put(row);
put2.addColumn(family, qualifier2, data1);
table.put(tx1, put2);
tm.commit(tx1);
commitTimestamp.set(tx1.getCommitTimestamp());
writerDone.countDown();
} catch (IOException | RollbackException e) {
e.printStackTrace();
}
}
};
writeThread.start();
afterCommit.await();
Optional<CommitTable.CommitTimestamp> ct1 = tm.getCommitTableClient().getCommitTimestamp(startTimestamp.get()).get();
Optional<CommitTable.CommitTimestamp> ct2 = tm.getCommitTableClient().getCommitTimestamp(startTimestamp.get() + 1).get();
beforeCTRemove.countDown();
writerDone.await();
assertEquals(commitTimestamp.get(), ct1.get().getValue());
assertEquals(commitTimestamp.get(), ct2.get().getValue());
assertTrue(hasCell(row, family, qualifier, startTimestamp.get(), new TTableCellGetterAdapter(table)),
"Cell should be there");
assertTrue(hasCell(row, family, qualifier2, startTimestamp.get()+1, new TTableCellGetterAdapter(table)),
"Cell should be there");
assertTrue(hasShadowCell(row, family, qualifier, startTimestamp.get(), new TTableCellGetterAdapter(table)),
"Cell should be there");
assertTrue(hasShadowCell(row, family, qualifier2, startTimestamp.get()+1, new TTableCellGetterAdapter(table)),
"Cell should be there");
}
/**
* @return true if we actually showed, false if we just called toFront()
*/
private boolean conditionalShow(Component toFocus, AtomicLong time) {
boolean retval;
closeSplashScreen();
synchronized (getTreeLock()) {
if (peer == null) {
addNotify();
}
validateUnconditionally();
if (visible) {
toFront();
retval = false;
} else {
visible = retval = true;
// check if this dialog should be modal blocked BEFORE calling peer.show(),
// otherwise, a pair of FOCUS_GAINED and FOCUS_LOST may be mistakenly
// generated for the dialog
if (!isModal()) {
checkShouldBeBlocked(this);
} else {
modalDialogs.add(this);
modalShow();
}
if (toFocus != null && time != null && isFocusable() &&
isEnabled() && !isModalBlocked()) {
// keep the KeyEvents from being dispatched
// until the focus has been transfered
time.set(Toolkit.getEventQueue().getMostRecentKeyEventTime());
KeyboardFocusManager.getCurrentKeyboardFocusManager().
enqueueKeyEvents(time.get(), toFocus);
}
// This call is required as the show() method of the Dialog class
// does not invoke the super.show(). So wried... :(
mixOnShowing();
peer.setVisible(true); // now guaranteed never to block
if (isModalBlocked()) {
modalBlocker.toFront();
}
setLocationByPlatform(false);
for (int i = 0; i < ownedWindowList.size(); i++) {
Window child = ownedWindowList.elementAt(i).get();
if ((child != null) && child.showWithParent) {
child.show();
child.showWithParent = false;
} // endif
} // endfor
Window.updateChildFocusableWindowState(this);
createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
this, parent,
HierarchyEvent.SHOWING_CHANGED,
Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
if (componentListener != null ||
(eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
ComponentEvent e =
new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN);
Toolkit.getEventQueue().postEvent(e);
}
}
}
if (retval && (state & OPENED) == 0) {
postWindowEvent(WindowEvent.WINDOW_OPENED);
state |= OPENED;
}
return retval;
}
/**
* @return true if we actually showed, false if we just called toFront()
*/
@SuppressWarnings("deprecation")
private boolean conditionalShow(Component toFocus, AtomicLong time) {
boolean retval;
closeSplashScreen();
synchronized (getTreeLock()) {
if (peer == null) {
addNotify();
}
validateUnconditionally();
if (visible) {
toFront();
retval = false;
} else {
visible = retval = true;
// check if this dialog should be modal blocked BEFORE calling peer.show(),
// otherwise, a pair of FOCUS_GAINED and FOCUS_LOST may be mistakenly
// generated for the dialog
if (!isModal()) {
checkShouldBeBlocked(this);
} else {
modalDialogs.add(this);
modalShow();
}
if (toFocus != null && time != null && isFocusable() &&
isEnabled() && !isModalBlocked()) {
// keep the KeyEvents from being dispatched
// until the focus has been transferred
time.set(Toolkit.getEventQueue().getMostRecentKeyEventTime());
KeyboardFocusManager.getCurrentKeyboardFocusManager().
enqueueKeyEvents(time.get(), toFocus);
}
// This call is required as the show() method of the Dialog class
// does not invoke the super.show(). So wried... :(
mixOnShowing();
peer.setVisible(true); // now guaranteed never to block
if (isModalBlocked()) {
modalBlocker.toFront();
}
setLocationByPlatform(false);
for (int i = 0; i < ownedWindowList.size(); i++) {
Window child = ownedWindowList.elementAt(i).get();
if ((child != null) && child.showWithParent) {
child.show();
child.showWithParent = false;
} // endif
} // endfor
Window.updateChildFocusableWindowState(this);
createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
this, parent,
HierarchyEvent.SHOWING_CHANGED,
Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
if (componentListener != null ||
(eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
ComponentEvent e =
new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN);
Toolkit.getEventQueue().postEvent(e);
}
}
}
if (retval && (state & OPENED) == 0) {
postWindowEvent(WindowEvent.WINDOW_OPENED);
state |= OPENED;
}
return retval;
}
@Override
public void deliver(final Deliver<M> processor,
final Outbox outbox,
final int headIndex,
final int tailIndex,
final WorkerDeliver<?> nextWorker,
boolean isTail)
throws Exception
{
final AtomicLong []counterGroup = _counterGroup;
final AtomicLong headCounter = counterGroup[headIndex];
final AtomicLong tailCounter = counterGroup[tailIndex];
final RingGetter<M> ringGetter = isTail ? _tailGetter : _nonTailGetter;
int tailChunk = 2;
long initialTail = tailCounter.get();
long tail = initialTail;
long head = headCounter.get();
try {
do {
long tailChunkEnd = Math.min(head, tail + tailChunk);
while (tail < tailChunkEnd) {
M item = ringGetter.get(tail);
if (item != null) {
tail++;
processor.deliver(item, outbox);
}
}
tailCounter.set(tail);
initialTail = tail;
tailChunk = Math.min(256, 2 * tailChunk);
nextWorker.wake();
head = headCounter.get();
} while (head != tail);
} finally {
if (tail != initialTail) {
tailCounter.set(tail);
}
nextWorker.wake();
}
}