-
Couldn't load subscription status.
- Fork 0
Stream bluesky documents to kafka #263
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
+126
−9
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
84cf039
Stream documents to kafka
Tom-Willemsen fde0bb8
Add docs
Tom-Willemsen 9d63d12
Disable idempotence
Tom-Willemsen a1f678e
Re-enable idempotence
Tom-Willemsen 503418c
Add ADR
Tom-Willemsen 1327136
Suppress kafka log
Tom-Willemsen 7944746
Mention we considered json_json.fbs
Tom-Willemsen 34702e6
Merge branch 'main' into kafka
Tom-Willemsen 0fc2358
Merge branch 'main' into kafka
Tom-Willemsen 028f21c
Update comment
Tom-Willemsen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Kafka | ||
|
|
||
| `ibex_bluesky_core` uses [the `bluesky-kafka` library](https://github.com/bluesky/bluesky-kafka) to send documents | ||
| emitted by the `RunEngine` to kafka. The kafka callback is automatically added by | ||
| {py:obj}`ibex_bluesky_core.run_engine.get_run_engine`, and so no user configuration is required - the callback is always | ||
| enabled. | ||
|
|
||
| Documents are encoded using [the `msgpack` format](https://msgpack.org/index.html). | ||
|
|
||
| The kafka broker to send to can be controlled using the `IBEX_BLUESKY_CORE_KAFKA_BROKER` environment variable, if | ||
| an instrument needs to override the default. The kafka topic will be `<INSTRUMENT>_bluesky`, where `INSTRUMENT` is the | ||
| instrument name with any NDX or NDH prefix stripped. | ||
|
|
||
| The message key will always be `doc` for bluesky documents; specifying a non-null key enforces message ordering. |
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
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 |
|---|---|---|
|
|
@@ -9,15 +9,21 @@ | |
| from typing import Any, cast | ||
|
|
||
| import bluesky.preprocessors as bpp | ||
| import msgpack | ||
| from bluesky.run_engine import RunEngine, RunEngineResult | ||
| from bluesky.utils import DuringTask, Msg, RunEngineControlException, RunEngineInterrupted | ||
|
|
||
| from ibex_bluesky_core.callbacks import DocLoggingCallback | ||
| from ibex_bluesky_core.preprocessors import add_rb_number_processor | ||
|
|
||
| __all__ = ["get_run_engine", "run_plan"] | ||
| __all__ = ["get_kafka_topic_name", "get_run_engine", "run_plan"] | ||
|
|
||
|
|
||
| import os | ||
| import socket | ||
|
|
||
| from bluesky_kafka import Publisher | ||
|
|
||
| from ibex_bluesky_core.plan_stubs import CALL_QT_AWARE_MSG_KEY, CALL_SYNC_MSG_KEY | ||
| from ibex_bluesky_core.run_engine._msg_handlers import call_qt_aware_handler, call_sync_handler | ||
| from ibex_bluesky_core.utils import is_matplotlib_backend_qt | ||
|
|
@@ -26,6 +32,21 @@ | |
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| DEFAULT_KAFKA_BROKER = "livedata.isis.cclrc.ac.uk:31092" | ||
|
|
||
|
|
||
| def get_kafka_topic_name() -> str: | ||
| """Get the name of the bluesky kafka topic for this machine.""" | ||
| computer_name = os.environ.get("COMPUTERNAME", socket.gethostname()).upper() | ||
| computer_name = computer_name.upper() | ||
| if computer_name.startswith(("NDX", "NDH")): | ||
|
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. we should really pull this out of genie and into server_common but we'll do that later |
||
| name = computer_name[3:] | ||
| else: | ||
| name = computer_name | ||
|
|
||
| return f"{name}_bluesky" | ||
|
|
||
|
|
||
| class _DuringTask(DuringTask): | ||
| def block(self, blocking_event: Event) -> None: | ||
| """On windows, event.wait() on the main thread is not interruptible by a CTRL-C. | ||
|
|
@@ -103,6 +124,15 @@ def get_run_engine() -> RunEngine: | |
| log_callback = DocLoggingCallback() | ||
| RE.subscribe(log_callback) | ||
|
|
||
| kafka_callback = Publisher( | ||
| topic=get_kafka_topic_name(), | ||
| bootstrap_servers=os.environ.get("IBEX_BLUESKY_CORE_KAFKA_BROKER", DEFAULT_KAFKA_BROKER), | ||
| key="doc", | ||
| serializer=msgpack.dumps, | ||
| producer_config={"enable.idempotence": True}, | ||
| ) | ||
| RE.subscribe(kafka_callback) | ||
|
|
||
| RE.register_command(CALL_SYNC_MSG_KEY, call_sync_handler) | ||
| RE.register_command(CALL_QT_AWARE_MSG_KEY, call_qt_aware_handler) | ||
|
|
||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.