use of org.apache.kafka.common.message.CreateTopicsRequestData 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.CreateTopicsRequestData in project kafka by apache.
the class QuorumControllerTest method testMissingInMemorySnapshot.
@Test
public void testMissingInMemorySnapshot() throws Exception {
int numBrokers = 3;
int numPartitions = 3;
String topicName = "topic-name";
try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty());
QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
QuorumController controller = controlEnv.activeController();
Map<Integer, Long> brokerEpochs = registerBrokers(controller, numBrokers);
// Create a lot of partitions
List<CreatableReplicaAssignment> partitions = IntStream.range(0, numPartitions).mapToObj(partitionIndex -> new CreatableReplicaAssignment().setPartitionIndex(partitionIndex).setBrokerIds(Arrays.asList(0, 1, 2))).collect(Collectors.toList());
Uuid topicId = controller.createTopics(new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName(topicName).setNumPartitions(-1).setReplicationFactor((short) -1).setAssignments(new CreatableReplicaAssignmentCollection(partitions.iterator()))).iterator()))).get().topics().find(topicName).topicId();
// Create a lot of alter isr
List<AlterIsrRequestData.PartitionData> alterIsrs = IntStream.range(0, numPartitions).mapToObj(partitionIndex -> {
PartitionRegistration partitionRegistration = controller.replicationControl().getPartition(topicId, partitionIndex);
return new AlterIsrRequestData.PartitionData().setPartitionIndex(partitionIndex).setLeaderEpoch(partitionRegistration.leaderEpoch).setCurrentIsrVersion(partitionRegistration.partitionEpoch).setNewIsr(Arrays.asList(0, 1));
}).collect(Collectors.toList());
AlterIsrRequestData.TopicData topicData = new AlterIsrRequestData.TopicData().setName(topicName);
topicData.partitions().addAll(alterIsrs);
int leaderId = 0;
AlterIsrRequestData alterIsrRequest = new AlterIsrRequestData().setBrokerId(leaderId).setBrokerEpoch(brokerEpochs.get(leaderId));
alterIsrRequest.topics().add(topicData);
logEnv.logManagers().get(0).resignAfterNonAtomicCommit();
int oldClaimEpoch = controller.curClaimEpoch();
assertThrows(ExecutionException.class, () -> controller.alterIsr(alterIsrRequest).get());
// Wait for the controller to become active again
assertSame(controller, controlEnv.activeController());
assertTrue(oldClaimEpoch < controller.curClaimEpoch(), String.format("oldClaimEpoch = %s, newClaimEpoch = %s", oldClaimEpoch, controller.curClaimEpoch()));
// Since the alterIsr partially failed we expect to see
// some partitions to still have 2 in the ISR.
int partitionsWithReplica2 = Utils.toList(controller.replicationControl().brokersToIsrs().partitionsWithBrokerInIsr(2)).size();
int partitionsWithReplica0 = Utils.toList(controller.replicationControl().brokersToIsrs().partitionsWithBrokerInIsr(0)).size();
assertEquals(numPartitions, partitionsWithReplica0);
assertNotEquals(0, partitionsWithReplica2);
assertTrue(partitionsWithReplica0 > partitionsWithReplica2, String.format("partitionsWithReplica0 = %s, partitionsWithReplica2 = %s", partitionsWithReplica0, partitionsWithReplica2));
}
}
use of org.apache.kafka.common.message.CreateTopicsRequestData 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.CreateTopicsRequestData in project kafka by apache.
the class InternalTopicManagerTest method shouldThrowInformativeExceptionForOlderBrokers.
@Test
public void shouldThrowInformativeExceptionForOlderBrokers() {
final AdminClient admin = new MockAdminClient() {
@Override
public CreateTopicsResult createTopics(final Collection<NewTopic> newTopics, final CreateTopicsOptions options) {
final CreatableTopic topicToBeCreated = new CreatableTopic();
topicToBeCreated.setAssignments(new CreatableReplicaAssignmentCollection());
topicToBeCreated.setNumPartitions((short) 1);
// set unsupported replication factor for older brokers
topicToBeCreated.setReplicationFactor((short) -1);
final CreatableTopicCollection topicsToBeCreated = new CreatableTopicCollection();
topicsToBeCreated.add(topicToBeCreated);
try {
new CreateTopicsRequest.Builder(new CreateTopicsRequestData().setTopics(topicsToBeCreated).setTimeoutMs(0).setValidateOnly(options.shouldValidateOnly())).build(// pass in old unsupported request version for old brokers
(short) 3);
throw new IllegalStateException("Building CreateTopicRequest should have thrown.");
} catch (final UnsupportedVersionException expected) {
final KafkaFutureImpl<TopicMetadataAndConfig> future = new KafkaFutureImpl<>();
future.completeExceptionally(expected);
return new CreateTopicsResult(Collections.singletonMap(topic1, future)) {
};
}
}
};
final StreamsConfig streamsConfig = new StreamsConfig(config);
final InternalTopicManager topicManager = new InternalTopicManager(time, admin, streamsConfig);
final InternalTopicConfig topicConfig = new RepartitionTopicConfig(topic1, Collections.emptyMap());
topicConfig.setNumberOfPartitions(1);
final StreamsException exception = assertThrows(StreamsException.class, () -> topicManager.makeReady(Collections.singletonMap(topic1, topicConfig)));
assertThat(exception.getMessage(), equalTo("Could not create topic " + topic1 + ", because brokers don't support configuration replication.factor=-1." + " You can change the replication.factor config or upgrade your brokers to version 2.4 or newer to avoid this error."));
}
use of org.apache.kafka.common.message.CreateTopicsRequestData in project kafka by apache.
the class RequestResponseTest method testSerializeWithInconsistentHeaderApiKey.
@Test
public void testSerializeWithInconsistentHeaderApiKey() {
CreateTopicsRequest createTopicsRequest = new CreateTopicsRequest.Builder(new CreateTopicsRequestData()).build();
short requestVersion = CREATE_TOPICS.latestVersion();
RequestHeader requestHeader = new RequestHeader(DELETE_TOPICS, requestVersion, "client", 2);
assertThrows(IllegalArgumentException.class, () -> createTopicsRequest.serializeWithHeader(requestHeader));
}
Aggregations