Search in sources :

Example 1 with AssignmentConfigs

use of org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs in project kafka by apache.

the class HighAvailabilityTaskAssignorTest method shouldAssignActiveStatefulTasksEvenlyOverClientsWithMoreClientsThanTasks.

@Test
public void shouldAssignActiveStatefulTasksEvenlyOverClientsWithMoreClientsThanTasks() {
    final Set<TaskId> allTaskIds = mkSet(TASK_0_0, TASK_0_1);
    final Map<TaskId, Long> lags = allTaskIds.stream().collect(Collectors.toMap(k -> k, k -> 10L));
    final ClientState clientState1 = new ClientState(emptySet(), emptySet(), lags, 1);
    final ClientState clientState2 = new ClientState(emptySet(), emptySet(), lags, 1);
    final ClientState clientState3 = new ClientState(emptySet(), emptySet(), lags, 1);
    final Map<UUID, ClientState> clientStates = getClientStatesMap(clientState1, clientState2, clientState3);
    final boolean unstable = new HighAvailabilityTaskAssignor().assign(clientStates, allTaskIds, allTaskIds, new AssignmentConfigs(0L, 1, 0, 60_000L));
    assertThat(unstable, is(false));
    assertValidAssignment(0, allTaskIds, emptySet(), clientStates, new StringBuilder());
    assertBalancedActiveAssignment(clientStates, new StringBuilder());
    assertBalancedStatefulAssignment(allTaskIds, clientStates, new StringBuilder());
    assertBalancedTasks(clientStates);
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Matchers.not(org.hamcrest.Matchers.not) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) AssignmentTestUtils.analyzeTaskAssignmentBalance(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.analyzeTaskAssignmentBalance) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) AssignmentTestUtils.hasStandbyTasks(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.hasStandbyTasks) Collections.singletonMap(java.util.Collections.singletonMap) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UUID_1(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.UUID_1) AssignmentTestUtils.hasAssignedTasks(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.hasAssignedTasks) UUID_3(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.UUID_3) AssignmentTestUtils.assertValidAssignment(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.assertValidAssignment) UUID_2(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.UUID_2) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Collections.emptySet(java.util.Collections.emptySet) TASK_0_0(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_0) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) TASK_0_1(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_1) TASK_1_1(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_1) TASK_2_0(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_2_0) Set(java.util.Set) TASK_0_2(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_2) TASK_1_0(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_0) Test(org.junit.Test) TASK_0_3(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_3) TASK_1_3(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_3) TASK_2_2(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_2_2) UUID(java.util.UUID) TASK_1_2(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_1_2) TASK_2_1(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_2_1) AssignmentTestUtils.assertBalancedActiveAssignment(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.assertBalancedActiveAssignment) Collectors(java.util.stream.Collectors) EMPTY_TASKS(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.EMPTY_TASKS) AssignmentTestUtils.getClientStatesMap(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.getClientStatesMap) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) AssignmentTestUtils.assertBalancedStatefulAssignment(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.assertBalancedStatefulAssignment) Matchers.is(org.hamcrest.Matchers.is) AssignmentTestUtils.hasActiveTasks(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.hasActiveTasks) AssignmentTestUtils.assertBalancedTasks(org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.assertBalancedTasks) TaskId(org.apache.kafka.streams.processor.TaskId) AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with AssignmentConfigs

use of org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs in project kafka by apache.

the class TaskAssignorConvergenceTest method staticAssignmentShouldConvergeWithTheFirstAssignment.

@Test
public void staticAssignmentShouldConvergeWithTheFirstAssignment() {
    final AssignmentConfigs configs = new AssignmentConfigs(100L, 2, 0, 60_000L);
    final Harness harness = Harness.initializeCluster(1, 1, 1, () -> 1);
    testForConvergence(harness, configs, 1);
    verifyValidAssignment(0, harness);
    verifyBalancedAssignment(harness);
}
Also used : AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs) Test(org.junit.Test)

Example 3 with AssignmentConfigs

use of org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs in project kafka by apache.

the class TaskAssignorConvergenceTest method runRandomizedScenario.

