-
Notifications
You must be signed in to change notification settings - Fork 3
Improve handling of kafka offsets and capabilities of the monitor #86
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
Open
flowln
wants to merge
12
commits into
cnpem:main
Choose a base branch
from
flowln:better_kafka_monitor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9846e4d
feat: add run uid to start document logging line
flowln a5e0ee6
meta: replace kafka-python-ng with kafka-python
flowln e0610d5
feat: improve 'seek_start' logic in kafka monitor
flowln 75d9416
test: add unit tests for 'seek_start' logic in kafka monitor
flowln 97257e4
test: add special-case for kafka-python-ng in seek test
flowln 5519358
refactor: downgrade error to warning when save queue is full
flowln 3bc075f
fix: overhaul seek_start test logic and remove unneeded seek
flowln f2bf456
refactor: streamline kafka event handling and improve seek_start testing
flowln 1a0932e
feat: add 'seek_back_in_time' function for kafka consumers
flowln 27794ce
feat: add 'hour_offset' option for kafka monitors
flowln 2f859e7
refactor: move general kafka consumer utilities into separate module
flowln 422eca2
fix: add retries and runtime handling of failures in seek start docum…
flowln File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,66 @@ | ||
| from datetime import datetime, timedelta, timezone, tzinfo | ||
|
|
||
| from kafka import KafkaConsumer | ||
| from kafka.consumer.fetcher import ConsumerRecord | ||
| from kafka.structs import TopicPartition | ||
|
|
||
|
|
||
| def seek_start_document(consumer: KafkaConsumer, record: ConsumerRecord): | ||
| """ | ||
| Attempt to seek into the start document of the current run, based on data from the last received document. | ||
| """ | ||
| topic_partition = TopicPartition(record.topic, record.partition) | ||
|
|
||
| offset = record.offset | ||
| event_name, event_data = record.value | ||
|
|
||
| beginning_offset = consumer.beginning_offsets([topic_partition])[topic_partition] | ||
| while event_name != "start" and offset != beginning_offset: | ||
| if "seq_num" in event_data: | ||
| offset = offset - event_data["seq_num"] - 1 | ||
| else: | ||
| offset -= 1 | ||
| consumer.seek(topic_partition, offset) | ||
|
|
||
| for attempt_number in range(1, 4): | ||
| timeout = 1_000 * attempt_number | ||
| records = consumer.poll( | ||
| timeout_ms=timeout, max_records=1, update_offsets=False | ||
| ) | ||
|
|
||
| if topic_partition in records: | ||
| break | ||
|
|
||
| if topic_partition not in records: | ||
| raise RuntimeError( | ||
| f"Failed to retrieve records for the current partition ('{record.partition}' for '{record.topic}')" | ||
| ) | ||
|
|
||
| event_name, event_data = records[topic_partition][0].value | ||
|
|
||
|
|
||
| def seek_back_in_time( | ||
| consumer: KafkaConsumer, | ||
| rewind_time: timedelta, | ||
| server_timezone: tzinfo = timezone.utc, | ||
| ): | ||
| """ | ||
| Rewind the consumer by 'rewind_time', up to the beginning offset. | ||
| """ | ||
| now = datetime.now(server_timezone) | ||
|
|
||
| all_partitions = [ | ||
| TopicPartition(topic_name, p) | ||
| for topic_name in consumer.subscription() | ||
| for p in consumer.partitions_for_topic(topic_name) | ||
| ] | ||
|
|
||
| # NOTE: offsets_for_times expects timestamps in ms, so we multiply by 1000. | ||
| rewind_timestamp = int((now - rewind_time).timestamp() * 1000) | ||
| timestamp_offsets = consumer.offsets_for_times( | ||
| {p: rewind_timestamp for p in all_partitions} | ||
| ) | ||
|
|
||
| for partition, offset_ts in timestamp_offsets.items(): | ||
| if offset_ts is not None: | ||
| consumer.seek(partition, offset_ts.offset) | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea