Search in sources :

Example 11 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException in project kafka by apache.

the class KafkaAdminClientTest method testDeleteRecords.

@Test
public void testDeleteRecords() throws Exception {
    HashMap<Integer, Node> nodes = new HashMap<>();
    nodes.put(0, new Node(0, "localhost", 8121));
    List<PartitionInfo> partitionInfos = new ArrayList<>();
    partitionInfos.add(new PartitionInfo("my_topic", 0, nodes.get(0), new Node[] { nodes.get(0) }, new Node[] { nodes.get(0) }));
    partitionInfos.add(new PartitionInfo("my_topic", 1, nodes.get(0), new Node[] { nodes.get(0) }, new Node[] { nodes.get(0) }));
    partitionInfos.add(new PartitionInfo("my_topic", 2, null, new Node[] { nodes.get(0) }, new Node[] { nodes.get(0) }));
    partitionInfos.add(new PartitionInfo("my_topic", 3, nodes.get(0), new Node[] { nodes.get(0) }, new Node[] { nodes.get(0) }));
    partitionInfos.add(new PartitionInfo("my_topic", 4, nodes.get(0), new Node[] { nodes.get(0) }, new Node[] { nodes.get(0) }));
    Cluster cluster = new Cluster("mockClusterId", nodes.values(), partitionInfos, Collections.<String>emptySet(), Collections.<String>emptySet(), nodes.get(0));
    TopicPartition myTopicPartition0 = new TopicPartition("my_topic", 0);
    TopicPartition myTopicPartition1 = new TopicPartition("my_topic", 1);
    TopicPartition myTopicPartition2 = new TopicPartition("my_topic", 2);
    TopicPartition myTopicPartition3 = new TopicPartition("my_topic", 3);
    TopicPartition myTopicPartition4 = new TopicPartition("my_topic", 4);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        DeleteRecordsResponseData m = new DeleteRecordsResponseData();
        m.topics().add(new DeleteRecordsResponseData.DeleteRecordsTopicResult().setName(myTopicPartition0.topic()).setPartitions(new DeleteRecordsResponseData.DeleteRecordsPartitionResultCollection(asList(new DeleteRecordsResponseData.DeleteRecordsPartitionResult().setPartitionIndex(myTopicPartition0.partition()).setLowWatermark(3).setErrorCode(Errors.NONE.code()), new DeleteRecordsResponseData.DeleteRecordsPartitionResult().setPartitionIndex(myTopicPartition1.partition()).setLowWatermark(DeleteRecordsResponse.INVALID_LOW_WATERMARK).setErrorCode(Errors.OFFSET_OUT_OF_RANGE.code()), new DeleteRecordsResponseData.DeleteRecordsPartitionResult().setPartitionIndex(myTopicPartition3.partition()).setLowWatermark(DeleteRecordsResponse.INVALID_LOW_WATERMARK).setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code()), new DeleteRecordsResponseData.DeleteRecordsPartitionResult().setPartitionIndex(myTopicPartition4.partition()).setLowWatermark(DeleteRecordsResponse.INVALID_LOW_WATERMARK).setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code())).iterator())));
        List<MetadataResponse.TopicMetadata> t = new ArrayList<>();
        List<MetadataResponse.PartitionMetadata> p = new ArrayList<>();
        p.add(new MetadataResponse.PartitionMetadata(Errors.NONE, myTopicPartition0, Optional.of(nodes.get(0).id()), Optional.of(5), singletonList(nodes.get(0).id()), singletonList(nodes.get(0).id()), Collections.emptyList()));
        p.add(new MetadataResponse.PartitionMetadata(Errors.NONE, myTopicPartition1, Optional.of(nodes.get(0).id()), Optional.of(5), singletonList(nodes.get(0).id()), singletonList(nodes.get(0).id()), Collections.emptyList()));
        p.add(new MetadataResponse.PartitionMetadata(Errors.LEADER_NOT_AVAILABLE, myTopicPartition2, Optional.empty(), Optional.empty(), singletonList(nodes.get(0).id()), singletonList(nodes.get(0).id()), Collections.emptyList()));
        p.add(new MetadataResponse.PartitionMetadata(Errors.NONE, myTopicPartition3, Optional.of(nodes.get(0).id()), Optional.of(5), singletonList(nodes.get(0).id()), singletonList(nodes.get(0).id()), Collections.emptyList()));
        p.add(new MetadataResponse.PartitionMetadata(Errors.NONE, myTopicPartition4, Optional.of(nodes.get(0).id()), Optional.of(5), singletonList(nodes.get(0).id()), singletonList(nodes.get(0).id()), Collections.emptyList()));
        t.add(new MetadataResponse.TopicMetadata(Errors.NONE, "my_topic", false, p));
        env.kafkaClient().prepareResponse(RequestTestUtils.metadataResponse(cluster.nodes(), cluster.clusterResource().clusterId(), cluster.controller().id(), t));
        env.kafkaClient().prepareResponse(new DeleteRecordsResponse(m));
        Map<TopicPartition, RecordsToDelete> recordsToDelete = new HashMap<>();
        recordsToDelete.put(myTopicPartition0, RecordsToDelete.beforeOffset(3L));
        recordsToDelete.put(myTopicPartition1, RecordsToDelete.beforeOffset(10L));
        recordsToDelete.put(myTopicPartition2, RecordsToDelete.beforeOffset(10L));
        recordsToDelete.put(myTopicPartition3, RecordsToDelete.beforeOffset(10L));
        recordsToDelete.put(myTopicPartition4, RecordsToDelete.beforeOffset(10L));
        DeleteRecordsResult results = env.adminClient().deleteRecords(recordsToDelete);
        // success on records deletion for partition 0
        Map<TopicPartition, KafkaFuture<DeletedRecords>> values = results.lowWatermarks();
        KafkaFuture<DeletedRecords> myTopicPartition0Result = values.get(myTopicPartition0);
        long lowWatermark = myTopicPartition0Result.get().lowWatermark();
        assertEquals(lowWatermark, 3);
        // "offset out of range" failure on records deletion for partition 1
        KafkaFuture<DeletedRecords> myTopicPartition1Result = values.get(myTopicPartition1);
        try {
            myTopicPartition1Result.get();
            fail("get() should throw ExecutionException");
        } catch (ExecutionException e0) {
            assertTrue(e0.getCause() instanceof OffsetOutOfRangeException);
        }
        // "leader not available" failure on metadata request for partition 2
        KafkaFuture<DeletedRecords> myTopicPartition2Result = values.get(myTopicPartition2);
        try {
            myTopicPartition2Result.get();
            fail("get() should throw ExecutionException");
        } catch (ExecutionException e1) {
            assertTrue(e1.getCause() instanceof LeaderNotAvailableException);
        }
        // "not leader for partition" failure on records deletion for partition 3
        KafkaFuture<DeletedRecords> myTopicPartition3Result = values.get(myTopicPartition3);
        try {
            myTopicPartition3Result.get();
            fail("get() should throw ExecutionException");
        } catch (ExecutionException e1) {
            assertTrue(e1.getCause() instanceof NotLeaderOrFollowerException);
        }
        // "unknown topic or partition" failure on records deletion for partition 4
        KafkaFuture<DeletedRecords> myTopicPartition4Result = values.get(myTopicPartition4);
        try {
            myTopicPartition4Result.get();
            fail("get() should throw ExecutionException");
        } catch (ExecutionException e1) {
            assertTrue(e1.getCause() instanceof UnknownTopicOrPartitionException);
        }
    }
}
Also used : HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) ArrayList(java.util.ArrayList) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) DeleteRecordsResponse(org.apache.kafka.common.requests.DeleteRecordsResponse) PartitionInfo(org.apache.kafka.common.PartitionInfo) NotLeaderOrFollowerException(org.apache.kafka.common.errors.NotLeaderOrFollowerException) ExecutionException(java.util.concurrent.ExecutionException) KafkaFuture(org.apache.kafka.common.KafkaFuture) Cluster(org.apache.kafka.common.Cluster) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetOutOfRangeException(org.apache.kafka.common.errors.OffsetOutOfRangeException) DeleteRecordsResponseData(org.apache.kafka.common.message.DeleteRecordsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 12 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException in project kafka by apache.

the class InternalTopicManagerTest method shouldOnlyRetryDescribeTopicsWhenDescribeTopicsThrowsLeaderNotAvailableExceptionDuringValidation.

@Test
public void shouldOnlyRetryDescribeTopicsWhenDescribeTopicsThrowsLeaderNotAvailableExceptionDuringValidation() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, new StreamsConfig(config));
    final KafkaFutureImpl<TopicDescription> topicDescriptionFailFuture = new KafkaFutureImpl<>();
    topicDescriptionFailFuture.completeExceptionally(new LeaderNotAvailableException("Leader Not Available!"));
    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, topicDescriptionFailFuture)))).andReturn(new MockDescribeTopicsResult(mkMap(mkEntry(topic1, topicDescriptionSuccessfulFuture))));
    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, 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 13 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException in project kafka by apache.

