-
Notifications
You must be signed in to change notification settings - Fork 144
[Invoker] Drain invocation response stream #4497
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
+93
−21
Merged
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
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 |
|---|---|---|
|
|
@@ -147,8 +147,6 @@ where | |
| .try_into() | ||
| .expect("must be able to build a valid invocation path"); | ||
|
|
||
| let journal_size = journal_metadata.length; | ||
|
|
||
| debug!( | ||
| restate.invocation.id = %self.invocation_task.invocation_id, | ||
| deployment.address = %deployment.address_display(), | ||
|
|
@@ -159,15 +157,15 @@ where | |
|
|
||
| // Create an arc of the parent SpanContext. | ||
| // We send this with every journal entry to correctly link new spans generated from journal entries. | ||
| let service_invocation_span_context = journal_metadata.span_context; | ||
|
|
||
| let deployment_id = deployment.id; | ||
| // Prepare the request | ||
| let (mut http_stream_tx, request) = Self::prepare_request( | ||
| let (http_stream_tx, request) = Self::prepare_request( | ||
| path, | ||
| deployment, | ||
| self.service_protocol_version, | ||
| &self.invocation_task.invocation_id, | ||
| &service_invocation_span_context, | ||
| &journal_metadata.span_context, | ||
| ); | ||
|
|
||
| // Initialize the response stream state | ||
|
|
@@ -183,6 +181,78 @@ where | |
| .throttle(self.invocation_task.action_token_bucket.take()) | ||
| ); | ||
|
|
||
| let result = self | ||
| .run_inner( | ||
| txn, | ||
| protocol_type, | ||
| journal_metadata, | ||
| keyed_service_id, | ||
| cached_journal_items, | ||
| http_stream_tx, | ||
| &mut decoder_stream, | ||
| ) | ||
| .await; | ||
|
|
||
| // After successful termination decoder should | ||
| // not have any remaining buffer | ||
| if result.is_closed() || result.is_suspend() { | ||
| // Sanity check of the stream decoder | ||
| if decoder_stream.inner().has_remaining() { | ||
| warn_it!( | ||
| InvokerError::WriteAfterEndOfStream, | ||
| restate.invocation.id = %self.invocation_task.invocation_id, | ||
| deployment.id = %deployment_id, | ||
| deployment.service_protocol_version = %self.service_protocol_version.as_repr(), | ||
| "The read buffer is non empty after the stream has been closed." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| let inner_stream = &mut decoder_stream.inner_pin_mut().inner; | ||
|
|
||
| if tokio::time::timeout(Duration::from_secs(5), async { | ||
| loop { | ||
| match inner_stream.next().await { | ||
| None => break, | ||
| Some(Ok(_)) => {} | ||
| Some(Err(err)) => { | ||
| debug!(%err, "Error while draining invocation response stream"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| .await | ||
| .is_err() | ||
| { | ||
| warn!( | ||
| restate.invocation.id = %self.invocation_task.invocation_id, | ||
|
Contributor
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. Including deployment information would be helpful as well. |
||
| deployment.id = %deployment_id, | ||
| deployment.service_protocol_version = %self.service_protocol_version.as_repr(), | ||
| "Response stream draining timeout!" | ||
| ); | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| #[allow(clippy::too_many_arguments)] | ||
| async fn run_inner<Txn, S>( | ||
| &mut self, | ||
| txn: Txn, | ||
| protocol_type: ProtocolType, | ||
| journal_metadata: JournalMetadata, | ||
| keyed_service_id: Option<ServiceId>, | ||
| cached_journal_items: Option<Vec<JournalEntry>>, | ||
| mut http_stream_tx: mpsc::Sender<Result<Frame<Bytes>, Infallible>>, | ||
| decoder_stream: &mut S, | ||
| ) -> TerminalLoopState<()> | ||
| where | ||
| Txn: InvocationReaderTransaction, | ||
| S: Stream<Item = Result<DecoderStreamItem, InvokerError>> + Unpin, | ||
| { | ||
| let journal_size = journal_metadata.length; | ||
| let service_invocation_span_context = journal_metadata.span_context; | ||
| // === Replay phase (transaction alive) === | ||
| { | ||
| // Read state if needed (state is collected for the START message) | ||
|
|
@@ -215,7 +285,7 @@ where | |
| crate::shortcircuit!( | ||
| self.replay_loop( | ||
| &mut http_stream_tx, | ||
| &mut decoder_stream, | ||
| decoder_stream, | ||
| journal_stream, | ||
| journal_metadata.length | ||
| ) | ||
|
|
@@ -234,7 +304,7 @@ where | |
| crate::shortcircuit!( | ||
| self.replay_loop( | ||
| &mut http_stream_tx, | ||
| &mut decoder_stream, | ||
| decoder_stream, | ||
| journal_stream, | ||
| journal_metadata.length | ||
| ) | ||
|
|
@@ -255,32 +325,24 @@ where | |
| self.bidi_stream_loop( | ||
| &service_invocation_span_context, | ||
| http_stream_tx, | ||
| &mut decoder_stream | ||
| decoder_stream | ||
| ) | ||
| .await | ||
| ); | ||
| } else { | ||
| trace!("Protocol is in bidi stream mode, will now drop the sender side of the request"); | ||
| trace!( | ||
| "Protocol is not in bidi stream mode, will now drop the sender side of the request" | ||
| ); | ||
| // Drop the http_stream_tx. | ||
| // This is required in HTTP/1.1 to let the deployment send the headers back | ||
| drop(http_stream_tx) | ||
| } | ||
|
|
||
| // We don't have the invoker_rx, so we simply consume the response | ||
| trace!("Sender side of the request has been dropped, now processing the response"); | ||
| let result = self | ||
| .response_stream_loop(&service_invocation_span_context, &mut decoder_stream) | ||
| .await; | ||
|
|
||
| // Sanity check of the stream decoder | ||
| if decoder_stream.inner().has_remaining() { | ||
| warn_it!( | ||
| InvokerError::WriteAfterEndOfStream, | ||
| "The read buffer is non empty after the stream has been closed." | ||
| ); | ||
| } | ||
|
|
||
| result | ||
| self.response_stream_loop(&service_invocation_span_context, decoder_stream) | ||
| .await | ||
| } | ||
|
|
||
| fn prepare_request( | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.