use of org.apache.kafka.trogdor.rest.DestroyWorkerRequest 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);
}
}
}
use of org.apache.kafka.trogdor.rest.DestroyWorkerRequest in project kafka by apache.
the class AgentTest method testDestroyWorkers.
@Test
public void testDestroyWorkers() 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);
final NoOpTaskSpec fooSpec = new NoOpTaskSpec(0, 5);
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);
time.sleep(1);
client.destroyWorker(new DestroyWorkerRequest(0));
client.destroyWorker(new DestroyWorkerRequest(0));
client.destroyWorker(new DestroyWorkerRequest(1));
new ExpectedTasks().waitFor(client);
time.sleep(1);
final NoOpTaskSpec fooSpec2 = new NoOpTaskSpec(2, 1);
client.createWorker(new CreateWorkerRequest(1, "foo", fooSpec2));
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerRunning("foo", fooSpec2, 2, new TextNode("active"))).build()).waitFor(client);
time.sleep(2);
new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").workerState(new WorkerDone("foo", fooSpec2, 2, 4, new TextNode("done"), "")).build()).waitFor(client);
time.sleep(1);
client.destroyWorker(new DestroyWorkerRequest(1));
new ExpectedTasks().waitFor(client);
agent.beginShutdown();
agent.waitForShutdown();
}
Aggregations