Search in sources :

Example 11 with IStreamView

use of org.corfudb.runtime.view.stream.IStreamView in project CorfuDB by CorfuDB.

the class CmdletIT method testCorfuStreamCmdlet.

/**
     * Testing corfu_stream append and read.
     *
     * @throws Exception
     */
@Test
public void testCorfuStreamCmdlet() throws Exception {
    corfuServerProcess = new CorfuServerRunner().setPort(PORT).runServer();
    final String streamA = "streamA";
    CorfuRuntime runtime = createRuntime(ENDPOINT);
    IStreamView streamViewA = runtime.getStreamsView().get(CorfuRuntime.getStreamID(streamA));
    String payload1 = "Hello";
    streamViewA.append(payload1.getBytes());
    String commandRead = CORFU_PROJECT_DIR + "bin/corfu_stream -i " + streamA + " -c " + ENDPOINT + " read";
    String output = runCmdletGetOutput(commandRead);
    assertThat(output.contains(payload1)).isTrue();
    String payload2 = "World";
    String commandAppend = "echo '" + payload2 + "' | " + CORFU_PROJECT_DIR + "bin/corfu_stream -i " + streamA + " -c " + ENDPOINT + " append";
    runCmdletGetOutput(commandAppend);
    assertThat(streamViewA.next().getPayload(runtime)).isEqualTo(payload1.getBytes());
    assertThat(streamViewA.next().getPayload(runtime)).isEqualTo((payload2 + "\n").getBytes());
    assertThat(streamViewA.next()).isNull();
    shutdownCorfuServer(corfuServerProcess);
}
Also used : CorfuRuntime(org.corfudb.runtime.CorfuRuntime) IStreamView(org.corfudb.runtime.view.stream.IStreamView) Test(org.junit.Test)

Example 12 with IStreamView

use of org.corfudb.runtime.view.stream.IStreamView in project CorfuDB by CorfuDB.

the class StreamViewTest method canSeekOnStream.

@Test
public void canSeekOnStream() throws Exception {
    CorfuRuntime r = getDefaultRuntime().connect();
    IStreamView sv = r.getStreamsView().get(CorfuRuntime.getStreamID("stream  A"));
    // Append some entries
    sv.append("a".getBytes());
    sv.append("b".getBytes());
    sv.append("c".getBytes());
    // Try reading two entries
    assertThat(sv.next().getPayload(r)).isEqualTo("a".getBytes());
    assertThat(sv.next().getPayload(r)).isEqualTo("b".getBytes());
    // Seeking to the beginning
    sv.seek(0);
    assertThat(sv.next().getPayload(r)).isEqualTo("a".getBytes());
    // Seeking to the end
    sv.seek(2);
    assertThat(sv.next().getPayload(r)).isEqualTo("c".getBytes());
    // Seeking to the middle
    sv.seek(1);
    assertThat(sv.next().getPayload(r)).isEqualTo("b".getBytes());
}
Also used : CorfuRuntime(org.corfudb.runtime.CorfuRuntime) IStreamView(org.corfudb.runtime.view.stream.IStreamView) Test(org.junit.Test)

Example 13 with IStreamView

use of org.corfudb.runtime.view.stream.IStreamView in project CorfuDB by CorfuDB.

the class StreamViewTest method canFindInStream.