private static void runRandomizedScenario(final long seed) {
    Harness harness = null;
    try {
        final Random prng = new Random(seed);
        // These are all rand(limit)+1 because we need them to be at least 1 and the upper bound is exclusive
        final int initialClusterSize = prng.nextInt(10) + 1;
        final int numStatelessTasks = prng.nextInt(10) + 1;
        final int numStatefulTasks = prng.nextInt(10) + 1;
        final int maxWarmupReplicas = prng.nextInt(numStatefulTasks) + 1;
        // This one is rand(limit+1) because we _want_ to test zero and the upper bound is exclusive
        final int numStandbyReplicas = prng.nextInt(initialClusterSize + 1);
        final int numberOfEvents = prng.nextInt(10) + 1;
        final AssignmentConfigs configs = new AssignmentConfigs(100L, maxWarmupReplicas, numStandbyReplicas, 60_000L);
        harness = Harness.initializeCluster(numStatelessTasks, numStatefulTasks, initialClusterSize, () -> prng.nextInt(10) + 1);
        testForConvergence(harness, configs, 1);
        verifyValidAssignment(numStandbyReplicas, harness);
        verifyBalancedAssignment(harness);
        for (int i = 0; i < numberOfEvents; i++) {
            final int event = prng.nextInt(2);
            switch(event) {
                case 0:
                    harness.dropRandomNodes(prng.nextInt(initialClusterSize), prng);
                    break;
                case 1:
                    harness.addOrResurrectNodesRandomly(prng, initialClusterSize);
                    break;
                default:
                    throw new IllegalStateException("Unexpected event: " + event);
            }
            if (!harness.clientStates.isEmpty()) {
                testForConvergence(harness, configs, 2 * (numStatefulTasks + numStatefulTasks * numStandbyReplicas));
                verifyValidAssignment(numStandbyReplicas, harness);
                verifyBalancedAssignment(harness);
            }
        }
    } catch (final AssertionError t) {
        throw new AssertionError("Assertion failed in randomized test. Reproduce with: `runRandomizedScenario(" + seed + ")`.", t);
    } catch (final Throwable t) {
        final StringBuilder builder = new StringBuilder().append("Exception in randomized scenario. Reproduce with: `runRandomizedScenario(").append(seed).append(")`. ");
        if (harness != null) {
            builder.append(harness.history);
        }
        throw new AssertionError(builder.toString(), t);
    }
}
Also used : Random(java.util.Random) AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs)

Example 4 with AssignmentConfigs

use of org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs in project kafka by apache.

the class TaskAssignorConvergenceTest method assignmentShouldConvergeAfterAddingNode.

@Test
public void assignmentShouldConvergeAfterAddingNode() {
    final int numStatelessTasks = 7;
    final int numStatefulTasks = 11;
    final int maxWarmupReplicas = 2;
    final int numStandbyReplicas = 0;
    final AssignmentConfigs configs = new AssignmentConfigs(100L, maxWarmupReplicas, numStandbyReplicas, 60_000L);
    final Harness harness = Harness.initializeCluster(numStatelessTasks, numStatefulTasks, 1, () -> 5);
    testForConvergence(harness, configs, 1);
    harness.addNode();
    // we expect convergence to involve moving each task at most once, and we can move "maxWarmupReplicas" number
    // of tasks at once, hence the iteration limit
    testForConvergence(harness, configs, numStatefulTasks / maxWarmupReplicas + 1);
    verifyValidAssignment(numStandbyReplicas, harness);
    verifyBalancedAssignment(harness);
}
Also used : AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs) Test(org.junit.Test)

Example 5 with AssignmentConfigs

use of org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs in project kafka by apache.

the class TaskAssignorConvergenceTest method droppingNodesShouldConverge.

@Test
public void droppingNodesShouldConverge() {
    final int numStatelessTasks = 11;
    final int numStatefulTasks = 13;
    final int maxWarmupReplicas = 2;
    final int numStandbyReplicas = 0;
    final AssignmentConfigs configs = new AssignmentConfigs(100L, maxWarmupReplicas, numStandbyReplicas, 60_000L);
    final Harness harness = Harness.initializeCluster(numStatelessTasks, numStatefulTasks, 7, () -> 5);
    testForConvergence(harness, configs, 1);
    harness.dropNode();
    // This time, we allow one extra iteration because the
    // first stateful task needs to get shuffled back to the first node
    testForConvergence(harness, configs, numStatefulTasks / maxWarmupReplicas + 2);
    verifyValidAssignment(numStandbyReplicas, harness);
    verifyBalancedAssignment(harness);
}
Also used : AssignmentConfigs(org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs) Test(org.junit.Test)

Aggregations

AssignmentConfigs (org.apache.kafka.streams.processor.internals.assignment.AssignorConfiguration.AssignmentConfigs)21 Test (org.junit.Test)20 UUID (java.util.UUID)17 TaskId (org.apache.kafka.streams.processor.TaskId)17 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 Collections.emptySet (java.util.Collections.emptySet)9 Collections.singleton (java.util.Collections.singleton)9 Collections.singletonMap (java.util.Collections.singletonMap)9 Map (java.util.Map)9 Set (java.util.Set)9 Collectors (java.util.stream.Collectors)9 Utils.mkEntry (org.apache.kafka.common.utils.Utils.mkEntry)9 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)9 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)9 EMPTY_TASKS (org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.EMPTY_TASKS)9 TASK_0_0 (org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_0)9 TASK_0_1 (org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_1)9 TASK_0_2 (org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_2)9 TASK_0_3 (org.apache.kafka.streams.processor.internals.assignment.AssignmentTestUtils.TASK_0_3)9