Search in sources :

Example 1 with CreateWorkerRequest

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

the class AgentClient method main.

public static void main(String[] args) throws Exception {
    ArgumentParser rootParser = ArgumentParsers.newArgumentParser("trogdor-agent-client").defaultHelp(true).description("The Trogdor agent client.");
    Subparsers subParsers = rootParser.addSubparsers().dest("command");
    Subparser uptimeParser = subParsers.addParser("uptime").help("Get the agent uptime.");
    addTargetArgument(uptimeParser);
    addJsonArgument(uptimeParser);
    Subparser statusParser = subParsers.addParser("status").help("Get the agent status.");
    addTargetArgument(statusParser);
    addJsonArgument(statusParser);
    Subparser createWorkerParser = subParsers.addParser("createWorker").help("Create a new worker.");
    addTargetArgument(createWorkerParser);
    addWorkerIdArgument(createWorkerParser, "The worker ID to create.");
    createWorkerParser.addArgument("--taskId").action(store()).required(true).type(String.class).dest("taskId").metavar("TASK_ID").help("The task ID to create.");
    createWorkerParser.addArgument("--spec", "-s").action(store()).required(true).type(String.class).dest("taskSpec").metavar("TASK_SPEC").help("The task spec to create, or a path to a file containing the task spec.");
    Subparser stopWorkerParser = subParsers.addParser("stopWorker").help("Stop a worker.");
    addTargetArgument(stopWorkerParser);
    addWorkerIdArgument(stopWorkerParser, "The worker ID to stop.");
    Subparser destroyWorkerParser = subParsers.addParser("destroyWorker").help("Destroy a worker.");
    addTargetArgument(destroyWorkerParser);
    addWorkerIdArgument(destroyWorkerParser, "The worker ID to destroy.");
    Subparser shutdownParser = subParsers.addParser("shutdown").help("Shut down the agent.");
    addTargetArgument(shutdownParser);
    Namespace res = rootParser.parseArgsOrFail(args);
    String target = res.getString("target");
    AgentClient client = new Builder().maxTries(3).target(target).build();
    ZoneOffset localOffset = OffsetDateTime.now().getOffset();
    switch(res.getString("command")) {
        case "uptime":
            {
                UptimeResponse uptime = client.uptime();
                if (res.getBoolean("json")) {
                    System.out.println(JsonUtil.toJsonString(uptime));
                } else {
                    System.out.printf("Agent is running at %s.%n", target);
                    System.out.printf("\tStart time: %s%n", dateString(uptime.serverStartMs(), localOffset));
                    System.out.printf("\tCurrent server time: %s%n", dateString(uptime.nowMs(), localOffset));
                    System.out.printf("\tUptime: %s%n", durationString(uptime.nowMs() - uptime.serverStartMs()));
                }
                break;
            }
        case "status":
            {
                AgentStatusResponse status = client.status();
                if (res.getBoolean("json")) {
                    System.out.println(JsonUtil.toJsonString(status));
                } else {
                    System.out.printf("Agent is running at %s.%n", target);
                    System.out.printf("\tStart time: %s%n", dateString(status.serverStartMs(), localOffset));
                    List<List<String>> lines = new ArrayList<>();
                    List<String> header = new ArrayList<>(Arrays.asList("WORKER_ID", "TASK_ID", "STATE", "TASK_TYPE"));
                    lines.add(header);
                    for (Map.Entry<Long, WorkerState> entry : status.workers().entrySet()) {
                        List<String> cols = new ArrayList<>();
                        cols.add(Long.toString(entry.getKey()));
                        cols.add(entry.getValue().taskId());
                        cols.add(entry.getValue().getClass().getSimpleName());
                        cols.add(entry.getValue().spec().getClass().getCanonicalName());
                        lines.add(cols);
                    }
                    System.out.print(StringFormatter.prettyPrintGrid(lines));
                }
                break;
            }
        case "createWorker":
            {
                long workerId = res.getLong("workerId");
                String taskId = res.getString("taskId");
                TaskSpec taskSpec = JsonUtil.objectFromCommandLineArgument(res.getString("taskSpec"), TaskSpec.class);
                CreateWorkerRequest req = new CreateWorkerRequest(workerId, taskId, taskSpec);
                client.createWorker(req);
                System.out.printf("Sent CreateWorkerRequest for worker %d%n.", req.workerId());
                break;
            }
        case "stopWorker":
            {
                long workerId = res.getLong("workerId");
                client.stopWorker(new StopWorkerRequest(workerId));
                System.out.printf("Sent StopWorkerRequest for worker %d%n.", workerId);
                break;
            }
        case "destroyWorker":
            {
                long workerId = res.getLong("workerId");
                client.destroyWorker(new DestroyWorkerRequest(workerId));
                System.out.printf("Sent DestroyWorkerRequest for worker %d%n.", workerId);
                break;
            }
        case "shutdown":
            {
                client.invokeShutdown();
                System.out.println("Sent ShutdownRequest.");
                break;
            }
        default:
            {
                System.out.println("You must choose an action. Type --help for help.");
                Exit.exit(1);
            }
    }
}
Also used : UriBuilder(javax.ws.rs.core.UriBuilder) TaskSpec(org.apache.kafka.trogdor.task.TaskSpec) StringFormatter.durationString(org.apache.kafka.trogdor.common.StringFormatter.durationString) StringFormatter.dateString(org.apache.kafka.trogdor.common.StringFormatter.dateString) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace) ZoneOffset(java.time.ZoneOffset) UptimeResponse(org.apache.kafka.trogdor.rest.UptimeResponse) DestroyWorkerRequest(org.apache.kafka.trogdor.rest.DestroyWorkerRequest) Subparsers(net.sourceforge.argparse4j.inf.Subparsers) AgentStatusResponse(org.apache.kafka.trogdor.rest.AgentStatusResponse) StopWorkerRequest(org.apache.kafka.trogdor.rest.StopWorkerRequest) Subparser(net.sourceforge.argparse4j.inf.Subparser) ArrayList(java.util.ArrayList) List(java.util.List) CreateWorkerRequest(org.apache.kafka.trogdor.rest.CreateWorkerRequest)

