Search in sources :

Example 1 with ElectLeadersRequestData

use of org.apache.kafka.common.message.ElectLeadersRequestData in project kafka by apache.

the class QuorumControllerTest method testTimeouts.

/**
 * Test that certain controller operations time out if they stay on the controller
 * queue for too long.
 */
@Test
public void testTimeouts() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
            QuorumController controller = controlEnv.activeController();
            CountDownLatch countDownLatch = controller.pause();
            CompletableFuture<CreateTopicsResponseData> createFuture = controller.createTopics(new CreateTopicsRequestData().setTimeoutMs(0).setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo")).iterator())));
            long now = controller.time().nanoseconds();
            CompletableFuture<Map<Uuid, ApiError>> deleteFuture = controller.deleteTopics(now, Collections.singletonList(Uuid.ZERO_UUID));
            CompletableFuture<Map<String, ResultOrError<Uuid>>> findTopicIdsFuture = controller.findTopicIds(now, Collections.singletonList("foo"));
            CompletableFuture<Map<Uuid, ResultOrError<String>>> findTopicNamesFuture = controller.findTopicNames(now, Collections.singletonList(Uuid.ZERO_UUID));
            CompletableFuture<List<CreatePartitionsTopicResult>> createPartitionsFuture = controller.createPartitions(now, Collections.singletonList(new CreatePartitionsTopic()));
            CompletableFuture<ElectLeadersResponseData> electLeadersFuture = controller.electLeaders(new ElectLeadersRequestData().setTimeoutMs(0).setTopicPartitions(null));
            CompletableFuture<AlterPartitionReassignmentsResponseData> alterReassignmentsFuture = controller.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTimeoutMs(0).setTopics(Collections.singletonList(new ReassignableTopic())));
            CompletableFuture<ListPartitionReassignmentsResponseData> listReassignmentsFuture = controller.listPartitionReassignments(new ListPartitionReassignmentsRequestData().setTopics(null).setTimeoutMs(0));
            while (controller.time().nanoseconds() == now) {
                Thread.sleep(0, 10);
            }
            countDownLatch.countDown();
            assertYieldsTimeout(createFuture);
            assertYieldsTimeout(deleteFuture);
            assertYieldsTimeout(findTopicIdsFuture);
            assertYieldsTimeout(findTopicNamesFuture);
            assertYieldsTimeout(createPartitionsFuture);
            assertYieldsTimeout(electLeadersFuture);
            assertYieldsTimeout(alterReassignmentsFuture);
            assertYieldsTimeout(listReassignmentsFuture);
        }
    }
}
Also used : ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) List(java.util.List) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) CountDownLatch(java.util.concurrent.CountDownLatch) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) Map(java.util.Map) HashMap(java.util.HashMap) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) Test(org.junit.jupiter.api.Test)

Example 2 with ElectLeadersRequestData

use of org.apache.kafka.common.message.ElectLeadersRequestData in project kafka by apache.

the class QuorumControllerTest method testEarlyControllerResults.

/**
 * Test that certain controller operations finish immediately without putting an event
 * on the controller queue, if there is nothing to do.
 */
@Test
public void testEarlyControllerResults() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
            QuorumController controller = controlEnv.activeController();
            CountDownLatch countDownLatch = controller.pause();
            CompletableFuture<CreateTopicsResponseData> createFuture = controller.createTopics(new CreateTopicsRequestData().setTimeoutMs(120000));
            long deadlineMs = controller.time().nanoseconds() + HOURS.toNanos(1);
            CompletableFuture<Map<Uuid, ApiError>> deleteFuture = controller.deleteTopics(deadlineMs, Collections.emptyList());
            CompletableFuture<Map<String, ResultOrError<Uuid>>> findTopicIdsFuture = controller.findTopicIds(deadlineMs, Collections.emptyList());
            CompletableFuture<Map<Uuid, ResultOrError<String>>> findTopicNamesFuture = controller.findTopicNames(deadlineMs, Collections.emptyList());
            CompletableFuture<List<CreatePartitionsTopicResult>> createPartitionsFuture = controller.createPartitions(deadlineMs, Collections.emptyList());
            CompletableFuture<ElectLeadersResponseData> electLeadersFuture = controller.electLeaders(new ElectLeadersRequestData().setTimeoutMs(120000));
            CompletableFuture<AlterPartitionReassignmentsResponseData> alterReassignmentsFuture = controller.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTimeoutMs(12000));
            createFuture.get();
            deleteFuture.get();
            findTopicIdsFuture.get();
            findTopicNamesFuture.get();
            createPartitionsFuture.get();
            electLeadersFuture.get();
            alterReassignmentsFuture.get();
            countDownLatch.countDown();
        }
    }
}
Also used : LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) CountDownLatch(java.util.concurrent.CountDownLatch) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Uuid(org.apache.kafka.common.Uuid) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 3 with ElectLeadersRequestData

