-
Notifications
You must be signed in to change notification settings - Fork 22
docs: add Go external storage snipsync snippets #804
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
lennessyy
wants to merge
5
commits into
main
Choose a base branch
from
docs/external-storage-snippets-go
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
5 commits
Select commit
Hold shift + click to select a range
22a9ab4
docs: add Go external storage snipsync snippets
lennessyy cb790aa
address engineer feedback on Go external storage snippets
lennessyy 9553f4c
address remaining engineer feedback on Go snippets
lennessyy 41dadb9
add comment clarifying StorageDriverActivityInfo usage
lennessyy 8a5a761
Merge branch 'main' into docs/external-storage-snippets-go
lennessyy 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
91 changes: 91 additions & 0 deletions
91
features/snippets/external_storage/custom_driver/custom_storage_driver.go
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 @@ | ||
| package customdriver | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/google/uuid" | ||
| commonpb "go.temporal.io/api/common/v1" | ||
| "go.temporal.io/sdk/converter" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| // @@@SNIPSTART go-custom-storage-driver | ||
| type LocalDiskStorageDriver struct { | ||
| storeDir string | ||
| } | ||
|
|
||
| func NewLocalDiskStorageDriver(storeDir string) converter.StorageDriver { | ||
| return &LocalDiskStorageDriver{storeDir: storeDir} | ||
| } | ||
|
|
||
| func (d *LocalDiskStorageDriver) Name() string { | ||
| return "my-local-disk" | ||
| } | ||
|
|
||
| func (d *LocalDiskStorageDriver) Type() string { | ||
| return "local-disk" | ||
| } | ||
|
|
||
| func (d *LocalDiskStorageDriver) Store( | ||
| ctx converter.StorageDriverStoreContext, | ||
| payloads []*commonpb.Payload, | ||
| ) ([]converter.StorageDriverClaim, error) { | ||
| dir := d.storeDir | ||
| switch info := ctx.Target.(type) { | ||
| case converter.StorageDriverWorkflowInfo: | ||
| if info.WorkflowID != "" { | ||
| dir = filepath.Join(d.storeDir, info.Namespace, info.WorkflowID) | ||
| } | ||
| case converter.StorageDriverActivityInfo: | ||
| // StorageDriverActivityInfo is only used for standalone (non-workflow-bound) | ||
| // activities. Activities started by a workflow use StorageDriverWorkflowInfo. | ||
| if info.ActivityID != "" { | ||
| dir = filepath.Join(d.storeDir, info.Namespace, info.ActivityID) | ||
| } | ||
| } | ||
| if err := os.MkdirAll(dir, 0o755); err != nil { | ||
| return nil, fmt.Errorf("create store directory: %w", err) | ||
| } | ||
|
|
||
| claims := make([]converter.StorageDriverClaim, len(payloads)) | ||
| for i, payload := range payloads { | ||
| key := uuid.NewString() + ".bin" | ||
| filePath := filepath.Join(dir, key) | ||
|
|
||
| data, err := proto.Marshal(payload) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("marshal payload: %w", err) | ||
| } | ||
| if err := os.WriteFile(filePath, data, 0o644); err != nil { | ||
| return nil, fmt.Errorf("write payload: %w", err) | ||
| } | ||
|
|
||
| claims[i] = converter.StorageDriverClaim{ | ||
| ClaimData: map[string]string{"path": filePath}, | ||
| } | ||
| } | ||
| return claims, nil | ||
| } | ||
|
|
||
| func (d *LocalDiskStorageDriver) Retrieve( | ||
| ctx converter.StorageDriverRetrieveContext, | ||
| claims []converter.StorageDriverClaim, | ||
| ) ([]*commonpb.Payload, error) { | ||
| payloads := make([]*commonpb.Payload, len(claims)) | ||
| for i, claim := range claims { | ||
| filePath := claim.ClaimData["path"] | ||
| data, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read payload: %w", err) | ||
| } | ||
| payload := &commonpb.Payload{} | ||
| if err := proto.Unmarshal(data, payload); err != nil { | ||
| return nil, fmt.Errorf("unmarshal payload: %w", err) | ||
| } | ||
| payloads[i] = payload | ||
| } | ||
| return payloads, nil | ||
| } | ||
| // @@@SNIPEND |
26 changes: 26 additions & 0 deletions
26
features/snippets/external_storage/multiple_drivers/multiple_drivers.go
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,26 @@ | ||
| package multipledrivers | ||
|
|
||
| import ( | ||
| commonpb "go.temporal.io/api/common/v1" | ||
| "go.temporal.io/sdk/converter" | ||
| ) | ||
|
|
||
| // @@@SNIPSTART go-external-storage-multiple-drivers | ||
| type PreferredSelector struct { | ||
| preferred converter.StorageDriver | ||
| } | ||
|
|
||
| func (s *PreferredSelector) SelectDriver( | ||
| ctx converter.StorageDriverStoreContext, | ||
| payload *commonpb.Payload, | ||
| ) (converter.StorageDriver, error) { | ||
| return s.preferred, nil | ||
| } | ||
|
|
||
| func MultipleDriversSetup(preferredDriver, legacyDriver converter.StorageDriver) converter.ExternalStorage { | ||
| return converter.ExternalStorage{ | ||
| Drivers: []converter.StorageDriver{preferredDriver, legacyDriver}, | ||
| DriverSelector: &PreferredSelector{preferred: preferredDriver}, | ||
| } | ||
| } | ||
| // @@@SNIPEND |
32 changes: 32 additions & 0 deletions
32
features/snippets/external_storage/s3_setup/s3_driver_create.go
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,32 @@ | ||
| package s3setup | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/service/s3" | ||
| "go.temporal.io/sdk/contrib/aws/s3driver" | ||
| "go.temporal.io/sdk/contrib/aws/s3driver/awssdkv2" | ||
| "go.temporal.io/sdk/converter" | ||
| ) | ||
|
|
||
| func CreateS3Driver() converter.StorageDriver { | ||
| // @@@SNIPSTART go-s3-driver-create | ||
| cfg, err := config.LoadDefaultConfig(context.Background(), | ||
| config.WithRegion("us-east-2"), | ||
| ) | ||
| if err != nil { | ||
| log.Fatalf("load AWS config: %v", err) | ||
| } | ||
|
|
||
| driver, err := s3driver.NewDriver(s3driver.Options{ | ||
| Client: awssdkv2.NewClient(s3.NewFromConfig(cfg)), | ||
| Bucket: s3driver.StaticBucket("my-temporal-payloads"), | ||
| }) | ||
| if err != nil { | ||
| log.Fatalf("create S3 driver: %v", err) | ||
| } | ||
| // @@@SNIPEND | ||
| return driver | ||
| } |
27 changes: 27 additions & 0 deletions
27
features/snippets/external_storage/s3_setup/s3_external_storage_setup.go
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,27 @@ | ||
| package s3setup | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| "go.temporal.io/sdk/client" | ||
| "go.temporal.io/sdk/converter" | ||
| "go.temporal.io/sdk/worker" | ||
| ) | ||
|
|
||
| func SetupExternalStorage(driver converter.StorageDriver) { | ||
| // @@@SNIPSTART go-s3-external-storage-setup | ||
| c, err := client.Dial(client.Options{ | ||
| HostPort: "localhost:7233", | ||
| ExternalStorage: converter.ExternalStorage{ | ||
| Drivers: []converter.StorageDriver{driver}, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| log.Fatalf("connect to Temporal: %v", err) | ||
| } | ||
| defer c.Close() | ||
|
|
||
| w := worker.New(c, "my-task-queue", worker.Options{}) | ||
| // @@@SNIPEND | ||
| _ = w | ||
| } |
23 changes: 23 additions & 0 deletions
23
features/snippets/external_storage/threshold/threshold_config.go
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,23 @@ | ||
| package threshold | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| "go.temporal.io/sdk/client" | ||
| "go.temporal.io/sdk/converter" | ||
| ) | ||
|
|
||
| func ThresholdConfig(driver converter.StorageDriver) { | ||
| // @@@SNIPSTART go-external-storage-threshold | ||
| c, err := client.Dial(client.Options{ | ||
| ExternalStorage: converter.ExternalStorage{ | ||
| Drivers: []converter.StorageDriver{driver}, | ||
| PayloadSizeThreshold: 1, | ||
| }, | ||
| }) | ||
| // @@@SNIPEND | ||
| if err != nil { | ||
| log.Fatalf("connect to Temporal: %v", err) | ||
| } | ||
| defer c.Close() | ||
| } | ||
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 it not expected that these functions are called/tested in this repo?
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.
I don't think the snippet bar is "called/tested" but "does it compile"?