Search in sources :

Example 46 with CorfuRuntime

use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.

the class ChainReplicationProtocolTest method failedWriteIsPropagated.

/** Check to see that a writer correctly
     * completes a failed write from another client.
     */
@Test
public void failedWriteIsPropagated() throws Exception {
    setupNodes();
    //begin tests
    final CorfuRuntime r = getDefaultRuntime();
    final IReplicationProtocol rp = getProtocol();
    final Layout layout = r.getLayoutView().getLayout();
    LogData failedWrite = getLogData(0, "failed".getBytes());
    LogData incompleteWrite = getLogData(0, "incomplete".getBytes());
    // Write the incomplete write to the head of the chain
    r.getRouter(SERVERS.ENDPOINT_0).getClient(LogUnitClient.class).write(incompleteWrite);
    // Attempt to write using the replication protocol.
    // Should result in an overwrite exception
    assertThatThrownBy(() -> rp.write(layout, failedWrite)).isInstanceOf(OverwriteException.class);
    // At this point, a direct read of the tail should
    // reflect the -other- clients value
    ILogData readResult = r.getRouter(SERVERS.ENDPOINT_0).getClient(LogUnitClient.class).read(0).get().getReadSet().get(0L);
    assertThat(readResult.getPayload(r)).isEqualTo("incomplete".getBytes());
}
Also used : ILogData(org.corfudb.protocols.wireprotocol.ILogData) ILogData(org.corfudb.protocols.wireprotocol.ILogData) LogData(org.corfudb.protocols.wireprotocol.LogData) LogUnitClient(org.corfudb.runtime.clients.LogUnitClient) Layout(org.corfudb.runtime.view.Layout) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Test(org.junit.Test)

Example 47 with CorfuRuntime

use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.

the class QuorumReplicationProtocolAdditionalTests method canReadWrite.

@Test
@SuppressWarnings("unchecked")
public void canReadWrite() throws Exception {
    CorfuRuntime r = getDefaultRuntime();
    UUID streamA = UUID.nameUUIDFromBytes("stream A".getBytes());
    byte[] testPayload = "hello world".getBytes();
    r.getAddressSpaceView().write(new TokenResponse(0, r.getLayoutView().getLayout().getEpoch(), Collections.singletonMap(streamA, Address.NO_BACKPOINTER)), testPayload);
    ILogData x = r.getAddressSpaceView().read(0);
    assertNotNull(x.getRank());
    assertThat(r.getAddressSpaceView().read(0L).getPayload(r)).isEqualTo("hello world".getBytes());
    assertThat(r.getAddressSpaceView().read(0L).containsStream(streamA)).isTrue();
    assertThat((IMetadata.DataRank) r.getAddressSpaceView().read(0L).getMetadataMap().get(IMetadata.LogUnitMetadataType.RANK)).isNotNull();
}
Also used : ILogData(org.corfudb.protocols.wireprotocol.ILogData) TokenResponse(org.corfudb.protocols.wireprotocol.TokenResponse) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) UUID(java.util.UUID) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 48 with CorfuRuntime

use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.

the class ObjectsViewTest method canCopyObject.

@Test
@SuppressWarnings("unchecked")
public void canCopyObject() throws Exception {
    //begin tests
    CorfuRuntime r = getDefaultRuntime();
    Map<String, String> smrMap = r.getObjectsView().build().setStreamName("map a").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    smrMap.put("a", "a");
    Map<String, String> smrMapCopy = r.getObjectsView().copy(smrMap, "map a copy");
    smrMapCopy.put("b", "b");
    assertThat(smrMapCopy).containsEntry("a", "a").containsEntry("b", "b");
    assertThat(smrMap).containsEntry("a", "a").doesNotContainEntry("b", "b");
}
Also used : TypeToken(com.google.common.reflect.TypeToken) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Test(org.junit.Test)

Example 49 with CorfuRuntime

use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.

the class ObjectsViewTest method abortedTransactionDoesNotConflict.

