Search in sources :

Example 76 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class KafkaAdminClientTest method testListOffsetsMaxTimestampUnsupportedMultipleOffsetSpec.

@Test
public void testListOffsetsMaxTimestampUnsupportedMultipleOffsetSpec() throws Exception {
    Node node = new Node(0, "localhost", 8120);
    List<Node> nodes = Collections.singletonList(node);
    List<PartitionInfo> pInfos = new ArrayList<>();
    pInfos.add(new PartitionInfo("foo", 0, node, new Node[] { node }, new Node[] { node }));
    pInfos.add(new PartitionInfo("foo", 1, node, new Node[] { node }, new Node[] { node }));
    final Cluster cluster = new Cluster("mockClusterId", nodes, pInfos, Collections.emptySet(), Collections.emptySet(), node);
    final TopicPartition tp0 = new TopicPartition("foo", 0);
    final TopicPartition tp1 = new TopicPartition("foo", 1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(cluster, AdminClientConfig.RETRIES_CONFIG, "2")) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create(ApiKeys.LIST_OFFSETS.id, (short) 0, (short) 6));
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        // listoffsets response from broker 0
        env.kafkaClient().prepareUnsupportedVersionResponse(request -> request instanceof ListOffsetsRequest);
        ListOffsetsTopicResponse topicResponse = ListOffsetsResponse.singletonListOffsetsTopicResponse(tp1, Errors.NONE, -1L, 345L, 543);
        ListOffsetsResponseData responseData = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Arrays.asList(topicResponse));
        env.kafkaClient().prepareResponseFrom(// ensure that no max timestamp requests are retried
        request -> request instanceof ListOffsetsRequest && ((ListOffsetsRequest) request).topics().stream().flatMap(t -> t.partitions().stream()).noneMatch(p -> p.timestamp() == ListOffsetsRequest.MAX_TIMESTAMP), new ListOffsetsResponse(responseData), node);
        ListOffsetsResult result = env.adminClient().listOffsets(new HashMap<TopicPartition, OffsetSpec>() {

            {
                put(tp0, OffsetSpec.maxTimestamp());
                put(tp1, OffsetSpec.latest());
            }
        });
        TestUtils.assertFutureThrows(result.partitionResult(tp0), UnsupportedVersionException.class);
        ListOffsetsResultInfo tp1Offset = result.partitionResult(tp1).get();
        assertEquals(345L, tp1Offset.offset());
        assertEquals(543, tp1Offset.leaderEpoch().get().intValue());
        assertEquals(-1L, tp1Offset.timestamp());
    }
}
Also used : ListGroupsResponse(org.apache.kafka.common.requests.ListGroupsResponse) ThrottlingQuotaExceededException(org.apache.kafka.common.errors.ThrottlingQuotaExceededException) ListOffsetsRequest(org.apache.kafka.common.requests.ListOffsetsRequest) ClientQuotaFilter(org.apache.kafka.common.quota.ClientQuotaFilter) AclPermissionType(org.apache.kafka.common.acl.AclPermissionType) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) KafkaException(org.apache.kafka.common.KafkaException) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) Collections.singletonList(java.util.Collections.singletonList) ClientUtils(org.apache.kafka.clients.ClientUtils) Future(java.util.concurrent.Future) DescribeTransactionsRequest(org.apache.kafka.common.requests.DescribeTransactionsRequest) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DeletableTopicResultCollection(org.apache.kafka.common.message.DeleteTopicsResponseData.DeletableTopicResultCollection) DescribeLogDirsResponseData(org.apache.kafka.common.message.DescribeLogDirsResponseData) ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) AlterReplicaLogDirTopicResult(org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirTopicResult) AlterReplicaLogDirsResponseData(org.apache.kafka.common.message.AlterReplicaLogDirsResponseData) ListGroupsRequest(org.apache.kafka.common.requests.ListGroupsRequest) CountDownLatch(java.util.concurrent.CountDownLatch) DeleteTopicsResponseData(org.apache.kafka.common.message.DeleteTopicsResponseData) Stream(java.util.stream.Stream) ApiMessageType(org.apache.kafka.common.message.ApiMessageType) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) DescribeGroupsResponse(org.apache.kafka.common.requests.DescribeGroupsResponse) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) Errors(org.apache.kafka.common.protocol.Errors) Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) DescribedGroupMember(org.apache.kafka.common.message.DescribeGroupsResponseData.DescribedGroupMember) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) Assertions.fail(org.junit.jupiter.api.Assertions.fail) AlterUserScramCredentialsResponseData(org.apache.kafka.common.message.AlterUserScramCredentialsResponseData) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) CreatableTopicResultCollection(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResultCollection) AlterUserScramCredentialsResponse(org.apache.kafka.common.requests.AlterUserScramCredentialsResponse) ClientDnsLookup(org.apache.kafka.clients.ClientDnsLookup) DescribeClusterResponse(org.apache.kafka.common.requests.DescribeClusterResponse) WriteTxnMarkersRequest(org.apache.kafka.common.requests.WriteTxnMarkersRequest) DescribeClusterBroker(org.apache.kafka.common.message.DescribeClusterResponseData.DescribeClusterBroker) DescribeAclsResponse(org.apache.kafka.common.requests.DescribeAclsResponse) WriteTxnMarkersResponseData(org.apache.kafka.common.message.WriteTxnMarkersResponseData) SecurityDisabledException(org.apache.kafka.common.errors.SecurityDisabledException) AlterReplicaLogDirsResponse(org.apache.kafka.common.requests.AlterReplicaLogDirsResponse) OptionalLong(java.util.OptionalLong) UnregisterBrokerResponseData(org.apache.kafka.common.message.UnregisterBrokerResponseData) UpdateFeaturesRequest(org.apache.kafka.common.requests.UpdateFeaturesRequest) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) DescribeTransactionsResponseData(org.apache.kafka.common.message.DescribeTransactionsResponseData) ResourcePattern(org.apache.kafka.common.resource.ResourcePattern) OffsetDeleteResponsePartitionCollection(org.apache.kafka.common.message.OffsetDeleteResponseData.OffsetDeleteResponsePartitionCollection) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) IncrementalAlterConfigsResponse(org.apache.kafka.common.requests.IncrementalAlterConfigsResponse) MemberResponse(org.apache.kafka.common.message.LeaveGroupResponseData.MemberResponse) FilterResults(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResults) JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) GroupAuthorizationException(org.apache.kafka.common.errors.GroupAuthorizationException) DescribeUserScramCredentialsResponse(org.apache.kafka.common.requests.DescribeUserScramCredentialsResponse) CredentialInfo(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData.CredentialInfo) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) DeletableGroupResult(org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResult) LeaveGroupRequestData(org.apache.kafka.common.message.LeaveGroupRequestData) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) DescribeConfigsResponseData(org.apache.kafka.common.message.DescribeConfigsResponseData) ApiVersionsResponseData(org.apache.kafka.common.message.ApiVersionsResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) Time(org.apache.kafka.common.utils.Time) Collection(java.util.Collection) CreateAclsResponse(org.apache.kafka.common.requests.CreateAclsResponse) DescribeClusterRequest(org.apache.kafka.common.requests.DescribeClusterRequest) DescribeUserScramCredentialsResponseData(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData) DescribeTransactionsResponse(org.apache.kafka.common.requests.DescribeTransactionsResponse) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ResourceType(org.apache.kafka.common.resource.ResourceType) CreatePartitionsResponse(org.apache.kafka.common.requests.CreatePartitionsResponse) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Uuid(org.apache.kafka.common.Uuid) DeleteTopicsRequest(org.apache.kafka.common.requests.DeleteTopicsRequest) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) OffsetDeleteResponseTopicCollection(org.apache.kafka.common.message.OffsetDeleteResponseData.OffsetDeleteResponseTopicCollection) DeleteAclsResponse(org.apache.kafka.common.requests.DeleteAclsResponse) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) AlterPartitionReassignmentsResponse(org.apache.kafka.common.requests.AlterPartitionReassignmentsResponse) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeConfigsResponse(org.apache.kafka.common.requests.DescribeConfigsResponse) PatternType(org.apache.kafka.common.resource.PatternType) OptionalInt(java.util.OptionalInt) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) DeleteRecordsResponseData(org.apache.kafka.common.message.DeleteRecordsResponseData) HashSet(java.util.HashSet) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) AlterConfigsResourceResponse(org.apache.kafka.common.message.IncrementalAlterConfigsResponseData.AlterConfigsResourceResponse) TopicCollection(org.apache.kafka.common.TopicCollection) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) UnknownMemberIdException(org.apache.kafka.common.errors.UnknownMemberIdException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) DeletableGroupResultCollection(org.apache.kafka.common.message.DeleteGroupsResponseData.DeletableGroupResultCollection) MockClient(org.apache.kafka.clients.MockClient) Logger(org.slf4j.Logger) GroupSubscribedToTopicException(org.apache.kafka.common.errors.GroupSubscribedToTopicException) CreateTopicsResponse(org.apache.kafka.common.requests.CreateTopicsResponse) Semaphore(java.util.concurrent.Semaphore) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) ConfigException(org.apache.kafka.common.config.ConfigException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest) NotLeaderOrFollowerException(org.apache.kafka.common.errors.NotLeaderOrFollowerException) Cluster(org.apache.kafka.common.Cluster) ApiVersionsResponse(org.apache.kafka.common.requests.ApiVersionsResponse) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) DescribeAclsResponseData(org.apache.kafka.common.message.DescribeAclsResponseData) TestUtils(org.apache.kafka.test.TestUtils) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) PartitionInfo(org.apache.kafka.common.PartitionInfo) DeleteGroupsResponse(org.apache.kafka.common.requests.DeleteGroupsResponse) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) TopicDeletionDisabledException(org.apache.kafka.common.errors.TopicDeletionDisabledException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ConsumerGroupState(org.apache.kafka.common.ConsumerGroupState) CreatableTopicResult(org.apache.kafka.common.message.CreateTopicsResponseData.CreatableTopicResult) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) DescribeGroupsResponseData(org.apache.kafka.common.message.DescribeGroupsResponseData) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) ArrayList(java.util.ArrayList) Features(org.apache.kafka.common.feature.Features) OffsetDeleteResponsePartition(org.apache.kafka.common.message.OffsetDeleteResponseData.OffsetDeleteResponsePartition) CreateAclsResponseData(org.apache.kafka.common.message.CreateAclsResponseData) ElectionType(org.apache.kafka.common.ElectionType) FindCoordinatorResponseData(org.apache.kafka.common.message.FindCoordinatorResponseData) MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) ListTransactionsResponseData(org.apache.kafka.common.message.ListTransactionsResponseData) FindCoordinatorRequest(org.apache.kafka.common.requests.FindCoordinatorRequest) OffsetFetchResponse(org.apache.kafka.common.requests.OffsetFetchResponse) UnknownTopicIdException(org.apache.kafka.common.errors.UnknownTopicIdException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ListGroupsResponseData(org.apache.kafka.common.message.ListGroupsResponseData) SaslAuthenticationException(org.apache.kafka.common.errors.SaslAuthenticationException) ValueSource(org.junit.jupiter.params.provider.ValueSource) AlterClientQuotasResponse(org.apache.kafka.common.requests.AlterClientQuotasResponse) TopicPartitionReplica(org.apache.kafka.common.TopicPartitionReplica) FencedInstanceIdException(org.apache.kafka.common.errors.FencedInstanceIdException) DeleteRecordsResponse(org.apache.kafka.common.requests.DeleteRecordsResponse) LEAVE_GROUP_REASON(org.apache.kafka.clients.admin.KafkaAdminClient.LEAVE_GROUP_REASON) AclOperation(org.apache.kafka.common.acl.AclOperation) DescribeClusterResponseData(org.apache.kafka.common.message.DescribeClusterResponseData) DeleteGroupsResponseData(org.apache.kafka.common.message.DeleteGroupsResponseData) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) WriteTxnMarkersResponse(org.apache.kafka.common.requests.WriteTxnMarkersResponse) ResourcePatternFilter(org.apache.kafka.common.resource.ResourcePatternFilter) CreatePartitionsResponseData(org.apache.kafka.common.message.CreatePartitionsResponseData) FindCoordinatorResponse(org.apache.kafka.common.requests.FindCoordinatorResponse) LoggerFactory(org.slf4j.LoggerFactory) CreatePartitionsTopicResult(org.apache.kafka.common.message.CreatePartitionsResponseData.CreatePartitionsTopicResult) AlterReplicaLogDirPartitionResult(org.apache.kafka.common.message.AlterReplicaLogDirsResponseData.AlterReplicaLogDirPartitionResult) ListPartitionReassignmentsResponse(org.apache.kafka.common.requests.ListPartitionReassignmentsResponse) ByteBuffer(java.nio.ByteBuffer) DescribeClientQuotasResponse(org.apache.kafka.common.requests.DescribeClientQuotasResponse) UpdateFeaturesResponse(org.apache.kafka.common.requests.UpdateFeaturesResponse) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) DescribeProducersResponse(org.apache.kafka.common.requests.DescribeProducersResponse) Collections.singleton(java.util.Collections.singleton) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) UnregisterBrokerResponse(org.apache.kafka.common.requests.UnregisterBrokerResponse) CreatePartitionsRequest(org.apache.kafka.common.requests.CreatePartitionsRequest) RecordVersion(org.apache.kafka.common.record.RecordVersion) Collections.emptyList(java.util.Collections.emptyList) KafkaFuture(org.apache.kafka.common.KafkaFuture) LogDirNotFoundException(org.apache.kafka.common.errors.LogDirNotFoundException) InetSocketAddress(java.net.InetSocketAddress) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) List(java.util.List) DescribeProducersResponseData(org.apache.kafka.common.message.DescribeProducersResponseData) DeletableTopicResult(org.apache.kafka.common.message.DeleteTopicsResponseData.DeletableTopicResult) OffsetDeleteResponseData(org.apache.kafka.common.message.OffsetDeleteResponseData) Optional(java.util.Optional) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ConsumerProtocol(org.apache.kafka.clients.consumer.internals.ConsumerProtocol) ClientQuotaFilterComponent(org.apache.kafka.common.quota.ClientQuotaFilterComponent) CreateTopicsRequest(org.apache.kafka.common.requests.CreateTopicsRequest) ElectLeadersResponse(org.apache.kafka.common.requests.ElectLeadersResponse) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) DescribeLogDirsTopic(org.apache.kafka.common.message.DescribeLogDirsResponseData.DescribeLogDirsTopic) IncrementalAlterConfigsResponseData(org.apache.kafka.common.message.IncrementalAlterConfigsResponseData) AccessControlEntryFilter(org.apache.kafka.common.acl.AccessControlEntryFilter) HashMap(java.util.HashMap) DescribeProducersRequest(org.apache.kafka.common.requests.DescribeProducersRequest) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) ApiError(org.apache.kafka.common.requests.ApiError) ConfigResource(org.apache.kafka.common.config.ConfigResource) OffsetOutOfRangeException(org.apache.kafka.common.errors.OffsetOutOfRangeException) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) AclBinding(org.apache.kafka.common.acl.AclBinding) OffsetDeleteResponseTopic(org.apache.kafka.common.message.OffsetDeleteResponseData.OffsetDeleteResponseTopic) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) ConsumerPartitionAssignor(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor) Utils(org.apache.kafka.common.utils.Utils) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) DeleteAclsResponseData(org.apache.kafka.common.message.DeleteAclsResponseData) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) TimeUnit(java.util.concurrent.TimeUnit) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) DeleteTopicsResponse(org.apache.kafka.common.requests.DeleteTopicsResponse) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) OffsetDeleteResponse(org.apache.kafka.common.requests.OffsetDeleteResponse) DescribeLogDirsResponse(org.apache.kafka.common.requests.DescribeLogDirsResponse) Timeout(org.junit.jupiter.api.Timeout) Collections(java.util.Collections) ApiException(org.apache.kafka.common.errors.ApiException) ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) Cluster(org.apache.kafka.common.Cluster) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) ListOffsetsRequest(org.apache.kafka.common.requests.ListOffsetsRequest) TopicPartition(org.apache.kafka.common.TopicPartition) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) PartitionInfo(org.apache.kafka.common.PartitionInfo) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 77 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class KafkaAdminClientTest method testListOffsetsMetadataRetriableErrors.

