コード例 #1
0
    def create_consumer_group_offset_change_plan(
        self,
        consumer_id: str,
        topic_name: str,
        offset_to_value: Optional[int],
        offset_by_delta: Optional[int],
        offset_to_timestamp: Optional[str],
        offset_from_group: Optional[str],
    ) -> List[ConsumerGroupOffsetPlan]:

        consumer_group_state, offset_plans = self._read_current_consumergroup_offsets(
            consumer_id=consumer_id, topic_name_expression=topic_name)
        if consumer_group_state == "Dead":
            self._logger.error(
                "The consumer group {} does not exist.".format(consumer_id))
            return None
        elif consumer_group_state == "Empty":
            if offset_to_value:
                for plan_element in offset_plans.values():
                    (allowed_offset, error,
                     message) = self._select_new_offset_for_consumer(
                         offset_to_value, plan_element)
                    plan_element.proposed_offset = allowed_offset
                    if error:
                        self._logger.error(message)
            elif offset_by_delta:
                for plan_element in offset_plans.values():
                    requested_offset = plan_element.current_offset + offset_by_delta
                    (allowed_offset, error,
                     message) = self._select_new_offset_for_consumer(
                         requested_offset, plan_element)
                    plan_element.proposed_offset = allowed_offset
                    if error:
                        self._logger.error(message)
            elif offset_to_timestamp:
                timestamp_limit = pendulum.parse(offset_to_timestamp)
                current_offset_dict = TopicController(
                    self.cluster, None).get_offsets_closest_to_timestamp(
                        topic_name=topic_name, timestamp_limit=timestamp_limit)
                for plan_element in offset_plans.values():
                    plan_element.current_offset = current_offset_dict.get(
                        plan_element.partition_id, 0)
            elif offset_from_group:
                _, mirror_consumer_group = self._read_current_consumergroup_offsets(
                    consumer_id=offset_from_group,
                    topic_name_expression=topic_name)
                for key, value in mirror_consumer_group.items():
                    if key in offset_plans.keys():
                        offset_plans[
                            key].proposed_offset = value.current_offset
                    else:
                        value.current_offset = 0
                        offset_plans[key] = value
            return list(offset_plans.values())
        else:
            self._logger.error(
                "Consumergroup {} is not empty. Use the {} option if you want to override this safety mechanism."
                .format(consumer_id, red_bold("--force")))
            return list(offset_plans.values())
コード例 #2
0
 def _set_offset_to_timestamp(self, offset_plans, offset_to_timestamp,
                              topic_name):
     timestamp_limit = pendulum.parse(offset_to_timestamp)
     proposed_offset_dict = TopicController(
         self.cluster).get_offsets_closest_to_timestamp(
             topic_name=topic_name, timestamp=timestamp_limit)
     for plan_element in offset_plans.values():
         plan_element.proposed_offset = proposed_offset_dict[
             plan_element.partition_id].offset
コード例 #3
0
 def topic_controller(self) -> TopicController:
     if self.__topic_controller is None:
         self.__topic_controller = TopicController(self, self._config)
     return self.__topic_controller