-
Notifications
You must be signed in to change notification settings - 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
Open
Tom-Willemsen
wants to merge
7
commits into
main
Choose a base branch
from
kafka
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.
+126
−9
Open
Changes from all commits
Commits
Show all changes
7 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 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,58 @@ | ||
# 9. Kafka streaming | ||
|
||
## Status | ||
|
||
Current | ||
|
||
## Context | ||
|
||
Many facilities stream bluesky documents to an event-bus for consumption by out-of-process listeners. | ||
Event buses used for this purpose at other facilities include ZeroMQ, RabbitMQ, Kafka, Redis, NATS, and | ||
others. | ||
|
||
The capability this provides is that callbacks can be run in different processes or on other computers, | ||
without holding up or interfering with the local `RunEngine`. Other groups at ISIS have expressed some | ||
interest in being able to subscribe to bluesky documents. | ||
|
||
## Decision | ||
|
||
- We will stream our messages to Kafka, as opposed to some other message bus. This is because we already | ||
have Kafka infrastructure available for other purposes (e.g. event data & sample-environment data). | ||
- At the time of writing, we will not **depend** on Kafka for anything critical. This is because the | ||
central Kafka instance is not currently considered "reliable" in an experiment controls context. However, | ||
streaming the documents will allow testing to be done. Kafka will eventually be deployed in a "reliable" | ||
way accessible to each instrument. | ||
- We will encode messages from bluesky using `msgpack` (with the `msgpack-numpy` extension), because: | ||
- It is the default encoder used by the upstream `bluesky-kafka` integration | ||
- It is a schema-less encoder, meaning we do not have to write/maintain fixed schemas for all the | ||
documents allowed by `event-model` | ||
- It has reasonable performance in terms of encoding speed and message size | ||
- `msgpack` is very widely supported in a range of programming languages | ||
- Kafka brokers will be configurable via an environment variable, `IBEX_BLUESKY_CORE_KAFKA_BROKER` | ||
|
||
```{note} | ||
Wherever Kafka is mentioned above, the actual implementation may be a Kafka-like (e.g. RedPanda). | ||
``` | ||
|
||
### Alternatives considered | ||
|
||
Encoding bluesky documents into JSON and then wrapping them in the | ||
[`json_json.fbs` flatbuffers schema](https://github.com/ess-dmsc/streaming-data-types/blob/58793c3dfa060f60b4a933bc085f831744e43f17/schemas/json_json.fbs) | ||
was considered. | ||
|
||
We chose `msgpack` instead of json strings + flatbuffers because: | ||
- It is more standard in the bluesky community (e.g. it is the default used in `bluesky-kafka`) | ||
- Bluesky events will be streamed to a dedicated topic, which is unlikely to be confused with data | ||
using any other schema. | ||
|
||
Performance/storage impacts are unlikely to be noticeable for bluesky documents, but nonetheless: | ||
- `msgpack`-encoded documents are 30-40% smaller than `json` + flatbuffers | ||
for a typical bluesky document | ||
- `msgpack`-encoding messages is ~5x faster than `json` + flatbuffers encoding | ||
for a typical bluesky document. | ||
|
||
## Justification & Consequences | ||
|
||
We will stream bluesky documents to Kafka, encoded using `msgpack-numpy`. | ||
|
||
At the time of writing this is purely to enable testing, and will not be used for "production" workflows. |
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,15 @@ | ||
# 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) - using the `msgpack-numpy` library | ||
to also handle numpy arrays transparently. | ||
|
||
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
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.