@Test
public void testListOffsetsMetadataRetriableErrors() throws Exception {
    Node node0 = new Node(0, "localhost", 8120);
    Node node1 = new Node(1, "localhost", 8121);
    List<Node> nodes = Arrays.asList(node0, node1);
    List<PartitionInfo> pInfos = new ArrayList<>();
    pInfos.add(new PartitionInfo("foo", 0, node0, new Node[] { node0 }, new Node[] { node0 }));
    pInfos.add(new PartitionInfo("foo", 1, node1, new Node[] { node1 }, new Node[] { node1 }));
    final Cluster cluster = new Cluster("mockClusterId", nodes, pInfos, Collections.<String>emptySet(), Collections.<String>emptySet(), node0);
    final TopicPartition tp0 = new TopicPartition("foo", 0);
    final TopicPartition tp1 = new TopicPartition("foo", 1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.LEADER_NOT_AVAILABLE));
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.UNKNOWN_TOPIC_OR_PARTITION));
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        // listoffsets response from broker 0
        ListOffsetsTopicResponse t0 = ListOffsetsResponse.singletonListOffsetsTopicResponse(tp0, Errors.NONE, -1L, 345L, 543);
        ListOffsetsResponseData responseData = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Arrays.asList(t0));
        env.kafkaClient().prepareResponseFrom(new ListOffsetsResponse(responseData), node0);
        // listoffsets response from broker 1
        ListOffsetsTopicResponse t1 = ListOffsetsResponse.singletonListOffsetsTopicResponse(tp1, Errors.NONE, -1L, 789L, 987);
        responseData = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Arrays.asList(t1));
        env.kafkaClient().prepareResponseFrom(new ListOffsetsResponse(responseData), node1);
        Map<TopicPartition, OffsetSpec> partitions = new HashMap<>();
        partitions.put(tp0, OffsetSpec.latest());
        partitions.put(tp1, OffsetSpec.latest());
        ListOffsetsResult result = env.adminClient().listOffsets(partitions);
        Map<TopicPartition, ListOffsetsResultInfo> offsets = result.all().get();
        assertFalse(offsets.isEmpty());
        assertEquals(345L, offsets.get(tp0).offset());
        assertEquals(543, offsets.get(tp0).leaderEpoch().get().intValue());
        assertEquals(-1L, offsets.get(tp0).timestamp());
        assertEquals(789L, offsets.get(tp1).offset());
        assertEquals(987, offsets.get(tp1).leaderEpoch().get().intValue());
        assertEquals(-1L, offsets.get(tp1).timestamp());
    }
}
Also used : ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) HashMap(java.util.HashMap) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) Cluster(org.apache.kafka.common.Cluster) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) PartitionInfo(org.apache.kafka.common.PartitionInfo) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 78 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class KafkaAdminClientTest method testListOffsetsRetriableErrorOnMetadata.

@Test
public void testListOffsetsRetriableErrorOnMetadata() throws Exception {
    Node node = new Node(0, "localhost", 8120);
    List<Node> nodes = Collections.singletonList(node);
    final Cluster cluster = new Cluster("mockClusterId", nodes, Collections.singleton(new PartitionInfo("foo", 0, node, new Node[] { node }, new Node[] { node })), Collections.emptySet(), Collections.emptySet(), node);
    final TopicPartition tp0 = new TopicPartition("foo", 0);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.UNKNOWN_TOPIC_OR_PARTITION, Errors.NONE));
        // metadata refresh because of UNKNOWN_TOPIC_OR_PARTITION
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        // listoffsets response from broker 0
        ListOffsetsResponseData responseData = new ListOffsetsResponseData().setThrottleTimeMs(0).setTopics(Collections.singletonList(ListOffsetsResponse.singletonListOffsetsTopicResponse(tp0, Errors.NONE, -1L, 123L, 321)));
        env.kafkaClient().prepareResponseFrom(new ListOffsetsResponse(responseData), node);
        ListOffsetsResult result = env.adminClient().listOffsets(Collections.singletonMap(tp0, OffsetSpec.latest()));
        Map<TopicPartition, ListOffsetsResultInfo> offsets = result.all().get(3, TimeUnit.SECONDS);
        assertEquals(1, offsets.size());
        assertEquals(123L, offsets.get(tp0).offset());
        assertEquals(321, offsets.get(tp0).leaderEpoch().get().intValue());
        assertEquals(-1L, offsets.get(tp0).timestamp());
    }
}
Also used : ListOffsetsResultInfo(org.apache.kafka.clients.admin.ListOffsetsResult.ListOffsetsResultInfo) TopicPartition(org.apache.kafka.common.TopicPartition) Node(org.apache.kafka.common.Node) Cluster(org.apache.kafka.common.Cluster) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) PartitionInfo(org.apache.kafka.common.PartitionInfo) ListOffsetsResponseData(org.apache.kafka.common.message.ListOffsetsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 79 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class KafkaAdminClientTest method testSuccessfulRetryAfterRequestTimeout.

@Test
public void testSuccessfulRetryAfterRequestTimeout() throws Exception {
    HashMap<Integer, Node> nodes = new HashMap<>();
    MockTime time = new MockTime();
    Node node0 = new Node(0, "localhost", 8121);
    nodes.put(0, node0);
    Cluster cluster = new Cluster("mockClusterId", nodes.values(), Arrays.asList(new PartitionInfo("foo", 0, node0, new Node[] { node0 }, new Node[] { node0 })), Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), nodes.get(0));
    final int requestTimeoutMs = 1000;
    final int retryBackoffMs = 100;
    final int apiTimeoutMs = 3000;
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, String.valueOf(retryBackoffMs), AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, String.valueOf(requestTimeoutMs))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        final ListTopicsResult result = env.adminClient().listTopics(new ListTopicsOptions().timeoutMs(apiTimeoutMs));
        // Wait until the first attempt has been sent, then advance the time
        TestUtils.waitForCondition(() -> env.kafkaClient().hasInFlightRequests(), "Timed out waiting for Metadata request to be sent");
        time.sleep(requestTimeoutMs + 1);
        // Wait for the request to be timed out before backing off
        TestUtils.waitForCondition(() -> !env.kafkaClient().hasInFlightRequests(), "Timed out waiting for inFlightRequests to be timed out");
        time.sleep(retryBackoffMs);
        // Since api timeout bound is not hit, AdminClient should retry
        TestUtils.waitForCondition(() -> env.kafkaClient().hasInFlightRequests(), "Failed to retry Metadata request");
        env.kafkaClient().respond(prepareMetadataResponse(cluster, Errors.NONE));
        assertEquals(1, result.listings().get().size());
        assertEquals("foo", result.listings().get().iterator().next().name());
    }
}
Also used : HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) Cluster(org.apache.kafka.common.Cluster) PartitionInfo(org.apache.kafka.common.PartitionInfo) MockTime(org.apache.kafka.common.utils.MockTime) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 80 with PartitionInfo