Example 2 with CreateWorkerRequest

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

the class AgentTest method testKiboshFaults.

@Test
public void testKiboshFaults() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    MockScheduler scheduler = new MockScheduler(time);
    Agent agent = createAgent(scheduler);
    AgentClient client = new AgentClient.Builder().maxTries(10).target("localhost", agent.port()).build();
    new ExpectedTasks().waitFor(client);
    try (MockKibosh mockKibosh = new MockKibosh()) {
        assertEquals(KiboshControlFile.EMPTY, mockKibosh.read());
        FilesUnreadableFaultSpec fooSpec = new FilesUnreadableFaultSpec(0, 900000, Collections.singleton("myAgent"), mockKibosh.tempDir.getPath(), "/foo", 123);
        client.createWorker(new CreateWorkerRequest(0, "foo", fooSpec));
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, 0, new TextNode("Added fault foo"))).build()).waitFor(client);
        assertEquals(new KiboshControlFile(Collections.singletonList(new KiboshFilesUnreadableFaultSpec("/foo", 123))), mockKibosh.read());
        FilesUnreadableFaultSpec barSpec = new FilesUnreadableFaultSpec(0, 900000, Collections.singleton("myAgent"), mockKibosh.tempDir.getPath(), "/bar", 456);
        client.createWorker(new CreateWorkerRequest(1, "bar", barSpec));
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, 0, new TextNode("Added fault foo"))).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning("bar", barSpec, 0, new TextNode("Added fault bar"))).build()).waitFor(client);
        assertEquals(new KiboshControlFile(asList(new KiboshFilesUnreadableFaultSpec("/foo", 123), new KiboshFilesUnreadableFaultSpec("/bar", 456))), mockKibosh.read());
        time.sleep(1);
        client.stopWorker(new StopWorkerRequest(0));
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerDone("foo", fooSpec, 0, 1, new TextNode("Removed fault foo"), "")).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning("bar", barSpec, 0, new TextNode("Added fault bar"))).build()).waitFor(client);
        assertEquals(new KiboshControlFile(Collections.singletonList(new KiboshFilesUnreadableFaultSpec("/bar", 456))), mockKibosh.read());
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) WorkerDone(org.apache.kafka.trogdor.rest.WorkerDone) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) TextNode(com.fasterxml.jackson.databind.node.TextNode) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) KiboshFilesUnreadableFaultSpec(org.apache.kafka.trogdor.fault.Kibosh.KiboshFilesUnreadableFaultSpec) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) StopWorkerRequest(org.apache.kafka.trogdor.rest.StopWorkerRequest) KiboshControlFile(org.apache.kafka.trogdor.fault.Kibosh.KiboshControlFile) KiboshFilesUnreadableFaultSpec(org.apache.kafka.trogdor.fault.Kibosh.KiboshFilesUnreadableFaultSpec) FilesUnreadableFaultSpec(org.apache.kafka.trogdor.fault.FilesUnreadableFaultSpec) CreateWorkerRequest(org.apache.kafka.trogdor.rest.CreateWorkerRequest) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 3 with CreateWorkerRequest

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

