Search in sources :

Example 1 with NoOpTaskSpec

use of org.apache.kafka.trogdor.task.NoOpTaskSpec in project apache-kafka-on-k8s by banzaicloud.

the class CoordinatorTest method testCreateTask.

@Test
public void testCreateTask() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    Scheduler scheduler = new MockScheduler(time);
    try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
        new ExpectedTasks().waitFor(cluster.coordinatorClient());
        NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 2);
        cluster.coordinatorClient().createTask(new CreateTaskRequest("foo", fooSpec));
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskPending(fooSpec)).build()).waitFor(cluster.coordinatorClient());
        time.sleep(2);
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskRunning(fooSpec, 2)).workerState(new WorkerRunning(fooSpec, 2, "")).build()).waitFor(cluster.coordinatorClient()).waitFor(cluster.agentClient("node02"));
        time.sleep(3);
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskDone(fooSpec, 2, 5, "", false)).build()).waitFor(cluster.coordinatorClient());
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) MockScheduler(org.apache.kafka.common.utils.MockScheduler) Scheduler(org.apache.kafka.common.utils.Scheduler) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) TaskDone(org.apache.kafka.trogdor.rest.TaskDone) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) NoOpTaskSpec(org.apache.kafka.trogdor.task.NoOpTaskSpec) TaskPending(org.apache.kafka.trogdor.rest.TaskPending) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) CreateTaskRequest(org.apache.kafka.trogdor.rest.CreateTaskRequest) MiniTrogdorCluster(org.apache.kafka.trogdor.common.MiniTrogdorCluster) TaskRunning(org.apache.kafka.trogdor.rest.TaskRunning) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 2 with NoOpTaskSpec

use of org.apache.kafka.trogdor.task.NoOpTaskSpec in project kafka by apache.

the class CoordinatorTest method testTasksRequest.

@Test
public void testTasksRequest() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    Scheduler scheduler = new MockScheduler(time);
    try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
        CoordinatorClient coordinatorClient = cluster.coordinatorClient();
        new ExpectedTasks().waitFor(coordinatorClient);
        NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 10);
        NoOpTaskSpec barSpec = new NoOpTaskSpec(3, 1);
        coordinatorClient.createTask(new CreateTaskRequest("foo", fooSpec));
        coordinatorClient.createTask(new CreateTaskRequest("bar", barSpec));
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskPending(fooSpec)).build()).addTask(new ExpectedTaskBuilder("bar").taskState(new TaskPending(barSpec)).build()).waitFor(coordinatorClient);
        assertEquals(0, coordinatorClient.tasks(new TasksRequest(null, 10, 0, 10, 0, Optional.empty())).tasks().size());
        TasksResponse resp1 = coordinatorClient.tasks(new TasksRequest(Arrays.asList("foo", "baz"), 0, 0, 0, 0, Optional.empty()));
        assertTrue(resp1.tasks().containsKey("foo"));
        assertFalse(resp1.tasks().containsKey("bar"));
        assertEquals(1, resp1.tasks().size());
        time.sleep(2);
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskRunning(fooSpec, 2, new TextNode("active"))).workerState(new WorkerRunning("foo", fooSpec, 2, new TextNode("active"))).build()).addTask(new ExpectedTaskBuilder("bar").taskState(new TaskPending(barSpec)).build()).waitFor(coordinatorClient).waitFor(cluster.agentClient("node02"));
        TasksResponse resp2 = coordinatorClient.tasks(new TasksRequest(null, 1, 0, 0, 0, Optional.empty()));
        assertTrue(resp2.tasks().containsKey("foo"));
        assertFalse(resp2.tasks().containsKey("bar"));
        assertEquals(1, resp2.tasks().size());
        assertEquals(0, coordinatorClient.tasks(new TasksRequest(null, 3, 0, 0, 0, Optional.empty())).tasks().size());
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) TasksResponse(org.apache.kafka.trogdor.rest.TasksResponse) WorkerRunning(org.apache.kafka.trogdor.rest.WorkerRunning) MockScheduler(org.apache.kafka.common.utils.MockScheduler) Scheduler(org.apache.kafka.common.utils.Scheduler) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) TextNode(com.fasterxml.jackson.databind.node.TextNode) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) NoOpTaskSpec(org.apache.kafka.trogdor.task.NoOpTaskSpec) TaskPending(org.apache.kafka.trogdor.rest.TaskPending) ExpectedTasks(org.apache.kafka.trogdor.common.ExpectedTasks) CreateTaskRequest(org.apache.kafka.trogdor.rest.CreateTaskRequest) MiniTrogdorCluster(org.apache.kafka.trogdor.common.MiniTrogdorCluster) TasksRequest(org.apache.kafka.trogdor.rest.TasksRequest) TaskRunning(org.apache.kafka.trogdor.rest.TaskRunning) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 3 with NoOpTaskSpec

