Search in sources :

Example 1 with WrongEpochException

use of org.corfudb.runtime.exceptions.WrongEpochException 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)

Example 2 with WrongEpochException

use of org.corfudb.runtime.exceptions.WrongEpochException in project CorfuDB by CorfuDB.

the class NettyClientRouter method validateEpochAndClientID.

/**
     * Validate the epoch of a CorfuMsg, and send a WRONG_EPOCH response if
     * the server is in the wrong epoch. Ignored if the message type is reset (which
     * is valid in any epoch).
     *
     * @param msg The incoming message to validate.
     * @param ctx The context of the channel handler.
     * @return True, if the epoch is correct, but false otherwise.
     */
private boolean validateEpochAndClientID(CorfuMsg msg, ChannelHandlerContext ctx) {
    // Check if the message is intended for us. If not, drop the message.
    if (!msg.getClientID().equals(clientID)) {
        log.warn("Incoming message intended for client {}, our id is {}, dropping!", msg.getClientID(), clientID);
        return false;
    }
    // Check if the message is in the right epoch.
    if (!msg.getMsgType().ignoreEpoch && msg.getEpoch() != epoch) {
        log.trace("Incoming message with wrong epoch, got {}, expected {}, message was: {}", msg.getEpoch(), epoch, msg);
        /* If this message was pending a completion, complete it with an error. */
        completeExceptionally(msg.getRequestID(), new WrongEpochException(msg.getEpoch()));
        return false;
    }
    return true;
}
Also used : WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException)

Example 3 with WrongEpochException

use of org.corfudb.runtime.exceptions.WrongEpochException in project CorfuDB by CorfuDB.

the class AbstractView method layoutHelper.

/**
     * Helper function for view to retrieve layouts.
     * This function will retry the given function indefinitely, invalidating the view if there was a exception
     * contacting the endpoint.
     *
     * @param function The function to execute.
     * @param <T>      The return type of the function.
     * @param <A>      Any exception the function may throw.
     * @param <B>      Any exception the function may throw.
     * @param <C>      Any exception the function may throw.
     * @param <D>      Any exception the function may throw.
     * @return The return value of the function.
     */
public <T, A extends RuntimeException, B extends RuntimeException, C extends RuntimeException, D extends RuntimeException> T layoutHelper(LayoutFunction<Layout, T, A, B, C, D> function) throws A, B, C, D {
    while (true) {
        try {
            return function.apply(runtime.layout.get());
        } catch (RuntimeException re) {
            if (re.getCause() instanceof TimeoutException) {
                log.warn("Timeout executing remote call, invalidating view and retrying in {}s", runtime.retryRate);
                runtime.invalidateLayout();
                try {
                    Thread.sleep(runtime.retryRate * 1000);
                } catch (InterruptedException ie) {
                }
            } else if (re instanceof WrongEpochException) {
                WrongEpochException we = (WrongEpochException) re;
                log.warn("Got a wrong epoch exception, updating epoch to {} and invalidate view", we.getCorrectEpoch());
                Long newEpoch = (we.getCorrectEpoch());
                runtime.nodeRouters.values().forEach(x -> x.setEpoch(newEpoch));
                runtime.invalidateLayout();
            } else {
                throw re;
            }
        } catch (InterruptedException | ExecutionException ex) {
            log.warn("Error executing remote call, invalidating view and retrying in {}s", runtime.retryRate, ex);
            runtime.invalidateLayout();
            try {
                Thread.sleep(runtime.retryRate * 1000);
            } catch (InterruptedException ie) {
            }
        }
    }
}
Also used : WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with WrongEpochException

use of org.corfudb.runtime.exceptions.WrongEpochException in project CorfuDB by CorfuDB.

the class AddressSpaceView method write.

/** Write the given log data using a token, returning
     * either when the write has been completed successfully,
     * or throwing an OverwriteException if another value
     * has been adopted, or a WrongEpochException if the
     * token epoch is invalid.
     *
     * @param token     The token to use for the write.
     * @param data      The data to write.
     * @throws OverwriteException   If the globalAddress given
     *                              by the token has adopted
     *                              another value.
     * @throws WrongEpochException  If the token epoch is invalid.
     */
public void write(IToken token, Object data) throws OverwriteException {
    final ILogData ld = new LogData(DataType.DATA, data);
    layoutHelper(l -> {
        if (token.getEpoch() != l.getEpoch()) {
            throw new WrongEpochException(l.getEpoch());
        }
        ld.useToken(token);
        l.getReplicationMode(token.getTokenValue()).getReplicationProtocol(runtime).write(l, ld);
        return null;
    });
    // Cache the successful write
    if (!runtime.isCacheDisabled()) {
        readCache.put(token.getTokenValue(), ld);
    }
}
Also used : ILogData(org.corfudb.protocols.wireprotocol.ILogData) ILogData(org.corfudb.protocols.wireprotocol.ILogData) LogData(org.corfudb.protocols.wireprotocol.LogData) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException)

Example 5 with WrongEpochException

use of org.corfudb.runtime.exceptions.WrongEpochException in project CorfuDB by CorfuDB.

the class TestClientRouter method validateEpochAndClientID.

/**
     * Validate the epoch of a CorfuMsg, and send a WRONG_EPOCH response if
     * the server is in the wrong epoch. Ignored if the message type is reset (which
     * is valid in any epoch).
     *
     * @param msg The incoming message to validate.
     * @param ctx The context of the channel handler.
     * @return True, if the epoch is correct, but false otherwise.
     */
public boolean validateEpochAndClientID(CorfuMsg msg, ChannelHandlerContext ctx) {
    // Check if the message is intended for us. If not, drop the message.
    if (!msg.getClientID().equals(clientID)) {
        log.warn("Incoming message intended for client {}, our id is {}, dropping!", msg.getClientID(), clientID);
        return false;
    }
    // Check if the message is in the right epoch.
    if (!msg.getMsgType().ignoreEpoch && msg.getEpoch() != getEpoch()) {
        CorfuMsg m = new CorfuMsg();
        log.trace("Incoming message with wrong epoch, got {}, expected {}, message was: {}", msg.getEpoch(), getEpoch(), msg);
        /* If this message was pending a completion, complete it with an error. */
        completeExceptionally(msg.getRequestID(), new WrongEpochException(getEpoch()));
        return false;
    }
    return true;
}
Also used : CorfuMsg(org.corfudb.protocols.wireprotocol.CorfuMsg) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException)

Aggregations

WrongEpochException (org.corfudb.runtime.exceptions.WrongEpochException)5 ExecutionException (java.util.concurrent.ExecutionException)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 LayoutServer (org.corfudb.infrastructure.LayoutServer)1 CorfuMsg (org.corfudb.protocols.wireprotocol.CorfuMsg)1 ILogData (org.corfudb.protocols.wireprotocol.ILogData)1 LayoutPrepareResponse (org.corfudb.protocols.wireprotocol.LayoutPrepareResponse)1 LogData (org.corfudb.protocols.wireprotocol.LogData)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 Layout (org.corfudb.runtime.view.Layout)1 Docopt (org.docopt.Docopt)1