Search in sources :

Example 16 with AtomicLongArray

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();
    }
}
Also used : ArrayList(java.util.ArrayList) QueryableStateClient(org.apache.flink.runtime.query.QueryableStateClient) KeySelector(org.apache.flink.api.java.functions.KeySelector) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) Deadline(scala.concurrent.duration.Deadline) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) Tuple2(org.apache.flink.api.java.tuple.Tuple2) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) CancellationSuccess(org.apache.flink.runtime.messages.JobManagerMessages.CancellationSuccess) Future(scala.concurrent.Future) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 17 with AtomicLongArray

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);
}
Also used : AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 18 with AtomicLongArray

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(']');
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 19 with AtomicLongArray

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();
    }
}
Also used : AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 20 with AtomicLongArray

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);
    }
}
Also used : AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Aggregations

AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)22 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Random (java.util.Random)2 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)2 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridRandom (org.apache.ignite.internal.util.GridRandom)2 NotNull (org.jetbrains.annotations.NotNull)2 Test (org.junit.Test)2 Deadline (scala.concurrent.duration.Deadline)2 ActorRef (akka.actor.ActorRef)1 ActorSystem (akka.actor.ActorSystem)1 JSONArray (com.alibaba.fastjson.JSONArray)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1