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: invariant on global stream ending is upheld #92

Merged
merged 2 commits into from
Sep 27, 2024
Merged
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
24 changes: 18 additions & 6 deletions src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,28 @@ where
mut sink_stream: Streaming<sink_pb::SinkRequest>,
grpc_resp_tx: mpsc::Sender<Result<SinkResponse, Status>>,
) -> Result<(), Error> {
// loop until the global stream has been shutdown.
loop {
let done = Self::process_sink_batch(
// for every batch, we need to read from the stream. The end-of-batch is
// encoded in the request.
let stream_ended = Self::process_sink_batch(
sink_handle.clone(),
&mut sink_stream,
grpc_resp_tx.clone(),
)
.await?;
if done {

if stream_ended {
// shutting down, hence exiting the loop
break;
}
}
Ok(())
}

/// processes a batch of messages from the client, sends them to the sink handler and sends the
/// responses back to the client batches are separated by an EOT message
/// responses back to the client batches are separated by an EOT message.
/// Returns true if the global bidi-stream has ended, otherwise false.
async fn process_sink_batch(
sink_handle: Arc<T>,
sink_stream: &mut Streaming<sink_pb::SinkRequest>,
Expand All @@ -270,12 +276,18 @@ where
}
});

let mut global_stream_ended = false;

// loop until eot happens on stream is closed.
loop {
let message = match sink_stream.message().await {
Ok(Some(m)) => m,
Ok(None) => {
info!("global bidi stream ended");
return Ok(true); // bidi stream ended
// NOTE: this will only happen during shutdown. We can be certain that there
// are no messages left hanging in the UDF.
global_stream_ended = true;
break; // bidi stream ended
}
Err(e) => {
return Err(SinkError(InternalError(format!(
Expand Down Expand Up @@ -315,7 +327,7 @@ where
.await
.map_err(|e| SinkError(UserDefinedError(e.to_string())))?;

Ok(false)
Ok(global_stream_ended)
}

/// handles errors from the sink handler and sends them to the client via the response channel
Expand Down Expand Up @@ -354,7 +366,7 @@ where
}
}

// performs handshake with the client
/// performs handshake with the client
async fn perform_handshake(
&self,
sink_stream: &mut Streaming<sink_pb::SinkRequest>,
Expand Down
Loading