use of org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic in project kafka by apache.
the class ReplicationControlManagerTest method testCreateTopicsWithValidateOnlyFlag.
@Test
public void testCreateTopicsWithValidateOnlyFlag() throws Exception {
ReplicationControlTestContext ctx = new ReplicationControlTestContext();
ctx.registerBrokers(0, 1, 2);
ctx.unfenceBrokers(0, 1, 2);
CreateTopicsRequestData request = new CreateTopicsRequestData().setValidateOnly(true);
request.topics().add(new CreatableTopic().setName("foo").setNumPartitions(1).setReplicationFactor((short) 3));
ControllerResult<CreateTopicsResponseData> result = ctx.replicationControl.createTopics(request);
assertEquals(0, result.records().size());
CreatableTopicResult topicResult = result.response().topics().find("foo");
assertEquals((short) 0, topicResult.errorCode());
}
use of org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic 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.CreatableTopic 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.CreatableTopic in project kafka by apache.
the class MockController method createTopics.
@Override
public synchronized CompletableFuture<CreateTopicsResponseData> createTopics(CreateTopicsRequestData request) {
CreateTopicsResponseData response = new CreateTopicsResponseData();
for (CreatableTopic topic : request.topics()) {
if (topicNameToId.containsKey(topic.name())) {
response.topics().add(new CreatableTopicResult().setName(topic.name()).setErrorCode(Errors.TOPIC_ALREADY_EXISTS.code()));
} else {
long topicId = nextTopicId.getAndIncrement();
Uuid topicUuid = new Uuid(0, topicId);
topicNameToId.put(topic.name(), topicUuid);
topics.put(topicUuid, new MockTopic(topic.name(), topicUuid));
response.topics().add(new CreatableTopicResult().setName(topic.name()).setErrorCode(Errors.NONE.code()).setTopicId(topicUuid));
// For a better mock, we might want to return configs, replication factor,
// etc. Right now, the tests that use MockController don't need these
// things.
}
}
return CompletableFuture.completedFuture(response);
}
use of org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic 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."));
}
Aggregations