Search in sources :

Example 51 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project kafka by apache.

the class InternalTopicManagerTest method shouldOnlyRetryDescribeConfigsWhenDescribeConfigsThrowsLeaderNotAvailableExceptionDuringValidation.

@Test
public void shouldOnlyRetryDescribeConfigsWhenDescribeConfigsThrowsLeaderNotAvailableExceptionDuringValidation() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, new StreamsConfig(config));
    final KafkaFutureImpl<TopicDescription> topicDescriptionSuccessfulFuture = new KafkaFutureImpl<>();
    topicDescriptionSuccessfulFuture.complete(new TopicDescription(topic1, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, cluster, Collections.emptyList()))));
    EasyMock.expect(admin.describeTopics(Collections.singleton(topic1))).andReturn(new MockDescribeTopicsResult(mkMap(mkEntry(topic1, topicDescriptionSuccessfulFuture))));
    final KafkaFutureImpl<Config> topicConfigsFailFuture = new KafkaFutureImpl<>();
    topicConfigsFailFuture.completeExceptionally(new LeaderNotAvailableException("Leader Not Available!"));
    final KafkaFutureImpl<Config> topicConfigSuccessfulFuture = new KafkaFutureImpl<>();
    topicConfigSuccessfulFuture.complete(new Config(repartitionTopicConfig().entrySet().stream().map(entry -> new ConfigEntry(entry.getKey(), entry.getValue())).collect(Collectors.toSet())));
    final ConfigResource topicResource = new ConfigResource(Type.TOPIC, topic1);
    EasyMock.expect(admin.describeConfigs(Collections.singleton(topicResource))).andReturn(new MockDescribeConfigsResult(mkMap(mkEntry(topicResource, topicConfigsFailFuture)))).andReturn(new MockDescribeConfigsResult(mkMap(mkEntry(topicResource, topicConfigSuccessfulFuture))));
    EasyMock.replay(admin);
    final InternalTopicConfig internalTopicConfig = setupRepartitionTopicConfig(topic1, 1);
    final ValidationResult validationResult = topicManager.validate(Collections.singletonMap(topic1, internalTopicConfig));
    assertThat(validationResult.missingTopics(), empty());
    assertThat(validationResult.misconfigurationsForTopics(), anEmptyMap());
    EasyMock.verify(admin);
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) Matchers.not(org.hamcrest.Matchers.not) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) StreamsException(org.apache.kafka.streams.errors.StreamsException) ValidationResult(org.apache.kafka.streams.processor.internals.InternalTopicManager.ValidationResult) Matchers.hasKey(org.hamcrest.Matchers.hasKey) AdminClient(org.apache.kafka.clients.admin.AdminClient) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) After(org.junit.After) Map(java.util.Map) DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) Assert.fail(org.junit.Assert.fail) TopicConfig(org.apache.kafka.common.config.TopicConfig) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) Collection(java.util.Collection) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) KafkaFuture(org.apache.kafka.common.KafkaFuture) Collectors(java.util.stream.Collectors) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) List(java.util.List) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) Matchers.is(org.hamcrest.Matchers.is) CreateTopicsOptions(org.apache.kafka.clients.admin.CreateTopicsOptions) Type(org.apache.kafka.common.config.ConfigResource.Type) Config(org.apache.kafka.clients.admin.Config) Uuid(org.apache.kafka.common.Uuid) StreamsConfig(org.apache.kafka.streams.StreamsConfig) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) Assert.assertThrows(org.junit.Assert.assertThrows) HashMap(java.util.HashMap) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) ArrayList(java.util.ArrayList) ConfigResource(org.apache.kafka.common.config.ConfigResource) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) Before(org.junit.Before) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Matchers.empty(org.hamcrest.Matchers.empty) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertNull(org.junit.Assert.assertNull) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TopicConfig(org.apache.kafka.common.config.TopicConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Config(org.apache.kafka.clients.admin.Config) StreamsConfig(org.apache.kafka.streams.StreamsConfig) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ValidationResult(org.apache.kafka.streams.processor.internals.InternalTopicManager.ValidationResult) ConfigResource(org.apache.kafka.common.config.ConfigResource) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 52 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project kafka by apache.

the class InternalTopicManagerTest method shouldCompleteTopicValidationOnRetry.

@Test
public void shouldCompleteTopicValidationOnRetry() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, new StreamsConfig(config));
    final TopicPartitionInfo partitionInfo = new TopicPartitionInfo(0, broker1, Collections.singletonList(broker1), Collections.singletonList(broker1));
    final KafkaFutureImpl<TopicDescription> topicDescriptionSuccessFuture = new KafkaFutureImpl<>();
    final KafkaFutureImpl<TopicDescription> topicDescriptionFailFuture = new KafkaFutureImpl<>();
    topicDescriptionSuccessFuture.complete(new TopicDescription(topic1, false, Collections.singletonList(partitionInfo), Collections.emptySet()));
    topicDescriptionFailFuture.completeExceptionally(new UnknownTopicOrPartitionException("KABOOM!"));
    final KafkaFutureImpl<CreateTopicsResult.TopicMetadataAndConfig> topicCreationFuture = new KafkaFutureImpl<>();
    topicCreationFuture.completeExceptionally(new TopicExistsException("KABOOM!"));
    // let the first describe succeed on topic, and fail on topic2, and then let creation throws topics-existed;
    // it should retry with just topic2 and then let it succeed
    EasyMock.expect(admin.describeTopics(mkSet(topic1, topic2))).andReturn(new MockDescribeTopicsResult(mkMap(mkEntry(topic1, topicDescriptionSuccessFuture), mkEntry(topic2, topicDescriptionFailFuture)))).once();
    EasyMock.expect(admin.createTopics(Collections.singleton(new NewTopic(topic2, Optional.of(1), Optional.of((short) 1)).configs(mkMap(mkEntry(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT), mkEntry(TopicConfig.MESSAGE_TIMESTAMP_TYPE_CONFIG, "CreateTime")))))).andReturn(new MockCreateTopicsResult(Collections.singletonMap(topic2, topicCreationFuture))).once();
    EasyMock.expect(admin.describeTopics(Collections.singleton(topic2))).andReturn(new MockDescribeTopicsResult(Collections.singletonMap(topic2, topicDescriptionSuccessFuture)));
    EasyMock.replay(admin);
    final InternalTopicConfig topicConfig = new UnwindowedChangelogTopicConfig(topic1, Collections.emptyMap());
    topicConfig.setNumberOfPartitions(1);
    final InternalTopicConfig topic2Config = new UnwindowedChangelogTopicConfig(topic2, Collections.emptyMap());
    topic2Config.setNumberOfPartitions(1);
    topicManager.makeReady(mkMap(mkEntry(topic1, topicConfig), mkEntry(topic2, topic2Config)));
    EasyMock.verify(admin);
}
Also used : UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) NewTopic(org.apache.kafka.clients.admin.NewTopic) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 53 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project kafka by apache.