the class AgentTest method testAgentCreateWorkers.

@Test
public void testAgentCreateWorkers() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    MockScheduler scheduler = new MockScheduler(time);
    Agent agent = createAgent(scheduler);
    AgentClient client = new AgentClient.Builder().maxTries(10).target("localhost", agent.port()).build();
    AgentStatusResponse status = client.status();
    assertEquals(Collections.emptyMap(), status.workers());
    new ExpectedTasks().waitFor(client);
    final NoOpTaskSpec fooSpec = new NoOpTaskSpec(1000, 600000);
    client.createWorker(new CreateWorkerRequest(0, "foo", fooSpec));
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, 0, new TextNode("active"))).build()).waitFor(client);
    final NoOpTaskSpec barSpec = new NoOpTaskSpec(2000, 900000);
    client.createWorker(new CreateWorkerRequest(1, "bar", barSpec));
    client.createWorker(new CreateWorkerRequest(1, "bar", barSpec));
    assertThrows(RequestConflictException.class, () -> client.createWorker(new CreateWorkerRequest(1, "foo", barSpec)), "Recreating a request with a different taskId is not allowed");
    assertThrows(RequestConflictException.class, () -> client.createWorker(new CreateWorkerRequest(1, "bar", fooSpec)), "Recreating a request with a different spec is not allowed");
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, 0, new TextNode("active"))).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning("bar", barSpec, 0, new TextNode("active"))).build()).waitFor(client);
    final NoOpTaskSpec bazSpec = new NoOpTaskSpec(1, 450000);
    client.createWorker(new CreateWorkerRequest(2, "baz", bazSpec));
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, 0, new TextNode("active"))).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning("bar", barSpec, 0, new TextNode("active"))).build()).addTask(new ExpectedTaskBuilder("baz").workerState(new WorkerRunning("baz", bazSpec, 0, new TextNode("active"))).build()).waitFor(client);
    agent.beginShutdown();
    agent.waitForShutdown();
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) AgentStatusResponse(org.apache.kafka.trogdor.rest.AgentStatusResponse) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) TextNode(com.fasterxml.jackson.databind.node.TextNode) CreateWorkerRequest(org.apache.kafka.trogdor.rest.CreateWorkerRequest) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) MockTime(org.apache.kafka.common.utils.MockTime) NoOpTaskSpec(org.apache.kafka.trogdor.task.NoOpTaskSpec) Test(org.junit.jupiter.api.Test)

Example 4 with CreateWorkerRequest

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

the class AgentTest method testAgentFinishesTasks.

@Test
public void testAgentFinishesTasks() throws Exception {
    long startTimeMs = 2000;
    MockTime time = new MockTime(0, startTimeMs, 0);
    MockScheduler scheduler = new MockScheduler(time);
    Agent agent = createAgent(scheduler);
    AgentClient client = new AgentClient.Builder().maxTries(10).target("localhost", agent.port()).build();
    new ExpectedTasks().waitFor(client);
    final NoOpTaskSpec fooSpec = new NoOpTaskSpec(startTimeMs, 2);
    long fooSpecStartTimeMs = startTimeMs;
    client.createWorker(new CreateWorkerRequest(0, "foo", fooSpec));
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, startTimeMs, new TextNode("active"))).build()).waitFor(client);
    time.sleep(1);
    long barSpecWorkerId = 1;
    long barSpecStartTimeMs = startTimeMs + 1;
    final NoOpTaskSpec barSpec = new NoOpTaskSpec(startTimeMs, 900000);
    client.createWorker(new CreateWorkerRequest(barSpecWorkerId, "bar", barSpec));
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec, fooSpecStartTimeMs, new TextNode("active"))).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning("bar", barSpec, barSpecStartTimeMs, new TextNode("active"))).build()).waitFor(client);
    time.sleep(1);
    // foo task expired
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerDone("foo", fooSpec, fooSpecStartTimeMs, fooSpecStartTimeMs + 2, new TextNode("done"), "")).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning("bar", barSpec, barSpecStartTimeMs, new TextNode("active"))).build()).waitFor(client);
    time.sleep(5);
    client.stopWorker(new StopWorkerRequest(barSpecWorkerId));
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerDone("foo", fooSpec, fooSpecStartTimeMs, fooSpecStartTimeMs + 2, new TextNode("done"), "")).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerDone("bar", barSpec, barSpecStartTimeMs, startTimeMs + 7, new TextNode("done"), "")).build()).waitFor(client);
    agent.beginShutdown();
    agent.waitForShutdown();
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) WorkerDone(org.apache.kafka.trogdor.rest.WorkerDone) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) TextNode(com.fasterxml.jackson.databind.node.TextNode) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) NoOpTaskSpec(org.apache.kafka.trogdor.task.NoOpTaskSpec) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) StopWorkerRequest(org.apache.kafka.trogdor.rest.StopWorkerRequest) CreateWorkerRequest(org.apache.kafka.trogdor.rest.CreateWorkerRequest) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 5 with CreateWorkerRequest