@Test
public void canFindInStream() throws Exception {
    CorfuRuntime r = getDefaultRuntime().connect();
    IStreamView svA = r.getStreamsView().get(CorfuRuntime.getStreamID("stream  A"));
    IStreamView svB = r.getStreamsView().get(CorfuRuntime.getStreamID("stream  B"));
    // Append some entries
    final long A_GLOBAL = 0;
    svA.append("a".getBytes());
    final long B_GLOBAL = 1;
    svB.append("b".getBytes());
    final long C_GLOBAL = 2;
    svA.append("c".getBytes());
    final long D_GLOBAL = 3;
    svB.append("d".getBytes());
    final long E_GLOBAL = 4;
    svA.append("e".getBytes());
    // See if we can find entries:
    // Should find entry "c"
    assertThat(svA.find(B_GLOBAL, IStreamView.SearchDirection.FORWARD)).isEqualTo(C_GLOBAL);
    // Should find entry "a"
    assertThat(svA.find(B_GLOBAL, IStreamView.SearchDirection.REVERSE)).isEqualTo(A_GLOBAL);
    // Should find entry "e"
    assertThat(svA.find(E_GLOBAL, IStreamView.SearchDirection.FORWARD_INCLUSIVE)).isEqualTo(E_GLOBAL);
    // Should find entry "c"
    assertThat(svA.find(C_GLOBAL, IStreamView.SearchDirection.REVERSE_INCLUSIVE)).isEqualTo(C_GLOBAL);
    // From existing to existing:
    // Should find entry "b"
    assertThat(svB.find(D_GLOBAL, IStreamView.SearchDirection.REVERSE)).isEqualTo(B_GLOBAL);
    // Should find entry "d"
    assertThat(svB.find(B_GLOBAL, IStreamView.SearchDirection.FORWARD)).isEqualTo(D_GLOBAL);
    // Bounds:
    assertThat(svB.find(D_GLOBAL, IStreamView.SearchDirection.FORWARD)).isEqualTo(Address.NOT_FOUND);
}
Also used : CorfuRuntime(org.corfudb.runtime.CorfuRuntime) IStreamView(org.corfudb.runtime.view.stream.IStreamView) Test(org.junit.Test)

Example 14 with IStreamView

use of org.corfudb.runtime.view.stream.IStreamView in project CorfuDB by CorfuDB.

the class StreamsViewTest method canCopyStream.

@Test
@SuppressWarnings("unchecked")
public void canCopyStream() throws Exception {
    //begin tests
    CorfuRuntime r = getDefaultRuntime().connect();
    UUID streamA = CorfuRuntime.getStreamID("stream A");
    UUID streamACopy = CorfuRuntime.getStreamID("stream A copy");
    byte[] testPayload = "hello world".getBytes();
    byte[] testPayloadCopy = "hello world copy".getBytes();
    IStreamView sv = r.getStreamsView().get(streamA);
    sv.append(testPayload);
    assertThat(sv.next().getPayload(getRuntime())).isEqualTo(testPayload);
    assertThat(sv.next()).isEqualTo(null);
    SequencerView sequencerView = r.getSequencerView();
    IStreamView svCopy = r.getStreamsView().copy(streamA, streamACopy, sequencerView.nextToken(Collections.singleton(sv.getID()), 0).getToken().getTokenValue());
    assertThat(svCopy.next().getPayload(getRuntime())).isEqualTo(testPayload);
    assertThat(svCopy.next()).isEqualTo(null);
    svCopy.append(testPayloadCopy);
    assertThat(svCopy.next().getPayload(getRuntime())).isEqualTo(testPayloadCopy);
    assertThat(svCopy.next()).isEqualTo(null);
    assertThat(sv.next()).isEqualTo(null);
}
Also used : CorfuRuntime(org.corfudb.runtime.CorfuRuntime) UUID(java.util.UUID) IStreamView(org.corfudb.runtime.view.stream.IStreamView) Test(org.junit.Test)

Example 15 with IStreamView

use of org.corfudb.runtime.view.stream.IStreamView 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)

Aggregations

IStreamView (org.corfudb.runtime.view.stream.IStreamView)19 Test (org.junit.Test)18 CorfuRuntime (org.corfudb.runtime.CorfuRuntime)11 UUID (java.util.UUID)10 MultiObjectSMREntry (org.corfudb.protocols.logprotocol.MultiObjectSMREntry)3 MultiSMREntry (org.corfudb.protocols.logprotocol.MultiSMREntry)3 SMREntry (org.corfudb.protocols.logprotocol.SMREntry)3 ILogData (org.corfudb.protocols.wireprotocol.ILogData)3 TypeToken (com.google.common.reflect.TypeToken)2 TestLayoutBuilder (org.corfudb.infrastructure.TestLayoutBuilder)2 TokenResponse (org.corfudb.protocols.wireprotocol.TokenResponse)2 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Semaphore (java.util.concurrent.Semaphore)1 LogUnitServer (org.corfudb.infrastructure.LogUnitServer)1 TestRule (org.corfudb.runtime.clients.TestRule)1 SMRMap (org.corfudb.runtime.collections.SMRMap)1 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)1