Skip to content
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

fix(kafka): Flush on shutdown #4020

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions relay-kafka/src/producer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ impl KafkaClient {
})?;
producer.send(key, headers, variant, payload)
}

/// Flush all messages.
pub fn flush(&self, timeout: Duration) {
let start = Instant::now();
for (topic, producer) in &self.producers {
if let Err(e) = producer
.producer
.flush(timeout.saturating_sub(start.elapsed()))
{
relay_log::error!(
error = &e as &dyn std::error::Error,
tags.topic = ?topic,
"error while flushing kafka topic"
);
}
}
}
}

/// Helper structure responsible for building the actual [`KafkaClient`].
Expand Down
31 changes: 24 additions & 7 deletions relay-server/src/services/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::borrow::Cow;
use std::collections::BTreeMap;
use std::error::Error;
use std::sync::Arc;
use std::time::Instant;
use std::time::{Duration, Instant};

use bytes::Bytes;
use relay_base_schema::data_category::DataCategory;
Expand All @@ -22,7 +22,7 @@ use relay_metrics::{
};
use relay_quotas::Scoping;
use relay_statsd::metric;
use relay_system::{Addr, FromMessage, Interface, NoResponse, Service};
use relay_system::{Addr, Controller, FromMessage, Interface, NoResponse, Service, Shutdown};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use serde_json::Deserializer;
Expand Down Expand Up @@ -164,6 +164,10 @@ impl StoreService {
})
}

fn flush(&self, timeout: Duration) {
self.producer.client.flush(timeout);
}

fn handle_store_envelope(&self, message: StoreEnvelope) {
let StoreEnvelope {
envelope: mut managed,
Expand Down Expand Up @@ -1047,14 +1051,27 @@ impl Service for StoreService {
fn spawn_handler(self, mut rx: relay_system::Receiver<Self::Interface>) {
let this = Arc::new(self);

let mut shutdown = Controller::shutdown_handle();

tokio::spawn(async move {
relay_log::info!("store forwarder started");

while let Some(message) = rx.recv().await {
let service = Arc::clone(&this);
this.workers
.spawn(move || service.handle_message(message))
.await;
loop {
tokio::select! {
Some(message) = rx.recv() => {
let service = Arc::clone(&this);
this.workers
.spawn(move || service.handle_message(message))
.await;
},
Shutdown{ timeout: Some(timeout) } = shutdown.notified() => {
let service = Arc::clone(&this);
this.workers.spawn(move || {
service.flush(timeout);
}).await;
},
else => break,
}
}

relay_log::info!("store forwarder stopped");
Expand Down
Loading