use of org.apache.kafka.trogdor.task.NoOpTaskSpec in project kafka by apache.

the class CoordinatorTest method testTaskRequestWithOldStartMsGetsUpdated.

@Test
public void testTaskRequestWithOldStartMsGetsUpdated() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    Scheduler scheduler = new MockScheduler(time);
    try (MiniTrogdorCluster cluster = new MiniTrogdorCluster.Builder().addCoordinator("node01").addAgent("node02").scheduler(scheduler).build()) {
        NoOpTaskSpec fooSpec = new NoOpTaskSpec(1, 500);
        time.sleep(552);
        CoordinatorClient coordinatorClient = cluster.coordinatorClient();
        NoOpTaskSpec updatedSpec = new NoOpTaskSpec(552, 500);
        coordinatorClient.createTask(new CreateTaskRequest("fooSpec", fooSpec));
        TaskState expectedState = new ExpectedTaskBuilder("fooSpec").taskState(new TaskRunning(updatedSpec, 552, new TextNode("receiving"))).build().taskState();
        TaskState resp = coordinatorClient.task(new TaskRequest("fooSpec"));
        assertEquals(expectedState, resp);
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) MockScheduler(org.apache.kafka.common.utils.MockScheduler) Scheduler(org.apache.kafka.common.utils.Scheduler) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) TextNode(com.fasterxml.jackson.databind.node.TextNode) DestroyTaskRequest(org.apache.kafka.trogdor.rest.DestroyTaskRequest) StopTaskRequest(org.apache.kafka.trogdor.rest.StopTaskRequest) CreateTaskRequest(org.apache.kafka.trogdor.rest.CreateTaskRequest) TaskRequest(org.apache.kafka.trogdor.rest.TaskRequest) ExpectedTaskBuilder(org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder) NoOpTaskSpec(org.apache.kafka.trogdor.task.NoOpTaskSpec) CreateTaskRequest(org.apache.kafka.trogdor.rest.CreateTaskRequest) MiniTrogdorCluster(org.apache.kafka.trogdor.common.MiniTrogdorCluster) TaskRunning(org.apache.kafka.trogdor.rest.TaskRunning) TaskState(org.apache.kafka.trogdor.rest.TaskState) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 4 with NoOpTaskSpec

use of org.apache.kafka.trogdor.task.NoOpTaskSpec 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 5 with NoOpTaskSpec

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

Aggregations

NoOpTaskSpec (org.apache.kafka.trogdor.task.NoOpTaskSpec)20 MockScheduler (org.apache.kafka.common.utils.MockScheduler)18 MockTime (org.apache.kafka.common.utils.MockTime)18 ExpectedTaskBuilder (org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder)18 ExpectedTasks (org.apache.kafka.trogdor.common.ExpectedTasks)16 Test (org.junit.jupiter.api.Test)15 WorkerRunning (org.apache.kafka.trogdor.rest.WorkerRunning)14 Scheduler (org.apache.kafka.common.utils.Scheduler)12 MiniTrogdorCluster (org.apache.kafka.trogdor.common.MiniTrogdorCluster)12 CreateTaskRequest (org.apache.kafka.trogdor.rest.CreateTaskRequest)12 TaskPending (org.apache.kafka.trogdor.rest.TaskPending)12 TaskRunning (org.apache.kafka.trogdor.rest.TaskRunning)12 TextNode (com.fasterxml.jackson.databind.node.TextNode)11 TaskDone (org.apache.kafka.trogdor.rest.TaskDone)9 WorkerDone (org.apache.kafka.trogdor.rest.WorkerDone)9 CreateWorkerRequest (org.apache.kafka.trogdor.rest.CreateWorkerRequest)6 DestroyTaskRequest (org.apache.kafka.trogdor.rest.DestroyTaskRequest)6 StopTaskRequest (org.apache.kafka.trogdor.rest.StopTaskRequest)6 AgentClient (org.apache.kafka.trogdor.agent.AgentClient)5 Test (org.junit.Test)5