Skip to content

producer: flush callback #56

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

Merged
merged 6 commits into from
Sep 27, 2022
Merged
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
4 changes: 3 additions & 1 deletion src/hstreamdb/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use url::Url;

use crate::appender::Appender;
use crate::channel_provider::{new_channel_provider, ChannelProviderSettings, Channels};
use crate::producer::{FlushSettings, Producer};
use crate::producer::{BoxedFlushCallback, FlushSettings, Producer};
use crate::{common, flow_controller, format_url, producer};

pub struct Client {
Expand Down Expand Up @@ -172,6 +172,7 @@ impl Client {
flow_control_bytes_limit: Option<usize>,
flush_settings: FlushSettings,
channel_provider_settings: ChannelProviderSettings,
flush_callback: Option<BoxedFlushCallback>,
) -> common::Result<(Appender, Producer)> {
let (request_sender, request_receiver) =
tokio::sync::mpsc::unbounded_channel::<producer::Request>();
Expand All @@ -195,6 +196,7 @@ impl Client {
flow_controller,
compression_type,
flush_settings,
flush_callback,
)
.await?;
Ok((appender, producer))
Expand Down
27 changes: 24 additions & 3 deletions src/hstreamdb/src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ pub struct Producer {
compression_type: CompressionType,
flush_settings: FlushSettings,
shards: Vec<Shard>,
flush_callback: Option<BoxedFlushCallback>,
}

pub type BoxedFlushCallback = Arc<dyn Fn(bool, usize, usize) + Send + Sync>;

#[derive(Default)]
struct BufferState {
len: usize,
Expand Down Expand Up @@ -132,6 +135,7 @@ impl BufferState {
}

impl Producer {
#[allow(clippy::too_many_arguments)]
pub(crate) async fn new(
channels: Channels,
url_scheme: String,
Expand All @@ -140,6 +144,7 @@ impl Producer {
flow_controller: Option<FlowControllerClient>,
compression_type: CompressionType,
flush_settings: FlushSettings,
flush_callback: Option<BoxedFlushCallback>,
) -> common::Result<Self> {
let shards = channels
.channel()
Expand Down Expand Up @@ -169,6 +174,7 @@ impl Producer {
compression_type,
flush_settings,
shards,
flush_callback,
};
Ok(producer)
}
Expand Down Expand Up @@ -296,7 +302,9 @@ impl Producer {
shard_url,
self.compression_type,
buffer,
buffer_size,
results,
self.flush_callback.clone(),
);
let task = tokio::spawn(async move {
task.await;
Expand Down Expand Up @@ -326,14 +334,17 @@ impl Producer {
}
}

#[allow(clippy::too_many_arguments)]
async fn flush(
channels: Channels,
stream_name: String,
shard_id: ShardId,
shard_url: String,
compression_type: CompressionType,
buffer: Vec<Record>,
buffer_size: usize,
results: ResultVec,
flush_callback: Option<BoxedFlushCallback>,
) -> Result<(), String> {
if buffer.is_empty() {
Ok(())
Expand All @@ -342,15 +353,20 @@ async fn flush(
.channel_at(shard_url.clone())
.await
.map_err(|err| format!("producer connect error: url = {shard_url}, {err}"))?;
match append(
let append_result = append(
channel,
stream_name,
shard_id,
compression_type,
buffer.to_vec(),
)
.await
{
.await;

if let Some(flush_callback) = flush_callback.as_ref() {
flush_callback(append_result.is_ok(), buffer.len(), buffer_size)
}

match append_result {
Err(err) => {
let err = Arc::new(err);
for sender in results.into_iter() {
Expand All @@ -377,14 +393,17 @@ async fn flush(
}
}

#[allow(clippy::too_many_arguments)]
async fn flush_(
channels: Channels,
stream_name: String,
shard_id: ShardId,
shard_url: String,
compression_type: CompressionType,
buffer: Vec<Record>,
buffer_size: usize,
results: ResultVec,
flush_callback: Option<BoxedFlushCallback>,
) {
flush(
channels,
Expand All @@ -393,7 +412,9 @@ async fn flush_(
shard_url,
compression_type,
buffer,
buffer_size,
results,
flush_callback,
)
.await
.unwrap_or_else(|err| log::error!("{err}"))
Expand Down
1 change: 1 addition & 0 deletions src/hstreamdb/tests/consumer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async fn test_consumer() {
ChannelProviderSettings {
concurrency_limit: Some(8),
},
None,
)
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/hstreamdb/tests/producer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async fn test_producer() {
ChannelProviderSettings {
concurrency_limit: Some(8),
},
None,
)
.await
.unwrap();
Expand Down
31 changes: 19 additions & 12 deletions src/x/hstreamdb-erl-nifs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ mod runtime;
rustler::atoms! {
compression_type, none, gzip, zstd,
concurrency_limit,
max_batch_len, max_batch_size, batch_deadline
max_batch_len, max_batch_size, batch_deadline,
record_id
}

rustler::init!(
Expand All @@ -33,7 +34,7 @@ rustler::init!(

#[derive(Debug)]
enum AppendResult {
RecordId(String),
RecordId(RecordId),
Error(String),
}

Expand Down Expand Up @@ -129,6 +130,7 @@ pub fn try_start_producer(
None,
flush_settings,
ChannelProviderSettings { concurrency_limit },
None,
)
.await?;

Expand Down Expand Up @@ -207,23 +209,28 @@ fn await_append_result(env: Env, x: ResourceArc<AppendResultFuture>) -> Term {
let receiver: &Mutex<_> = &x.0;
let mut receiver: MutexGuard<Option<_>> = receiver.lock();
let receiver = mem::take(&mut (*receiver));
let append_result: Result<String, Arc<_>> = receiver
.unwrap()
.blocking_recv()
.unwrap()
.map(|x| x.to_string());
let append_result: Result<hstreamdb::RecordId, Arc<_>> =
receiver.unwrap().blocking_recv().unwrap();
let append_result = match append_result {
Ok(record_id) => RecordId(record_id),
Err(err) => Error(err.to_string()),
};
result.set(append_result).unwrap()
}

let result = match result.get().unwrap() {
RecordId(record_id) => (ok(), record_id.to_string()),
Error(err) => (error(), err.to_string()),
};
result.encode(env)
match result.get().unwrap() {
RecordId(record_id_v) => (
ok(),
(
record_id(),
record_id_v.shard_id,
record_id_v.batch_id,
record_id_v.batch_index,
),
)
.encode(env),
Error(err) => (error(), err.to_string()).encode(env),
}
}

fn atom_to_compression_type(compression_type: Atom) -> Option<CompressionType> {
Expand Down