use of org.corfudb.runtime.exceptions.TransactionAbortedException in project CorfuDB by CorfuDB.
the class TXsFromTwoRuntimesTest method staggeredTXsNoConflict.
@Test
public void staggeredTXsNoConflict() throws Exception {
final int nTXs = 5;
final Semaphore sem0 = new Semaphore(0), sem1 = new Semaphore(0);
// create two parallel worlds, with separate runtimes.
// both instantiate the same shared map
//
final Thread thread1 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
ISMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
assertThat(mymap.get("world1")).isEqualTo(null);
for (int t = 0; t < nTXs; t++) {
myruntime.getObjectsView().TXBegin();
mymap.put(nTXs + t, t);
myruntime.getObjectsView().TXEnd();
}
// expect to see nTXS entries in this map
assertThat(mymap.size()).isEqualTo(nTXs);
// now allow for thread0 to commit its own transaction
sem0.release();
// next, wait for the commit to have completed
try {
sem1.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
// expect to (still) see nTXS+1 entries in this map
assertThat(mymap.size()).isEqualTo(nTXs + 1);
assertThat(mymap.get(0)).isEqualTo(0);
});
final Thread thread0 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
ISMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
// start a transaction and then hand over to thread 1
myruntime.getObjectsView().TXBegin();
assertThat(mymap.get(0)).isEqualTo(null);
mymap.computeIfAbsent(0, (K) -> {
// should be computed deterministically, does it?
return mymap.size();
});
// enable thread1: it will do nTXS increments on the stream
thread1.start();
boolean isAbort = false;
try {
// wait for thread 1 to do its work;
// completion is indicated thru sem0
sem0.acquire();
myruntime.getObjectsView().TXEnd();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TransactionAbortedException te) {
isAbort = true;
}
// release thread1 to complete
sem1.release();
// expect to abort
assertThat(isAbort).isFalse();
// this currently fails, due to incorrect sync on commit in VersionLockedObject
// expect to see nTXs+1 entries on the stream
assertThat(mymap.size()).isEqualTo(nTXs + 1);
assertThat(mymap.get(0)).isEqualTo(0);
});
thread0.start();
final long WAIT_TIME = 10000;
try {
thread0.join(WAIT_TIME);
thread1.join(WAIT_TIME);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
}
use of org.corfudb.runtime.exceptions.TransactionAbortedException in project CorfuDB by CorfuDB.
the class TXsFromTwoRuntimesTest method staggeredTXsConflict.
@Test
public void staggeredTXsConflict() throws Exception {
final int nTXs = 5;
final Semaphore sem0 = new Semaphore(0), sem1 = new Semaphore(0);
// create two parallel worlds, with separate runtimes.
// both instantiate the same shared map
//
final Thread thread1 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
ISMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
assertThat(mymap.get("world1")).isEqualTo(null);
for (int t = 0; t < nTXs; t++) {
myruntime.getObjectsView().TXBegin();
mymap.put(nTXs + t, t);
myruntime.getObjectsView().TXEnd();
}
// expect to see nTXS entries in this map
assertThat(mymap.size()).isEqualTo(nTXs);
// now allow for thread0 to commit its own transaction
sem0.release();
// next, wait for the commit to have completed
try {
sem1.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
// expect to (still) see nTXS entries in this map
assertThat(mymap.size()).isEqualTo(nTXs);
});
final Thread thread0 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
SMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
// start a transaction and then hand over to thread 1
myruntime.getObjectsView().TXBegin();
assertThat(mymap.get(nTXs)).isEqualTo(null);
mymap.put(0, mymap.size());
// enable thread1: it will do nTXS increments on the stream
thread1.start();
boolean isAbort = false;
try {
// wait for thread 1 to do its work;
// completion is indicated thru sem0
sem0.acquire();
myruntime.getObjectsView().TXEnd();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TransactionAbortedException te) {
isAbort = true;
}
// release thread1 to complete
sem1.release();
// expect to abort
assertThat(isAbort).isTrue();
// expect to see nTXs entries on the stream
assertThat(mymap.size()).isEqualTo(nTXs);
});
thread0.start();
final long WAIT_TIME = 10000;
try {
thread0.join(WAIT_TIME);
thread1.join(WAIT_TIME);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
}
use of org.corfudb.runtime.exceptions.TransactionAbortedException 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));
}
}
use of org.corfudb.runtime.exceptions.TransactionAbortedException in project CorfuDB by CorfuDB.
the class FGMapTest method abortRateIsLowForSimpleTX.
@Test
@SuppressWarnings("unchecked")
public void abortRateIsLowForSimpleTX() throws Exception {
getDefaultRuntime();
Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<FGMap<String, String>>() {
}).open();
final int num_threads = PARAMETERS.CONCURRENCY_SOME;
final int num_records = PARAMETERS.NUM_ITERATIONS_LOW;
AtomicInteger aborts = new AtomicInteger();
testMap.clear();
scheduleConcurrently(num_threads, threadNumber -> {
int base = threadNumber * num_records;
for (int i = base; i < base + num_records; i++) {
try {
getRuntime().getObjectsView().TXBegin();
assertThat(testMap.put(Integer.toString(i), Integer.toString(i))).isEqualTo(null);
getRuntime().getObjectsView().TXEnd();
} catch (TransactionAbortedException tae) {
aborts.incrementAndGet();
}
}
});
long startTime = System.currentTimeMillis();
executeScheduled(num_threads, PARAMETERS.TIMEOUT_LONG);
calculateRequestsPerSecond("TPS", num_records * num_threads, startTime);
calculateAbortRate(aborts.get(), num_records * num_threads);
}
use of org.corfudb.runtime.exceptions.TransactionAbortedException in project CorfuDB by CorfuDB.
the class TXConflictScenariosTest method finegrainedUpdatesDoNotConflict.
@Test
@SuppressWarnings("unchecked")
public void finegrainedUpdatesDoNotConflict() throws Exception {
AtomicBoolean commitStatus = new AtomicBoolean(true);
Map<String, String> testMap = getMap();
Map<String, String> testMap2 = (Map<String, String>) instantiateCorfuObject(new TypeToken<SMRMap<String, String>>() {
}, "test stream");
t(1, () -> TXBegin());
t(1, () -> testMap.put("a", "a"));
t(1, () -> assertThat(testMap.put("a", "b")).isEqualTo("a"));
t(2, () -> TXBegin());
t(2, () -> testMap2.put("b", "f"));
t(2, () -> assertThat(testMap2.put("b", "g")).isEqualTo("f"));
t(2, () -> TXEnd());
t(1, () -> {
try {
TXEnd();
} catch (TransactionAbortedException te) {
commitStatus.set(false);
}
});
// verify that both transactions committed successfully
assertThat(commitStatus.get()).isTrue();
}
Aggregations