Search in sources :

Example 56 with CorfuRuntime

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

the class LogUnitClientTest method valueCanBeAdopted.

@Test
public void valueCanBeAdopted() throws ExecutionException, InterruptedException {
    byte[] testString = "hello world".getBytes();
    client.write(0, Collections.<UUID>emptySet(), new IMetadata.DataRank(1), testString, Collections.emptyMap()).get();
    LogData r = client.read(0).get().getReadSet().get(0L);
    assertThat(r.getType()).isEqualTo(DataType.DATA);
    assertThat(r.getPayload(new CorfuRuntime())).isEqualTo(testString);
    try {
        ILogData data = createEmptyData(0, DataType.RANK_ONLY, new IMetadata.DataRank(2)).getSerialized();
        client.write(data).get();
        fail();
    } catch (Exception e) {
        // expected
        assertEquals(ValueAdoptedException.class, e.getCause().getClass());
        ValueAdoptedException ex = (ValueAdoptedException) e.getCause();
        ReadResponse read = ex.getReadResponse();
        LogData log = read.getReadSet().get(0l);
        assertThat(log.getType()).isEqualTo(DataType.DATA);
        assertThat(log.getPayload(new CorfuRuntime())).isEqualTo(testString);
        ;
    }
    r = client.read(0).get().getReadSet().get(0L);
    assertThat(r.getType()).isEqualTo(DataType.DATA);
    assertThat(r.getPayload(new CorfuRuntime())).isEqualTo(testString);
}
Also used : ILogData(org.corfudb.protocols.wireprotocol.ILogData) ILogData(org.corfudb.protocols.wireprotocol.ILogData) LogData(org.corfudb.protocols.wireprotocol.LogData) ReadResponse(org.corfudb.protocols.wireprotocol.ReadResponse) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) UUID(java.util.UUID) DataCorruptionException(org.corfudb.runtime.exceptions.DataCorruptionException) OverwriteException(org.corfudb.runtime.exceptions.OverwriteException) TrimmedException(org.corfudb.runtime.exceptions.TrimmedException) DataOutrankedException(org.corfudb.runtime.exceptions.DataOutrankedException) ValueAdoptedException(org.corfudb.runtime.exceptions.ValueAdoptedException) ExecutionException(java.util.concurrent.ExecutionException) ValueAdoptedException(org.corfudb.runtime.exceptions.ValueAdoptedException) Test(org.junit.Test)

Example 57 with CorfuRuntime

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

the class ChainReplicationViewTest method canReadWriteToSingle.

@Test
@SuppressWarnings("unchecked")
public void canReadWriteToSingle() throws Exception {
    CorfuRuntime r = getDefaultRuntime();
    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();
}
Also used : TokenResponse(org.corfudb.protocols.wireprotocol.TokenResponse) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) UUID(java.util.UUID) Test(org.junit.Test)

Example 58 with CorfuRuntime

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

the class HelloCorfu method main.

public static void main(String[] args) {
    // Parse the options given, using docopt.
    Map<String, Object> opts = new Docopt(USAGE).withVersion(GitRepositoryState.getRepositoryState().describe).parse(args);
    String corfuConfigurationString = (String) opts.get("-c");
    /**
         * First, the application needs to instantiate a CorfuRuntime,
         * which is a Java object that contains all of the Corfu utilities exposed to applications.
         */
    CorfuRuntime runtime = getRuntimeAndConnect(corfuConfigurationString);
    /**
         * Obviously, this application is not doing much yet,
         * but you can already invoke getRuntimeAndConnect to test if you can connect to a deployed Corfu service.
         *
         * Above, you will need to point it to a host and port which is running the service.
         * See {@link https://github.com/CorfuDB/CorfuDB} for instructions on how to deploy Corfu.
         */
    /**
         * Next, we will illustrate how to declare a Java object backed by a Corfu Stream.
         * A Corfu Stream is a log dedicated specifically to the history of updates of one object.
         * We will instantiate a stream by giving it a name "A",
         * and then instantiate an object by specifying its class
         */
    Map<String, Integer> map = runtime.getObjectsView().build().setStreamName(// stream name
    "A").setType(// object class backed by this stream
    SMRMap.class).open();
    /**
         * The magic has aleady happened! mapis an in-memory view of a shared map, backed by the Corfu log.
         * The application can perform put and get on this map from different application instances,
         * crash and restart applications, and so on.
         * The map will persist and be consistent across all applications.
         *
         * For example, try the following code repeatedly in a sequence, in between run/exit,
         * from multiple instances, and see the different interleaving of values that result.
         */
    Integer previous = map.get("a");
    if (previous == null) {
        System.out.println("This is the first time we were run!");
        map.put("a", 1);
    } else {
        map.put("a", ++previous);
        System.out.println("This is the " + previous + " time we were run!");
    }
}
Also used : SMRMap(org.corfudb.runtime.collections.SMRMap) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Docopt(org.docopt.Docopt)

