Search in sources :

Example 11 with Uuid

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

the class LeaderAndIsrRequestTest method testGetErrorResponse.

@Test
public void testGetErrorResponse() {
    Uuid topicId = Uuid.randomUuid();
    String topicName = "topic";
    int partition = 0;
    for (short version : LEADER_AND_ISR.allVersions()) {
        LeaderAndIsrRequest request = new LeaderAndIsrRequest.Builder(version, 0, 0, 0, Collections.singletonList(new LeaderAndIsrPartitionState().setTopicName(topicName).setPartitionIndex(partition)), Collections.singletonMap(topicName, topicId), Collections.emptySet()).build(version);
        LeaderAndIsrResponse response = request.getErrorResponse(0, new ClusterAuthorizationException("Not authorized"));
        assertEquals(Errors.CLUSTER_AUTHORIZATION_FAILED, response.error());
        if (version < 5) {
            assertEquals(Collections.singletonList(new LeaderAndIsrPartitionError().setTopicName(topicName).setPartitionIndex(partition).setErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code())), response.data().partitionErrors());
            assertEquals(0, response.data().topics().size());
        } else {
            LeaderAndIsrTopicError topicState = response.topics().find(topicId);
            assertEquals(topicId, topicState.topicId());
            assertEquals(Collections.singletonList(new LeaderAndIsrPartitionError().setPartitionIndex(partition).setErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code())), topicState.partitionErrors());
            assertEquals(0, response.data().partitionErrors().size());
        }
    }
}
Also used : LeaderAndIsrPartitionError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrPartitionError) Uuid(org.apache.kafka.common.Uuid) LeaderAndIsrTopicError(org.apache.kafka.common.message.LeaderAndIsrResponseData.LeaderAndIsrTopicError) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) LeaderAndIsrPartitionState(org.apache.kafka.common.message.LeaderAndIsrRequestData.LeaderAndIsrPartitionState) Test(org.junit.jupiter.api.Test)

Example 12 with Uuid

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

the class KafkaAdminClient method handleDescribeTopicsByIds.

private Map<Uuid, KafkaFuture<TopicDescription>> handleDescribeTopicsByIds(Collection<Uuid> topicIds, DescribeTopicsOptions options) {
    final Map<Uuid, KafkaFutureImpl<TopicDescription>> topicFutures = new HashMap<>(topicIds.size());
    final List<Uuid> topicIdsList = new ArrayList<>();
    for (Uuid topicId : topicIds) {
        if (topicIdIsUnrepresentable(topicId)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new InvalidTopicException("The given topic id '" + topicId + "' cannot be represented in a request."));
            topicFutures.put(topicId, future);
        } else if (!topicFutures.containsKey(topicId)) {
            topicFutures.put(topicId, new KafkaFutureImpl<>());
            topicIdsList.add(topicId);
        }
    }
    final long now = time.milliseconds();
    Call call = new Call("describeTopicsWithIds", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        MetadataRequest.Builder createRequest(int timeoutMs) {
            return new MetadataRequest.Builder(new MetadataRequestData().setTopics(convertTopicIdsToMetadataRequestTopic(topicIdsList)).setAllowAutoTopicCreation(false).setIncludeTopicAuthorizedOperations(options.includeAuthorizedOperations()));
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            MetadataResponse response = (MetadataResponse) abstractResponse;
            // Handle server responses for particular topics.
            Cluster cluster = response.buildCluster();
            Map<Uuid, Errors> errors = response.errorsByTopicId();
            for (Map.Entry<Uuid, KafkaFutureImpl<TopicDescription>> entry : topicFutures.entrySet()) {
                Uuid topicId = entry.getKey();
                KafkaFutureImpl<TopicDescription> future = entry.getValue();
                String topicName = cluster.topicName(topicId);
                if (topicName == null) {
                    future.completeExceptionally(new InvalidTopicException("TopicId " + topicId + " not found."));
                    continue;
                }
                Errors topicError = errors.get(topicId);
                if (topicError != null) {
                    future.completeExceptionally(topicError.exception());
                    continue;
                }
                Integer authorizedOperations = response.topicAuthorizedOperations(topicName).get();
                TopicDescription topicDescription = getTopicDescriptionFromCluster(cluster, topicName, topicId, authorizedOperations);
                future.complete(topicDescription);
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(topicFutures.values(), throwable);
        }
    };
    if (!topicIdsList.isEmpty()) {
        runnable.call(call, now);
    }
    return new HashMap<>(topicFutures);
}
Also used : HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) Cluster(org.apache.kafka.common.Cluster) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MetadataRequestData(org.apache.kafka.common.message.MetadataRequestData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Errors(org.apache.kafka.common.protocol.Errors) Uuid(org.apache.kafka.common.Uuid) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 13 with Uuid

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

the class KafkaAdminClient method handleDescribeTopicsByNames.

private Map<String, KafkaFuture<TopicDescription>> handleDescribeTopicsByNames(final Collection<String> topicNames, DescribeTopicsOptions options) {
    final Map<String, KafkaFutureImpl<TopicDescription>> topicFutures = new HashMap<>(topicNames.size());
    final ArrayList<String> topicNamesList = new ArrayList<>();
    for (String topicName : topicNames) {
        if (topicNameIsUnrepresentable(topicName)) {
            KafkaFutureImpl<TopicDescription> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new InvalidTopicException("The given topic name '" + topicName + "' cannot be represented in a request."));
            topicFutures.put(topicName, future);
        } else if (!topicFutures.containsKey(topicName)) {
            topicFutures.put(topicName, new KafkaFutureImpl<>());
            topicNamesList.add(topicName);
        }
    }
    final long now = time.milliseconds();
    Call call = new Call("describeTopics", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        private boolean supportsDisablingTopicCreation = true;

        @Override
        MetadataRequest.Builder createRequest(int timeoutMs) {
            if (supportsDisablingTopicCreation)
                return new MetadataRequest.Builder(new MetadataRequestData().setTopics(convertToMetadataRequestTopic(topicNamesList)).setAllowAutoTopicCreation(false).setIncludeTopicAuthorizedOperations(options.includeAuthorizedOperations()));
            else
                return MetadataRequest.Builder.allTopics();
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            MetadataResponse response = (MetadataResponse) abstractResponse;
            // Handle server responses for particular topics.
            Cluster cluster = response.buildCluster();
            Map<String, Errors> errors = response.errors();
            for (Map.Entry<String, KafkaFutureImpl<TopicDescription>> entry : topicFutures.entrySet()) {
                String topicName = entry.getKey();
                KafkaFutureImpl<TopicDescription> future = entry.getValue();
                Errors topicError = errors.get(topicName);
                if (topicError != null) {
                    future.completeExceptionally(topicError.exception());
                    continue;
                }
                if (!cluster.topics().contains(topicName)) {
                    future.completeExceptionally(new UnknownTopicOrPartitionException("Topic " + topicName + " not found."));
                    continue;
                }
                Uuid topicId = cluster.topicId(topicName);
                Integer authorizedOperations = response.topicAuthorizedOperations(topicName).get();
                TopicDescription topicDescription = getTopicDescriptionFromCluster(cluster, topicName, topicId, authorizedOperations);
                future.complete(topicDescription);
            }
        }

        @Override
        boolean handleUnsupportedVersionException(UnsupportedVersionException exception) {
            if (supportsDisablingTopicCreation) {
                supportsDisablingTopicCreation = false;
                return true;
            }
            return false;
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(topicFutures.values(), throwable);
        }
    };
    if (!topicNamesList.isEmpty()) {
        runnable.call(call, now);
    }
    return new HashMap<>(topicFutures);
}
Also used : HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) ArrayList(java.util.ArrayList) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) Cluster(org.apache.kafka.common.Cluster) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MetadataRequestData(org.apache.kafka.common.message.MetadataRequestData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Errors(org.apache.kafka.common.protocol.Errors) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) Uuid(org.apache.kafka.common.Uuid) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 14 with Uuid

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

the class Metadata method handleMetadataResponse.

/**
 * Transform a MetadataResponse into a new MetadataCache instance.
 */
private MetadataCache handleMetadataResponse(MetadataResponse metadataResponse, boolean isPartialUpdate, long nowMs) {
    // All encountered topics.
    Set<String> topics = new HashSet<>();
    // Retained topics to be passed to the metadata cache.
    Set<String> internalTopics = new HashSet<>();
    Set<String> unauthorizedTopics = new HashSet<>();
    Set<String> invalidTopics = new HashSet<>();
    List<MetadataResponse.PartitionMetadata> partitions = new ArrayList<>();
    Map<String, Uuid> topicIds = new HashMap<>();
    Map<String, Uuid> oldTopicIds = cache.topicIds();
    for (MetadataResponse.TopicMetadata metadata : metadataResponse.topicMetadata()) {
        String topicName = metadata.topic();
        Uuid topicId = metadata.topicId();
        topics.add(topicName);
        // We can only reason about topic ID changes when both IDs are valid, so keep oldId null unless the new metadata contains a topic ID
        Uuid oldTopicId = null;
        if (!Uuid.ZERO_UUID.equals(topicId)) {
            topicIds.put(topicName, topicId);
            oldTopicId = oldTopicIds.get(topicName);
        } else {
            topicId = null;
        }
        if (!retainTopic(topicName, metadata.isInternal(), nowMs))
            continue;
        if (metadata.isInternal())
            internalTopics.add(topicName);
        if (metadata.error() == Errors.NONE) {
            for (MetadataResponse.PartitionMetadata partitionMetadata : metadata.partitionMetadata()) {
                // Even if the partition's metadata includes an error, we need to handle
                // the update to catch new epochs
                updateLatestMetadata(partitionMetadata, metadataResponse.hasReliableLeaderEpochs(), topicId, oldTopicId).ifPresent(partitions::add);
                if (partitionMetadata.error.exception() instanceof InvalidMetadataException) {
                    log.debug("Requesting metadata update for partition {} due to error {}", partitionMetadata.topicPartition, partitionMetadata.error);
                    requestUpdate();
                }
            }
        } else {
            if (metadata.error().exception() instanceof InvalidMetadataException) {
                log.debug("Requesting metadata update for topic {} due to error {}", topicName, metadata.error());
                requestUpdate();
            }
            if (metadata.error() == Errors.INVALID_TOPIC_EXCEPTION)
                invalidTopics.add(topicName);
            else if (metadata.error() == Errors.TOPIC_AUTHORIZATION_FAILED)
                unauthorizedTopics.add(topicName);
        }
    }
    Map<Integer, Node> nodes = metadataResponse.brokersById();
    if (isPartialUpdate)
        return this.cache.mergeWith(metadataResponse.clusterId(), nodes, partitions, unauthorizedTopics, invalidTopics, internalTopics, metadataResponse.controller(), topicIds, (topic, isInternal) -> !topics.contains(topic) && retainTopic(topic, isInternal, nowMs));
    else
        return new MetadataCache(metadataResponse.clusterId(), nodes, partitions, unauthorizedTopics, invalidTopics, internalTopics, metadataResponse.controller(), topicIds);
}
Also used : Uuid(org.apache.kafka.common.Uuid) KafkaException(org.apache.kafka.common.KafkaException) HashMap(java.util.HashMap) NO_PARTITION_LEADER_EPOCH(org.apache.kafka.common.record.RecordBatch.NO_PARTITION_LEADER_EPOCH) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Cluster(org.apache.kafka.common.Cluster) InvalidMetadataException(org.apache.kafka.common.errors.InvalidMetadataException) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) TopicPartition(org.apache.kafka.common.TopicPartition) Logger(org.slf4j.Logger) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) InetSocketAddress(java.net.InetSocketAddress) Objects(java.util.Objects) List(java.util.List) Closeable(java.io.Closeable) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Collections(java.util.Collections) HashMap(java.util.HashMap) InvalidMetadataException(org.apache.kafka.common.errors.InvalidMetadataException) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) Uuid(org.apache.kafka.common.Uuid) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) HashSet(java.util.HashSet)

Example 15 with Uuid

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

the class SimpleExampleMessageTest method shouldRoundTripFieldThroughBufferWithNullable.

@Test
public void shouldRoundTripFieldThroughBufferWithNullable() {
    final Uuid uuid = Uuid.randomUuid();
    final ByteBuffer buf1 = ByteBuffer.wrap(new byte[] { 1, 2, 3 });
    final ByteBuffer buf2 = ByteBuffer.wrap(new byte[] { 4, 5, 6 });
    final SimpleExampleMessageData out = new SimpleExampleMessageData();
    out.setProcessId(uuid);
    out.setZeroCopyByteBuffer(buf1);
    out.setNullableZeroCopyByteBuffer(buf2);
    final ByteBuffer buffer = MessageUtil.toByteBuffer(out, (short) 1);
    final SimpleExampleMessageData in = new SimpleExampleMessageData();
    in.read(new ByteBufferAccessor(buffer), (short) 1);
    buf1.rewind();
    buf2.rewind();
    assertEquals(uuid, in.processId());
    assertEquals(buf1, in.zeroCopyByteBuffer());
    assertEquals(buf2, in.nullableZeroCopyByteBuffer());
}
Also used : Uuid(org.apache.kafka.common.Uuid) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

Uuid (org.apache.kafka.common.Uuid)95 Test (org.junit.jupiter.api.Test)55 HashMap (java.util.HashMap)42 TopicPartition (org.apache.kafka.common.TopicPartition)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)21 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)21 LinkedHashMap (java.util.LinkedHashMap)18 List (java.util.List)15 FetchRequest (org.apache.kafka.common.requests.FetchRequest)14 TopicIdPartition (org.apache.kafka.common.TopicIdPartition)13 Errors (org.apache.kafka.common.protocol.Errors)12 FetchResponse (org.apache.kafka.common.requests.FetchResponse)12 Collections (java.util.Collections)11 ByteBuffer (java.nio.ByteBuffer)10 Node (org.apache.kafka.common.Node)10 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)10 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)10 PartitionRegistration (org.apache.kafka.metadata.PartitionRegistration)10