@Test
@SuppressWarnings("unchecked")
public void abortedTransactionDoesNotConflict() throws Exception {
    final String mapA = "map a";
    //Enbale transaction logging
    CorfuRuntime r = getDefaultRuntime().setTransactionLogging(true);
    SMRMap<String, String> map = getDefaultRuntime().getObjectsView().build().setStreamName(mapA).setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    // TODO: fix so this does not require mapCopy.
    SMRMap<String, String> mapCopy = getDefaultRuntime().getObjectsView().build().setStreamName(mapA).setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).addOption(ObjectOpenOptions.NO_CACHE).open();
    map.put("initial", "value");
    Semaphore s1 = new Semaphore(0);
    Semaphore s2 = new Semaphore(0);
    // Schedule two threads, the first starts a transaction and reads,
    // then waits for the second thread to finish.
    // the second starts a transaction, waits for the first tx to read
    // and commits.
    // The first thread then resumes and attempts to commit. It should abort.
    scheduleConcurrently(1, t -> {
        assertThatThrownBy(() -> {
            getRuntime().getObjectsView().TXBegin();
            map.get("k");
            s1.release();
            s2.acquire();
            map.put("k", "v1");
            getRuntime().getObjectsView().TXEnd();
        }).isInstanceOf(TransactionAbortedException.class);
    });
    scheduleConcurrently(1, t -> {
        s1.acquire();
        getRuntime().getObjectsView().TXBegin();
        mapCopy.put("k", "v2");
        getRuntime().getObjectsView().TXEnd();
        s2.release();
    });
    executeScheduled(2, PARAMETERS.TIMEOUT_LONG);
    // The result should contain T2s modification.
    assertThat(map).containsEntry("k", "v2");
    IStreamView txStream = r.getStreamsView().get(ObjectsView.TRANSACTION_STREAM_ID);
    List<ILogData> txns = txStream.remainingUpTo(Long.MAX_VALUE);
    assertThat(txns).hasSize(1);
    assertThat(txns.get(0).getLogEntry(getRuntime()).getType()).isEqualTo(LogEntry.LogEntryType.MULTIOBJSMR);
    MultiObjectSMREntry tx1 = (MultiObjectSMREntry) txns.get(0).getLogEntry(getRuntime());
    MultiSMREntry entryMap = tx1.getEntryMap().get(CorfuRuntime.getStreamID(mapA));
    assertThat(entryMap).isNotNull();
    assertThat(entryMap.getUpdates().size()).isEqualTo(1);
    SMREntry smrEntry = entryMap.getUpdates().get(0);
    Object[] args = smrEntry.getSMRArguments();
    assertThat(smrEntry.getSMRMethod()).isEqualTo("put");
    assertThat((String) args[0]).isEqualTo("k");
    assertThat((String) args[1]).isEqualTo("v2");
}
Also used : ILogData(org.corfudb.protocols.wireprotocol.ILogData) SMRMap(org.corfudb.runtime.collections.SMRMap) Semaphore(java.util.concurrent.Semaphore) MultiSMREntry(org.corfudb.protocols.logprotocol.MultiSMREntry) MultiSMREntry(org.corfudb.protocols.logprotocol.MultiSMREntry) SMREntry(org.corfudb.protocols.logprotocol.SMREntry) MultiObjectSMREntry(org.corfudb.protocols.logprotocol.MultiObjectSMREntry) MultiObjectSMREntry(org.corfudb.protocols.logprotocol.MultiObjectSMREntry) TypeToken(com.google.common.reflect.TypeToken) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) IStreamView(org.corfudb.runtime.view.stream.IStreamView) Test(org.junit.Test)

Example 50 with CorfuRuntime

use of org.corfudb.runtime.CorfuRuntime in project CorfuDB by CorfuDB.

the class ObjectsViewTest method unrelatedTransactionDoesNotConflict.

@Test
@SuppressWarnings("unchecked")
public void unrelatedTransactionDoesNotConflict() throws Exception {
    //begin tests
    CorfuRuntime r = getDefaultRuntime();
    Map<String, String> smrMap = r.getObjectsView().build().setStreamName("map a").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    Map<String, String> smrMapB = r.getObjectsView().build().setStreamName("map b").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    smrMap.put("a", "b");
    r.getObjectsView().TXBegin();
    String b = smrMap.get("a");
    smrMapB.put("b", b);
    r.getObjectsView().TXEnd();
    //this TX should not conflict
    assertThat(smrMap).doesNotContainKey("b");
    r.getObjectsView().TXBegin();
    b = smrMap.get("a");
    smrMap.put("b", b);
    r.getObjectsView().TXEnd();
    assertThat(smrMap).containsEntry("b", "b");
}
Also used : TypeToken(com.google.common.reflect.TypeToken) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Test(org.junit.Test)

Aggregations

CorfuRuntime (org.corfudb.runtime.CorfuRuntime)68 Test (org.junit.Test)56 UUID (java.util.UUID)18 ILogData (org.corfudb.protocols.wireprotocol.ILogData)13 IStreamView (org.corfudb.runtime.view.stream.IStreamView)13 TestLayoutBuilder (org.corfudb.infrastructure.TestLayoutBuilder)12 TokenResponse (org.corfudb.protocols.wireprotocol.TokenResponse)11 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)11 Layout (org.corfudb.runtime.view.Layout)10 TypeToken (com.google.common.reflect.TypeToken)9 LogData (org.corfudb.protocols.wireprotocol.LogData)9 TransactionAbortedException (org.corfudb.runtime.exceptions.TransactionAbortedException)6 Semaphore (java.util.concurrent.Semaphore)5 TestRule (org.corfudb.runtime.clients.TestRule)5 SMRMap (org.corfudb.runtime.collections.SMRMap)5 Map (java.util.Map)4 Token (org.corfudb.protocols.wireprotocol.Token)4 Collections (java.util.Collections)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3