use of org.apache.kafka.trogdor.rest.CreateWorkerRequest in project apache-kafka-on-k8s by banzaicloud.

the class AgentTest method testWorkerCompletions.

@Test
public void testWorkerCompletions() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    MockScheduler scheduler = new MockScheduler(time);
    Agent agent = createAgent(scheduler);
    AgentClient client = new AgentClient.Builder().maxTries(10).target("localhost", agent.port()).build();
    new ExpectedTasks().waitFor(client);
    SampleTaskSpec fooSpec = new SampleTaskSpec(0, 900000, 1, "");
    client.createWorker(new CreateWorkerRequest("foo", fooSpec));
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning(fooSpec, 0, "")).build()).waitFor(client);
    SampleTaskSpec barSpec = new SampleTaskSpec(0, 900000, 2, "baz");
    client.createWorker(new CreateWorkerRequest("bar", barSpec));
    time.sleep(1);
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerDone(fooSpec, 0, 1, "", "")).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerRunning(barSpec, 0, "")).build()).waitFor(client);
    time.sleep(1);
    new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerDone(fooSpec, 0, 1, "", "")).build()).addTask(new ExpectedTaskBuilder("bar").workerState(new WorkerDone(barSpec, 0, 2, "", "baz")).build()).waitFor(client);
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) WorkerDone(org.apache.kafka.trogdor.rest.WorkerDone) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) SampleTaskSpec(org.apache.kafka.trogdor.task.SampleTaskSpec) CreateWorkerRequest(org.apache.kafka.trogdor.rest.CreateWorkerRequest) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Aggregations

CreateWorkerRequest (org.apache.kafka.trogdor.rest.CreateWorkerRequest)11 MockScheduler (org.apache.kafka.common.utils.MockScheduler)10 MockTime (org.apache.kafka.common.utils.MockTime)10 ExpectedTasks (org.apache.kafka.trogdor.common.ExpectedTasks)10 ExpectedTaskBuilder (org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder)10 WorkerRunning (org.apache.kafka.trogdor.rest.WorkerRunning)9 WorkerDone (org.apache.kafka.trogdor.rest.WorkerDone)8 NoOpTaskSpec (org.apache.kafka.trogdor.task.NoOpTaskSpec)6 Test (org.junit.jupiter.api.Test)6 TextNode (com.fasterxml.jackson.databind.node.TextNode)5 StopWorkerRequest (org.apache.kafka.trogdor.rest.StopWorkerRequest)5 AgentStatusResponse (org.apache.kafka.trogdor.rest.AgentStatusResponse)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 FilesUnreadableFaultSpec (org.apache.kafka.trogdor.fault.FilesUnreadableFaultSpec)2 KiboshControlFile (org.apache.kafka.trogdor.fault.Kibosh.KiboshControlFile)2 KiboshFilesUnreadableFaultSpec (org.apache.kafka.trogdor.fault.Kibosh.KiboshFilesUnreadableFaultSpec)2 DestroyWorkerRequest (org.apache.kafka.trogdor.rest.DestroyWorkerRequest)2 SampleTaskSpec (org.apache.kafka.trogdor.task.SampleTaskSpec)2 ZoneOffset (java.time.ZoneOffset)1