Search in sources :

Example 1 with Docopt

use of org.docopt.Docopt 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 Docopt

use of org.docopt.Docopt in project CorfuDB by CorfuDB.

the class CorfuServer method main.

public static void main(String[] args) {
    serverRunning = true;
    // Parse the options given, using docopt.
    Map<String, Object> opts = new Docopt(USAGE).withVersion(GitRepositoryState.getRepositoryState().describe).parse(args);
    int port = Integer.parseInt((String) opts.get("<port>"));
    // Print a nice welcome message.
    AnsiConsole.systemInstall();
    printLogo();
    System.out.println(ansi().a("Welcome to ").fg(RED).a("CORFU ").fg(MAGENTA).a("SERVER").reset());
    System.out.println(ansi().a("Version ").a(Version.getVersionString()).a(" (").fg(BLUE).a(GitRepositoryState.getRepositoryState().commitIdAbbrev).reset().a(")"));
    System.out.println(ansi().a("Serving on port ").fg(WHITE).a(port).reset());
    System.out.println(ansi().a("Service directory: ").fg(WHITE).a((Boolean) opts.get("--memory") ? "MEMORY mode" : opts.get("--log-path")).reset());
    // Pick the correct logging level before outputting error messages.
    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    switch((String) opts.get("--log-level")) {
        case "ERROR":
            root.setLevel(Level.ERROR);
            break;
        case "WARN":
            root.setLevel(Level.WARN);
            break;
        case "INFO":
            root.setLevel(Level.INFO);
            break;
        case "DEBUG":
            root.setLevel(Level.DEBUG);
            break;
        case "TRACE":
            root.setLevel(Level.TRACE);
            break;
        default:
            root.setLevel(Level.INFO);
            log.warn("Level {} not recognized, defaulting to level INFO", opts.get("--log-level"));
    }
    log.debug("Started with arguments: " + opts);
    // Create the service directory if it does not exist.
    if (!(Boolean) opts.get("--memory")) {
        File serviceDir = new File((String) opts.get("--log-path"));
        if (!serviceDir.exists()) {
            if (serviceDir.mkdirs()) {
                log.info("Created new service directory at {}.", serviceDir);
            }
        } else if (!serviceDir.isDirectory()) {
            log.error("Service directory {} does not point to a directory. Aborting.", serviceDir);
            throw new RuntimeException("Service directory must be a directory!");
        }
    }
    // Now, we start the Netty router, and have it route to the correct port.
    router = new NettyServerRouter(opts);
    // Create a common Server Context for all servers to access.
    serverContext = new ServerContext(opts, router);
    // Add each role to the router.
    addSequencer();
    addLayoutServer();
    addLogUnit();
    addManagementServer();
    router.baseServer.setOptionsMap(opts);
    // Setup SSL if needed
    Boolean tlsEnabled = (Boolean) opts.get("--enable-tls");
    Boolean tlsMutualAuthEnabled = (Boolean) opts.get("--enable-tls-mutual-auth");
    if (tlsEnabled) {
        // Get the TLS cipher suites to enable
        String ciphs = (String) opts.get("--tls-ciphers");
        if (ciphs != null) {
            List<String> ciphers = Pattern.compile(",").splitAsStream(ciphs).map(String::trim).collect(Collectors.toList());
            enabledTlsCipherSuites = ciphers.toArray(new String[ciphers.size()]);
        }
        // Get the TLS protocols to enable
        String protos = (String) opts.get("--tls-protocols");
        if (protos != null) {
            List<String> protocols = Pattern.compile(",").splitAsStream(protos).map(String::trim).collect(Collectors.toList());
            enabledTlsProtocols = protocols.toArray(new String[protocols.size()]);
        }
        try {
            sslContext = TlsUtils.enableTls(TlsUtils.SslContextType.SERVER_CONTEXT, (String) opts.get("--keystore"), e -> {
                log.error("Could not load keys from the key store.");
                System.exit(1);
            }, (String) opts.get("--keystore-password-file"), e -> {
                log.error("Could not read the key store password file.");
                System.exit(1);
            }, (String) opts.get("--truststore"), e -> {
                log.error("Could not load keys from the trust store.");
                System.exit(1);
            }, (String) opts.get("--truststore-password-file"), e -> {
                log.error("Could not read the trust store password file.");
                System.exit(1);
            });
        } catch (Exception ex) {
            log.error("Could not build the SSL context");
            System.exit(1);
        }
    }
    Boolean saslPlainTextAuth = (Boolean) opts.get("--enable-sasl-plain-text-auth");
    // Create the event loops responsible for servicing inbound messages.
    EventLoopGroup bossGroup;
    EventLoopGroup workerGroup;
    EventExecutorGroup ee;
    bossGroup = new NioEventLoopGroup(1, new ThreadFactory() {

        final AtomicInteger threadNum = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("accept-" + threadNum.getAndIncrement());
            return t;
        }
    });
    workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {

        final AtomicInteger threadNum = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("io-" + threadNum.getAndIncrement());
            return t;
        }
    });
    ee = new DefaultEventExecutorGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {

        final AtomicInteger threadNum = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("event-" + threadNum.getAndIncrement());
            return t;
        }
    });
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(io.netty.channel.socket.SocketChannel ch) throws Exception {
                if (tlsEnabled) {
                    SSLEngine engine = sslContext.newEngine(ch.alloc());
                    engine.setEnabledCipherSuites(enabledTlsCipherSuites);
                    engine.setEnabledProtocols(enabledTlsProtocols);
                    if (tlsMutualAuthEnabled) {
                        engine.setNeedClientAuth(true);
                    }
                    ch.pipeline().addLast("ssl", new SslHandler(engine));
                }
                ch.pipeline().addLast(new LengthFieldPrepender(4));
                ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                if (saslPlainTextAuth) {
                    ch.pipeline().addLast("sasl/plain-text", new PlainTextSaslNettyServer());
                }
                ch.pipeline().addLast(ee, new NettyCorfuMessageDecoder());
                ch.pipeline().addLast(ee, new NettyCorfuMessageEncoder());
                ch.pipeline().addLast(ee, router);
            }
        });
        ChannelFuture f = b.bind(port).sync();
        while (true) {
            try {
                f.channel().closeFuture().sync();
            } catch (InterruptedException ie) {
            }
        }
    } catch (InterruptedException ie) {
    } catch (Exception ex) {
        log.error("Corfu server shut down unexpectedly due to exception", ex);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelOption(io.netty.channel.ChannelOption) Getter(lombok.Getter) GitRepositoryState(org.corfudb.util.GitRepositoryState) LoggerFactory(org.slf4j.LoggerFactory) NettyCorfuMessageEncoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageEncoder) Docopt(org.docopt.Docopt) SSLEngine(javax.net.ssl.SSLEngine) PlainTextSaslNettyServer(org.corfudb.security.sasl.plaintext.PlainTextSaslNettyServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) Map(java.util.Map) Color(org.fusesource.jansi.Ansi.Color) ThreadFactory(java.util.concurrent.ThreadFactory) SocketChannel(io.netty.channel.socket.SocketChannel) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) TlsUtils(org.corfudb.security.tls.TlsUtils) Ansi.ansi(org.fusesource.jansi.Ansi.ansi) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NettyCorfuMessageDecoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageDecoder) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) SslContext(io.netty.handler.ssl.SslContext) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Level(ch.qos.logback.classic.Level) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AnsiConsole(org.fusesource.jansi.AnsiConsole) Logger(ch.qos.logback.classic.Logger) SslHandler(io.netty.handler.ssl.SslHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Pattern(java.util.regex.Pattern) Version(org.corfudb.util.Version) ThreadFactory(java.util.concurrent.ThreadFactory) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) SSLEngine(javax.net.ssl.SSLEngine) NettyCorfuMessageEncoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageEncoder) Logger(ch.qos.logback.classic.Logger) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) Docopt(org.docopt.Docopt) PlainTextSaslNettyServer(org.corfudb.security.sasl.plaintext.PlainTextSaslNettyServer) SocketChannel(io.netty.channel.socket.SocketChannel) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) ChannelFuture(io.netty.channel.ChannelFuture) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslHandler(io.netty.handler.ssl.SslHandler) NettyCorfuMessageDecoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageDecoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File)

