use of org.corfudb.runtime.CorfuRuntime 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.CorfuRuntime in project CorfuDB by CorfuDB.
the class AddressSpaceViewTest method ensureStripingReadAllWorks.
@Test
@SuppressWarnings("unchecked")
public void ensureStripingReadAllWorks() throws Exception {
addServer(SERVERS.PORT_0);
addServer(SERVERS.PORT_1);
addServer(SERVERS.PORT_2);
bootstrapAllServers(new TestLayoutBuilder().setEpoch(1L).addLayoutServer(SERVERS.PORT_0).addSequencer(SERVERS.PORT_0).buildSegment().buildStripe().addLogUnit(SERVERS.PORT_0).addToSegment().buildStripe().addLogUnit(SERVERS.PORT_1).addToSegment().buildStripe().addLogUnit(SERVERS.PORT_2).addToSegment().addToLayout().build());
//configure the layout accordingly
CorfuRuntime r = getRuntime().connect();
UUID streamA = UUID.nameUUIDFromBytes("stream A".getBytes());
byte[] testPayload = "hello world".getBytes();
final long ADDRESS_0 = 0;
final long ADDRESS_1 = 1;
final long ADDRESS_2 = 3;
Token token = new Token(ADDRESS_0, r.getLayoutView().getLayout().getEpoch());
r.getAddressSpaceView().write(token, testPayload);
assertThat(r.getAddressSpaceView().read(ADDRESS_0).getPayload(getRuntime())).isEqualTo("hello world".getBytes());
r.getAddressSpaceView().write(new Token(ADDRESS_1, r.getLayoutView().getLayout().getEpoch()), "1".getBytes());
r.getAddressSpaceView().write(new Token(ADDRESS_2, r.getLayoutView().getLayout().getEpoch()), "3".getBytes());
List<Long> rs = new ArrayList<>();
rs.add(ADDRESS_0);
rs.add(ADDRESS_1);
rs.add(ADDRESS_2);
Map<Long, ILogData> m = r.getAddressSpaceView().read(rs);
assertThat(m.get(ADDRESS_0).getPayload(getRuntime())).isEqualTo("hello world".getBytes());
assertThat(m.get(ADDRESS_1).getPayload(getRuntime())).isEqualTo("1".getBytes());
assertThat(m.get(ADDRESS_2).getPayload(getRuntime())).isEqualTo("3".getBytes());
}
use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.
the class AddressSpaceViewTest method ensureStripingWorks.
@Test
@SuppressWarnings("unchecked")
public void ensureStripingWorks() throws Exception {
addServer(SERVERS.PORT_0);
addServer(SERVERS.PORT_1);
addServer(SERVERS.PORT_2);
//configure the layout accordingly
bootstrapAllServers(new TestLayoutBuilder().setEpoch(1L).addLayoutServer(SERVERS.PORT_0).addSequencer(SERVERS.PORT_0).buildSegment().buildStripe().addLogUnit(SERVERS.PORT_0).addToSegment().buildStripe().addLogUnit(SERVERS.PORT_1).addToSegment().buildStripe().addLogUnit(SERVERS.PORT_2).addToSegment().addToLayout().build());
CorfuRuntime r = getRuntime().connect();
UUID streamA = UUID.nameUUIDFromBytes("stream A".getBytes());
byte[] testPayload = "hello world".getBytes();
final long epoch = r.getLayoutView().getLayout().getEpoch();
r.getAddressSpaceView().write(new TokenResponse(0, epoch, Collections.singletonMap(streamA, Address.NO_BACKPOINTER)), "hello world".getBytes());
assertThat(r.getAddressSpaceView().read(0L).getPayload(getRuntime())).isEqualTo("hello world".getBytes());
assertThat(r.getAddressSpaceView().read(0L).containsStream(streamA)).isTrue();
// Ensure that the data was written to each logunit.
LogUnitServerAssertions.assertThat(getLogUnit(SERVERS.PORT_0)).matchesDataAtAddress(0, testPayload);
LogUnitServerAssertions.assertThat(getLogUnit(SERVERS.PORT_1)).isEmptyAtAddress(0);
LogUnitServerAssertions.assertThat(getLogUnit(SERVERS.PORT_2)).isEmptyAtAddress(0);
r.getAddressSpaceView().write(new TokenResponse(1, epoch, Collections.singletonMap(streamA, Address.NO_BACKPOINTER)), "1".getBytes());
LogUnitServerAssertions.assertThat(getLogUnit(SERVERS.PORT_0)).matchesDataAtAddress(0, testPayload);
LogUnitServerAssertions.assertThat(getLogUnit(SERVERS.PORT_1)).matchesDataAtAddress(1, "1".getBytes());
LogUnitServerAssertions.assertThat(getLogUnit(SERVERS.PORT_2)).isEmptyAtAddress(0);
}
use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.
the class ChainReplicationViewTest method ensureAllUnitsContainData.
@Test
@SuppressWarnings("unchecked")
public void ensureAllUnitsContainData() throws Exception {
addServer(SERVERS.PORT_0);
addServer(SERVERS.PORT_1);
addServer(SERVERS.PORT_2);
bootstrapAllServers(new TestLayoutBuilder().addLayoutServer(SERVERS.PORT_0).addSequencer(SERVERS.PORT_0).buildSegment().setReplicationMode(Layout.ReplicationMode.CHAIN_REPLICATION).buildStripe().addLogUnit(SERVERS.PORT_0).addLogUnit(SERVERS.PORT_1).addLogUnit(SERVERS.PORT_2).addToSegment().addToLayout().build());
//configure the layout accordingly
CorfuRuntime r = getRuntime().connect();
UUID streamA = UUID.nameUUIDFromBytes("stream A".getBytes());
byte[] testPayload = "hello world".getBytes();
r.getAddressSpaceView().write(new TokenResponse(0, 0, Collections.singletonMap(streamA, Address.NO_BACKPOINTER)), testPayload);
assertThat(r.getAddressSpaceView().read(0L).getPayload(getRuntime())).isEqualTo("hello world".getBytes());
assertThat(r.getAddressSpaceView().read(0L).containsStream(streamA));
// Ensure that the data was written to each logunit.
assertThat(getLogUnit(SERVERS.PORT_0)).matchesDataAtAddress(0, testPayload);
assertThat(getLogUnit(SERVERS.PORT_1)).matchesDataAtAddress(0, testPayload);
assertThat(getLogUnit(SERVERS.PORT_2)).matchesDataAtAddress(0, testPayload);
}
use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.
the class ChainReplicationViewTest method canReadWriteToMultiple.
@Test
@SuppressWarnings("unchecked")
public void canReadWriteToMultiple() throws Exception {
addServer(SERVERS.PORT_0);
addServer(SERVERS.PORT_1);
addServer(SERVERS.PORT_2);
bootstrapAllServers(new TestLayoutBuilder().addLayoutServer(SERVERS.PORT_0).addSequencer(SERVERS.PORT_0).buildSegment().setReplicationMode(Layout.ReplicationMode.CHAIN_REPLICATION).buildStripe().addLogUnit(SERVERS.PORT_0).addLogUnit(SERVERS.PORT_1).addLogUnit(SERVERS.PORT_2).addToSegment().addToLayout().build());
//configure the layout accordingly
CorfuRuntime r = getRuntime().connect();
UUID streamA = UUID.nameUUIDFromBytes("stream A".getBytes());
byte[] testPayload = "hello world".getBytes();
r.getAddressSpaceView().write(new TokenResponse(0, runtime.getLayoutView().getLayout().getEpoch(), Collections.singletonMap(streamA, Address.NO_BACKPOINTER)), testPayload);
assertThat(r.getAddressSpaceView().read(0L).getPayload(getRuntime())).isEqualTo("hello world".getBytes());
assertThat(r.getAddressSpaceView().read(0L).containsStream(streamA)).isTrue();
}
Aggregations