Search in sources :

Example 6 with Platform

use of org.apache.kafka.trogdor.common.Platform 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)

Example 7 with Platform

use of org.apache.kafka.trogdor.common.Platform in project kafka by apache.

the class WorkerManager method createWorker.

public KafkaFuture<String> createWorker(long workerId, String taskId, TaskSpec spec) throws Throwable {
    try (ShutdownManager.Reference ref = shutdownManager.takeReference()) {
        final Worker worker = stateChangeExecutor.submit(new CreateWorker(workerId, taskId, spec, time.milliseconds())).get();
        if (worker.doneFuture != null) {
            log.info("{}: Ignoring request to create worker {}, because there is already " + "a worker with that id.", nodeName, workerId);
            return worker.doneFuture;
        }
        worker.doneFuture = new KafkaFutureImpl<>();
        if (worker.spec.endMs() <= time.milliseconds()) {
            log.info("{}: Will not run worker {} as it has expired.", nodeName, worker);
            stateChangeExecutor.submit(new HandleWorkerHalting(worker, "worker expired", true));
            return worker.doneFuture;
        }
        KafkaFutureImpl<String> haltFuture = new KafkaFutureImpl<>();
        haltFuture.thenApply((KafkaFuture.BaseFunction<String, Void>) errorString -> {
            if (errorString == null)
                errorString = "";
            if (errorString.isEmpty()) {
                log.info("{}: Worker {} is halting.", nodeName, worker);
            } else {
                log.info("{}: Worker {} is halting with error {}", nodeName, worker, errorString);
            }
            stateChangeExecutor.submit(new HandleWorkerHalting(worker, errorString, false));
            return null;
        });
        try {
            worker.taskWorker.start(platform, worker.status, haltFuture);
        } catch (Exception e) {
            log.info("{}: Worker {} start() exception", nodeName, worker, e);
            stateChangeExecutor.submit(new HandleWorkerHalting(worker, "worker.start() exception: " + Utils.stackTrace(e), true));
        }
        stateChangeExecutor.submit(new FinishCreatingWorker(worker));
        return worker.doneFuture;
    } catch (ExecutionException e) {
        if (e.getCause() instanceof RequestConflictException) {
            log.info("{}: request conflict while creating worker {} for task {} with spec {}.", nodeName, workerId, taskId, spec);
        } else {
            log.info("{}: Error creating worker {} for task {} with spec {}", nodeName, workerId, taskId, spec, e);
        }
        throw e.getCause();
    }
}
Also used : WorkerState(org.apache.kafka.trogdor.rest.WorkerState) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) LoggerFactory(org.slf4j.LoggerFactory) KafkaException(org.apache.kafka.common.KafkaException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Platform(org.apache.kafka.trogdor.common.Platform) AgentWorkerStatusTracker(org.apache.kafka.trogdor.task.AgentWorkerStatusTracker) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Scheduler(org.apache.kafka.common.utils.Scheduler) ThreadUtils(org.apache.kafka.common.utils.ThreadUtils) ExecutorService(java.util.concurrent.ExecutorService) Utils(org.apache.kafka.common.utils.Utils) Logger(org.slf4j.Logger) Time(org.apache.kafka.common.utils.Time) KafkaFuture(org.apache.kafka.common.KafkaFuture) TaskSpec(org.apache.kafka.trogdor.task.TaskSpec) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) WorkerStarting(org.apache.kafka.trogdor.rest.WorkerStarting) TreeMap(java.util.TreeMap) RequestConflictException(org.apache.kafka.trogdor.rest.RequestConflictException) WorkerDone(org.apache.kafka.trogdor.rest.WorkerDone) WorkerStopping(org.apache.kafka.trogdor.rest.WorkerStopping) TaskWorker(org.apache.kafka.trogdor.task.TaskWorker) KafkaFuture(org.apache.kafka.common.KafkaFuture) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) KafkaException(org.apache.kafka.common.KafkaException) ExecutionException(java.util.concurrent.ExecutionException) RequestConflictException(org.apache.kafka.trogdor.rest.RequestConflictException) RequestConflictException(org.apache.kafka.trogdor.rest.RequestConflictException) TaskWorker(org.apache.kafka.trogdor.task.TaskWorker) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Platform (org.apache.kafka.trogdor.common.Platform)7 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)4 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)4 Namespace (net.sourceforge.argparse4j.inf.Namespace)4 JsonRestServer (org.apache.kafka.trogdor.rest.JsonRestServer)4 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 TaskSpec (org.apache.kafka.trogdor.task.TaskSpec)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1