use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.
the class TopicAdminTest method describeTopicConfigShouldReturnTopicConfigWhenTopicExists.
@Test
public void describeTopicConfigShouldReturnTopicConfigWhenTopicExists() {
String topicName = "myTopic";
NewTopic newTopic = TopicAdmin.defineTopic(topicName).config(Collections.singletonMap("foo", "bar")).partitions(1).compacted().build();
Cluster cluster = createCluster(1);
try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
mockAdminClient.addTopic(false, topicName, Collections.singletonList(topicPartitionInfo), null);
TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
Map<String, Config> result = admin.describeTopicConfigs(newTopic.name());
assertFalse(result.isEmpty());
assertEquals(1, result.size());
Config config = result.get("myTopic");
assertNotNull(config);
config.entries().forEach(entry -> assertEquals(newTopic.configs().get(entry.name()), entry.value()));
}
}
use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.
the class TopicAdminTest method shouldNotCreateTopicWhenItAlreadyExists.
@Test
public void shouldNotCreateTopicWhenItAlreadyExists() {
NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(1).compacted().build();
Cluster cluster = createCluster(1);
try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
mockAdminClient.addTopic(false, "myTopic", Collections.singletonList(topicPartitionInfo), null);
TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
assertFalse(admin.createTopic(newTopic));
assertTrue(admin.createTopics(newTopic).isEmpty());
assertTrue(admin.createOrFindTopic(newTopic));
TopicAdmin.TopicCreationResponse response = admin.createOrFindTopics(newTopic);
assertTrue(response.isCreatedOrExisting(newTopic.name()));
assertTrue(response.isExisting(newTopic.name()));
assertFalse(response.isCreated(newTopic.name()));
}
}
use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.
the class TopicAdminTest method verifyingTopicCleanupPolicyShouldFailWhenTopicHasDeleteAndCompactPolicy.
@Test
public void verifyingTopicCleanupPolicyShouldFailWhenTopicHasDeleteAndCompactPolicy() {
String topicName = "myTopic";
Map<String, String> topicConfigs = Collections.singletonMap("cleanup.policy", "delete,compact");
Cluster cluster = createCluster(1);
try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
mockAdminClient.addTopic(false, topicName, Collections.singletonList(topicPartitionInfo), topicConfigs);
TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
ConfigException e = assertThrows(ConfigException.class, () -> admin.verifyTopicCleanupPolicyOnlyCompact("myTopic", "worker.topic", "purpose"));
assertTrue(e.getMessage().contains("to guarantee consistency and durability"));
}
}
use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.
the class KafkaAdminClient method getTopicDescriptionFromCluster.
private TopicDescription getTopicDescriptionFromCluster(Cluster cluster, String topicName, Uuid topicId, Integer authorizedOperations) {
boolean isInternal = cluster.internalTopics().contains(topicName);
List<PartitionInfo> partitionInfos = cluster.partitionsForTopic(topicName);
List<TopicPartitionInfo> partitions = new ArrayList<>(partitionInfos.size());
for (PartitionInfo partitionInfo : partitionInfos) {
TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(partitionInfo.partition(), leader(partitionInfo), Arrays.asList(partitionInfo.replicas()), Arrays.asList(partitionInfo.inSyncReplicas()));
partitions.add(topicPartitionInfo);
}
partitions.sort(Comparator.comparingInt(TopicPartitionInfo::partition));
return new TopicDescription(topicName, isInternal, partitions, validAclOperations(authorizedOperations), topicId);
}
use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.
the class WorkerUtilsTest method makeExistingTopicWithOneReplica.
private void makeExistingTopicWithOneReplica(String topicName, int numPartitions) {
List<TopicPartitionInfo> tpInfo = new ArrayList<>();
int brokerIndex = 0;
for (int i = 0; i < numPartitions; ++i) {
Node broker = cluster.get(brokerIndex);
tpInfo.add(new TopicPartitionInfo(i, broker, singleReplica, Collections.<Node>emptyList()));
brokerIndex = (brokerIndex + 1) % cluster.size();
}
adminClient.addTopic(false, topicName, tpInfo, null);
}
Aggregations