Example 3 with Docopt

use of org.docopt.Docopt 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 4 with Docopt

use of org.docopt.Docopt in project CorfuDB by CorfuDB.

the class logReader method init.

public final boolean init(final String[] args) {
    Docopt parser = new Docopt(USAGE);
    parser.withExit(false);
    Map<String, Object> opts = parser.parse(args);
    op = new Operation(Operation.OperationType.REPORT);
    String logFileName = new String();
    boolean useOutputFile = false;
    if (opts.get("<log_file>") != null) {
        logFileName = (String) opts.get("<log_file>");
        System.out.println("Log file: " + logFileName);
    } else {
        System.out.print(USAGE);
        return false;
    }
    int startAddr = 0;
    int finalAddr = -1;
    if (opts.get("--from") != null) {
        startAddr = Integer.parseInt((String) opts.get("--from"));
    }
    if (opts.get("--to") != null) {
        finalAddr = Integer.parseInt((String) opts.get("--to"));
    }
    if ((Boolean) opts.get("display")) {
        Boolean showBinary = (Boolean) opts.get("--show_binary");
        System.out.format("display from %d to %d show_binary=%s\n", startAddr, finalAddr, showBinary.toString());
        if (showBinary) {
            op = new Operation(Operation.OperationType.DISPLAY, startAddr, finalAddr);
        } else {
            op = new Operation(Operation.OperationType.DISPLAY_ALL, startAddr, finalAddr);
        }
    } else if ((Boolean) opts.get("erase")) {
        System.out.format("erase from %d to %d\n", startAddr, finalAddr);
        op = new Operation(Operation.OperationType.ERASE_RANGE, startAddr, finalAddr);
        useOutputFile = true;
    }
    Metadata md = Metadata.newBuilder().setChecksum(NON_ZERO).setLength(// size is arbitrary but cannot be 0 (default)
    NON_ZERO).build();
    metadataSize = md.getSerializedSize();
    File fIn = new File(logFileName);
    File fOut = useOutputFile ? new File(logFileName + ".modified") : null;
    if (fIn.canRead()) {
        try {
            fileStreamIn = new FileInputStream(fIn);
            fileChannelIn = fileStreamIn.getChannel();
            if (useOutputFile) {
                fileStreamOut = new FileOutputStream(fOut);
                fileChannelOut = fileStreamOut.getChannel();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
    return false;
}
Also used : Metadata(org.corfudb.format.Types.Metadata) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Docopt(org.docopt.Docopt) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 5 with Docopt

use of org.docopt.Docopt in project CorfuDB by CorfuDB.

the class QCSMRobject method main.

public static String[] main(String[] args) {
    if (args != null && args.length > 0 && args[0].contentEquals("reboot")) {
        ManagementServer ms = CorfuServer.getManagementServer();
        ms.shutdown();
        CorfuServer.addManagementServer();
        LogUnitServer ls = CorfuServer.getLogUnitServer();
        if (ls != null) {
            ls.shutdown();
            CorfuServer.addLogUnit();
            return replyOk();
        } else {
            return replyErr("No active log server");
        }
    }
    // Parse the options given, using docopt.
    Map<String, Object> opts = new Docopt(USAGE).withVersion(GitRepositoryState.getRepositoryState().describe).parse(args);
    // Get a org.corfudb.runtime instance from the options.
    String config = (String) opts.get("--config");
    String qapp = (String) opts.get("--quickcheck-ap-prefix");
    String addressportPrefix = "";
    if (qapp != null) {
        addressportPrefix = qapp;
    }
    CorfuRuntime rt;
    if (rtMap.get(addressportPrefix + config) == null) {
        rt = configureRuntime(opts);
        rtMap.putIfAbsent(addressportPrefix + config, rt);
    }
    rt = (CorfuRuntime) rtMap.get(addressportPrefix + config);
    String argz = ((String) opts.get("<args>"));
    int arity;
    String[] split;
    if (argz == null) {
        split = new String[0];
        arity = 0;
    } else {
        split = argz.split(",");
        if (argz.charAt(argz.length() - 1) == ',') {
            arity = split.length + 1;
            String[] new_split = new String[arity];
            for (int i = 0; i < arity - 1; i++) {
                new_split[i] = split[i];
            }
            new_split[arity - 1] = "";
            split = new_split;
        } else {
            arity = split.length;
        }
    }
    // Attempt to open the object
    Class<?> cls;
    try {
        cls = Class.forName((String) opts.get("<class>"));
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException(cnfe);
    }
    Object o = rt.getObjectsView().build().setStreamName((String) opts.get("--stream-id")).setType(cls).open();
    // Use reflection to find the method...
    Method m;
    try {
        m = Arrays.stream(cls.getDeclaredMethods()).filter(x -> x.getName().equals(opts.get("<method>"))).filter(x -> x.getParameterCount() == arity).findFirst().get();
    } catch (NoSuchElementException nsee) {
        return replyErr("Method " + opts.get("<method>") + " with " + arity + " arguments not found!");
    }
    if (m == null) {
        return replyErr("Method " + opts.get("<method>") + " with " + arity + " arguments not found!");
    }
    Object ret;
    final int c10 = 10, c50 = 50;
    for (int i = 0; i < c10; i++) {
        try {
            ret = m.invoke(o, split);
        } catch (InvocationTargetException e) {
            Throwable c = ExceptionUtils.getCause(e);
            if (c.getClass() == org.corfudb.runtime.exceptions.NetworkException.class && c.toString().matches(".*Disconnected endpoint.*")) {
                // caused by a disconnection with the remote endpoint.
                try {
                    Thread.sleep(c50);
                } catch (InterruptedException ie) {
                }
                ;
                continue;
            } else {
                return replyErr("exception", e.getClass().getSimpleName(), "stack: " + ExceptionUtils.getStackTrace(e), "cause: " + ExceptionUtils.getCause(e));
            }
        } catch (IllegalAccessException e) {
            return replyErr("exception", e.getClass().getSimpleName(), "stack: " + ExceptionUtils.getStackTrace(e));
        } catch (Exception e) {
            return replyErr("Exception on object: " + e, "stack: " + ExceptionUtils.getStackTrace(e), "cause: " + ExceptionUtils.getCause(e));
        }
        if (ret != null) {
            return replyOk(ret.toString());
        } else {
            return replyOk();
        }
    }
    return replyErr("Exhausted for loop retries");
}
Also used : QCUtil.replyErr(org.corfudb.util.quickcheck.QCUtil.replyErr) Arrays(java.util.Arrays) GitRepositoryState(org.corfudb.util.GitRepositoryState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LogUnitServer(org.corfudb.infrastructure.LogUnitServer) Docopt(org.docopt.Docopt) QCUtil.replyOk(org.corfudb.util.quickcheck.QCUtil.replyOk) ManagementServer(org.corfudb.infrastructure.ManagementServer) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExceptionUtils(org.codehaus.plexus.util.ExceptionUtils) Map(java.util.Map) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) CorfuServer(org.corfudb.infrastructure.CorfuServer) QCUtil.configureRuntime(org.corfudb.util.quickcheck.QCUtil.configureRuntime) NoSuchElementException(java.util.NoSuchElementException) Method(java.lang.reflect.Method) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchElementException(java.util.NoSuchElementException) ManagementServer(org.corfudb.infrastructure.ManagementServer) Docopt(org.docopt.Docopt) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) LogUnitServer(org.corfudb.infrastructure.LogUnitServer) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Docopt (org.docopt.Docopt)5 CorfuRuntime (org.corfudb.runtime.CorfuRuntime)3 File (java.io.File)2 List (java.util.List)2 Map (java.util.Map)2 Level (ch.qos.logback.classic.Level)1 Logger (ch.qos.logback.classic.Logger)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ByteString (com.google.protobuf.ByteString)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelOption (io.netty.channel.ChannelOption)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)1