-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from typing import Any | ||
|
||
import time | ||
import signal | ||
from contextlib import closing | ||
import os | ||
import logging | ||
from typing import Mapping | ||
|
||
from arroyo.types import Commit, Message, Partition, Topic | ||
from arroyo.backends.kafka.configuration import build_kafka_consumer_configuration | ||
from arroyo.backends.kafka.consumer import KafkaConsumer, KafkaPayload | ||
from arroyo.processing.strategies import RunTask, CommitOffsets, ProcessingStrategy | ||
from arroyo.processing.strategies.abstract import ProcessingStrategyFactory | ||
from arroyo.processing.processor import StreamProcessor | ||
from arroyo.backends.kafka import KafkaProducer | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
TOPIC = "test-kip848" | ||
|
||
def print_msg(message: Message[Any]) -> Message[Any]: | ||
((partition, offset),) = message.committable.items() | ||
print(f"message: {partition.index}-{offset}") | ||
return message | ||
|
||
class Strat(RunTask[Any, Any]): | ||
def join(self, *args: Any, **kwargs: Any) -> None: | ||
print("joining strategy, sleeping 5 seconds") | ||
time.sleep(5) | ||
print("joining strategy, sleeping 5 seconds -- DONE") | ||
return super().join(*args, **kwargs) | ||
|
||
class Factory(ProcessingStrategyFactory[KafkaPayload]): | ||
def create_with_partitions(self, commit: Commit, partitions: Mapping[Partition, int]) -> ProcessingStrategy[KafkaPayload]: | ||
print("assign: ", [p.index for p in partitions]) | ||
return Strat(print_msg, CommitOffsets(commit)) | ||
|
||
|
||
default_config = { | ||
"bootstrap.servers": os.environ.get("DEFAULT_BROKERS", "localhost:9092") | ||
} | ||
|
||
producer = KafkaProducer(default_config) | ||
|
||
with closing(producer): | ||
for i in range(30): | ||
message = KafkaPayload(None, i.to_bytes(), []) | ||
producer.produce(Topic(TOPIC), message).result() | ||
|
||
consumer_config = build_kafka_consumer_configuration( | ||
default_config, | ||
group_id="kip848", | ||
) | ||
|
||
consumer_config["group.protocol"] = "consumer" | ||
consumer_config.pop("session.timeout.ms", None) | ||
consumer_config.pop("max.poll.interval.ms", None) | ||
consumer_config.pop("partition.assignment.strategy", None) | ||
consumer_config.pop("group.protocol.type", None) | ||
consumer_config.pop("heartbeat.interval.ms", None) | ||
|
||
consumer = KafkaConsumer(consumer_config) | ||
|
||
processor = StreamProcessor( | ||
consumer=consumer, topic=Topic(TOPIC), processor_factory=Factory() | ||
) | ||
|
||
|
||
def handler(signum: int, frame: Any) -> None: | ||
processor.signal_shutdown() | ||
|
||
|
||
signal.signal(signal.SIGINT, handler) | ||
signal.signal(signal.SIGTERM, handler) | ||
|
||
processor.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters