-
Notifications
You must be signed in to change notification settings - Fork 211
feat(extstore): Add support for Nexus task handling #1676
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
Open
jmaeagle99
wants to merge
2
commits into
main
Choose a base branch
from
extstore-nexus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| import concurrent.futures | ||
| import contextvars | ||
| import threading | ||
| from collections.abc import Callable, Mapping, Sequence | ||
| from collections.abc import Awaitable, Callable, Mapping, Sequence | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timezone | ||
| from functools import reduce | ||
|
|
@@ -30,6 +30,8 @@ | |
| import temporalio.common | ||
| import temporalio.converter | ||
| import temporalio.nexus | ||
| from temporalio.bridge._visitor import PayloadVisitor | ||
| from temporalio.bridge._visitor_functions import PayloadSequence, VisitorFunctions | ||
| from temporalio.bridge.worker import PollShutdownError | ||
| from temporalio.exceptions import ( | ||
| ApplicationError, | ||
|
|
@@ -216,6 +218,19 @@ async def _complete_task( | |
| ): | ||
| await asyncio.shield(self._bridge_worker().complete_nexus_task(completion)) | ||
|
|
||
| async def _encode_completion( | ||
|
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. I'm confused by the lack of decoding. Is that because it was already happening and just didn't do anything since it saw no claims? |
||
| self, completion: temporalio.bridge.proto.nexus.NexusTaskCompletion | ||
| ) -> None: | ||
| """Apply the payload codec then external storage to the completion's payloads.""" | ||
| dc = self._data_converter | ||
| await PayloadVisitor(skip_search_attributes=True, skip_headers=True).visit( | ||
| _PayloadTransformVisitor(dc._encode_payload_sequence), completion | ||
| ) | ||
| await PayloadVisitor(skip_search_attributes=True).visit( | ||
| _PayloadTransformVisitor(dc._external_store_payload_sequence), | ||
| completion, | ||
| ) | ||
|
|
||
| # TODO(nexus-preview): stack trace pruning. See sdk-typescript NexusHandler.execute | ||
| # "Any call up to this function and including this one will be trimmed out of stack traces."" | ||
|
|
||
|
|
@@ -260,6 +275,14 @@ async def _handle_cancel_operation_task( | |
| try: | ||
| try: | ||
| await self._handler.cancel_operation(ctx, request.operation_token) | ||
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
| completed=temporalio.api.nexus.v1.Response( | ||
| cancel_operation=temporalio.api.nexus.v1.CancelOperationResponse() | ||
| ), | ||
| ) | ||
| # No-op but keeps the cancel covered if it ever carries a payload. | ||
| await self._encode_completion(completion) | ||
| except asyncio.CancelledError: | ||
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
|
|
@@ -271,16 +294,12 @@ async def _handle_cancel_operation_task( | |
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
| ) | ||
| await self._data_converter.encode_failure( | ||
| handler_error, completion.failure | ||
| ) | ||
| else: | ||
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
| completed=temporalio.api.nexus.v1.Response( | ||
| cancel_operation=temporalio.api.nexus.v1.CancelOperationResponse() | ||
| ), | ||
| self._data_converter.failure_converter.to_failure( | ||
| handler_error, | ||
| self._data_converter.payload_converter, | ||
| completion.failure, | ||
| ) | ||
| await self._encode_completion(completion) | ||
| await self._complete_task(completion) | ||
| except Exception: | ||
| logger.exception("Failed to send Nexus task completion") | ||
|
|
@@ -315,6 +334,13 @@ async def _handle_start_operation_task( | |
| request_deadline, | ||
| endpoint, | ||
| ) | ||
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
| completed=temporalio.api.nexus.v1.Response( | ||
| start_operation=start_response | ||
| ), | ||
| ) | ||
| await self._encode_completion(completion) | ||
| except asyncio.CancelledError: | ||
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
|
|
@@ -326,19 +352,15 @@ async def _handle_start_operation_task( | |
| task_token=task_token, | ||
| ) | ||
| handler_error = _exception_to_handler_error(err) | ||
| await self._data_converter.encode_failure( | ||
| handler_error, completion.failure | ||
| self._data_converter.failure_converter.to_failure( | ||
| handler_error, | ||
| self._data_converter.payload_converter, | ||
| completion.failure, | ||
| ) | ||
|
|
||
| if isinstance(err, concurrent.futures.BrokenExecutor): | ||
| self._fail_worker_exception_queue.put_nowait(err) | ||
| else: | ||
| completion = temporalio.bridge.proto.nexus.NexusTaskCompletion( | ||
| task_token=task_token, | ||
| completed=temporalio.api.nexus.v1.Response( | ||
| start_operation=start_response | ||
| ), | ||
| ) | ||
| await self._encode_completion(completion) | ||
|
|
||
| await self._complete_task(completion) | ||
| except Exception: | ||
|
|
@@ -417,7 +439,9 @@ async def _start_operation( | |
| ) | ||
| ) | ||
| elif isinstance(result, nexusrpc.handler.StartOperationResultSync): | ||
| [payload] = await self._data_converter.encode([result.value]) | ||
| [payload] = self._data_converter.payload_converter.to_payloads( | ||
| [result.value] | ||
| ) | ||
| return temporalio.api.nexus.v1.StartOperationResponse( | ||
| sync_success=temporalio.api.nexus.v1.StartOperationResponse.Sync( | ||
| payload=payload, | ||
|
|
@@ -446,10 +470,41 @@ async def _start_operation( | |
| ) from err.__cause__ | ||
| except FailureError as new_err: | ||
| response = temporalio.api.nexus.v1.StartOperationResponse() | ||
| await self._data_converter.encode_failure(new_err, response.failure) | ||
| self._data_converter.failure_converter.to_failure( | ||
| new_err, | ||
| self._data_converter.payload_converter, | ||
| response.failure, | ||
| ) | ||
| return response | ||
|
|
||
|
|
||
| class _PayloadTransformVisitor(VisitorFunctions): | ||
| """Adapts a payload-sequence transform for use with :class:`PayloadVisitor`.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| f: Callable[ | ||
| [Sequence[temporalio.api.common.v1.Payload]], | ||
| Awaitable[list[temporalio.api.common.v1.Payload]], | ||
| ], | ||
| ) -> None: | ||
| self._f = f | ||
|
|
||
| async def visit_payload(self, payload: temporalio.api.common.v1.Payload) -> None: | ||
| new_payload = (await self._f([payload]))[0] | ||
| if new_payload is not payload: | ||
| payload.CopyFrom(new_payload) | ||
|
|
||
| async def visit_payloads(self, payloads: PayloadSequence) -> None: | ||
| if len(payloads) == 0: | ||
| return | ||
| new_payloads = await self._f(payloads) | ||
| if new_payloads is payloads: | ||
| return | ||
| del payloads[:] | ||
| payloads.extend(new_payloads) | ||
|
|
||
|
|
||
| @dataclass | ||
| class _DummyPayloadSerializer: | ||
| data_converter: temporalio.converter.DataConverter | ||
|
|
@@ -465,18 +520,35 @@ async def deserialize( | |
| content: nexusrpc.Content, # type:ignore[reportUnusedParameter] | ||
| as_type: type[Any] | None = None, | ||
| ) -> Any: | ||
| payload = self.payload | ||
| if self.data_converter.payload_codec: | ||
| try: | ||
| [payload] = await self.data_converter.payload_codec.decode([payload]) | ||
| except Exception as err: | ||
| raise nexusrpc.HandlerError( | ||
| "Payload codec failed to decode Nexus operation input", | ||
| type=nexusrpc.HandlerErrorType.INTERNAL, | ||
| ) from err | ||
| dc = self.data_converter | ||
| # The visitor mutates in place, so work on a copy to leave the request | ||
| # payload untouched. | ||
| payload = temporalio.api.common.v1.Payload() | ||
| payload.CopyFrom(self.payload) | ||
| try: | ||
| await PayloadVisitor(skip_search_attributes=True).visit( | ||
| _PayloadTransformVisitor(dc._external_retrieve_payload_sequence), | ||
| payload, | ||
| ) | ||
| except Exception as err: | ||
| raise nexusrpc.HandlerError( | ||
| "Failed to retrieve Nexus operation input from external storage", | ||
| type=nexusrpc.HandlerErrorType.INTERNAL, | ||
| retryable_override=True, | ||
| ) from err | ||
|
|
||
| try: | ||
| await PayloadVisitor(skip_search_attributes=True, skip_headers=True).visit( | ||
| _PayloadTransformVisitor(dc._decode_payload_sequence), payload | ||
| ) | ||
| except Exception as err: | ||
| raise nexusrpc.HandlerError( | ||
| "Payload codec failed to decode Nexus operation input", | ||
| type=nexusrpc.HandlerErrorType.INTERNAL, | ||
| ) from err | ||
|
|
||
| try: | ||
| [input] = self.data_converter.payload_converter.from_payloads( | ||
| [input] = dc.payload_converter.from_payloads( | ||
| [payload], | ||
| type_hints=[as_type] if as_type else None, | ||
| ) | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Nexus task transport drops the
external_payloadsfield. Regardless, it shouldn't be checking for this field anyway and should only rely on the metadata.