forked from aws/aws-durable-execution-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add distributed map operation #1
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
Draft
nvasiu
wants to merge
1
commit into
main
Choose a base branch
from
feat/map-run
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.
Draft
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 |
|---|---|---|
|
|
@@ -5,13 +5,14 @@ | |
| import json | ||
| import logging | ||
| from collections import Counter | ||
| from dataclasses import dataclass | ||
| from dataclasses import dataclass, field | ||
| from enum import Enum | ||
| from typing import TYPE_CHECKING, Generic, TypeVar | ||
| from typing import TYPE_CHECKING, Any, Generic, TypeVar | ||
|
|
||
| from aws_durable_execution_sdk_python.exceptions import ( | ||
| ChildContextError, | ||
| InvalidStateError, | ||
| DistributedMapError, | ||
| ) | ||
| from aws_durable_execution_sdk_python.lambda_service import ErrorObject | ||
| from aws_durable_execution_sdk_python.types import BatchResult as BatchResultProtocol | ||
|
|
@@ -550,3 +551,137 @@ def fatal(cls, index: int, error: BaseException) -> BranchEvent[ResultType]: | |
|
|
||
|
|
||
| # endregion concurrency models | ||
|
|
||
|
|
||
| # region map run result models | ||
| class DistributedMapStatus(Enum): | ||
| SUCCEEDED = "SUCCEEDED" | ||
| FAILED = "FAILED" | ||
| STOPPED = "STOPPED" | ||
| TIMED_OUT = "TIMED_OUT" | ||
|
|
||
|
|
||
| class DistributedMapCompletionReason(Enum): | ||
| ALL_COMPLETED = "ALL_COMPLETED" | ||
| ITEM_LIMIT_REACHED = "ITEM_LIMIT_REACHED" | ||
| STOPPED = "STOPPED" | ||
| TIMED_OUT = "TIMED_OUT" | ||
| FAILURE_TOLERANCE_EXCEEDED = "FAILURE_TOLERANCE_EXCEEDED" | ||
| SOURCE_FAILED = "SOURCE_FAILED" | ||
| DESTINATION_FAILED = "DESTINATION_FAILED" | ||
| INLINE_RESULT_LIMIT_EXCEEDED = "INLINE_RESULT_LIMIT_EXCEEDED" | ||
| INVALID_CONFIGURATION = "INVALID_CONFIGURATION" | ||
| QUOTA_EXCEEDED = "QUOTA_EXCEEDED" | ||
| KMS_ACCESS_DENIED = "KMS_ACCESS_DENIED" | ||
| INTERNAL_ERROR = "INTERNAL_ERROR" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DistributedMapSummary: | ||
| """Outcome of a map run without per-item results. | ||
|
|
||
| Resolved by ``ctx.distributed_map`` for every terminal state. A non-``SUCCEEDED`` | ||
| run resolves with this summary rather than raising. Use | ||
| :meth:`throw_if_error` to opt into raising. | ||
| """ | ||
|
|
||
| status: DistributedMapStatus | ||
| completion_reason: DistributedMapCompletionReason | ||
| success_count: int | ||
| failure_count: int | ||
| unprocessed_count: int | ||
| distributed_map_run_arn: str | None = None | ||
| completion_details: str | None = None | ||
| total_count: int | None = None | ||
|
|
||
| @property | ||
| def distributed_map_id(self) -> str | None: | ||
| """Map-run id derived from the ARN, or ``None`` when the ARN is absent.""" | ||
| if not self.distributed_map_run_arn: | ||
| return None | ||
| return self.distributed_map_run_arn.rsplit(":", 1)[-1] | ||
|
|
||
| @property | ||
| def has_failure(self) -> bool: | ||
| """``True`` when any item permanently failed.""" | ||
| return self.failure_count > 0 | ||
|
|
||
| def throw_if_error(self) -> None: | ||
| """Raise :class:`DistributedMapError` on any non-success outcome.""" | ||
| if self.status is not DistributedMapStatus.SUCCEEDED: | ||
| detail = f", {self.completion_details}" if self.completion_details else "" | ||
| msg = ( | ||
| f"Map run ended {self.status.value} " | ||
| f"(reason: {self.completion_reason.value}{detail})" | ||
| ) | ||
| raise DistributedMapError(msg) | ||
| if self.failure_count > 0: | ||
| msg = ( | ||
| f"Map run succeeded but {self.failure_count} item(s) permanently failed" | ||
| ) | ||
| raise DistributedMapError(msg) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DistributedMapItemError: | ||
| """Error for a single failed map run item.""" | ||
|
|
||
| error_type: str | ||
| error_message: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DistributedMapResultItem: | ||
| """Outcome of a single map run item.""" | ||
|
|
||
| item_id: str | ||
| status: str | ||
|
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. suggestion: this would benefit from being an enum rather than a raw string, matching the other wire enums in this SDK (e.g. |
||
| output: Any | None = None | ||
| error: DistributedMapItemError | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DistributedMapResult(DistributedMapSummary): | ||
| """Outcome of a map run with per-item results. | ||
|
|
||
| Resolved by ``ctx.distributed_map`` when inline result collection is enabled. | ||
| Extends :class:`DistributedMapSummary` with the retained per-item results. | ||
| """ | ||
|
|
||
| all: list[DistributedMapResultItem] = field(default_factory=list) | ||
|
|
||
| def succeeded(self) -> list[DistributedMapResultItem]: | ||
| """Return the items that succeeded.""" | ||
| return [item for item in self.all if item.status == "SUCCEEDED"] | ||
|
|
||
| def failed(self) -> list[DistributedMapResultItem]: | ||
| """Return the items that permanently failed.""" | ||
| return [item for item in self.all if item.status == "FAILED"] | ||
|
|
||
| def get_results(self) -> list[Any]: | ||
| """Return the outputs of the succeeded items.""" | ||
| return [item.output for item in self.all if item.status == "SUCCEEDED"] | ||
|
|
||
| def get_errors(self) -> list[DistributedMapItemError]: | ||
| """Return the errors of the failed items.""" | ||
| return [ | ||
| item.error | ||
| for item in self.all | ||
| if item.status == "FAILED" and item.error is not None | ||
| ] | ||
|
|
||
| def throw_if_error(self) -> None: | ||
| """Raise the first failed item's error, otherwise defer to the summary rule.""" | ||
| if self.status is DistributedMapStatus.SUCCEEDED and self.failure_count > 0: | ||
| failed = self.failed() | ||
| if failed: | ||
| first = failed[0] | ||
| if first.error is not None: | ||
| msg = f"{first.error.error_type}: {first.error.error_message}" | ||
| raise DistributedMapError(msg) | ||
| msg = f"item {first.item_id} failed" | ||
| raise DistributedMapError(msg) | ||
| super().throw_if_error() | ||
|
|
||
|
|
||
| # endregion map run result models | ||
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.
is the run ARN's final shape settled? this only works if the id follows the last colon. If the ARN ends up a child resource like durable execution ARNs (
.../distributed-map-run/<id>), this silently returns the whole trailing path instead of the id. The tests won't catch it because they assert against an invented ARN shape.