Search in sources :

Example 56 with TypeToken

use of com.google.common.reflect.TypeToken in project cdap by caskdata.

the class StreamHandlerTest method testSimpleStreamEnqueue.

@Test
public void testSimpleStreamEnqueue() throws Exception {
    // Create new stream.
    HttpURLConnection urlConn = openURL(createURL("streams/test_stream_enqueue"), HttpMethod.PUT);
    Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
    urlConn.disconnect();
    // Enqueue 10 entries
    for (int i = 0; i < 10; ++i) {
        urlConn = openURL(createURL("streams/test_stream_enqueue"), HttpMethod.POST);
        urlConn.setDoOutput(true);
        urlConn.addRequestProperty("test_stream_enqueue.header1", Integer.toString(i));
        urlConn.getOutputStream().write(Integer.toString(i).getBytes(Charsets.UTF_8));
        Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
        urlConn.disconnect();
    }
    // Fetch 10 entries
    urlConn = openURL(createURL("streams/test_stream_enqueue/events?limit=10"), HttpMethod.GET);
    List<StreamEvent> events = GSON.fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), new TypeToken<List<StreamEvent>>() {
    }.getType());
    for (int i = 0; i < 10; i++) {
        StreamEvent event = events.get(i);
        int actual = Integer.parseInt(Charsets.UTF_8.decode(event.getBody()).toString());
        Assert.assertEquals(i, actual);
        Assert.assertEquals(Integer.toString(i), event.getHeaders().get("header1"));
    }
    urlConn.disconnect();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TypeToken(com.google.common.reflect.TypeToken) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) Test(org.junit.Test)

Example 57 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class ObjectsViewTest method unrelatedStreamDoesNotConflict.

@Test
@SuppressWarnings("unchecked")
public void unrelatedStreamDoesNotConflict() throws Exception {
    //begin tests
    CorfuRuntime r = getDefaultRuntime();
    Map<String, String> smrMap = r.getObjectsView().build().setStreamName("map a").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    IStreamView streamB = r.getStreamsView().get(CorfuRuntime.getStreamID("b"));
    smrMap.put("a", "b");
    streamB.append(new SMREntry("hi", new Object[] { "hello" }, Serializers.PRIMITIVE));
    //this TX should not conflict
    assertThat(smrMap).doesNotContainKey("b");
    r.getObjectsView().TXBegin();
    String b = smrMap.get("a");
    smrMap.put("b", b);
    r.getObjectsView().TXEnd();
    assertThat(smrMap).containsEntry("b", "b");
}
Also used : MultiSMREntry(org.corfudb.protocols.logprotocol.MultiSMREntry) SMREntry(org.corfudb.protocols.logprotocol.SMREntry) 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 58 with TypeToken

use of com.google.common.reflect.TypeToken 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();
    }
}
Also used : TypeToken(com.google.common.reflect.TypeToken) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Semaphore(java.util.concurrent.Semaphore) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) Test(org.junit.Test)

Example 59 with TypeToken

use of com.google.common.reflect.TypeToken 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();
    }
}
Also used : Semaphore(java.util.concurrent.Semaphore) TypeToken(com.google.common.reflect.TypeToken) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) Test(org.junit.Test)

Example 60 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class UndoTest method ckMultiStreamRollback2.

/**
     * In this test, a variant of multi-stream interleaving is
     * tested.
     *
     * transactions are started on two threads, 1, 2.
     * Then two things happen:
     *
     * 1. some updates are committed
     * 2. then t2 is resumed and makes optimistic updates
     *
     *
     * then t1 resumes and should roll back both the optimistic updates
     * adn these commits.
     *
     * @throws Exception
     */
@Test
public void ckMultiStreamRollback2() throws Exception {
    ArrayList<Map> maps = new ArrayList<>();
    final int nmaps = 3;
    for (int i = 0; i < nmaps; i++) maps.add((SMRMap<Integer, String>) instantiateCorfuObject(new TypeToken<SMRMap<Integer, String>>() {
    }, "test stream" + i));
    // before t1 starts
    crossStream(maps, normalValue);
    // t1 starts transaction.
    // snapshot should include all the keys inserted above
    t(t1, () -> {
        WWTXBegin();
        // size() is called to make the TX obtains a snapshot at this point,
        maps.get(0).size();
    // and does not wait to lazily obtain it later, when it reads for
    // the first time
    });
    // t2 starts transaction.
    t(t2, () -> {
        WWTXBegin();
        // size() is called to make the TX obtains a snapshot at this point,
        maps.get(0).size();
    // and does not wait to lazily obtain it later, when it reads for
    // the first time
    });
    // t3 modifies everything
    t(t3, () -> crossStream(maps, specialValue));
    // now, t2 optimistically modifying everything, but
    // not yet committing
    t(t2, () -> {
        for (Map m : maps) m.put(specialKey, specialValue2);
    });
    // t1 should undo everything by t2 and by t3
    t(t1, () -> {
        for (Map m : maps) {
            assertThat(m.get(specialKey)).isEqualTo(normalValue);
            assertThat(m.get(specialKey + 1)).isEqualTo(normalValue);
        }
    });
    // main thread, t2's work should be committed
    for (Map m : maps) {
        assertThat(m.get(specialKey)).isEqualTo(specialValue);
        assertThat(m.get(specialKey + 1)).isEqualTo(specialValue);
    }
}
Also used : SMRMap(org.corfudb.runtime.collections.SMRMap) TypeToken(com.google.common.reflect.TypeToken) ArrayList(java.util.ArrayList) Map(java.util.Map) SMRMap(org.corfudb.runtime.collections.SMRMap) Test(org.junit.Test)

Aggregations

TypeToken (com.google.common.reflect.TypeToken)135 Test (org.junit.Test)60 HttpResponse (co.cask.common.http.HttpResponse)26 URL (java.net.URL)24 ServiceResponse (com.microsoft.rest.ServiceResponse)22 Response (retrofit2.Response)22 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)18 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)17 PipedInputStream (java.io.PipedInputStream)17 PipedOutputStream (java.io.PipedOutputStream)17 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)16 List (java.util.List)16 Map (java.util.Map)11 ImmutableList (com.google.common.collect.ImmutableList)9 Type (java.lang.reflect.Type)9 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)9 NotFoundException (co.cask.cdap.common.NotFoundException)8 VirtualMachineScaleSetVMInstanceIDs (com.microsoft.azure.management.compute.VirtualMachineScaleSetVMInstanceIDs)8 Gson (com.google.gson.Gson)7 JsonObject (com.google.gson.JsonObject)7