-
Notifications
You must be signed in to change notification settings - Fork 102
Enhance batch future sample #106
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
Bueller87
merged 9 commits into
cadence-workflow:master
from
Bueller87:enhance-batch-future-sample
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ccacb04
Add batch to Makefile
cc61cfb
Create batch sample entry point
12f5483
Create main package
fcce0cb
Add comments to workflow code for clarity
ccc862f
Add readme file for batch sample
8dc3e86
Upgrade go-client version from rc.8 to rc.10
e406741
Remove x module reference
9da3e1d
Make readme more compeling and informative
6671c56
Change order of TEST_DIRS to match bins
Bueller87 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,58 @@ | ||
| # Batch Processing Sample | ||
|
|
||
| This sample demonstrates how to process large batches of activities with controlled concurrency using Cadence's `workflow.NewBatchFuture` functionality, while respecting the 1024 pending activities limit per workflow. | ||
|
|
||
| ## The problem it solves | ||
|
|
||
| **The Problem**: When processing large datasets (thousands of records, files, or API calls), you face a dilemma: | ||
| - **Sequential processing**: Too slow, poor user experience | ||
| - **Unlimited concurrency**: Overwhelms databases, APIs, or downstream services | ||
| - **Manual concurrency control**: Complex error handling and resource management | ||
| - **Cadence limits**: Max 1024 pending activities per workflow | ||
|
|
||
| **Real-world scenarios**: | ||
| - Processing 10,000 user records for a migration | ||
| - Sending emails to 50,000 subscribers | ||
| - Generating reports for 1,000 customers | ||
| - Processing files in a data pipeline | ||
|
|
||
| ### The Solution | ||
| `workflow.NewBatchFuture` provides a robust solution: | ||
|
|
||
| **Controlled Concurrency**: Process items in parallel while respecting system limits | ||
| **Automatic Error Handling**: Failed activities don't crash the entire batch | ||
| **Resource Efficiency**: Optimal throughput without overwhelming downstream services | ||
| **Built-in Observability**: Monitoring, retries, and failure tracking | ||
| **Workflow Integration**: Seamless integration with Cadence's workflow engine | ||
|
|
||
| This eliminates the need to build custom concurrency control, error handling, and monitoring systems. | ||
|
|
||
| ## Sample behavior | ||
|
|
||
| - Creates a configurable number of activities (default: 10) | ||
| - Executes them with controlled concurrency (default: 3) | ||
| - Simulates work with random delays (900-999ms per activity) | ||
| - Handles cancellation gracefully | ||
|
|
||
| ## Technical considerations | ||
|
|
||
| - **Cadence limit**: Maximum 1024 pending activities per workflow | ||
| - **Resource management**: Controlled concurrency prevents system overload | ||
| - **Error handling**: Failed activities don't crash the entire batch | ||
|
|
||
| ## How to run | ||
|
|
||
| 1. Build the sample: | ||
| ```bash | ||
| make batch | ||
| ``` | ||
|
|
||
| 2. Start Worker: | ||
| ```bash | ||
| ./bin/batch -m worker | ||
| ``` | ||
|
|
||
| 3. Start Workflow: | ||
| ```bash | ||
| ./bin/batch -m trigger | ||
| ``` |
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,71 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "time" | ||
|
|
||
| "github.com/pborman/uuid" | ||
| "go.uber.org/cadence/client" | ||
| "go.uber.org/cadence/worker" | ||
|
|
||
| "github.com/uber-common/cadence-samples/cmd/samples/common" | ||
| ) | ||
|
|
||
| // This needs to be done as part of a bootstrap step when the process starts. | ||
| // The workers are supposed to be long running. | ||
| func startWorkers(h *common.SampleHelper) { | ||
| // Configure worker options. | ||
| workerOptions := worker.Options{ | ||
| MetricsScope: h.WorkerMetricScope, | ||
| Logger: h.Logger, | ||
| FeatureFlags: client.FeatureFlags{ | ||
| WorkflowExecutionAlreadyCompletedErrorEnabled: true, | ||
| }, | ||
| } | ||
| h.StartWorkers(h.Config.DomainName, ApplicationName, workerOptions) | ||
| } | ||
|
|
||
| func startWorkflow(h *common.SampleHelper) { | ||
| workflowOptions := client.StartWorkflowOptions{ | ||
| ID: "batch_" + uuid.New(), | ||
| TaskList: ApplicationName, | ||
| ExecutionStartToCloseTimeout: time.Minute * 5, | ||
| DecisionTaskStartToCloseTimeout: time.Minute * 5, | ||
| } | ||
|
|
||
| // Default batch configuration | ||
| input := BatchWorkflowInput{ | ||
| Concurrency: 3, | ||
| TotalSize: 10, | ||
| } | ||
|
|
||
| h.StartWorkflow(workflowOptions, batchWorkflowName, input) | ||
| } | ||
|
|
||
| func registerWorkflowAndActivity( | ||
| h *common.SampleHelper, | ||
| ) { | ||
| h.RegisterWorkflowWithAlias(BatchWorkflow, batchWorkflowName) | ||
| h.RegisterActivity(BatchActivity) | ||
| } | ||
|
|
||
| func main() { | ||
| var mode string | ||
| flag.StringVar(&mode, "m", "trigger", "Mode is worker or trigger.") | ||
| flag.Parse() | ||
|
|
||
| var h common.SampleHelper | ||
| h.SetupServiceConfig() | ||
|
|
||
| switch mode { | ||
| case "worker": | ||
| registerWorkflowAndActivity(&h) | ||
| startWorkers(&h) | ||
|
|
||
| // The workers are supposed to be long running process that should not exit. | ||
| // Use select{} to block indefinitely for samples, you can quit by CMD+C. | ||
| select {} | ||
| case "trigger": | ||
| startWorkflow(&h) | ||
| } | ||
| } |
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
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.
nit: keep the original order
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 think I addressed your comment. With the latest commit, please check.