Search in sources :

Example 1 with CorfuRuntime

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

the class PeriodicPollPolicyTest method pollingEnvironmentSetup.

@Before
public void pollingEnvironmentSetup() {
    addServer(SERVERS.PORT_0);
    addServer(SERVERS.PORT_1);
    addServer(SERVERS.PORT_2);
    layout = new TestLayoutBuilder().setEpoch(1L).addLayoutServer(SERVERS.PORT_0).addLayoutServer(SERVERS.PORT_1).addLayoutServer(SERVERS.PORT_2).addSequencer(SERVERS.PORT_0).addSequencer(SERVERS.PORT_1).addSequencer(SERVERS.PORT_2).buildSegment().buildStripe().addLogUnit(SERVERS.PORT_0).addLogUnit(SERVERS.PORT_1).addLogUnit(SERVERS.PORT_2).addToSegment().addToLayout().build();
    bootstrapAllServers(layout);
    getManagementServer(SERVERS.PORT_0).shutdown();
    getManagementServer(SERVERS.PORT_1).shutdown();
    getManagementServer(SERVERS.PORT_2).shutdown();
    corfuRuntime = new CorfuRuntime();
    layout.getLayoutServers().forEach(corfuRuntime::addLayoutServer);
    corfuRuntime.connect();
    layout.getAllServers().forEach(serverEndpoint -> {
        corfuRuntime.getRouter(serverEndpoint).setTimeoutConnect(PARAMETERS.TIMEOUT_VERY_SHORT.toMillis());
        corfuRuntime.getRouter(serverEndpoint).setTimeoutResponse(PARAMETERS.TIMEOUT_VERY_SHORT.toMillis());
        corfuRuntime.getRouter(serverEndpoint).setTimeoutRetry(PARAMETERS.TIMEOUT_VERY_SHORT.toMillis());
    });
    failureDetectorPolicy = new PeriodicPollPolicy();
}
Also used : CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Before(org.junit.Before)

Example 2 with CorfuRuntime

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

the class QuorumReplicationProtocolAdditionalTests method canReadWriteToMultiple.

@Test
@SuppressWarnings("unchecked")
public void canReadWriteToMultiple() throws Exception {
    //configure the layout accordingly
    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);
    assertThat(r.getAddressSpaceView().read(0L).getPayload(getRuntime())).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 : 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 3 with CorfuRuntime

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

the class QuorumReplicationProtocolAdditionalTests method checkRecoveryWriteTriggeredFromReadRecoversDataWhenTheQuorumIsLost.

