Search in sources :

Example 1 with MockScheduler

use of org.apache.kafka.common.utils.MockScheduler 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 MockScheduler

use of org.apache.kafka.common.utils.MockScheduler in project kafka by apache.

the class ExpiringCredentialRefreshingLoginTest method testRefreshWithExpirationSmallerThanConfiguredBuffers.

@Test
public void testRefreshWithExpirationSmallerThanConfiguredBuffers() throws Exception {
    int numExpectedRefreshes = 1;
    boolean clientReloginAllowedBeforeLogout = true;
    final LoginContext mockLoginContext = mock(LoginContext.class);
    Subject subject = new Subject();
    when(mockLoginContext.getSubject()).thenReturn(subject);
    MockTime mockTime = new MockTime();
    long startMs = mockTime.milliseconds();
    /*
         * Identify the lifetime of each expiring credential
         */
    long lifetimeMinutes = 10L;
    /*
         * Identify the point at which refresh will occur in that lifetime
         */
    long refreshEveryMinutes = 8L;
    /*
         * Set an absolute last refresh time that will cause the login thread to exit
         * after a certain number of re-logins (by adding an extra half of a refresh
         * interval).
         */
    long absoluteLastRefreshMs = startMs + (1 + numExpectedRefreshes) * 1000 * 60 * refreshEveryMinutes - 1000 * 60 * refreshEveryMinutes / 2;
    /*
         * Identify buffer time on either side for the refresh algorithm that will cause
         * the entire lifetime to be taken up. In other words, make sure there is no way
         * to honor the buffers.
         */
    short minPeriodSeconds = (short) (1 + lifetimeMinutes * 60 / 2);
    short bufferSeconds = minPeriodSeconds;
    /*
         * Define some listeners so we can keep track of who gets done and when. All
         * added listeners should end up done except the last, extra one, which should
         * not.
         */
    MockScheduler mockScheduler = new MockScheduler(mockTime);
    List<KafkaFutureImpl<Long>> waiters = addWaiters(mockScheduler, 1000 * 60 * refreshEveryMinutes, numExpectedRefreshes + 1);
    // Create the ExpiringCredentialRefreshingLogin instance under test
    TestLoginContextFactory testLoginContextFactory = new TestLoginContextFactory();
    TestExpiringCredentialRefreshingLogin testExpiringCredentialRefreshingLogin = new TestExpiringCredentialRefreshingLogin(refreshConfigThatPerformsReloginEveryGivenPercentageOfLifetime(1.0 * refreshEveryMinutes / lifetimeMinutes, minPeriodSeconds, bufferSeconds, clientReloginAllowedBeforeLogout), testLoginContextFactory, mockTime, 1000 * 60 * lifetimeMinutes, absoluteLastRefreshMs, clientReloginAllowedBeforeLogout);
    testLoginContextFactory.configure(mockLoginContext, testExpiringCredentialRefreshingLogin);
    /*
         * Perform the login, wait up to a certain amount of time for the refresher
         * thread to exit, and make sure the correct calls happened at the correct times
         */
    long expectedFinalMs = startMs + numExpectedRefreshes * 1000 * 60 * refreshEveryMinutes;
    assertFalse(testLoginContextFactory.refresherThreadStartedFuture().isDone());
    assertFalse(testLoginContextFactory.refresherThreadDoneFuture().isDone());
    testExpiringCredentialRefreshingLogin.login();
    assertTrue(testLoginContextFactory.refresherThreadStartedFuture().isDone());
    testLoginContextFactory.refresherThreadDoneFuture().get(1L, TimeUnit.SECONDS);
    assertEquals(expectedFinalMs, mockTime.milliseconds());
    for (int i = 0; i < numExpectedRefreshes; ++i) {
        KafkaFutureImpl<Long> waiter = waiters.get(i);
        assertTrue(waiter.isDone());
        assertEquals((i + 1) * 1000 * 60 * refreshEveryMinutes, waiter.get().longValue() - startMs);
    }
    assertFalse(waiters.get(numExpectedRefreshes).isDone());
    InOrder inOrder = inOrder(mockLoginContext);
    inOrder.verify(mockLoginContext).login();
    for (int i = 0; i < numExpectedRefreshes; ++i) {
        inOrder.verify(mockLoginContext).login();
        inOrder.verify(mockLoginContext).logout();
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) InOrder(org.mockito.InOrder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 3 with MockScheduler

use of org.apache.kafka.common.utils.MockScheduler in project kafka by apache.

the class ExpiringCredentialRefreshingLoginTest method testRefreshWithMinPeriodIntrusion.

@Test
public void testRefreshWithMinPeriodIntrusion() throws Exception {
    int numExpectedRefreshes = 1;
    boolean clientReloginAllowedBeforeLogout = true;
    Subject subject = new Subject();
    final LoginContext mockLoginContext = mock(LoginContext.class);
    when(mockLoginContext.getSubject()).thenReturn(subject);
    MockTime mockTime = new MockTime();
    long startMs = mockTime.milliseconds();
    /*
         * Identify the lifetime of each expiring credential
         */
    long lifetimeMinutes = 10L;
    /*
         * Identify the point at which refresh will occur in that lifetime
         */
    long refreshEveryMinutes = 8L;
    /*
         * Set an absolute last refresh time that will cause the login thread to exit
         * after a certain number of re-logins (by adding an extra half of a refresh
         * interval).
         */
    long absoluteLastRefreshMs = startMs + (1 + numExpectedRefreshes) * 1000 * 60 * refreshEveryMinutes - 1000 * 60 * refreshEveryMinutes / 2;
    /*
         * Identify a minimum period that will cause the refresh time to be delayed a
         * bit.
         */
    int bufferIntrusionSeconds = 1;
    short minPeriodSeconds = (short) (refreshEveryMinutes * 60 + bufferIntrusionSeconds);
    short bufferSeconds = (short) 0;
    /*
         * Define some listeners so we can keep track of who gets done and when. All
         * added listeners should end up done except the last, extra one, which should
         * not.
         */
    MockScheduler mockScheduler = new MockScheduler(mockTime);
    List<KafkaFutureImpl<Long>> waiters = addWaiters(mockScheduler, 1000 * (60 * refreshEveryMinutes + bufferIntrusionSeconds), numExpectedRefreshes + 1);
    // Create the ExpiringCredentialRefreshingLogin instance under test
    TestLoginContextFactory testLoginContextFactory = new TestLoginContextFactory();
    TestExpiringCredentialRefreshingLogin testExpiringCredentialRefreshingLogin = new TestExpiringCredentialRefreshingLogin(refreshConfigThatPerformsReloginEveryGivenPercentageOfLifetime(1.0 * refreshEveryMinutes / lifetimeMinutes, minPeriodSeconds, bufferSeconds, clientReloginAllowedBeforeLogout), testLoginContextFactory, mockTime, 1000 * 60 * lifetimeMinutes, absoluteLastRefreshMs, clientReloginAllowedBeforeLogout);
    testLoginContextFactory.configure(mockLoginContext, testExpiringCredentialRefreshingLogin);
    /*
         * Perform the login, wait up to a certain amount of time for the refresher
         * thread to exit, and make sure the correct calls happened at the correct times
         */
    long expectedFinalMs = startMs + numExpectedRefreshes * 1000 * (60 * refreshEveryMinutes + bufferIntrusionSeconds);
    assertFalse(testLoginContextFactory.refresherThreadStartedFuture().isDone());
    assertFalse(testLoginContextFactory.refresherThreadDoneFuture().isDone());
    testExpiringCredentialRefreshingLogin.login();
    assertTrue(testLoginContextFactory.refresherThreadStartedFuture().isDone());
    testLoginContextFactory.refresherThreadDoneFuture().get(1L, TimeUnit.SECONDS);
    assertEquals(expectedFinalMs, mockTime.milliseconds());
    for (int i = 0; i < numExpectedRefreshes; ++i) {
        KafkaFutureImpl<Long> waiter = waiters.get(i);
        assertTrue(waiter.isDone());
        assertEquals((i + 1) * 1000 * (60 * refreshEveryMinutes + bufferIntrusionSeconds), waiter.get().longValue() - startMs);
    }
    assertFalse(waiters.get(numExpectedRefreshes).isDone());
    InOrder inOrder = inOrder(mockLoginContext);
    inOrder.verify(mockLoginContext).login();
    for (int i = 0; i < numExpectedRefreshes; ++i) {
        inOrder.verify(mockLoginContext).login();
        inOrder.verify(mockLoginContext).logout();
    }
}
Also used : MockScheduler(org.apache.kafka.common.utils.MockScheduler) InOrder(org.mockito.InOrder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Subject(javax.security.auth.Subject) LoginContext(javax.security.auth.login.LoginContext) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 4 with MockScheduler

use of org.apache.kafka.common.utils.MockScheduler 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 5 with MockScheduler

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

Aggregations

MockScheduler (org.apache.kafka.common.utils.MockScheduler)31 MockTime (org.apache.kafka.common.utils.MockTime)31 ExpectedTaskBuilder (org.apache.kafka.trogdor.common.ExpectedTasks.ExpectedTaskBuilder)24 Test (org.junit.jupiter.api.Test)24 ExpectedTasks (org.apache.kafka.trogdor.common.ExpectedTasks)22 WorkerRunning (org.apache.kafka.trogdor.rest.WorkerRunning)19 NoOpTaskSpec (org.apache.kafka.trogdor.task.NoOpTaskSpec)18 Scheduler (org.apache.kafka.common.utils.Scheduler)15 MiniTrogdorCluster (org.apache.kafka.trogdor.common.MiniTrogdorCluster)15 TextNode (com.fasterxml.jackson.databind.node.TextNode)14 CreateTaskRequest (org.apache.kafka.trogdor.rest.CreateTaskRequest)14 WorkerDone (org.apache.kafka.trogdor.rest.WorkerDone)14 TaskPending (org.apache.kafka.trogdor.rest.TaskPending)12 TaskRunning (org.apache.kafka.trogdor.rest.TaskRunning)12 CreateWorkerRequest (org.apache.kafka.trogdor.rest.CreateWorkerRequest)10 TaskDone (org.apache.kafka.trogdor.rest.TaskDone)9 Test (org.junit.Test)7 DestroyTaskRequest (org.apache.kafka.trogdor.rest.DestroyTaskRequest)6 StopTaskRequest (org.apache.kafka.trogdor.rest.StopTaskRequest)6 Subject (javax.security.auth.Subject)5