Пример #1
0
    def describe_configs(self, config_resources, include_synonyms=None):
        """Fetch configuration parameters for one or more kafka resources.

        :param config_resources: An array of ConfigResource objects.
            Any keys in ConfigResource.configs dict will be used to filter the result.  The configs dict should be None
            to get all values.  An empty dict will get zero values (as per kafka protocol).
        :param include_synonyms: If True, return synonyms in response.  Not supported by all versions.
        :return: Appropriate version of DescribeConfigsResponse class
        """
        version = self._matching_api_version(DescribeConfigsRequest)
        if version == 0:
            if include_synonyms:
                raise UnsupportedVersionError(
                    "include_synonyms not supported on cluster version {}".
                    format(self.config['api_version']))
            request = DescribeConfigsRequest[version](resources=[
                self._convert_describe_config_resource_request(config_resource)
                for config_resource in config_resources
            ])
        elif version <= 1:
            include_synonyms = include_synonyms or False
            request = DescribeConfigsRequest[version](
                resources=[
                    self._convert_describe_config_resource_request(
                        config_resource)
                    for config_resource in config_resources
                ],
                include_synonyms=include_synonyms)
        else:
            raise UnsupportedVersionError(
                "missing implementation of DescribeConfigs for library supported version {}"
                .format(version))
        return self._send(request)
Пример #2
0
    def create_topics(self, new_topics, timeout_ms=None, validate_only=None):
        """Create new topics in the cluster.

        :param new_topics: Array of NewTopic objects
        :param timeout_ms: Milliseconds to wait for new topics to be created before broker returns
        :param validate_only: If True, don't actually create new topics.  Not supported by all versions.
        :return: Appropriate version of CreateTopicResponse class
        """
        version = self._matching_api_version(CreateTopicsRequest)
        timeout_ms = self._validate_timeout(timeout_ms)
        if version == 0:
            if validate_only:
                raise UnsupportedVersionError(
                    "validate_only not supported on cluster version {}".format(
                        self.config['api_version']))
            request = CreateTopicsRequest[version](create_topic_requests=[
                self._convert_new_topic_request(new_topic)
                for new_topic in new_topics
            ],
                                                   timeout=timeout_ms)
        elif version <= 2:
            validate_only = validate_only or False
            request = CreateTopicsRequest[version](create_topic_requests=[
                self._convert_new_topic_request(new_topic)
                for new_topic in new_topics
            ],
                                                   timeout=timeout_ms,
                                                   validate_only=validate_only)
        else:
            raise UnsupportedVersionError(
                "missing implementation of CreateTopics for library supported version {}"
                .format(version))
        return self._send(request)
Пример #3
0
    def create_partitions(self,
                          topic_partitions,
                          timeout_ms=None,
                          validate_only=None):
        """Create additional partitions for an existing topic.

        :param topic_partitions: A map of topic name strings to NewPartition objects
        :param timeout_ms: Milliseconds to wait for new partitions to be created before broker returns
        :param validate_only: If True, don't actually create new partitions.
        :return: Appropriate version of CreatePartitionsResponse class
        """
        version = self._matching_api_version(CreatePartitionsRequest)
        timeout_ms = self._validate_timeout(timeout_ms)
        validate_only = validate_only or False
        if version == 0:
            request = CreatePartitionsRequest[version](
                topic_partitions=[
                    self._convert_create_partitions_request(
                        topic_name, new_partitions)
                    for topic_name, new_partitions in topic_partitions.items()
                ],
                timeout=timeout_ms,
                validate_only=validate_only)
        else:
            raise UnsupportedVersionError(
                "missing implementation of CreatePartitions for library supported version {}"
                .format(version))
        return self._send(request)
Пример #4
0
 def _refresh_controller_id(self):
     """Determine the kafka cluster controller
     """
     response = self._send_request_to_node(self._client.least_loaded_node(),
                                           MetadataRequest[1]([]))
     self._controller_id = response.controller_id
     version = self._client.check_version(self._controller_id)
     if version < (0, 10, 0):
         raise UnsupportedVersionError(
             "Kafka Admin interface not supported for cluster controller version {} < 0.10.0.0"
             .format(version))
Пример #5
0
    def list_consumer_groups(self):
        """List all consumer groups known to the cluster.

        :return: Appropriate version of ListGroupsResponse class
        """
        version = self._matching_api_version(ListGroupsRequest)
        if version <= 1:
            request = ListGroupsRequest[version]()
        else:
            raise UnsupportedVersionError(
                "missing implementation of ListGroups for library supported version {}"
                .format(version))
        return self._send(request)
Пример #6
0
    def describe_consumer_groups(self, group_ids):
        """Describe a set of consumer groups.

        :param group_ids: A list of consumer group id names
        :return: Appropriate version of DescribeGroupsResponse class
        """
        version = self._matching_api_version(DescribeGroupsRequest)
        if version <= 1:
            request = DescribeGroupsRequest[version](groups=group_ids)
        else:
            raise UnsupportedVersionError(
                "missing implementation of DescribeGroups for library supported version {}"
                .format(version))
        return self._send(request)
Пример #7
0
    def alter_configs(self, config_resources):
        """Alter configuration parameters of one or more kafka resources.

        :param config_resources: An array of ConfigResource objects.
        :return: Appropriate version of AlterConfigsResponse class
        """
        version = self._matching_api_version(AlterConfigsRequest)
        if version == 0:
            request = AlterConfigsRequest[version](
                resources = [self._convert_alter_config_resource_request(config_resource) for config_resource in config_resources]
            )
        else:
            raise UnsupportedVersionError(
                "missing implementation of AlterConfigs for library supported version {}"
                    .format(version))
        return self._send(request)
Пример #8
0
    def _matching_api_version(self, operation):
        """Find matching api version, the lesser of either the latest api version the library supports, or
        the max version supported by the broker

        :param operation: An operation array from kafka.protocol
        :return: The max matching version number between client and broker
        """
        version = min(len(operation) - 1,
                      self._client.get_api_versions()[operation[0].API_KEY][1])
        if version < self._client.get_api_versions()[operation[0].API_KEY][0]:
            # max library version is less than min broker version.  Not sure any brokers
            # actually set a min version greater than 0 right now, tho.  But maybe in the future?
            raise UnsupportedVersionError(
                "Could not find matching protocol version for {}"
                .format(operation.__name__))
        return version
Пример #9
0
    def delete_topics(self, topics, timeout_ms=None):
        """Delete topics from the cluster

        :param topics: Array of topic name strings
        :param timeout_ms: Milliseconds to wait for topics to be deleted before broker returns
        :return: Appropriate version of DeleteTopicsResponse class
        """
        version = self._matching_api_version(DeleteTopicsRequest)
        timeout_ms = self._validate_timeout(timeout_ms)
        if version <= 1:
            request = DeleteTopicsRequest[version](topics=topics,
                                                   timeout=timeout_ms)
        else:
            raise UnsupportedVersionError(
                "missing implementation of DeleteTopics for library supported version {}"
                .format(version))
        return self._send(request)