Class LicenseTopicAdmin

java.lang.Object
io.confluent.license.util.LicenseTopicAdmin
All Implemented Interfaces:
AutoCloseable

public class LicenseTopicAdmin extends Object implements AutoCloseable
Utility to simplify creating and managing topics via the Admin.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A builder of NewTopic instances.
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    static final int
     
    static final short
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new topic admin component with the given configuration.
    LicenseTopicAdmin(Map<String,Object> adminConfig, org.apache.kafka.clients.admin.Admin adminClient)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    close(Duration timeout)
     
    boolean
    createOrFindTopic(org.apache.kafka.clients.admin.NewTopic topic)
    Attempt to find or create the topic described by the given definition, returning true if the topic was created or had already existed, or false if the topic did not exist and could not be created.
    createOrFindTopics(org.apache.kafka.clients.admin.NewTopic... topics)
    Attempt to create the topics described by the given definitions, returning all of the names of those topics that were created by this request.
    boolean
    createTopic(org.apache.kafka.clients.admin.NewTopic topic)
    Attempt to create the topic described by the given definition, returning true if the topic was created or false if the topic already existed.
    createTopics(org.apache.kafka.clients.admin.NewTopic... topics)
    Attempt to create the topics described by the given definitions, returning all of the names of those topics that were created by this request.
    createTopicsWithRetry(org.apache.kafka.clients.admin.NewTopic topicDescription, long timeoutMs, long backOffMs, org.apache.kafka.common.utils.Time time)
    Implements a retry logic around creating topic(s) in case it'd fail due to specific type of exceptions, see retryableTopicCreationException(ConnectException)
    defineTopic(String topicName)
    Obtain a builder to define a NewTopic.
    org.apache.kafka.clients.admin.Config
    Attempt to fetch the topic configuration for the given topic.
    Map<String,org.apache.kafka.clients.admin.Config>
    Attempt to fetch the topic configurations for the given topics.
    Map<String,org.apache.kafka.clients.admin.TopicDescription>
    Attempt to fetch the descriptions of the given topics Apache Kafka added support for describing topics in 0.10.0.0, so this method works as expected with that and later versions.
    Map<org.apache.kafka.common.TopicPartition,Long>
    endOffsets(Set<org.apache.kafka.common.TopicPartition> partitions)
    Fetch the most recent offset for each of the supplied TopicPartition objects.
    static org.apache.kafka.connect.errors.ConnectException
    maybeWrap(Throwable t, String message)
     
    Map<org.apache.kafka.common.TopicPartition,Long>
    retryEndOffsets(Set<org.apache.kafka.common.TopicPartition> partitions, Duration timeoutDuration, long retryBackoffMs)
    Fetch the most recent offset for each of the supplied TopicPartition objects, and performs retry when RetriableException is thrown.
    Get the cleanup policy for a topic.
    boolean
    verifyTopicCleanupPolicyOnlyCompact(String topic, String workerTopicConfig, String topicPurpose)
    Verify the named topic uses only compaction for the cleanup policy.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • LicenseTopicAdmin

      public LicenseTopicAdmin(Map<String,Object> adminConfig)
      Create a new topic admin component with the given configuration.

      Note that this will create an underlying Admin instance which must be freed when this topic admin is no longer needed by calling close() or close(Duration).

      Parameters:
      adminConfig - the configuration for the Admin
    • LicenseTopicAdmin

      public LicenseTopicAdmin(Map<String,Object> adminConfig, org.apache.kafka.clients.admin.Admin adminClient)
  • Method Details

    • defineTopic

      public static LicenseTopicAdmin.NewTopicBuilder defineTopic(String topicName)
      Obtain a builder to define a NewTopic.
      Parameters:
      topicName - the name of the topic
      Returns:
      the NewTopic description of the topic; never null
    • createTopic

      public boolean createTopic(org.apache.kafka.clients.admin.NewTopic topic)
      Attempt to create the topic described by the given definition, returning true if the topic was created or false if the topic already existed.
      Parameters:
      topic - the specification of the topic
      Returns:
      true if the topic was created or false if the topic already existed.
      Throws:
      org.apache.kafka.connect.errors.ConnectException - if an error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
      org.apache.kafka.common.errors.UnsupportedVersionException - if the broker does not support the necessary APIs to perform this request
    • createTopics

      public Set<String> createTopics(org.apache.kafka.clients.admin.NewTopic... topics)
      Attempt to create the topics described by the given definitions, returning all of the names of those topics that were created by this request. Any existing topics with the same name are unchanged, and the names of such topics are excluded from the result.

      If multiple topic definitions have the same topic name, the last one with that name will be used.

      Apache Kafka added support for creating topics in 0.10.1.0, so this method works as expected with that and later versions. With brokers older than 0.10.1.0, this method is unable to create topics and always returns an empty set.

      Parameters:
      topics - the specifications of the topics
      Returns:
      the names of the topics that were created by this operation; never null but possibly empty
      Throws:
      org.apache.kafka.connect.errors.ConnectException - if an error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
    • createTopicsWithRetry

      public Set<String> createTopicsWithRetry(org.apache.kafka.clients.admin.NewTopic topicDescription, long timeoutMs, long backOffMs, org.apache.kafka.common.utils.Time time)
      Implements a retry logic around creating topic(s) in case it'd fail due to specific type of exceptions, see retryableTopicCreationException(ConnectException)
      Parameters:
      topicDescription - the specifications of the topic
      timeoutMs - Timeout in milliseconds
      backOffMs - Time for delay after initial failed attempt in milliseconds
      time - Time instance
      Returns:
      the names of the topics that were created by this operation; never null but possibly empty, the same as createTopics(NewTopic...)
    • createOrFindTopic

      public boolean createOrFindTopic(org.apache.kafka.clients.admin.NewTopic topic)
      Attempt to find or create the topic described by the given definition, returning true if the topic was created or had already existed, or false if the topic did not exist and could not be created.
      Parameters:
      topic - the specification of the topic
      Returns:
      true if the topic was created or existed, or false if the topic could not already existed.
      Throws:
      org.apache.kafka.connect.errors.ConnectException - if an error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
      org.apache.kafka.common.errors.UnsupportedVersionException - if the broker does not support the necessary APIs to perform this request
    • createOrFindTopics

      public LicenseTopicAdmin.TopicCreationResponse createOrFindTopics(org.apache.kafka.clients.admin.NewTopic... topics)
      Attempt to create the topics described by the given definitions, returning all of the names of those topics that were created by this request. Any existing topics with the same name are unchanged, and the names of such topics are excluded from the result.

      If multiple topic definitions have the same topic name, the last one with that name will be used.

      Apache Kafka added support for creating topics in 0.10.1.0, so this method works as expected with that and later versions. With brokers older than 0.10.1.0, this method is unable to create topics and always returns an empty set.

      Parameters:
      topics - the specifications of the topics
      Returns:
      the LicenseTopicAdmin.TopicCreationResponse with the names of the newly created and existing topics; never null but possibly empty
      Throws:
      org.apache.kafka.connect.errors.ConnectException - if an error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
    • describeTopics

      public Map<String,org.apache.kafka.clients.admin.TopicDescription> describeTopics(String... topics)
      Attempt to fetch the descriptions of the given topics Apache Kafka added support for describing topics in 0.10.0.0, so this method works as expected with that and later versions. With brokers older than 0.10.0.0, this method is unable to describe topics and always returns an empty set.
      Parameters:
      topics - the topics to describe
      Returns:
      a map of topic names to topic descriptions of the topics that were requested; never null but possibly empty
      Throws:
      org.apache.kafka.connect.errors.RetriableException - if a retriable error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
      org.apache.kafka.connect.errors.ConnectException - if a non retriable error occurs
    • verifyTopicCleanupPolicyOnlyCompact

      public boolean verifyTopicCleanupPolicyOnlyCompact(String topic, String workerTopicConfig, String topicPurpose)
      Verify the named topic uses only compaction for the cleanup policy.
      Parameters:
      topic - the name of the topic
      workerTopicConfig - the name of the worker configuration that specifies the topic name
      Returns:
      true if the admin client could be used to verify the topic setting, or false if the verification could not be performed, likely because the admin client principal did not have the required permissions or because the broker was older than 0.11.0.0
      Throws:
      org.apache.kafka.common.config.ConfigException - if the actual topic setting did not match the required setting
    • topicCleanupPolicy

      public Set<String> topicCleanupPolicy(String topic)
      Get the cleanup policy for a topic.
      Parameters:
      topic - the name of the topic
      Returns:
      the set of cleanup policies set for the topic; may be empty if the topic does not exist or the topic's cleanup policy could not be retrieved
    • describeTopicConfig

      public org.apache.kafka.clients.admin.Config describeTopicConfig(String topic)
      Attempt to fetch the topic configuration for the given topic. Apache Kafka added support for describing topic configurations in 0.11.0.0, so this method works as expected with that and later versions. With brokers older than 0.11.0.0, this method is unable get the topic configurations and always returns a null value.

      If the topic does not exist, a null value is returned.

      Parameters:
      topic - the name of the topic for which the topic configuration should be obtained
      Returns:
      the topic configuration if the topic exists, or null if the topic did not exist
      Throws:
      org.apache.kafka.connect.errors.RetriableException - if a retriable error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
      org.apache.kafka.connect.errors.ConnectException - if a non retriable error occurs
    • describeTopicConfigs

      public Map<String,org.apache.kafka.clients.admin.Config> describeTopicConfigs(String... topicNames)
      Attempt to fetch the topic configurations for the given topics. Apache Kafka added support for describing topic configurations in 0.11.0.0, so this method works as expected with that and later versions. With brokers older than 0.11.0.0, this method is unable get the topic configurations and always returns an empty set.

      An entry with a null Config is placed into the resulting map for any topic that does not exist on the brokers.

      Parameters:
      topicNames - the topics to obtain configurations
      Returns:
      the map of topic configurations for each existing topic, or an empty map if none of the topics exist
      Throws:
      org.apache.kafka.connect.errors.RetriableException - if a retriable error occurs, the operation takes too long, or the thread is interrupted while attempting to perform this operation
      org.apache.kafka.connect.errors.ConnectException - if a non retriable error occurs
    • endOffsets

      public Map<org.apache.kafka.common.TopicPartition,Long> endOffsets(Set<org.apache.kafka.common.TopicPartition> partitions)
      Fetch the most recent offset for each of the supplied TopicPartition objects.
      Parameters:
      partitions - the topic partitions
      Returns:
      the map of offset for each topic partition, or an empty map if the supplied partitions are null or empty
      Throws:
      org.apache.kafka.common.errors.UnsupportedVersionException - if the admin client cannot read end offsets
      org.apache.kafka.common.errors.TimeoutException - if the offset metadata could not be fetched before the amount of time allocated by request.timeout.ms expires, and this call can be retried
      org.apache.kafka.common.errors.LeaderNotAvailableException - if the leader was not available and this call can be retried
      org.apache.kafka.connect.errors.RetriableException - if a retriable error occurs, or the thread is interrupted while attempting to perform this operation
      org.apache.kafka.connect.errors.ConnectException - if a non retriable error occurs
    • retryEndOffsets

      public Map<org.apache.kafka.common.TopicPartition,Long> retryEndOffsets(Set<org.apache.kafka.common.TopicPartition> partitions, Duration timeoutDuration, long retryBackoffMs)
      Fetch the most recent offset for each of the supplied TopicPartition objects, and performs retry when RetriableException is thrown.
      Parameters:
      partitions - the topic partitions
      timeoutDuration - timeout duration; may not be null
      retryBackoffMs - the number of milliseconds to delay upon receiving a RetriableException before retrying again; must be 0 or more
      Returns:
      the map of offset for each topic partition, or an empty map if the supplied partitions are null or empty
      Throws:
      org.apache.kafka.common.errors.UnsupportedVersionException - if the broker is too old to support the admin client API to read end offsets
      org.apache.kafka.connect.errors.ConnectException - if timeoutDuration is exhausted
      See Also:
    • maybeWrap

      public static org.apache.kafka.connect.errors.ConnectException maybeWrap(Throwable t, String message)
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • close

      public void close(Duration timeout)