use of org.apache.kafka.common.message.ElectLeadersRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testPreferredElectionDoesNotTriggerUncleanElection.

@Test
public void testPreferredElectionDoesNotTriggerUncleanElection() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(1, 2, 3, 4);
    ctx.unfenceBrokers(1, 2, 3, 4);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 } }).topicId();
    TopicIdPartition partition = new TopicIdPartition(fooId, 0);
    ctx.fenceBrokers(Utils.mkSet(2, 3));
    ctx.fenceBrokers(Utils.mkSet(1, 2, 3));
    ctx.unfenceBrokers(Utils.mkSet(2));
    assertLeaderAndIsr(replication, partition, NO_LEADER, new int[] { 1 });
    ctx.alterTopicConfig("foo", "unclean.leader.election.enable", "true");
    ElectLeadersRequestData request = buildElectLeadersRequest(ElectionType.PREFERRED, singletonMap("foo", singletonList(0)));
    // No election should be done even though unclean election is available
    ControllerResult<ElectLeadersResponseData> result = replication.electLeaders(request);
    assertEquals(Collections.emptyList(), result.records());
    ElectLeadersResponseData expectedResponse = buildElectLeadersResponse(NONE, false, singletonMap(new TopicPartition("foo", 0), new ApiError(PREFERRED_LEADER_NOT_AVAILABLE)));
    assertEquals(expectedResponse, result.response());
}
Also used : Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) ApiError(org.apache.kafka.common.requests.ApiError) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with ElectLeadersRequestData

use of org.apache.kafka.common.message.ElectLeadersRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testElectUncleanLeaders.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testElectUncleanLeaders(boolean electAllPartitions) throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3, 4);
    ctx.unfenceBrokers(0, 1, 2, 3, 4);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 2, 3, 4 }, new int[] { 0, 2, 1 } }).topicId();
    TopicIdPartition partition0 = new TopicIdPartition(fooId, 0);
    TopicIdPartition partition1 = new TopicIdPartition(fooId, 1);
    TopicIdPartition partition2 = new TopicIdPartition(fooId, 2);
    ctx.fenceBrokers(Utils.mkSet(2, 3));
    ctx.fenceBrokers(Utils.mkSet(1, 2, 3));
    assertLeaderAndIsr(replication, partition0, NO_LEADER, new int[] { 1 });
    assertLeaderAndIsr(replication, partition1, 4, new int[] { 4 });
    assertLeaderAndIsr(replication, partition2, 0, new int[] { 0 });
    ElectLeadersRequestData request = buildElectLeadersRequest(ElectionType.UNCLEAN, electAllPartitions ? null : singletonMap("foo", asList(0, 1, 2)));
    // No election can be done yet because no replicas are available for partition 0
    ControllerResult<ElectLeadersResponseData> result1 = replication.electLeaders(request);
    assertEquals(Collections.emptyList(), result1.records());
    ElectLeadersResponseData expectedResponse1 = buildElectLeadersResponse(NONE, electAllPartitions, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), new ApiError(ELIGIBLE_LEADERS_NOT_AVAILABLE)), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("foo", 2), new ApiError(ELECTION_NOT_NEEDED))));
    assertElectLeadersResponse(expectedResponse1, result1.response());
    // Now we bring 2 back online which should allow the unclean election of partition 0
    ctx.unfenceBrokers(Utils.mkSet(2));
    // Bring 2 back into the ISR for partition 1. This allows us to verify that
    // preferred election does not occur as a result of the unclean election request.
    ctx.alterIsr(partition1, 4, asList(2, 4));
    ControllerResult<ElectLeadersResponseData> result = replication.electLeaders(request);
    assertEquals(1, result.records().size());
    ApiMessageAndVersion record = result.records().get(0);
    assertTrue(record.message() instanceof PartitionChangeRecord);
    PartitionChangeRecord partitionChangeRecord = (PartitionChangeRecord) record.message();
    assertEquals(0, partitionChangeRecord.partitionId());
    assertEquals(2, partitionChangeRecord.leader());
    assertEquals(singletonList(2), partitionChangeRecord.isr());
    ctx.replay(result.records());
    assertLeaderAndIsr(replication, partition0, 2, new int[] { 2 });
    assertLeaderAndIsr(replication, partition1, 4, new int[] { 2, 4 });
    assertLeaderAndIsr(replication, partition2, 0, new int[] { 0 });
    ElectLeadersResponseData expectedResponse = buildElectLeadersResponse(NONE, electAllPartitions, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), ApiError.NONE), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("foo", 2), new ApiError(ELECTION_NOT_NEEDED))));
    assertElectLeadersResponse(expectedResponse, result.response());
}
Also used : Uuid(org.apache.kafka.common.Uuid) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) TopicPartition(org.apache.kafka.common.TopicPartition) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) TopicIdPartition(org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition) ApiError(org.apache.kafka.common.requests.ApiError) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with ElectLeadersRequestData

use of org.apache.kafka.common.message.ElectLeadersRequestData in project kafka by apache.

the class ReplicationControlManagerTest method testElectPreferredLeaders.

@Test
public void testElectPreferredLeaders() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3, 4);
    ctx.unfenceBrokers(2, 3, 4);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 2, 3, 4 }, new int[] { 0, 2, 1 } }).topicId();
    ElectLeadersRequestData request1 = new ElectLeadersRequestData().setElectionType(ElectionType.PREFERRED.value).setTopicPartitions(new TopicPartitionsCollection(asList(new TopicPartitions().setTopic("foo").setPartitions(asList(0, 1)), new TopicPartitions().setTopic("bar").setPartitions(asList(0, 1))).iterator()));
    ControllerResult<ElectLeadersResponseData> election1Result = replication.electLeaders(request1);
    ElectLeadersResponseData expectedResponse1 = buildElectLeadersResponse(NONE, false, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), new ApiError(PREFERRED_LEADER_NOT_AVAILABLE)), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("bar", 0), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar")), Utils.mkEntry(new TopicPartition("bar", 1), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar"))));
    assertElectLeadersResponse(expectedResponse1, election1Result.response());
    assertEquals(Collections.emptyList(), election1Result.records());
    ctx.unfenceBrokers(0, 1);
    ControllerResult<AlterIsrResponseData> alterIsrResult = replication.alterIsr(new AlterIsrRequestData().setBrokerId(2).setBrokerEpoch(102).setTopics(asList(new AlterIsrRequestData.TopicData().setName("foo").setPartitions(asList(new AlterIsrRequestData.PartitionData().setPartitionIndex(0).setCurrentIsrVersion(0).setLeaderEpoch(0).setNewIsr(asList(1, 2, 3)))))));
    assertEquals(new AlterIsrResponseData().setTopics(asList(new AlterIsrResponseData.TopicData().setName("foo").setPartitions(asList(new AlterIsrResponseData.PartitionData().setPartitionIndex(0).setLeaderId(2).setLeaderEpoch(0).setIsr(asList(1, 2, 3)).setCurrentIsrVersion(1).setErrorCode(NONE.code()))))), alterIsrResult.response());
    ElectLeadersResponseData expectedResponse2 = buildElectLeadersResponse(NONE, false, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), ApiError.NONE), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("bar", 0), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar")), Utils.mkEntry(new TopicPartition("bar", 1), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar"))));
    ctx.replay(alterIsrResult.records());
    ControllerResult<ElectLeadersResponseData> election2Result = replication.electLeaders(request1);
    assertElectLeadersResponse(expectedResponse2, election2Result.response());
    assertEquals(asList(new ApiMessageAndVersion(new PartitionChangeRecord().setPartitionId(0).setTopicId(fooId).setLeader(1), (short) 0)), election2Result.records());
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) ApiError(org.apache.kafka.common.requests.ApiError) TopicPartitionsCollection(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitionsCollection) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

Uuid (org.apache.kafka.common.Uuid)5 ElectLeadersRequestData (org.apache.kafka.common.message.ElectLeadersRequestData)5 ElectLeadersResponseData (org.apache.kafka.common.message.ElectLeadersResponseData)5 Test (org.junit.jupiter.api.Test)4 TopicPartition (org.apache.kafka.common.TopicPartition)3 ApiError (org.apache.kafka.common.requests.ApiError)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)2 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)2 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)2 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)2 PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)2 TopicIdPartition (org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition)2 LocalLogManagerTestEnv (org.apache.kafka.metalog.LocalLogManagerTestEnv)2 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)2 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)1