the class InternalTopicManagerTest method shouldCreateTopicWhenTopicLeaderNotAvailableAndThenTopicNotFound.

@Test
public void shouldCreateTopicWhenTopicLeaderNotAvailableAndThenTopicNotFound() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, new StreamsConfig(config));
    final KafkaFutureImpl<TopicDescription> topicDescriptionLeaderNotAvailableFuture = new KafkaFutureImpl<>();
    topicDescriptionLeaderNotAvailableFuture.completeExceptionally(new LeaderNotAvailableException("Leader Not Available!"));
    final KafkaFutureImpl<TopicDescription> topicDescriptionUnknownTopicFuture = new KafkaFutureImpl<>();
    topicDescriptionUnknownTopicFuture.completeExceptionally(new UnknownTopicOrPartitionException("Unknown Topic!"));
    final KafkaFutureImpl<CreateTopicsResult.TopicMetadataAndConfig> topicCreationFuture = new KafkaFutureImpl<>();
    topicCreationFuture.complete(EasyMock.createNiceMock(CreateTopicsResult.TopicMetadataAndConfig.class));
    EasyMock.expect(admin.describeTopics(Collections.singleton(topic1))).andReturn(new MockDescribeTopicsResult(Collections.singletonMap(topic1, topicDescriptionLeaderNotAvailableFuture))).once();
    // we would not need to call create-topics for the first time
    EasyMock.expect(admin.describeTopics(Collections.singleton(topic1))).andReturn(new MockDescribeTopicsResult(Collections.singletonMap(topic1, topicDescriptionUnknownTopicFuture))).once();
    EasyMock.expect(admin.createTopics(Collections.singleton(new NewTopic(topic1, Optional.of(1), Optional.of((short) 1)).configs(mkMap(mkEntry(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_DELETE), mkEntry(TopicConfig.MESSAGE_TIMESTAMP_TYPE_CONFIG, "CreateTime"), mkEntry(TopicConfig.SEGMENT_BYTES_CONFIG, "52428800"), mkEntry(TopicConfig.RETENTION_MS_CONFIG, "-1")))))).andReturn(new MockCreateTopicsResult(Collections.singletonMap(topic1, topicCreationFuture))).once();
    EasyMock.replay(admin);
    final InternalTopicConfig internalTopicConfig = new RepartitionTopicConfig(topic1, Collections.emptyMap());
    internalTopicConfig.setNumberOfPartitions(1);
    topicManager.makeReady(Collections.singletonMap(topic1, internalTopicConfig));
    EasyMock.verify(admin);
}
Also used : UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicMetadataAndConfig(org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) NewTopic(org.apache.kafka.clients.admin.NewTopic) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 54 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient in project kafka by apache.

