Search in sources :

Example 6 with JsonRestServer

use of org.apache.kafka.trogdor.rest.JsonRestServer in project kafka by apache.

the class Agent method main.

public static void main(String[] args) throws Exception {
    ArgumentParser parser = ArgumentParsers.newArgumentParser("trogdor-agent").defaultHelp(true).description("The Trogdor fault injection agent");
    parser.addArgument("--agent.config", "-c").action(store()).required(true).type(String.class).dest("config").metavar("CONFIG").help("The configuration file to use.");
    parser.addArgument("--node-name", "-n").action(store()).required(true).type(String.class).dest("node_name").metavar("NODE_NAME").help("The name of this node.");
    parser.addArgument("--exec", "-e").action(store()).type(String.class).dest("task_spec").metavar("TASK_SPEC").help("Execute a single task spec and then exit.  The argument is the task spec to load when starting up, or a path to it.");
    Namespace res = null;
    try {
        res = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
        if (args.length == 0) {
            parser.printHelp();
            Exit.exit(0);
        } else {
            parser.handleError(e);
            Exit.exit(1);
        }
    }
    String configPath = res.getString("config");
    String nodeName = res.getString("node_name");
    String taskSpec = res.getString("task_spec");
    Platform platform = Platform.Config.parse(nodeName, configPath);
    JsonRestServer restServer = new JsonRestServer(Node.Util.getTrogdorAgentPort(platform.curNode()));
    AgentRestResource resource = new AgentRestResource();
    log.info("Starting agent process.");
    final Agent agent = new Agent(platform, Scheduler.SYSTEM, restServer, resource);
    restServer.start(resource);
    Exit.addShutdownHook("agent-shutdown-hook", () -> {
        log.warn("Running agent shutdown hook.");
        try {
            agent.beginShutdown();
            agent.waitForShutdown();
        } catch (Exception e) {
            log.error("Got exception while running agent shutdown hook.", e);
        }
    });
    if (taskSpec != null) {
        TaskSpec spec = null;
        try {
            spec = JsonUtil.objectFromCommandLineArgument(taskSpec, TaskSpec.class);
        } catch (Exception e) {
            System.out.println("Unable to parse the supplied task spec.");
            e.printStackTrace();
            Exit.exit(1);
        }
        TaskSpec effectiveSpec = agent.rebaseTaskSpecTime(spec);
        Exit.exit(agent.exec(effectiveSpec, System.out) ? 0 : 1);
    }
    agent.waitForShutdown();
}
Also used : Platform(org.apache.kafka.trogdor.common.Platform) JsonRestServer(org.apache.kafka.trogdor.rest.JsonRestServer) TaskSpec(org.apache.kafka.trogdor.task.TaskSpec) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException)

Aggregations

JsonRestServer (org.apache.kafka.trogdor.rest.JsonRestServer)6 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)4 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)4 Namespace (net.sourceforge.argparse4j.inf.Namespace)4 Platform (org.apache.kafka.trogdor.common.Platform)4 TaskSpec (org.apache.kafka.trogdor.task.TaskSpec)1