-
Notifications
You must be signed in to change notification settings - Fork 10
Dev mainloop integration 1 #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: poc-mainloop
Are you sure you want to change the base?
Changes from all commits
6463412
2c06bef
70f1542
4438165
6cee075
dea4dbd
8ac6cd2
4815394
a2c4183
50ea0b8
6b6990c
d8422c1
99ef937
74a1b43
47ad525
47c22c8
b61b83f
b97d924
99cd7ec
6062160
6d8bb81
64656bf
b87ba3b
d0d2d72
db44fe8
bcba7a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| from attrs import define, field, validators | ||
| from confluent_kafka import KafkaException, Message, Producer # type: ignore | ||
| from confluent_kafka.admin import AdminClient | ||
| from confluent_kafka.aio import AIOProducer | ||
|
|
||
| from logprep.metrics.metrics import GaugeMetric | ||
| from logprep.ng.abc.event import Event | ||
|
|
@@ -235,8 +236,8 @@ def _admin(self) -> AdminClient: | |
| return AdminClient(admin_config) | ||
|
|
||
| @cached_property | ||
| def _producer(self) -> Producer: | ||
| return Producer(self._kafka_config) | ||
| def _producer(self) -> AIOProducer: | ||
| return AIOProducer(self._kafka_config) | ||
|
|
||
| def _error_callback(self, error: KafkaException) -> None: | ||
| """Callback for generic/global error events, these errors are typically | ||
|
|
@@ -285,14 +286,12 @@ def describe(self) -> str: | |
|
|
||
| async def store_batch( | ||
| self, events: Sequence[Event], target: str | None = None | ||
| ) -> tuple[Sequence[Event], Sequence[Event]]: | ||
| ) -> Sequence[Event]: | ||
| store_target = target if target is not None else self.config.topic | ||
| for event in events: | ||
| await self.store_custom(event, store_target) | ||
| return ( | ||
| [e for e in events if e.state == EventStateType.DELIVERED], | ||
| [e for e in events if e.state == EventStateType.FAILED], | ||
| ) | ||
|
|
||
| return events | ||
|
|
||
| async def store(self, event: Event) -> None: | ||
| """Store a document in the producer topic. | ||
|
|
@@ -316,23 +315,35 @@ async def store_custom(self, event: Event, target: str) -> None: | |
| target : str | ||
| Topic to store event data in. | ||
| """ | ||
| event.state.current_state = EventStateType.STORING_IN_OUTPUT | ||
|
|
||
| document = event.data | ||
| self.metrics.number_of_processed_events += 1 | ||
|
|
||
| try: | ||
| self._producer.produce( | ||
| delivery_future = await self._producer.produce( | ||
| topic=target, | ||
| value=self._encoder.encode(document), | ||
| on_delivery=partial(self.on_delivery, event), | ||
| ) | ||
| logger.debug("Produced message %s to topic %s", str(document), target) | ||
| self._producer.poll(self.config.send_timeout) | ||
| self._producer.flush() | ||
| except BufferError: | ||
| # block program until buffer is empty or timeout is reached | ||
| self._producer.flush(timeout=self.config.flush_timeout) | ||
| logger.debug("Buffer full, flushing") | ||
| msg = await delivery_future | ||
| except KafkaException as err: | ||
| event.state.current_state = EventStateType.FAILED | ||
| event.errors.append(err) | ||
| logger.error("Kafka exception during produce: %s", err) | ||
| self.metrics.number_of_errors += 1 | ||
| return | ||
| except Exception as err: | ||
| event.state.current_state = EventStateType.FAILED | ||
| event.errors.append(err) | ||
| logger.error("Message delivery failed: %s", err) | ||
| self.metrics.number_of_errors += 1 | ||
| return | ||
|
|
||
| event.state.current_state = EventStateType.DELIVERED | ||
| logger.debug( | ||
| "Message delivered to '%s' partition %s, offset %s", | ||
| msg.topic(), | ||
| msg.partition(), | ||
| msg.offset(), | ||
| ) | ||
|
|
||
| async def flush(self) -> None: | ||
| """ensures that all messages are flushed. According to | ||
|
|
@@ -364,24 +375,17 @@ def health(self) -> bool: | |
| return super().health() | ||
|
|
||
| async def setup(self) -> None: | ||
| """Set the component up.""" | ||
| """Set the confluent kafka output connector.""" | ||
|
|
||
| try: | ||
| await super().setup() | ||
| except KafkaException as error: | ||
| raise FatalOutputError(self, f"Could not setup kafka producer: {error}") from error | ||
|
|
||
| def on_delivery(self, event: Event, err: KafkaException, msg: Message) -> None: | ||
| """Callback for delivery reports.""" | ||
| if err is not None: | ||
| event.state.current_state = EventStateType.FAILED | ||
| event.errors.append(err) | ||
| logger.error("Message delivery failed: %s", err) | ||
| self.metrics.number_of_errors += 1 | ||
| return | ||
| event.state.current_state = EventStateType.DELIVERED | ||
| logger.debug( | ||
| "Message delivered to '%s' partition %s, offset %s", | ||
| msg.topic(), | ||
| msg.partition(), | ||
| msg.offset(), | ||
| ) | ||
| async def shut_down(self) -> None: | ||
| """Shut down the confluent kafka output connector and cleanup resources.""" | ||
|
|
||
| if "_producer" in self.__dict__: | ||
| await self.flush() | ||
mhoff marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+388
to
+389
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we do this? Shouldnt we just always flush, I mmean shouldnt the flush be agnostic to, there is a producer and there is none? Also I dont like this if, isnt there any other way to check if we have a producer?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| await super().shut_down() | ||
Uh oh!
There was an error while loading. Please reload this page.