All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kafka.common.requests.MetadataRequest Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.kafka.common.requests;

import org.apache.kafka.common.Node;
import org.apache.kafka.common.errors.UnsupportedVersionException;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.Errors;
import org.apache.kafka.common.protocol.types.Struct;
import org.apache.kafka.common.utils.Utils;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MetadataRequest extends AbstractRequest {

    private static final String TOPICS_KEY_NAME = "topics";
    private static final String ALLOW_AUTO_TOPIC_CREATION_KEY_NAME = "allow_auto_topic_creation";

    public static class Builder extends AbstractRequest.Builder {
        private static final List ALL_TOPICS = null;

        // The list of topics, or null if we want to request metadata about all topics.
        private final List topics;
        private final boolean allowAutoTopicCreation;

        public static Builder allTopics() {
            // This never causes auto-creation, but we set the boolean to true because that is the default value when
            // deserializing V2 and older. This way, the value is consistent after serialization and deserialization.
            return new Builder(ALL_TOPICS, true);
        }

        public Builder(List topics, boolean allowAutoTopicCreation) {
            super(ApiKeys.METADATA);
            this.topics = topics;
            this.allowAutoTopicCreation = allowAutoTopicCreation;
        }

        public List topics() {
            return this.topics;
        }

        public boolean isAllTopics() {
            return this.topics == ALL_TOPICS;
        }

        @Override
        public MetadataRequest build(short version) {
            if (version < 1)
                throw new UnsupportedVersionException("MetadataRequest versions older than 1 are not supported.");
            if (!allowAutoTopicCreation && version < 4)
                throw new UnsupportedVersionException("MetadataRequest versions older than 4 don't support the " +
                        "allowAutoTopicCreation field");
            return new MetadataRequest(this.topics, allowAutoTopicCreation, version);
        }

        @Override
        public String toString() {
            StringBuilder bld = new StringBuilder();
            bld.append("(type=MetadataRequest").
                append(", topics=");
            if (topics == null) {
                bld.append("");
            } else {
                bld.append(Utils.join(topics, ","));
            }
            bld.append(")");
            return bld.toString();
        }
    }

    private final List topics;
    private final boolean allowAutoTopicCreation;

    /**
     * In v0 null is not allowed and an empty list indicates requesting all topics.
     * Note: modern clients do not support sending v0 requests.
     * In v1 null indicates requesting all topics, and an empty list indicates requesting no topics.
     */
    public MetadataRequest(List topics, boolean allowAutoTopicCreation, short version) {
        super(version);
        this.topics = topics;
        this.allowAutoTopicCreation = allowAutoTopicCreation;
    }

    public MetadataRequest(Struct struct, short version) {
        super(version);
        Object[] topicArray = struct.getArray(TOPICS_KEY_NAME);
        if (topicArray != null) {
            topics = new ArrayList<>();
            for (Object topicObj: topicArray) {
                topics.add((String) topicObj);
            }
        } else {
            topics = null;
        }
        if (struct.hasField(ALLOW_AUTO_TOPIC_CREATION_KEY_NAME))
            allowAutoTopicCreation = struct.getBoolean(ALLOW_AUTO_TOPIC_CREATION_KEY_NAME);
        else
            allowAutoTopicCreation = true;
    }

    @Override
    public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
        List topicMetadatas = new ArrayList<>();
        Errors error = Errors.forException(e);
        List partitions = Collections.emptyList();

        if (topics != null) {
            for (String topic : topics)
                topicMetadatas.add(new MetadataResponse.TopicMetadata(error, topic, false, partitions));
        }

        short versionId = version();
        switch (versionId) {
            case 0:
            case 1:
            case 2:
                return new MetadataResponse(Collections.emptyList(), null, MetadataResponse.NO_CONTROLLER_ID, topicMetadatas);
            case 3:
            case 4:
                return new MetadataResponse(throttleTimeMs, Collections.emptyList(), null, MetadataResponse.NO_CONTROLLER_ID, topicMetadatas);
            default:
                throw new IllegalArgumentException(String.format("Version %d is not valid. Valid versions for %s are 0 to %d",
                        versionId, this.getClass().getSimpleName(), ApiKeys.METADATA.latestVersion()));
        }
    }

    public boolean isAllTopics() {
        return topics == null;
    }

    public List topics() {
        return topics;
    }

    public boolean allowAutoTopicCreation() {
        return allowAutoTopicCreation;
    }

    public static MetadataRequest parse(ByteBuffer buffer, short version) {
        return new MetadataRequest(ApiKeys.METADATA.parseRequest(version, buffer), version);
    }

    @Override
    protected Struct toStruct() {
        Struct struct = new Struct(ApiKeys.METADATA.requestSchema(version()));
        if (topics == null)
            struct.set(TOPICS_KEY_NAME, null);
        else
            struct.set(TOPICS_KEY_NAME, topics.toArray());
        if (struct.hasField(ALLOW_AUTO_TOPIC_CREATION_KEY_NAME))
            struct.set(ALLOW_AUTO_TOPIC_CREATION_KEY_NAME, allowAutoTopicCreation);
        return struct;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy