-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat(taskbroker): Passthrough mode #617
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
untitaker
wants to merge
1
commit into
main
Choose a base branch
from
passthrough
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::Error; | ||
| use rdkafka::message::OwnedMessage; | ||
|
|
||
| use crate::config::Config; | ||
| use crate::store::activation::InflightActivation; | ||
|
|
||
| use super::deserialize_activation::{self, DeserializeActivationConfig}; | ||
| use super::deserialize_passthrough::{self, PassthroughConfig}; | ||
|
|
||
| pub struct DeserializeConfig { | ||
| activation_config: DeserializeActivationConfig, | ||
| passthrough_config: Option<PassthroughConfig>, | ||
| } | ||
|
|
||
| impl DeserializeConfig { | ||
| pub fn from_config(config: &Config) -> Self { | ||
| Self { | ||
| activation_config: DeserializeActivationConfig::from_config(config), | ||
| passthrough_config: PassthroughConfig::from_config(config), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Create a unified deserializer that handles both normal and passthrough modes. | ||
| /// In passthrough mode, raw Kafka bytes are wrapped into a TaskActivation. | ||
| /// In normal mode, Kafka messages are expected to contain encoded TaskActivation protos. | ||
| pub fn new( | ||
| config: DeserializeConfig, | ||
| ) -> impl Fn(Arc<OwnedMessage>) -> Result<InflightActivation, Error> { | ||
| let passthrough_deserializer = config.passthrough_config.map(deserialize_passthrough::new); | ||
| let activation_deserializer = deserialize_activation::new(config.activation_config); | ||
|
|
||
| move |msg: Arc<OwnedMessage>| { | ||
| if let Some(ref pt_deserializer) = passthrough_deserializer { | ||
| pt_deserializer(msg) | ||
| } else { | ||
| activation_deserializer(msg) | ||
| } | ||
| } | ||
| } |
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,227 @@ | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
|
|
||
| use anyhow::{Error, anyhow}; | ||
| use chrono::Utc; | ||
| use prost::Message as _; | ||
| use rdkafka::Message; | ||
| use rdkafka::message::OwnedMessage; | ||
| use sentry_protos::taskbroker::v1::{OnAttemptsExceeded, TaskActivation}; | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::config::Config; | ||
| use crate::store::activation::{InflightActivation, InflightActivationStatus}; | ||
|
|
||
| use super::deserialize_activation::bucket_from_id; | ||
|
|
||
| pub struct PassthroughConfig { | ||
| pub namespace: String, | ||
| pub application: String, | ||
| pub taskname: String, | ||
| pub processing_deadline_duration: u64, | ||
| } | ||
|
|
||
| impl PassthroughConfig { | ||
| pub fn from_config(config: &Config) -> Option<Self> { | ||
| if !config.passthrough_mode { | ||
| return None; | ||
| } | ||
| Some(Self { | ||
| namespace: config | ||
| .passthrough_namespace | ||
| .clone() | ||
| .expect("passthrough_namespace required when passthrough_mode is enabled"), | ||
| application: config | ||
| .passthrough_application | ||
| .clone() | ||
| .expect("passthrough_application required when passthrough_mode is enabled"), | ||
| taskname: config | ||
| .passthrough_taskname | ||
| .clone() | ||
| .expect("passthrough_taskname required when passthrough_mode is enabled"), | ||
| processing_deadline_duration: config.passthrough_processing_deadline_duration, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Encode raw bytes into msgpack format: {"args": [raw_bytes], "kwargs": {}} | ||
| fn encode_passthrough_params(raw_bytes: &[u8]) -> Result<Vec<u8>, Error> { | ||
| use serde::Serialize; | ||
|
|
||
| #[derive(Serialize)] | ||
| struct Params<'a> { | ||
| args: (&'a [u8],), | ||
| kwargs: HashMap<(), ()>, | ||
| } | ||
|
Comment on lines
+51
to
+55
Member
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. For my own understanding, why define this struct here vs outside of the function? |
||
|
|
||
| let params = Params { | ||
| args: (raw_bytes,), | ||
| kwargs: HashMap::new(), | ||
| }; | ||
|
|
||
| rmp_serde::to_vec_named(¶ms).map_err(|e| anyhow!("Failed to encode msgpack: {}", e)) | ||
| } | ||
|
|
||
| /// Create a deserializer closure for passthrough mode. | ||
| /// Wraps raw Kafka message bytes into a TaskActivation with msgpack-encoded parameters_bytes. | ||
| pub fn new( | ||
| config: PassthroughConfig, | ||
| ) -> impl Fn(Arc<OwnedMessage>) -> Result<InflightActivation, Error> { | ||
| move |msg: Arc<OwnedMessage>| { | ||
| let Some(payload) = msg.payload() else { | ||
| return Err(anyhow!("Message has no payload")); | ||
| }; | ||
|
|
||
| let id = Uuid::new_v4().to_string(); | ||
| let parameters_bytes = encode_passthrough_params(payload)?; | ||
| let now = Utc::now(); | ||
| let received_at = prost_types::Timestamp { | ||
| seconds: now.timestamp(), | ||
| nanos: 0, | ||
| }; | ||
|
|
||
| let activation = TaskActivation { | ||
| id: id.clone(), | ||
| application: Some(config.application.clone()), | ||
| namespace: config.namespace.clone(), | ||
| taskname: config.taskname.clone(), | ||
| #[allow(deprecated)] | ||
| parameters: String::new(), | ||
| parameters_bytes, | ||
| headers: HashMap::new(), | ||
| received_at: Some(received_at), | ||
| retry_state: None, | ||
| processing_deadline_duration: config.processing_deadline_duration, | ||
| expires: None, | ||
| delay: None, | ||
| }; | ||
|
|
||
| let activation_bytes = activation.encode_to_vec(); | ||
| let bucket = bucket_from_id(&id); | ||
|
|
||
| metrics::histogram!( | ||
| "consumer.passthrough.payload_size_bytes", | ||
| "namespace" => config.namespace.clone(), | ||
| "taskname" => config.taskname.clone() | ||
| ) | ||
| .record(payload.len() as f64); | ||
|
|
||
| Ok(InflightActivation { | ||
| id, | ||
| activation: activation_bytes, | ||
| status: InflightActivationStatus::Pending, | ||
| partition: msg.partition(), | ||
| offset: msg.offset(), | ||
| added_at: now, | ||
| received_at: now, | ||
| processing_deadline: None, | ||
| claim_expires_at: None, | ||
| processing_deadline_duration: config.processing_deadline_duration as i32, | ||
| processing_attempts: 0, | ||
| expires_at: None, | ||
| delay_until: None, | ||
| at_most_once: false, | ||
| application: config.application.clone(), | ||
| namespace: config.namespace.clone(), | ||
| taskname: config.taskname.clone(), | ||
| on_attempts_exceeded: OnAttemptsExceeded::Discard, | ||
| bucket, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use rdkafka::Timestamp; | ||
| use rdkafka::message::OwnedMessage; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_encode_passthrough_params() { | ||
| use serde::Deserialize; | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| struct Params { | ||
| args: (Vec<u8>,), | ||
| kwargs: HashMap<(), ()>, | ||
| } | ||
|
|
||
| let raw_bytes = b"hello world"; | ||
| let encoded = encode_passthrough_params(raw_bytes).unwrap(); | ||
|
|
||
| // Decode and verify | ||
| let decoded: Params = rmp_serde::from_slice(&encoded).unwrap(); | ||
| assert_eq!(decoded.args.0, raw_bytes); | ||
| assert!(decoded.kwargs.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_passthrough_deserializer() { | ||
| let config = PassthroughConfig { | ||
| namespace: "test-namespace".to_string(), | ||
| application: "test-app".to_string(), | ||
| taskname: "test-task".to_string(), | ||
| processing_deadline_duration: 60, | ||
| }; | ||
|
|
||
| let deserializer = new(config); | ||
|
|
||
| let raw_payload = b"raw kafka message bytes"; | ||
| let message = OwnedMessage::new( | ||
| Some(raw_payload.to_vec()), | ||
| None, | ||
| "legacy-topic".into(), | ||
| Timestamp::now(), | ||
| 0, | ||
| 42, | ||
| None, | ||
| ); | ||
|
|
||
| let result = deserializer(Arc::new(message)); | ||
| assert!(result.is_ok()); | ||
|
|
||
| let inflight = result.unwrap(); | ||
| assert_eq!(inflight.namespace, "test-namespace"); | ||
| assert_eq!(inflight.application, "test-app"); | ||
| assert_eq!(inflight.taskname, "test-task"); | ||
| assert_eq!(inflight.processing_deadline_duration, 60); | ||
| assert_eq!(inflight.offset, 42); | ||
| assert_eq!(inflight.status, InflightActivationStatus::Pending); | ||
|
|
||
| // Verify the activation can be decoded | ||
| let activation = TaskActivation::decode(inflight.activation.as_slice()).unwrap(); | ||
| assert_eq!(activation.namespace, "test-namespace"); | ||
| assert_eq!(activation.application, Some("test-app".to_string())); | ||
| assert_eq!(activation.taskname, "test-task"); | ||
| assert!(!activation.parameters_bytes.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_passthrough_deserializer_empty_payload() { | ||
| let config = PassthroughConfig { | ||
| namespace: "test-namespace".to_string(), | ||
| application: "test-app".to_string(), | ||
| taskname: "test-task".to_string(), | ||
| processing_deadline_duration: 60, | ||
| }; | ||
|
|
||
| let deserializer = new(config); | ||
|
|
||
| let message = OwnedMessage::new( | ||
| None, // No payload | ||
| None, | ||
| "legacy-topic".into(), | ||
| Timestamp::now(), | ||
| 0, | ||
| 0, | ||
| None, | ||
| ); | ||
|
|
||
| let result = deserializer(Arc::new(message)); | ||
| assert!(result.is_err()); | ||
| assert!(result.unwrap_err().to_string().contains("no payload")); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| pub mod admin; | ||
| pub mod consumer; | ||
| pub mod deserialize; | ||
| pub mod deserialize_activation; | ||
| pub mod deserialize_passthrough; | ||
| pub mod inflight_activation_batcher; | ||
| pub mod inflight_activation_writer; | ||
| pub mod os_stream_writer; |
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.
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.