|
| 1 | +import time |
| 2 | +from collections.abc import Callable, Mapping, MutableMapping |
| 3 | +from concurrent.futures import Future |
| 4 | +from datetime import datetime, timedelta, timezone |
| 5 | +from enum import Enum |
| 6 | + |
| 7 | +from arroyo.backends.kafka import KafkaPayload, KafkaProducer |
| 8 | +from arroyo.dlq import InvalidMessage, KafkaDlqProducer |
| 9 | +from arroyo.processing.strategies.abstract import ProcessingStrategy, ProcessingStrategyFactory |
| 10 | +from arroyo.types import FILTERED_PAYLOAD, BrokerValue, Message, Partition |
| 11 | +from arroyo.types import Topic as ArroyoTopic |
| 12 | +from arroyo.types import Value |
| 13 | + |
| 14 | +from sentry.conf.types.kafka_definition import Topic |
| 15 | +from sentry.utils.kafka_config import get_kafka_producer_cluster_options, get_topic_definition |
| 16 | + |
| 17 | + |
| 18 | +class RejectReason(Enum): |
| 19 | + STALE = "stale" |
| 20 | + INVALID = "invalid" |
| 21 | + |
| 22 | + |
| 23 | +class MultipleDestinationDlqProducer(KafkaDlqProducer): |
| 24 | + """ |
| 25 | + Produces to either the DLQ or stale message topic depending on the reason. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + producers: Mapping[RejectReason, KafkaDlqProducer], |
| 31 | + topic_selector: Callable[[BrokerValue[KafkaPayload], str], RejectReason], |
| 32 | + ) -> None: |
| 33 | + self.producers = producers |
| 34 | + self.topic_selector = topic_selector |
| 35 | + |
| 36 | + def produce( |
| 37 | + self, value: BrokerValue[KafkaPayload], reason: str |
| 38 | + ) -> Future[BrokerValue[KafkaPayload]]: |
| 39 | + return self.producers[self.topic_selector(value, reason)].produce(value) |
| 40 | + |
| 41 | + |
| 42 | +def _get_dlq_producer(topic: Topic | None) -> KafkaDlqProducer | None: |
| 43 | + if topic is None: |
| 44 | + return None |
| 45 | + |
| 46 | + topic_defn = get_topic_definition(topic) |
| 47 | + config = get_kafka_producer_cluster_options(topic_defn["cluster"]) |
| 48 | + real_topic = topic_defn["real_topic_name"] |
| 49 | + return KafkaDlqProducer(KafkaProducer(config), ArroyoTopic(real_topic)) |
| 50 | + |
| 51 | + |
| 52 | +def build_dlq_producer( |
| 53 | + dlq_topic: Topic | None, stale_topic: Topic | None |
| 54 | +) -> MultipleDestinationDlqProducer | None: |
| 55 | + if dlq_topic is None and stale_topic is None: |
| 56 | + return None |
| 57 | + |
| 58 | + producers = { |
| 59 | + RejectReason.INVALID: _get_dlq_producer(dlq_topic), |
| 60 | + RejectReason.STALE: _get_dlq_producer(stale_topic), |
| 61 | + } |
| 62 | + |
| 63 | + return MultipleDestinationDlqProducer(producers) |
| 64 | + |
| 65 | + |
| 66 | +class DlqStaleMessages(ProcessingStrategy): |
| 67 | + def __init__( |
| 68 | + self, stale_threshold_sec: int, next_step: ProcessingStrategy[KafkaPayload] |
| 69 | + ) -> None: |
| 70 | + self.stale_threshold_sec = stale_threshold_sec |
| 71 | + self.next_step = next_step |
| 72 | + |
| 73 | + # A filtered message is created so we commit periodically if all are stale. |
| 74 | + self.last_forwarded_offsets = time.time() |
| 75 | + self.offsets_to_forward: MutableMapping[Partition, int] = {} |
| 76 | + |
| 77 | + def submit(self, message: Message[KafkaPayload]) -> None: |
| 78 | + min_accepted_timestamp = datetime.now(timezone.utc) - timedelta( |
| 79 | + seconds=self.stale_threshold_sec |
| 80 | + ) |
| 81 | + |
| 82 | + if isinstance(message.value, BrokerValue): |
| 83 | + message_timestamp = message.timestamp.astimezone(timezone.utc) |
| 84 | + if message_timestamp < min_accepted_timestamp: |
| 85 | + self.offsets_to_forward[message.value.partition, message.value.next_offset] |
| 86 | + raise InvalidMessage( |
| 87 | + message.value.partition, message.value.offset, RejectReason.STALE.value |
| 88 | + ) |
| 89 | + |
| 90 | + if self.offsets_to_forward and time.time() > self.last_forwarded_offsets + 1: |
| 91 | + message = Message(Value(FILTERED_PAYLOAD), self.offsets_to_forward) |
| 92 | + self.offsets_to_forward = {} |
| 93 | + self.next_step.submit(message) |
| 94 | + |
| 95 | + def poll(self) -> None: |
| 96 | + self.next_step.poll() |
| 97 | + |
| 98 | + def join(self, timeout: float | None = None) -> None: |
| 99 | + self.next_step.join(timeout) |
| 100 | + |
| 101 | + def close(self) -> None: |
| 102 | + self.next_step.close() |
| 103 | + |
| 104 | + def terminate(self) -> None: |
| 105 | + self.next_step.terminate() |
| 106 | + |
| 107 | + |
| 108 | +class DlqStaleMessagesStrategyFactoryWrapper(ProcessingStrategyFactory): |
| 109 | + """ |
| 110 | + Wrapper used to dlq a message with a stale timestamp before it is passed to |
| 111 | + the rest of the pipeline. The InvalidMessage is raised with a |
| 112 | + "stale" reason so it can be routed to a separate stale topic. |
| 113 | + """ |
| 114 | + |
| 115 | + def __init__(self, stale_threshold_sec: int, inner: ProcessingStrategyFactory) -> None: |
| 116 | + self.stale_threshold_sec = stale_threshold_sec |
| 117 | + self.inner = inner |
| 118 | + |
| 119 | + def create_with_partitions(self, commit, partitions) -> ProcessingStrategy: |
| 120 | + rv = self.inner.create_with_partitions(commit, partitions) |
| 121 | + return DlqStaleMessages(self.stale_threshold_sec, rv) |
0 commit comments