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);
}
}
}
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();
}
}
}
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());
}
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());
}
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());
}
Aggregations