Example 59 with CorfuRuntime

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

the class ServerRestartIT method runSingleNodeRecoveryTransactionalClient.

private void runSingleNodeRecoveryTransactionalClient(boolean nested) throws Exception {
    try {
        // Total number of maps (streams) to write to.
        final int MAPS = 5;
        final int MAX_LIMIT_KEY_RANGE_PRE_SHUTDOWN = 20;
        final int MIN_LIMIT_KEY_RANGE_DURING_SHUTDOWN = 30;
        final int MAX_LIMIT_KEY_RANGE_DURING_SHUTDOWN = 60;
        final int MIN_LIMIT_KEY_RANGE_POST_SHUTDOWN = 100;
        final int MAX_LIMIT_KEY_RANGE_POST_SHUTDOWN = 200;
        final int CLIENT_DELAY_POST_SHUTDOWN = 50;
        // Run CORFU Server. Expect slight delay until server is running.
        System.out.println("Start Corfu Server");
        final Process corfuServerProcess = runCorfuServer();
        // Delay (time to start)
        Thread.sleep(PARAMETERS.TIMEOUT_NORMAL.toMillis());
        assertThat(corfuServerProcess.isAlive()).isTrue();
        // Initialize Client: Create Runtime (Client)
        CorfuRuntime runtime = createDefaultRuntime();
        // Create Maps
        List<Map<String, Integer>> smrMapList = new ArrayList<>();
        List<Map<String, Integer>> expectedMapList = new ArrayList<>();
        for (int i = 0; i < MAPS; i++) {
            smrMapList.add(createMap(runtime, Integer.toString(i)));
            Map<String, Integer> expectedMap = new HashMap<>();
            expectedMapList.add(expectedMap);
        }
        // Execute Transactions (while Corfu Server RUNNING)
        for (int i = 0; i < ITERATIONS; i++) {
            assertThat(executeTransaction(runtime, smrMapList, expectedMapList, 0, MAX_LIMIT_KEY_RANGE_PRE_SHUTDOWN, nested)).isTrue();
        }
        // ShutDown (STOP) CORFU Server
        System.out.println("Shutdown Corfu Server");
        assertThat(shutdownCorfuServer(corfuServerProcess)).isTrue();
        // Execute Transactions (once Corfu Server Shutdown)
        for (int i = 0; i < ITERATIONS; i++) {
            assertThat(executeTransaction(runtime, smrMapList, expectedMapList, MIN_LIMIT_KEY_RANGE_DURING_SHUTDOWN, MAX_LIMIT_KEY_RANGE_DURING_SHUTDOWN, nested)).isFalse();
        }
        // Restart Corfu Server
        System.out.println("Restart Corfu Server");
        Process corfuServerProcessRestart = runCorfuServer();
        // Delay (time to restart)
        Thread.sleep(PARAMETERS.TIMEOUT_NORMAL.toMillis());
        assertThat(corfuServerProcessRestart.isAlive()).isTrue();
        // Execute Transactions (once Corfu Server was restarted)
        for (int i = 0; i < ITERATIONS; i++) {
            assertThat(executeTransaction(runtime, smrMapList, expectedMapList, MIN_LIMIT_KEY_RANGE_POST_SHUTDOWN, MAX_LIMIT_KEY_RANGE_POST_SHUTDOWN, nested)).isTrue();
        }
        // Verify Correctness
        // Note: by triggering this from a separate thread we guarantee that we can catch any potential problems
        // related to transactional contexts not being removed for current thread.
        ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
        ScheduledFuture<Boolean> future = exec.schedule(new Callable<Boolean>() {

            public Boolean call() {
                for (int i = 0; i < expectedMapList.size(); i++) {
                    Map<String, Integer> expectedMap = expectedMapList.get(i);
                    for (Map.Entry<String, Integer> entry : expectedMap.entrySet()) {
                        if (smrMapList.get(i).get(entry.getKey()) != null) {
                            if (!(smrMapList.get(i).get(entry.getKey()).equals(entry.getValue().intValue()))) {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }
                }
                return true;
            }
        }, CLIENT_DELAY_POST_SHUTDOWN, TimeUnit.MILLISECONDS);
        // Wait for Executor to Finish
        exec.shutdown();
        try {
            exec.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Data Correctness Validation
        assertThat(future.get()).isTrue();
        // ShutDown the server before exiting
        System.out.println("Shutdown Corfu Server");
        assertThat(shutdownCorfuServer(corfuServerProcessRestart)).isTrue();
    } catch (Exception e) {
        throw e;
    }
}
Also used : NetworkException(org.corfudb.runtime.exceptions.NetworkException) IOException(java.io.IOException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) CorfuRuntime(org.corfudb.runtime.CorfuRuntime)

Example 60 with CorfuRuntime

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

the class ServerRestartIT method testRandomizedRecovery.

/**
     * Randomized tests with mixed client and server failovers.
     *
     * @throws Exception
     */
@Test
public void testRandomizedRecovery() throws Exception {
    // Total percentage.
    final int TOTAL_PERCENTAGE = 100;
    // Number of maps or streams to test recovery on.
    final int MAPS = 3;
    // Number of insertions in map in each iteration.
    final int INSERTIONS = 100;
    // Number of keys to be used throughout the test in each map.
    final int KEYS = 20;
    // Logs the server and client state in each iteration with the
    // maps used and keys and values inserted in each iteration.
    final boolean TEST_SEQUENCE_LOGGING = true;
    final File testSequenceLogFile = new File(TEST_SEQUENCE_LOG_PATH);
    if (!testSequenceLogFile.exists()) {
        testSequenceLogFile.createNewFile();
    }
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    // Keep this print at all times to reproduce any failed test.
    final Random randomSeed = new Random();
    final long SEED = randomSeed.nextLong();
    final Random rand = new Random(SEED);
    System.out.println("SEED = " + SEED);
    // Runs the corfu server. Expect slight delay until server is running.
    Process corfuServerProcess = runCorfuServer();
    // List of runtimes to free resources when not needed.
    List<CorfuRuntime> runtimeList = new ArrayList<>();
    List<Map<String, Integer>> smrMapList = new ArrayList<>();
    for (int i = 0; i < MAPS; i++) {
        final int ii = i;
        Future<Boolean> future = executorService.submit(() -> {
            CorfuRuntime runtime = createDefaultRuntime();
            runtimeList.add(runtime);
            smrMapList.add(createMap(runtime, Integer.toString(ii)));
            return true;
        });
        future.get(PARAMETERS.TIMEOUT_LONG.toMillis(), TimeUnit.MILLISECONDS);
    }
    List<Map<String, Integer>> expectedMapList = new ArrayList<>();
    for (int i = 0; i < MAPS; i++) {
        expectedMapList.add(new HashMap<>());
    }
    try (final FileOutputStream fos = new FileOutputStream(testSequenceLogFile)) {
        for (int i = 0; i < ITERATIONS; i++) {
            System.out.println("Iteration #" + i);
            boolean serverRestart = rand.nextInt(TOTAL_PERCENTAGE) < SERVER_RESTART_PERCENTAGE;
            boolean clientRestart = rand.nextInt(TOTAL_PERCENTAGE) < CLIENT_RESTART_PERCENTAGE;
            if (TEST_SEQUENCE_LOGGING) {
                fos.write(getRestartStateRecord(i, serverRestart, clientRestart).getBytes());
            }
            if (clientRestart) {
                smrMapList.clear();
                runtimeList.forEach(CorfuRuntime::shutdown);
                for (int j = 0; j < MAPS; j++) {
                    final int jj = j;
                    Future<Boolean> future = executorService.submit(() -> {
                        CorfuRuntime runtime = createDefaultRuntime();
                        runtimeList.add(runtime);
                        smrMapList.add(createMap(runtime, Integer.toString(jj)));
                        return true;
                    });
                    future.get(PARAMETERS.TIMEOUT_LONG.toMillis(), TimeUnit.MILLISECONDS);
                }
            }
            // Map assertions
            while (true) {
                try {
                    if (i != 0) {
                        for (int j = 0; j < MAPS; j++) {
                            assertThat(smrMapList.get(j)).isEqualTo(expectedMapList.get(j));
                        }
                    }
                    break;
                } catch (NetworkException ne) {
                    Thread.sleep(PARAMETERS.TIMEOUT_SHORT.toMillis());
                }
            }
            // Map insertions
            for (int j = 0; j < INSERTIONS; j++) {
                int value = rand.nextInt();
                int map = rand.nextInt(MAPS);
                String key = Integer.toString(rand.nextInt(KEYS));
                smrMapList.get(map).put(key, value);
                expectedMapList.get(map).put(key, value);
                if (TEST_SEQUENCE_LOGGING) {
                    fos.write(getMapInsertion(i, map, key, value).getBytes());
                }
            }
            if (TEST_SEQUENCE_LOGGING) {
                fos.write(getMapStateRecord(i, expectedMapList).getBytes());
            }
            if (serverRestart) {
                assertThat(shutdownCorfuServer(corfuServerProcess)).isTrue();
                corfuServerProcess = runCorfuServer();
            }
        }
        assertThat(shutdownCorfuServer(corfuServerProcess)).isTrue();
    } catch (Exception e) {
        throw e;
    }
}
Also used : NetworkException(org.corfudb.runtime.exceptions.NetworkException) IOException(java.io.IOException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) FileOutputStream(java.io.FileOutputStream) File(java.io.File) NetworkException(org.corfudb.runtime.exceptions.NetworkException) 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