Search in sources :

Example 1 with LayoutPrepareResponse

use of org.corfudb.protocols.wireprotocol.LayoutPrepareResponse 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

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 LayoutServer (org.corfudb.infrastructure.LayoutServer)1 LayoutPrepareResponse (org.corfudb.protocols.wireprotocol.LayoutPrepareResponse)1 CorfuRuntime (org.corfudb.runtime.CorfuRuntime)1 BaseClient (org.corfudb.runtime.clients.BaseClient)1 LayoutClient (org.corfudb.runtime.clients.LayoutClient)1 NettyClientRouter (org.corfudb.runtime.clients.NettyClientRouter)1 OutrankedException (org.corfudb.runtime.exceptions.OutrankedException)1 WrongEpochException (org.corfudb.runtime.exceptions.WrongEpochException)1 Layout (org.corfudb.runtime.view.Layout)1 Docopt (org.docopt.Docopt)1