the class InternalTopicManagerTest method shouldOnlyRetryNotSuccessfulFuturesDuringValidation.

@Test
public void shouldOnlyRetryNotSuccessfulFuturesDuringValidation() {
    final AdminClient admin = EasyMock.createNiceMock(AdminClient.class);
    final InternalTopicManager topicManager = new InternalTopicManager(time, admin, new StreamsConfig(config));
    final KafkaFutureImpl<TopicDescription> topicDescriptionFailFuture = new KafkaFutureImpl<>();
    topicDescriptionFailFuture.completeExceptionally(new LeaderNotAvailableException("Leader Not Available!"));
    final KafkaFutureImpl<TopicDescription> topicDescriptionSuccessfulFuture1 = new KafkaFutureImpl<>();
    topicDescriptionSuccessfulFuture1.complete(new TopicDescription(topic1, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, cluster, Collections.emptyList()))));
    final KafkaFutureImpl<TopicDescription> topicDescriptionSuccessfulFuture2 = new KafkaFutureImpl<>();
    topicDescriptionSuccessfulFuture2.complete(new TopicDescription(topic2, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, cluster, Collections.emptyList()))));
    EasyMock.expect(admin.describeTopics(mkSet(topic1, topic2))).andAnswer(() -> new MockDescribeTopicsResult(mkMap(mkEntry(topic1, topicDescriptionSuccessfulFuture1), mkEntry(topic2, topicDescriptionFailFuture))));
    EasyMock.expect(admin.describeTopics(mkSet(topic2))).andAnswer(() -> new MockDescribeTopicsResult(mkMap(mkEntry(topic2, topicDescriptionSuccessfulFuture2))));
    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 topicResource1 = new ConfigResource(Type.TOPIC, topic1);
    final ConfigResource topicResource2 = new ConfigResource(Type.TOPIC, topic2);
    EasyMock.expect(admin.describeConfigs(mkSet(topicResource1, topicResource2))).andAnswer(() -> new MockDescribeConfigsResult(mkMap(mkEntry(topicResource1, topicConfigSuccessfulFuture), mkEntry(topicResource2, topicConfigSuccessfulFuture))));
    EasyMock.replay(admin);
    final InternalTopicConfig internalTopicConfig1 = setupRepartitionTopicConfig(topic1, 1);
    final InternalTopicConfig internalTopicConfig2 = setupRepartitionTopicConfig(topic2, 1);
    final ValidationResult validationResult = topicManager.validate(mkMap(mkEntry(topic1, internalTopicConfig1), mkEntry(topic2, internalTopicConfig2)));
    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 14 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException 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 15 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException 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)

Aggregations

LeaderNotAvailableException (org.apache.kafka.common.errors.LeaderNotAvailableException)17 HashMap (java.util.HashMap)10 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)10 Test (org.junit.Test)10 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)8 KafkaFuture (org.apache.kafka.common.KafkaFuture)8 TimeoutException (org.apache.kafka.common.errors.TimeoutException)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)6 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)6 TopicPartition (org.apache.kafka.common.TopicPartition)6 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)6 StreamsConfig (org.apache.kafka.streams.StreamsConfig)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 NewTopic (org.apache.kafka.clients.admin.NewTopic)5 Node (org.apache.kafka.common.Node)5 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Set (java.util.Set)4