Search in sources :

Example 6 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project CorfuDB by CorfuDB.

the class TXConflictScenariosTest method testNoWriteConflictSimple.

void testNoWriteConflictSimple() throws Exception {
    final CorfuSharedCounter sharedCounter1 = instantiateCorfuObject(CorfuSharedCounter.class, "test" + 1);
    final CorfuSharedCounter sharedCounter2 = instantiateCorfuObject(CorfuSharedCounter.class, "test" + 2);
    commitStatus = new AtomicIntegerArray(2);
    t(1, this::TXBegin);
    t(2, this::TXBegin);
    t(1, () -> {
        sharedCounter1.setValue(OVERWRITE_ONCE);
    });
    t(2, () -> {
        sharedCounter2.setValue(OVERWRITE_ONCE);
    });
    t(1, () -> sharedCounter1.getValue());
    t(2, () -> sharedCounter2.getValue());
    t(1, () -> sharedCounter2.getValue());
    t(2, () -> sharedCounter1.getValue());
    t(1, () -> {
        sharedCounter1.setValue(OVERWRITE_TWICE);
    });
    t(2, () -> {
        sharedCounter2.setValue(OVERWRITE_TWICE);
    });
    t(1, () -> sharedCounter1.getValue());
    t(1, () -> sharedCounter2.getValue());
    t(2, () -> sharedCounter2.getValue());
    t(2, () -> sharedCounter1.getValue());
    t(1, () -> {
        try {
            TXEnd();
            commitStatus.set(0, COMMITVALUE);
        } catch (TransactionAbortedException tae) {
        // do nothing
        }
    });
    t(2, () -> {
        try {
            TXEnd();
            commitStatus.set(1, COMMITVALUE);
        } catch (TransactionAbortedException tae) {
        // do nothing
        }
    });
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CorfuSharedCounter(org.corfudb.runtime.object.CorfuSharedCounter) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException)

Example 7 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project CorfuDB by CorfuDB.

the class AbstractTransactionContextTest method setupCounters.

/**
     * build an array of shared counters for the test
     */
void setupCounters() {
    numTasks = PARAMETERS.NUM_ITERATIONS_MODERATE;
    sharedCounters = new ArrayList<>();
    for (int i = 0; i < numTasks; i++) sharedCounters.add(i, instantiateCorfuObject(CorfuSharedCounter.class, "test" + i));
    // initialize all shared counters
    for (int i = 0; i < numTasks; i++) sharedCounters.get(i).setValue(INITIAL);
    commitStatus = new AtomicIntegerArray(numTasks);
    snapStatus = new AtomicIntegerArray(numTasks);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 8 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project elasticsearch by elastic.

the class IndexActionIT method testCreatedFlagParallelExecution.

public void testCreatedFlagParallelExecution() throws Exception {
    createIndex("test");
    ensureGreen();
    int threadCount = 20;
    final int docCount = 300;
    int taskCount = docCount * threadCount;
    final AtomicIntegerArray createdCounts = new AtomicIntegerArray(docCount);
    ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
    List<Callable<Void>> tasks = new ArrayList<>(taskCount);
    final Random random = random();
    for (int i = 0; i < taskCount; i++) {
        tasks.add(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                int docId = random.nextInt(docCount);
                IndexResponse indexResponse = index("test", "type", Integer.toString(docId), "field1", "value");
                if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                    createdCounts.incrementAndGet(docId);
                }
                return null;
            }
        });
    }
    threadPool.invokeAll(tasks);
    for (int i = 0; i < docCount; i++) {
        assertThat(createdCounts.get(i), lessThanOrEqualTo(1));
    }
    terminate(threadPool);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Random(java.util.Random) IndexResponse(org.elasticsearch.action.index.IndexResponse) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException)

Example 9 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project fastjson by alibaba.

the class Bug_7 method test_AtomicIntegerArray.

public void test_AtomicIntegerArray() throws Exception {
    AtomicIntegerArray array = new AtomicIntegerArray(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 : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 10 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray 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)

Aggregations

AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 List (java.util.List)3 CacheException (javax.cache.CacheException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 GridRandom (org.apache.ignite.internal.util.GridRandom)3 CAX (org.apache.ignite.internal.util.typedef.CAX)3 ArrayList (java.util.ArrayList)2 AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteFutureTimeoutCheckedException (org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)2 JSONArray (com.alibaba.fastjson.JSONArray)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Ignite (org.apache.ignite.Ignite)1