@Test
@SuppressWarnings("unchecked")
public void checkRecoveryWriteTriggeredFromReadRecoversDataWhenTheQuorumIsLost() throws Exception {
    //configure the layout accordingly
    CorfuRuntime r = getDefaultRuntime();
    LogUnitServer u0 = getLogUnit(SERVERS.PORT_0);
    LogUnitServer u1 = getLogUnit(SERVERS.PORT_1);
    LogUnitServer u2 = getLogUnit(SERVERS.PORT_2);
    final long ADDRESS_0 = 0L;
    //write at 0
    ByteBuf b = Unpooled.buffer();
    Serializers.CORFU.serialize("0".getBytes(), b);
    WriteRequest m = WriteRequest.builder().writeMode(WriteMode.NORMAL).data(new LogData(DataType.DATA, b)).build();
    m.setGlobalAddress(ADDRESS_0);
    m.setRank(new IMetadata.DataRank(0));
    m.setBackpointerMap(Collections.emptyMap());
    sendMessage(u1, CorfuMsgType.WRITE.payloadMsg(m));
    sendMessage(u2, CorfuMsgType.WRITE.payloadMsg(m));
    u2.setShutdown(true);
    u2.shutdown();
    LogUnitServerAssertions.assertThat(u0).isEmptyAtAddress(ADDRESS_0);
    assertThat(r.getAddressSpaceView().read(0L).getPayload(getRuntime())).isEqualTo("0".getBytes());
    LogUnitServerAssertions.assertThat(u1).matchesDataAtAddress(ADDRESS_0, "0".getBytes());
    LogUnitServerAssertions.assertThat(u0).matchesDataAtAddress(ADDRESS_0, "0".getBytes());
}
Also used : ILogData(org.corfudb.protocols.wireprotocol.ILogData) LogData(org.corfudb.protocols.wireprotocol.LogData) IMetadata(org.corfudb.protocols.wireprotocol.IMetadata) WriteRequest(org.corfudb.protocols.wireprotocol.WriteRequest) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) LogUnitServer(org.corfudb.infrastructure.LogUnitServer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 4 with CorfuRuntime

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

the class QuorumReplicationProtocolAdditionalTests method canReadWriteConcurrent.

@Test
@SuppressWarnings("unchecked")
public void canReadWriteConcurrent() throws Exception {
    CorfuRuntime r = getDefaultRuntime();
    final int numberThreads = 5;
    final int numberRecords = 1_000;
    scheduleConcurrently(numberThreads, threadNumber -> {
        int base = threadNumber * numberRecords;
        for (int i = base; i < base + numberRecords; i++) {
            r.getAddressSpaceView().write(new TokenResponse((long) i, r.getLayoutView().getLayout().getEpoch(), Collections.singletonMap(CorfuRuntime.getStreamID("a"), Address.NO_BACKPOINTER)), Integer.toString(i).getBytes());
        }
    });
    executeScheduled(numberThreads, PARAMETERS.TIMEOUT_LONG);
    scheduleConcurrently(numberThreads, threadNumber -> {
        int base = threadNumber * numberRecords;
        for (int i = base; i < base + numberRecords; i++) {
            assertThat(r.getAddressSpaceView().read(i).getPayload(getRuntime())).isEqualTo(Integer.toString(i).getBytes());
        }
    });
    executeScheduled(numberThreads, PARAMETERS.TIMEOUT_LONG);
    assertNotNull(r.getAddressSpaceView().read(0L).getRank());
    assertThat((IMetadata.DataRank) r.getAddressSpaceView().read(0L).getMetadataMap().get(IMetadata.LogUnitMetadataType.RANK)).isNotNull();
}
Also used : TokenResponse(org.corfudb.protocols.wireprotocol.TokenResponse) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 5 with CorfuRuntime

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

the class QCLayout method main.

public static String[] main(String[] args) {
    if (args != null && args.length > 0 && args[0].contentEquals("reboot")) {
        LayoutServer ls = CorfuServer.getLayoutServer();
        if (ls != null) {
            ls.shutdown();
            CorfuServer.addLayoutServer();
            return replyOk();
        } else {
            return replyErr("No active layout server");
        }
    }
    // Parse the options given, using docopt.
    Map<String, Object> opts = new Docopt(USAGE).withVersion(GitRepositoryState.getRepositoryState().describe).parse(args);
    // Configure base options
    // configureBase(opts);
    // Parse host address and port
    String addressport = (String) opts.get("<address>:<port>");
    String host = addressport.split(":")[0];
    Integer port = Integer.parseInt(addressport.split(":")[1]);
    String qapp = (String) opts.get("<qapp>");
    String addressportPrefix = "";
    if (qapp != null) {
        addressportPrefix = qapp;
    }
    NettyClientRouter router;
    if ((router = routers.get(addressportPrefix + addressport)) == null) {
        // Create a client router and get layout.
        log.trace("Creating router for {} ++ {}:{}", addressportPrefix, port);
        router = new NettyClientRouter(host, port);
        router.addClient(new BaseClient()).addClient(new LayoutClient()).start();
        routers.putIfAbsent(addressportPrefix + addressport, router);
    }
    router = routers.get(addressportPrefix + addressport);
    Long epoch = 0L;
    if (opts.get("--epoch") != null) {
        epoch = Long.parseLong((String) opts.get("--epoch"));
        log.trace("Specify router's epoch as " + epoch);
        router.setEpoch(epoch);
    } else {
        try {
            Layout l = router.getClient(LayoutClient.class).getLayout().get();
            if (l != null) {
                log.trace("Set router's epoch to " + l.getEpoch());
                router.setEpoch(l.getEpoch());
            } else {
                log.trace("Cannot set router's epoch");
            }
        } catch (Exception e) {
            return replyErr("ERROR Exception getting initial epoch " + e.getCause());
        }
    }
    if ((Boolean) opts.get("getClientID")) {
        String clientID = router.getClientID().toString();
        return replyOk(clientID);
    } else if ((Boolean) opts.get("query")) {
        try {
            Layout l = router.getClient(LayoutClient.class).getLayout().get();
            Gson gs = new GsonBuilder().setPrettyPrinting().create();
            return replyOk("layout: " + gs.toJson(l));
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during query", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during query", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("ERROR Exception getting layout" + e);
        }
    } else if ((Boolean) opts.get("bootstrap")) {
        Layout l = getLayout(opts);
        log.debug("Bootstrapping with layout {}", l);
        try {
            if (router.getClient(LayoutClient.class).bootstrapLayout(l).get()) {
                router.getClient(ManagementClient.class).bootstrapManagement(l).get();
                return replyOk();
            } else {
                return replyErr("NACK");
            }
        } catch (ExecutionException ex) {
            return replyErr("Exception bootstrapping layout", ex.getCause().toString());
        } catch (Exception e) {
            return replyErr("Exception bootstrapping layout", e.toString());
        }
    } else if ((Boolean) opts.get("set_epoch")) {
        log.debug("Set epoch with new epoch={}", epoch);
        try {
            CorfuRuntime rt;
            if ((rt = setEpochRTs.get(addressport)) == null) {
                log.trace("Creating CorfuRuntime for set_epoch for {} ", addressport);
                rt = new CorfuRuntime().addLayoutServer(addressport);
                setEpochRTs.putIfAbsent(addressport, rt);
            }
            rt = setEpochRTs.get(addressport);
            // Construct a layout that contains just enough to allow .moveServersToEpoch()
            // to send SET_EPOCH to our desired endpoint.
            List<String> ls = new ArrayList(1);
            ls.add(addressport);
            List<String> none1 = new ArrayList(0);
            List<Layout.LayoutSegment> none2 = new ArrayList(0);
            Layout tmpLayout = new Layout(ls, none1, none2, epoch);
            tmpLayout.setRuntime(rt);
            tmpLayout.moveServersToEpoch();
            return replyOk();
        } catch (WrongEpochException we) {
            return replyErr("Exception during set_epoch", we.getCause() == null ? "WrongEpochException" : we.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(we));
        } catch (Exception e) {
            return replyErr("Exception during set_epoch", e.toString(), ExceptionUtils.getStackTrace(e));
        }
    } else if ((Boolean) opts.get("prepare")) {
        long rank = Long.parseLong((String) opts.get("--rank"));
        log.debug("Prepare with new rank={}", rank);
        try {
            LayoutPrepareResponse r = router.getClient(LayoutClient.class).prepare(epoch, rank).get();
            Layout r_layout = r.getLayout();
            if (r_layout == null) {
                return replyOk("ignored: ignored");
            } else {
                return replyOk("layout: " + r_layout.asJSONString());
            }
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == OutrankedException.class) {
                OutrankedException oe = (OutrankedException) ex.getCause();
                return replyErr("Exception during prepare", ex.getCause().toString(), "newRank: " + Long.toString(oe.getNewRank()), "layout: " + (oe.getLayout() == null ? "" : oe.getLayout().asJSONString()));
            } else if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during prepare", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during prepare", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("Exception during prepare", e.toString(), ExceptionUtils.getStackTrace(e));
        }
    } else if ((Boolean) opts.get("propose")) {
        long rank = Long.parseLong((String) opts.get("--rank"));
        Layout l = getLayout(opts);
        log.debug("Propose with new rank={}, layout={}", rank, l);
        try {
            if (router.getClient(LayoutClient.class).propose(l.getEpoch(), rank, l).get()) {
                return replyOk();
            } else {
                return replyErr("NACK");
            }
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == OutrankedException.class) {
                OutrankedException oe = (OutrankedException) ex.getCause();
                return replyErr("Exception during propose", ex.getCause().toString(), "newRank: " + Long.toString(oe.getNewRank()), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during propose", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during propose", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("Exception during propose", e.toString(), "stack: " + ExceptionUtils.getStackTrace(e));
        }
    } else if ((Boolean) opts.get("committed")) {
        long rank = Long.parseLong((String) opts.get("--rank"));
        Layout l = getLayout(opts);
        log.debug("Propose with new rank={}", rank);
        try {
            if (router.getClient(LayoutClient.class).committed(l.getEpoch(), l).get()) {
                return replyOk();
            } else {
                return replyErr("NACK");
            }
        } catch (ExecutionException ex) {
            if (ex.getCause().getClass() == WrongEpochException.class) {
                WrongEpochException we = (WrongEpochException) ex.getCause();
                return replyErr("Exception during commit", ex.getCause().toString(), "correctEpoch: " + we.getCorrectEpoch(), "stack: " + ExceptionUtils.getStackTrace(ex));
            } else {
                return replyErr("Exception during commit", ex.getCause().toString(), "stack: " + ExceptionUtils.getStackTrace(ex));
            }
        } catch (Exception e) {
            return replyErr("Exception during commit", e.toString(), "stack: " + ExceptionUtils.getStackTrace(e));
        }
    }
    return replyErr("Hush, compiler.");
}
Also used : NettyClientRouter(org.corfudb.runtime.clients.NettyClientRouter) GsonBuilder(com.google.gson.GsonBuilder) LayoutServer(org.corfudb.infrastructure.LayoutServer) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) BaseClient(org.corfudb.runtime.clients.BaseClient) LayoutClient(org.corfudb.runtime.clients.LayoutClient) OutrankedException(org.corfudb.runtime.exceptions.OutrankedException) ExecutionException(java.util.concurrent.ExecutionException) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) OutrankedException(org.corfudb.runtime.exceptions.OutrankedException) Docopt(org.docopt.Docopt) Layout(org.corfudb.runtime.view.Layout) LayoutPrepareResponse(org.corfudb.protocols.wireprotocol.LayoutPrepareResponse) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

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