Repository navigation
Add Google Cloud Storage support for external payloads #1980
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
diwu-sf
wants to merge
17
commits into
temporalio:main
Choose a base branch
from
diwu-sf:codex/gcs-storage-driver
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
17 commits
Select commit
Hold shift + click to select a range
e2a4228
Add async GCS external storage support
diwu-sf 96d214e
Clarify GCS object key encoding
diwu-sf c9c127d
Remove S3 comparison from GCS encoding
diwu-sf b8b1366
Handle expired GCS resumable upload sessions
diwu-sf 7436c06
Use single-request GCS uploads
diwu-sf 17e860e
Simplify GCS adapter tests
diwu-sf 8a4a62b
Describe GCS storage independently
diwu-sf ae983d7
Use canonical GCS object name claims
diwu-sf 2683ff5
Apply GCS object name escaping rules
diwu-sf 58f2645
Preserve empty GCS namespace segments
diwu-sf 9a12515
Fall back to namespace-scoped GCS object names
diwu-sf 6960a43
Alphabetize built-in storage drivers
diwu-sf ceca8f3
Run GCS emulator tests automatically with pytest
diwu-sf afea032
Always use pytest-managed GCS emulator
diwu-sf 84b7610
Remove custom GCS emulator binary override
diwu-sf 24a9da7
Simplify GCS driver documentation
diwu-sf 6bfb0ee
Simplify GCS testing instructions
diwu-sf 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add an experimental Google Cloud Storage external payload storage driver with an optional async `gcloud-aio-storage` adapter. |
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Google Cloud Storage Driver | ||
|
|
||
| > ⚠️ **This package is experimental.** ⚠️ | ||
|
|
||
| `GCSStorageDriver` stores and retrieves Temporal payloads in Google Cloud Storage. | ||
|
|
||
| Install the async GCS client: | ||
|
|
||
| ```bash | ||
| python -m pip install "temporalio[gcloud-aio-storage]" | ||
| ``` | ||
|
|
||
| ```python | ||
| import dataclasses | ||
|
|
||
| from gcloud.aio.storage import Storage | ||
|
|
||
| from temporalio.client import Client | ||
| from temporalio.contrib.gcp.gcsdriver import GCSStorageDriver | ||
| from temporalio.contrib.gcp.gcsdriver.gcloud_aio import new_gcloud_aio_client | ||
| from temporalio.converter import DataConverter, ExternalStorage | ||
|
|
||
| async with Storage() as storage: | ||
| driver = GCSStorageDriver( | ||
| client=new_gcloud_aio_client(storage), | ||
| bucket="my-temporal-payloads", | ||
| ) | ||
| client = await Client.connect( | ||
| "localhost:7233", | ||
| data_converter=dataclasses.replace( | ||
| DataConverter.default, | ||
| external_storage=ExternalStorage(drivers=[driver]), | ||
| ), | ||
| ) | ||
| # Run clients and workers inside this context so their storage client stays open. | ||
| ``` | ||
|
|
||
| Configure credentials, timeouts, and retries through the `Storage` client. See the | ||
| [gcloud-aio documentation](https://talkiq.github.io/gcloud-aio/) for details. | ||
| The adapter does not add retries, and interrupted uploads must restart. | ||
|
|
||
| ## Configuration | ||
|
|
||
| - Configure this driver on every Temporal client and worker that stores or retrieves | ||
| its payloads, using the same `driver_name` (default: `gcp.gcsdriver`). | ||
| - `bucket` accepts a name or a function taking `(context, payload)` and returning | ||
| a bucket name. Downloads use the bucket recorded at upload time. | ||
| - The bucket must already exist. Use a flat namespace bucket and credentials with | ||
| `storage.objects.get` and `storage.objects.create` permissions. | ||
| - `ExternalStorage.payload_size_threshold` defaults to 256 KiB. The driver accepts | ||
| serialized payloads up to `max_payload_size` (default: 50 MiB). | ||
| - Retain objects for as long as workflow histories may need them, including replay. | ||
| The driver leaves deletion and lifecycle policies to the application. | ||
|
|
||
| To use another async GCS client, implement `GCSStorageDriverClient` with | ||
| `object_exists`, `put_object`, and `get_object`. `put_object` must leave an existing | ||
| object unchanged. | ||
|
|
||
| ## Object names and integrity | ||
|
|
||
| Objects store the full Temporal payload, including metadata. Names include the | ||
| namespace, available workflow or activity identifiers, and a SHA-256 hash: | ||
|
|
||
| ```text | ||
| v0/ns/{namespace}/wt/{workflow-type}/wi/{workflow-id}/ri/{run-id}/d/sha256/{hex-digest} | ||
| v0/ns/{namespace}/at/{activity-type}/ai/{activity-id}/ri/{run-id}/d/sha256/{hex-digest} | ||
| v0/d/sha256/{hex-digest} | ||
| ``` | ||
|
|
||
| The last form is used when no target is available. Empty or missing fields use | ||
| `null`, including an empty namespace on a workflow or activity target. Names | ||
| preserve Unicode and escape characters that interfere with | ||
| [GCS tools](https://cloud.google.com/storage/docs/objects#naming), including `/` | ||
| and `%` within individual fields. | ||
|
|
||
| Names over 1,024 UTF-8 bytes fall back to | ||
| `v0/ns/{namespace}/d/sha256/{hex-digest}`, retaining the namespace for access policies. | ||
| Identical payload bytes in the same context reuse an object. Uploads never | ||
| overwrite existing objects, and downloads verify the hash before decoding. | ||
|
|
||
| ## Testing | ||
|
|
||
| Run the tests: | ||
|
|
||
| ```bash | ||
| poe test -s tests/contrib/gcp/gcsdriver | ||
| ``` | ||
|
|
||
| Pytest downloads and caches [fake-gcs-server](https://github.com/fsouza/fake-gcs-server) | ||
| in `.pytest_cache`, starts it locally, and cleans up after the tests. To use a | ||
| preinstalled binary, put `fake-gcs-server` on `PATH`. |
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """Google Cloud Storage driver for Temporal external storage. | ||
|
|
||
| .. warning:: | ||
| This API is experimental. | ||
| """ | ||
|
|
||
| from temporalio.contrib.gcp.gcsdriver._client import GCSStorageDriverClient | ||
| from temporalio.contrib.gcp.gcsdriver._driver import GCSStorageDriver | ||
|
|
||
| __all__ = [ | ||
| "GCSStorageDriverClient", | ||
| "GCSStorageDriver", | ||
| ] |
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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """GCS storage driver client abstraction for the GCS storage driver. | ||
|
|
||
| .. warning:: | ||
| This API is experimental. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Mapping | ||
|
|
||
|
|
||
| class GCSStorageDriverClient(ABC): | ||
| """Abstract base class for GCS object operations. | ||
|
|
||
| Implementations must support ``object_exists``, ``put_object``, and | ||
| ``get_object``. Resumable uploads (if needed) are an internal concern of | ||
| each implementation. Uploads must leave existing objects unchanged. | ||
|
|
||
| .. warning:: | ||
| This API is experimental. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| async def put_object(self, *, bucket: str, object_name: str, data: bytes) -> None: | ||
| """Upload *data* if absent at the given GCS *bucket* and *object_name*.""" | ||
|
|
||
| @abstractmethod | ||
| async def object_exists(self, *, bucket: str, object_name: str) -> bool: | ||
| """Return ``True`` if an object exists at the given *bucket* and *object_name*.""" | ||
|
|
||
| @abstractmethod | ||
| async def get_object(self, *, bucket: str, object_name: str) -> bytes: | ||
| """Download and return the bytes stored at the given GCS *bucket* and *object_name*.""" | ||
|
|
||
| def describe(self) -> Mapping[str, str]: | ||
| """Return client-specific diagnostic metadata (e.g. region, credentials | ||
| source) that the driver appends to error messages. Implementations may | ||
| override this to surface configuration that is useful for debugging | ||
| common misconfigurations. Returns an empty mapping by default. | ||
| """ | ||
| return {} |
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.
This should be listed first (alphabetical order).