use of java.util.concurrent.atomic.AtomicLongArray in project flink by apache.
the class AbstractQueryableStateITCase method testQueryableState.
/**
* Runs a simple topology producing random (key, 1) pairs at the sources (where
* number of keys is in fixed in range 0...numKeys). The records are keyed and
* a reducing queryable state instance is created, which sums up the records.
*
* After submitting the job in detached mode, the QueryableStateCLient is used
* to query the counts of each key in rounds until all keys have non-zero counts.
*/
@Test
@SuppressWarnings("unchecked")
public void testQueryableState() throws Exception {
// Config
final Deadline deadline = TEST_TIMEOUT.fromNow();
final int numKeys = 256;
final QueryableStateClient client = new QueryableStateClient(cluster.configuration());
JobID jobId = null;
try {
//
// Test program
//
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStateBackend(stateBackend);
env.setParallelism(NUM_SLOTS);
// Very important, because cluster is shared between tests and we
// don't explicitly check that all slots are available before
// submitting.
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(Integer.MAX_VALUE, 1000));
DataStream<Tuple2<Integer, Long>> source = env.addSource(new TestKeyRangeSource(numKeys));
// Reducing state
ReducingStateDescriptor<Tuple2<Integer, Long>> reducingState = new ReducingStateDescriptor<>("any-name", new SumReduce(), source.getType());
final String queryName = "hakuna-matata";
final QueryableStateStream<Integer, Tuple2<Integer, Long>> queryableState = source.keyBy(new KeySelector<Tuple2<Integer, Long>, Integer>() {
@Override
public Integer getKey(Tuple2<Integer, Long> value) throws Exception {
return value.f0;
}
}).asQueryableState(queryName, reducingState);
// Submit the job graph
JobGraph jobGraph = env.getStreamGraph().getJobGraph();
cluster.submitJobDetached(jobGraph);
//
// Start querying
//
jobId = jobGraph.getJobID();
final AtomicLongArray counts = new AtomicLongArray(numKeys);
boolean allNonZero = false;
while (!allNonZero && deadline.hasTimeLeft()) {
allNonZero = true;
final List<Future<byte[]>> futures = new ArrayList<>(numKeys);
for (int i = 0; i < numKeys; i++) {
final int key = i;
if (counts.get(key) > 0) {
// Skip this one
continue;
} else {
allNonZero = false;
}
final byte[] serializedKey = KvStateRequestSerializer.serializeKeyAndNamespace(key, queryableState.getKeySerializer(), VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);
Future<byte[]> serializedResult = getKvStateWithRetries(client, jobId, queryName, key, serializedKey, QUERY_RETRY_DELAY, false);
serializedResult.onSuccess(new OnSuccess<byte[]>() {
@Override
public void onSuccess(byte[] result) throws Throwable {
Tuple2<Integer, Long> value = KvStateRequestSerializer.deserializeValue(result, queryableState.getValueSerializer());
counts.set(key, value.f1);
assertEquals("Key mismatch", key, value.f0.intValue());
}
}, TEST_ACTOR_SYSTEM.dispatcher());
futures.add(serializedResult);
}
Future<Iterable<byte[]>> futureSequence = Futures.sequence(futures, TEST_ACTOR_SYSTEM.dispatcher());
Await.ready(futureSequence, deadline.timeLeft());
}
assertTrue("Not all keys are non-zero", allNonZero);
// All should be non-zero
for (int i = 0; i < numKeys; i++) {
long count = counts.get(i);
assertTrue("Count at position " + i + " is " + count, count > 0);
}
} finally {
// Free cluster resources
if (jobId != null) {
Future<CancellationSuccess> cancellation = cluster.getLeaderGateway(deadline.timeLeft()).ask(new JobManagerMessages.CancelJob(jobId), deadline.timeLeft()).mapTo(ClassTag$.MODULE$.<CancellationSuccess>apply(CancellationSuccess.class));
Await.ready(cancellation, deadline.timeLeft());
}
client.shutDown();
}
}
use of java.util.concurrent.atomic.AtomicLongArray in project fastjson by alibaba.
the class Bug_7 method test_AtomicLongArray.
public void test_AtomicLongArray() throws Exception {
AtomicLongArray array = new AtomicLongArray(3);
array.set(0, 1);
array.set(1, 2);
array.set(2, 3);
String text = JSON.toJSONString(array);
Assert.assertEquals("[1,2,3]", text);
}
use of java.util.concurrent.atomic.AtomicLongArray in project fastjson by alibaba.
the class AtomicCodec method write.
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.out;
if (object instanceof AtomicInteger) {
AtomicInteger val = (AtomicInteger) object;
out.writeInt(val.get());
return;
}
if (object instanceof AtomicLong) {
AtomicLong val = (AtomicLong) object;
out.writeLong(val.get());
return;
}
if (object instanceof AtomicBoolean) {
AtomicBoolean val = (AtomicBoolean) object;
out.append(val.get() ? "true" : "false");
return;
}
if (object == null) {
out.writeNull(SerializerFeature.WriteNullListAsEmpty);
return;
}
if (object instanceof AtomicIntegerArray) {
AtomicIntegerArray array = (AtomicIntegerArray) object;
int len = array.length();
out.write('[');
for (int i = 0; i < len; ++i) {
int val = array.get(i);
if (i != 0) {
out.write(',');
}
out.writeInt(val);
}
out.write(']');
return;
}
AtomicLongArray array = (AtomicLongArray) object;
int len = array.length();
out.write('[');
for (int i = 0; i < len; ++i) {
long val = array.get(i);
if (i != 0) {
out.write(',');
}
out.writeLong(val);
}
out.write(']');
}
use of java.util.concurrent.atomic.AtomicLongArray in project HdrHistogram by HdrHistogram.
the class ConcurrentHistogram method resize.
@Override
void resize(final long newHighestTrackableValue) {
try {
wrp.readerLock();
assert (countsArrayLength == activeCounts.length());
assert (countsArrayLength == inactiveCounts.length());
int newArrayLength = determineArrayLengthNeeded(newHighestTrackableValue);
int countsDelta = newArrayLength - countsArrayLength;
if (countsDelta <= 0) {
// This resize need was already covered by a concurrent resize op.
return;
}
int oldNormalizedZeroIndex = normalizeIndex(0, inactiveCounts.getNormalizingIndexOffset(), inactiveCounts.length());
// Resize the current inactiveCounts:
AtomicLongArray oldInactiveCounts = inactiveCounts;
inactiveCounts = new AtomicLongArrayWithNormalizingOffset(newArrayLength, inactiveCounts.getNormalizingIndexOffset());
// Copy inactive contents to newly sized inactiveCounts:
for (int i = 0; i < oldInactiveCounts.length(); i++) {
inactiveCounts.lazySet(i, oldInactiveCounts.get(i));
}
if (oldNormalizedZeroIndex != 0) {
// We need to shift the stuff from the zero index and up to the end of the array:
int newNormalizedZeroIndex = oldNormalizedZeroIndex + countsDelta;
int lengthToCopy = (newArrayLength - countsDelta) - oldNormalizedZeroIndex;
int src, dst;
for (src = oldNormalizedZeroIndex, dst = newNormalizedZeroIndex; src < oldNormalizedZeroIndex + lengthToCopy; src++, dst++) {
inactiveCounts.lazySet(dst, oldInactiveCounts.get(src));
}
for (dst = oldNormalizedZeroIndex; dst < newNormalizedZeroIndex; dst++) {
inactiveCounts.lazySet(dst, 0);
}
}
// switch active and inactive:
AtomicLongArrayWithNormalizingOffset tmp = activeCounts;
activeCounts = inactiveCounts;
inactiveCounts = tmp;
wrp.flipPhase();
// Resize the newly inactiveCounts:
oldInactiveCounts = inactiveCounts;
inactiveCounts = new AtomicLongArrayWithNormalizingOffset(newArrayLength, inactiveCounts.getNormalizingIndexOffset());
// Copy inactive contents to newly sized inactiveCounts:
for (int i = 0; i < oldInactiveCounts.length(); i++) {
inactiveCounts.lazySet(i, oldInactiveCounts.get(i));
}
if (oldNormalizedZeroIndex != 0) {
// We need to shift the stuff from the zero index and up to the end of the array:
int newNormalizedZeroIndex = oldNormalizedZeroIndex + countsDelta;
int lengthToCopy = (newArrayLength - countsDelta) - oldNormalizedZeroIndex;
int src, dst;
for (src = oldNormalizedZeroIndex, dst = newNormalizedZeroIndex; src < oldNormalizedZeroIndex + lengthToCopy; src++, dst++) {
inactiveCounts.lazySet(dst, oldInactiveCounts.get(src));
}
for (dst = oldNormalizedZeroIndex; dst < newNormalizedZeroIndex; dst++) {
inactiveCounts.lazySet(dst, 0);
}
}
// switch active and inactive again:
tmp = activeCounts;
activeCounts = inactiveCounts;
inactiveCounts = tmp;
wrp.flipPhase();
// At this point, both active and inactive have been safely resized,
// and the switch in each was done without any writers modifying it in flight.
// We resized things. We can now make the historam establish size accordingly for future recordings:
establishSize(newHighestTrackableValue);
assert (countsArrayLength == activeCounts.length());
assert (countsArrayLength == inactiveCounts.length());
} finally {
wrp.readerUnlock();
}
}
use of java.util.concurrent.atomic.AtomicLongArray in project intellij-community by JetBrains.
the class ConcurrentBitSet method nextClearBit.
/**
* Returns the index of the first bit that is set to {@code false}
* that occurs on or after the specified starting index.
*
* @param fromIndex the index to start checking from (inclusive)
* @return the index of the next clear bit
* @throws IndexOutOfBoundsException if the specified index is negative
*/
public int nextClearBit(int fromIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
}
int arrayIndex = arrayIndex(fromIndex);
AtomicLongArray array = arrays.get(arrayIndex);
int wordIndexInArray = wordIndexInArray(fromIndex);
if (array == null) {
return ((1 << arrayIndex) - 1 + wordIndexInArray) * BITS_PER_WORD + (fromIndex % BITS_PER_WORD);
}
long word = ~array.get(wordIndexInArray) & (WORD_MASK << fromIndex);
while (true) {
if (word != 0) {
return ((1 << arrayIndex) - 1 + wordIndexInArray) * BITS_PER_WORD + Long.numberOfTrailingZeros(word);
}
if (++wordIndexInArray == array.length()) {
wordIndexInArray = 0;
if (++arrayIndex == arrays.length())
return -1;
array = arrays.get(arrayIndex);
if (array == null) {
return ((1 << arrayIndex) - 1 + wordIndexInArray) * BITS_PER_WORD;
}
}
word = ~array.get(wordIndexInArray);
}
}
Aggregations