use of org.apache.kafka.common.PartitionInfo in project kafka by apache.

the class KafkaAdminClientTest method testRequestTimeoutExceedingDefaultApiTimeout.

@Test
public void testRequestTimeoutExceedingDefaultApiTimeout() throws Exception {
    HashMap<Integer, Node> nodes = new HashMap<>();
    MockTime time = new MockTime();
    Node node0 = new Node(0, "localhost", 8121);
    nodes.put(0, node0);
    Cluster cluster = new Cluster("mockClusterId", nodes.values(), Arrays.asList(new PartitionInfo("foo", 0, node0, new Node[] { node0 }, new Node[] { node0 })), Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), nodes.get(0));
    // This test assumes the default api timeout value of 60000. When the request timeout
    // is set to something larger, we should adjust the api timeout accordingly for compatibility.
    final int retryBackoffMs = 100;
    final int requestTimeoutMs = 120000;
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, String.valueOf(retryBackoffMs), AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, String.valueOf(requestTimeoutMs))) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        ListTopicsOptions options = new ListTopicsOptions();
        final ListTopicsResult result = env.adminClient().listTopics(options);
        // Wait until the first attempt has been sent, then advance the time by the default api timeout
        TestUtils.waitForCondition(() -> env.kafkaClient().hasInFlightRequests(), "Timed out waiting for Metadata request to be sent");
        time.sleep(60001);
        // The in-flight request should not be cancelled
        assertTrue(env.kafkaClient().hasInFlightRequests());
        // Now sleep the remaining time for the request timeout to expire
        time.sleep(60000);
        TestUtils.assertFutureThrows(result.future, TimeoutException.class);
    }
}
Also used : HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) Cluster(org.apache.kafka.common.Cluster) PartitionInfo(org.apache.kafka.common.PartitionInfo) MockTime(org.apache.kafka.common.utils.MockTime) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

PartitionInfo (org.apache.kafka.common.PartitionInfo)227 TopicPartition (org.apache.kafka.common.TopicPartition)142 HashMap (java.util.HashMap)87 Node (org.apache.kafka.common.Node)85 Test (org.junit.Test)82 Cluster (org.apache.kafka.common.Cluster)80 ArrayList (java.util.ArrayList)73 HashSet (java.util.HashSet)67 Set (java.util.Set)38 Map (java.util.Map)34 Test (org.junit.jupiter.api.Test)31 List (java.util.List)30 TaskId (org.apache.kafka.streams.processor.TaskId)25 StreamsConfig (org.apache.kafka.streams.StreamsConfig)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)16 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)15 Properties (java.util.Properties)13 MockTime (org.apache.kafka.common.utils.MockTime)13 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)11 HostInfo (org.apache.kafka.streams.state.HostInfo)11