Search in sources :

Example 1 with ObjectsView

use of org.corfudb.runtime.view.ObjectsView in project CorfuDB by CorfuDB.

the class SMRMultiLogunitTest method transactionalManyWritesThenRead.

/**
     * Multi Threaded.
     * Verify reads after multiple transactional writes done concurrently (using 2 threads)
     *
     * @throws TransactionAbortedException
     */
@Test(expected = TransactionAbortedException.class)
public void transactionalManyWritesThenRead() throws TransactionAbortedException {
    int numKeys = ONE_THOUSAND;
    ObjectsView view = getRuntime().getObjectsView();
    final CountDownLatch barrier = new CountDownLatch(2);
    Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    // concurrently run two conflicting transactions:  one or the other should succeed without overlap
    scheduleConcurrently((ignored_step) -> {
        view.TXBegin();
        for (int i = 0; i < numKeys; i++) {
            String key = "key" + String.valueOf(i);
            String val = "value0_" + String.valueOf(i);
            testMap.put(key, val);
            if (i == 0) {
                barrier.countDown();
                barrier.await();
            }
            if (i % ONE_HUNDRED == 0) {
                Thread.yield();
            }
        }
        view.TXEnd();
    });
    scheduleConcurrently((ignored_step) -> {
        view.TXBegin();
        for (int i = 0; i < numKeys; i++) {
            String key = "key" + String.valueOf(i);
            String val = "value1_" + String.valueOf(i);
            testMap.put(key, val);
            if (i == 0) {
                barrier.countDown();
                barrier.await();
            }
            if (i % ONE_HUNDRED == 0) {
                Thread.yield();
            }
        }
        view.TXEnd();
    });
    try {
        executeScheduled(2, PARAMETERS.TIMEOUT_NORMAL);
    } catch (TransactionAbortedException e) {
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
    }
    // check that all the values are either value0_ or value1_ not a mix
    String base = "invalid";
    for (int i = 0; i < numKeys; i++) {
        String key = "key" + String.valueOf(i);
        String val = testMap.get(key);
        if (val != null) {
            if (i == 0) {
                int underscore = val.indexOf("_");
                assertNotEquals(-1, underscore);
                base = val.substring(0, underscore);
                System.out.println("base is " + base);
            }
        }
        assertEquals(true, val.contains(base));
    }
}
Also used : TypeToken(com.google.common.reflect.TypeToken) ObjectsView(org.corfudb.runtime.view.ObjectsView) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 2 with ObjectsView

use of org.corfudb.runtime.view.ObjectsView in project CorfuDB by CorfuDB.

the class SMRMultiLogunitTest method multiThreadedManyWritesThenRead.

/**
     * Multi Threaded.
     * Verify reads after multiple non-transactional writes done concurrently (using 2 threads)
     *
     */
@Test
public void multiThreadedManyWritesThenRead() {
    int numKeys = ONE_THOUSAND;
    ObjectsView view = getRuntime().getObjectsView();
    Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    AtomicInteger threadsComplete = new AtomicInteger(0);
    addTestStep((step) -> {
        for (int i = 0; i < numKeys; i++) {
            String key = "key" + String.valueOf(i);
            String val = "value" + String.valueOf(step) + "_" + String.valueOf(i);
            testMap.put(key, val);
            if (i % ONE_HUNDRED == 0) {
                Thread.yield();
            }
        }
        threadsComplete.incrementAndGet();
    });
    try {
        scheduleThreaded(2, 2);
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertEquals(2, threadsComplete.get());
    for (int i = 0; i < numKeys; i++) {
        String key = "key" + String.valueOf(i);
        String val = testMap.get(key);
        assertNotNull(val);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeToken(com.google.common.reflect.TypeToken) ObjectsView(org.corfudb.runtime.view.ObjectsView) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Aggregations

TypeToken (com.google.common.reflect.TypeToken)2 TransactionAbortedException (org.corfudb.runtime.exceptions.TransactionAbortedException)2 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)2 ObjectsView (org.corfudb.runtime.view.ObjectsView)2 Test (org.junit.Test)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1