-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): Implement basic async kafka consumer
- Loading branch information
1 parent
86bd0c6
commit ee5a674
Showing
10 changed files
with
2,167 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
|
||
# Editors | ||
.DS_Store | ||
|
||
# Sqlite artifacts | ||
*.sqlite |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::{anyhow, Error}; | ||
use chrono::Utc; | ||
use prost::Message as _; | ||
use rdkafka::{message::OwnedMessage, Message}; | ||
use sentry_protos::sentry::v1::TaskActivation; | ||
|
||
use crate::inflight_activation_store::{InflightActivation, TaskActivationStatus}; | ||
|
||
pub async fn deserialize_activation(msg: Arc<OwnedMessage>) -> Result<InflightActivation, Error> { | ||
let Some(payload) = msg.payload() else { | ||
return Err(anyhow!("Message has no payload")); | ||
}; | ||
let activation = TaskActivation::decode(payload)?; | ||
Ok(InflightActivation { | ||
activation, | ||
status: TaskActivationStatus::Pending, | ||
offset: msg.offset(), | ||
added_at: Utc::now(), | ||
deadletter_at: None, | ||
processing_deadline: None, | ||
}) | ||
} |
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,78 @@ | ||
use std::{mem::replace, sync::Arc, time::Duration}; | ||
|
||
use tracing::info; | ||
|
||
use crate::inflight_activation_store::{InflightActivation, InflightActivationStore}; | ||
|
||
use super::kafka::{ | ||
ReduceConfig, ReduceShutdownBehaviour, ReduceShutdownCondition, Reducer, | ||
ReducerWhenFullBehaviour, | ||
}; | ||
|
||
pub struct InflightTaskWriterConfig { | ||
pub max_buf_len: usize, | ||
pub flush_interval: Option<Duration>, | ||
pub when_full_behaviour: ReducerWhenFullBehaviour, | ||
pub shutdown_behaviour: ReduceShutdownBehaviour, | ||
} | ||
|
||
pub struct InflightTaskWriter { | ||
store: Arc<InflightActivationStore>, | ||
buffer: Vec<InflightActivation>, | ||
max_buf_len: usize, | ||
reduce_config: ReduceConfig, | ||
} | ||
|
||
impl InflightTaskWriter { | ||
pub fn new(store: Arc<InflightActivationStore>, config: InflightTaskWriterConfig) -> Self { | ||
Self { | ||
store, | ||
buffer: Vec::with_capacity(config.max_buf_len), | ||
max_buf_len: config.max_buf_len, | ||
reduce_config: ReduceConfig { | ||
shutdown_condition: ReduceShutdownCondition::Signal, | ||
shutdown_behaviour: ReduceShutdownBehaviour::Flush, | ||
when_full_behaviour: config.when_full_behaviour, | ||
flush_interval: config.flush_interval, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl Reducer for InflightTaskWriter { | ||
type Input = InflightActivation; | ||
|
||
type Output = (); | ||
|
||
async fn reduce(&mut self, t: Self::Input) -> Result<(), anyhow::Error> { | ||
self.buffer.push(t); | ||
Ok(()) | ||
} | ||
|
||
async fn flush(&mut self) -> Result<Self::Output, anyhow::Error> { | ||
if self.buffer.is_empty() { | ||
return Ok(()); | ||
} | ||
let res = self | ||
.store | ||
.store(replace( | ||
&mut self.buffer, | ||
Vec::with_capacity(self.max_buf_len), | ||
)) | ||
.await?; | ||
info!("Inserted {:?} entries", res.rows_affected); | ||
Ok(()) | ||
} | ||
|
||
fn reset(&mut self) { | ||
self.buffer.clear(); | ||
} | ||
|
||
fn is_full(&self) -> bool { | ||
self.buffer.len() >= self.max_buf_len | ||
} | ||
|
||
fn get_reduce_config(&self) -> ReduceConfig { | ||
self.reduce_config.clone() | ||
} | ||
} |
Oops, something went wrong.