Search in sources :

Example 21 with MockScheduler

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

the class ExpiringCredentialRefreshingLoginTest method testRefresh.

@Test
public void testRefresh() throws Exception {
    for (int numExpectedRefreshes : new int[] { 0, 1, 2 }) {
        for (boolean clientReloginAllowedBeforeLogout : new boolean[] { true, false }) {
            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 = 100L;
            /*
                 * Identify the point at which refresh will occur in that lifetime
                 */
            long refreshEveryMinutes = 80L;
            /*
                 * 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
                 */
            short minPeriodSeconds = (short) 0;
            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());
            /*
                 * We expect login() to be invoked followed by getSubject() and then ultimately followed by
                 * numExpectedRefreshes pairs of either login()/logout() or logout()/login() calls
                 */
            InOrder inOrder = inOrder(mockLoginContext);
            inOrder.verify(mockLoginContext).login();
            inOrder.verify(mockLoginContext).getSubject();
            for (int i = 0; i < numExpectedRefreshes; ++i) {
                if (clientReloginAllowedBeforeLogout) {
                    inOrder.verify(mockLoginContext).login();
                    inOrder.verify(mockLoginContext).logout();
                } else {
                    inOrder.verify(mockLoginContext).logout();
                    inOrder.verify(mockLoginContext).login();
                }
            }
            testExpiringCredentialRefreshingLogin.close();
        }
    }
}
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 22 with MockScheduler

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

the class ExpiringCredentialRefreshingLoginTest method testRefreshWithPreExpirationBufferIntrusion.

@Test
public void testRefreshWithPreExpirationBufferIntrusion() 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 bufferSeconds = (short) ((lifetimeMinutes - refreshEveryMinutes) * 60 + bufferIntrusionSeconds);
    short minPeriodSeconds = (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 23 with MockScheduler

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

the class ExpiringCredentialRefreshingLoginTest method testRefreshWithExpirationSmallerThanConfiguredBuffersAndOlderCreateTime.

@Test
public void testRefreshWithExpirationSmallerThanConfiguredBuffersAndOlderCreateTime() 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) {

        @Override
        public long getCreateMs() {
            // distant past
            return super.getCreateMs() - 1000 * 60 * 60;
        }
    };
    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 24 with MockScheduler

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

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());
        // Re-creating a task with the same arguments is not an error.
        cluster.coordinatorClient().createTask(new CreateTaskRequest("foo", fooSpec));
        // Re-creating a task with different arguments gives a RequestConflictException.
        NoOpTaskSpec barSpec = new NoOpTaskSpec(1000, 2000);
        assertThrows(RequestConflictException.class, () -> cluster.coordinatorClient().createTask(new CreateTaskRequest("foo", barSpec)), "Recreating task with different task spec is not allowed");
        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()).waitFor(cluster.coordinatorClient()).waitFor(cluster.agentClient("node02"));
        time.sleep(3);
        new ExpectedTasks().addTask(new ExpectedTaskBuilder("foo").taskState(new TaskDone(fooSpec, 2, 5, "", false, new TextNode("done"))).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) 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) TaskRunning(org.apache.kafka.trogdor.rest.TaskRunning) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 25 with MockScheduler

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

the class AgentTest method testAgentGetUptime.

@Test
public void testAgentGetUptime() throws Exception {
    MockTime time = new MockTime(0, 111, 0);
    MockScheduler scheduler = new MockScheduler(time);
    Agent agent = createAgent(scheduler);
    AgentClient client = new AgentClient.Builder().maxTries(10).target("localhost", agent.port()).build();
    UptimeResponse uptime = client.uptime();
    assertEquals(agent.uptime(), uptime);
    time.setCurrentTimeMs(150);
    assertNotEquals(agent.uptime(), uptime);
    agent.beginShutdown();
    agent.waitForShutdown();
}
Also used : UptimeResponse(org.apache.kafka.trogdor.rest.UptimeResponse) MockScheduler(org.apache.kafka.common.utils.MockScheduler) 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