the class InternalTopicManagerTest method shouldThrowWhenDeleteTopicsThrowsUnexpectedException.

@Test
public void shouldThrowWhenDeleteTopicsThrowsUnexpectedException() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final StreamsConfig streamsConfig = new StreamsConfig(config);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, streamsConfig);
    final InternalTopicConfig internalTopicConfig1 = setupRepartitionTopicConfig(topic1, 1);
    final InternalTopicConfig internalTopicConfig2 = setupRepartitionTopicConfig(topic2, 1);
    setupCleanUpScenario(admin, streamsConfig, internalTopicConfig1, internalTopicConfig2);
    final KafkaFutureImpl<Void> deleteTopicFailFuture = new KafkaFutureImpl<>();
    deleteTopicFailFuture.completeExceptionally(new IllegalStateException("Nobody expects the Spanish inquisition"));
    EasyMock.expect(admin.deleteTopics(mkSet(topic1))).andStubAnswer(() -> new MockDeleteTopicsResult(mkMap(mkEntry(topic1, deleteTopicFailFuture))));
    EasyMock.replay(admin);
    assertThrows(StreamsException.class, () -> topicManager.setup(mkMap(mkEntry(topic1, internalTopicConfig1), mkEntry(topic2, internalTopicConfig2))));
}
Also used : KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 55 with AdminClient

use of org.apache.kafka.clients.admin.AdminClient 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."));
}
Also used : StreamsException(org.apache.kafka.streams.errors.StreamsException) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) CreateTopicsOptions(org.apache.kafka.clients.admin.CreateTopicsOptions) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) Collection(java.util.Collection) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreatableReplicaAssignmentCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableReplicaAssignmentCollection) AdminClient(org.apache.kafka.clients.admin.AdminClient) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Aggregations

AdminClient (org.apache.kafka.clients.admin.AdminClient)71 Test (org.junit.Test)38 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)31 NewTopic (org.apache.kafka.clients.admin.NewTopic)30 StreamsConfig (org.apache.kafka.streams.StreamsConfig)29 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)27 HashMap (java.util.HashMap)24 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)18 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)18 Config (org.apache.kafka.clients.admin.Config)15 Map (java.util.Map)14 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)14 ProducerConfig (org.apache.kafka.clients.producer.ProducerConfig)14 TopicConfig (org.apache.kafka.common.config.TopicConfig)13 MockTime (org.apache.kafka.common.utils.MockTime)13 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)11 ArrayList (java.util.ArrayList)10 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)10 ConfigResource (org.apache.kafka.common.config.ConfigResource)10 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)10