|
| 1 | +# Versioning Workflow Example |
| 2 | + |
| 3 | +This example demonstrates how to safely deploy versioned workflows using Cadence's versioning APIs. It shows how to handle workflow evolution while maintaining backward compatibility and enabling safe rollbacks. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The versioning sample implements a workflow that evolves through multiple versions (V1 → V2 → V3 → V4) with rollbacks, demonstrating: |
| 8 | + |
| 9 | +- **Safe Deployment**: How to deploy new workflow versions without breaking existing executions |
| 10 | +- **Backward Compatibility**: How to handle workflows started with older versions |
| 11 | +- **Rollback Capability**: How to safely rollback to previous versions |
| 12 | +- **Version Isolation**: How different versions can execute different logic paths |
| 13 | + |
| 14 | +## Workflow Versions |
| 15 | + |
| 16 | +### Version 1 (V1) |
| 17 | +- Executes `FooActivity` only |
| 18 | +- Uses `workflow.DefaultVersion` for the change ID |
| 19 | + |
| 20 | +### Version 2 (V2) |
| 21 | +- Supports both `FooActivity` and `BarActivity` |
| 22 | +- Uses `workflow.GetVersion()` with `workflow.ExecuteWithMinVersion()` to handle both old and new workflows |
| 23 | +- Workflows started by V1 continue using `FooActivity` |
| 24 | + |
| 25 | +### Version 3 (V3) |
| 26 | +- Similar to V2 but uses standard `workflow.GetVersion()` (without `ExecuteWithMinVersion`) |
| 27 | +- All new workflows use version 1 of the change ID |
| 28 | + |
| 29 | +### Version 4 (V4) |
| 30 | +- Only supports `BarActivity` |
| 31 | +- Forces all workflows to use version 1 of the change ID |
| 32 | +- **Breaking change**: Cannot execute workflows started by V1 |
| 33 | + |
| 34 | +## Key Cadence APIs Used |
| 35 | + |
| 36 | +- `workflow.GetVersion()`: Determines which version of code to execute |
| 37 | +- `workflow.ExecuteWithVersion()`: Executes code with a specific version |
| 38 | +- `workflow.ExecuteWithMinVersion()`: Executes code with minimum version requirement |
| 39 | +- `workflow.DefaultVersion`: Represents the original version before any changes |
| 40 | + |
| 41 | +## Safe Deployment Flow |
| 42 | + |
| 43 | +This example demonstrates a safe deployment strategy that allows you to: |
| 44 | + |
| 45 | +1. **Deploy new versions** while keeping old workers running |
| 46 | +2. **Test compatibility** before fully switching over |
| 47 | +3. **Rollback safely** if issues are discovered |
| 48 | +4. **Gradually migrate** workflows to new versions |
| 49 | + |
| 50 | + |
| 51 | +## Important Notes |
| 52 | + |
| 53 | +- **Single Workflow Limitation**: This sample allows only one workflow at a time to simplify the signal handling mechanism. In production, you would typically handle multiple workflows. |
| 54 | +- **Signal Method**: The workflow uses a simple signal method to stop gracefully, keeping the implementation straightforward. |
| 55 | +- **Breaking Changes**: V4 demonstrates what happens when you introduce a breaking change - workflows started by V1 cannot be executed. |
| 56 | + |
| 57 | +## Version Compatibility Matrix |
| 58 | + |
| 59 | +| Started By | V1 Worker | V2 Worker | V3 Worker | V4 Worker | |
| 60 | +|------------|-----------|-----------|-----------|-----------| |
| 61 | +| V1 | ✅ | ✅ | ✅ | ❌ | |
| 62 | +| V2 | ❌ | ✅ | ✅ | ✅ | |
| 63 | +| V3 | ❌ | ✅ | ✅ | ✅ | |
| 64 | +| V4 | ❌ | ✅ | ✅ | ✅ | |
| 65 | + |
| 66 | +## Running the Example |
| 67 | + |
| 68 | +### Prerequisites |
| 69 | + |
| 70 | +Make sure you have Cadence server running and the sample compiled: |
| 71 | + |
| 72 | +```bash |
| 73 | +# Build the sample |
| 74 | +go build -o bin/versioning cmd/samples/recipes/versioning/*.go |
| 75 | +``` |
| 76 | + |
| 77 | +### Step-by-Step Deployment Simulation |
| 78 | + |
| 79 | +#### 1. Start Worker V1 |
| 80 | +```bash |
| 81 | +./bin/versioning -m worker -v 1 |
| 82 | +``` |
| 83 | + |
| 84 | +#### 2. Trigger a Workflow |
| 85 | +```bash |
| 86 | +./bin/versioning -m trigger |
| 87 | +``` |
| 88 | + |
| 89 | +Wait for logs in the V1 worker to ensure that a workflow has been executed by worker V1. |
| 90 | + |
| 91 | +#### 3. Deploy Worker V2 |
| 92 | +Let's simulate a deployment from V1 to V2 and run a V2 worker alongside the V1 worker: |
| 93 | + |
| 94 | +```bash |
| 95 | +./bin/versioning -m worker -v 2 |
| 96 | +``` |
| 97 | + |
| 98 | +The workflow should still be executed by worker V1. |
| 99 | + |
| 100 | +#### 4. Test V2 Compatibility |
| 101 | +Let's simulate that worker V1 is shut down and the workflow will be rescheduled to the V2 worker: |
| 102 | +* Kill the process of worker V1 (Ctrl+C), then wait 5 seconds to see workflow rescheduling to worker V2 without errors. |
| 103 | + |
| 104 | +Verify logs of the V2 worker - it should handle the workflow started by V1. |
| 105 | + |
| 106 | +#### 5. Upgrade to Version V3 |
| 107 | +Let's continue the deployment and upgrade to V3, running a V3 worker alongside the V2 worker: |
| 108 | + |
| 109 | +```bash |
| 110 | +./bin/versioning -m worker -v 3 |
| 111 | +``` |
| 112 | + |
| 113 | +The workflow should still be executed by worker V2. |
| 114 | + |
| 115 | +#### 6. Test V3 Compatibility |
| 116 | +Let's simulate that worker V2 is shut down and the workflow will be rescheduled to the V3 worker: |
| 117 | + |
| 118 | +* Kill the process of worker V2, then wait 5 seconds to see workflow rescheduling to worker V3 without errors. |
| 119 | + |
| 120 | +Verify logs of the V3 worker - it should handle the workflow started by V2. |
| 121 | + |
| 122 | +#### 7. Gracefully Stop the Workflow |
| 123 | +Before upgrading to V4, we should ensure that the workflow has been stopped, otherwise it will fail. For this, we need to send a signal to stop it gracefully: |
| 124 | + |
| 125 | +```bash |
| 126 | +./bin/versioning -m stop |
| 127 | +``` |
| 128 | + |
| 129 | +You should see that the workflow has been stopped. |
| 130 | + |
| 131 | +#### 8. Start a New Workflow |
| 132 | +Let's start a new workflow: |
| 133 | + |
| 134 | +```bash |
| 135 | +./bin/versioning -m trigger |
| 136 | +``` |
| 137 | + |
| 138 | +The workflow will use version 1 of the change ID (V3's and V4's default). |
| 139 | + |
| 140 | +#### 9. Rollback to Worker V2 |
| 141 | +Let's imagine that V3 has an issue and we need to rollback to V2. Let's start a worker V2: |
| 142 | + |
| 143 | +```bash |
| 144 | +./bin/versioning -m worker -v 2 |
| 145 | +``` |
| 146 | + |
| 147 | +* Kill the process of worker V3, then wait for workflow rescheduling. |
| 148 | +* Verify logs of V2 worker - V2 worker should handle workflows started by V3. |
| 149 | + |
| 150 | +#### 10. Aggressive Upgrade: V2 to V4 (Breaking Change) |
| 151 | +We decide to combine getting rid of support for V1 and make an upgrade straightforward to V4: |
| 152 | + |
| 153 | +```bash |
| 154 | +./bin/versioning -m worker -v 4 |
| 155 | +``` |
| 156 | + |
| 157 | +* Kill the process of worker V2, then wait for workflow rescheduling. |
| 158 | +* Verify logs of V4 worker - V4 worker should handle workflows started by V4. |
| 159 | + |
| 160 | + |
| 161 | +## Command Reference |
| 162 | + |
| 163 | +```bash |
| 164 | +# Start a worker with specific version |
| 165 | +./bin/versioning -m worker -v <version> |
| 166 | + |
| 167 | +# Start a new workflow |
| 168 | +./bin/versioning -m trigger |
| 169 | + |
| 170 | +# Stop the running workflow |
| 171 | +./bin/versioning -m stop |
| 172 | +``` |
| 173 | + |
| 174 | +Where `<version>` can be: |
| 175 | +- `1` or `v1` - Version 1 (FooActivity only, DefaultVersion) |
| 176 | +- `2` or `v2` - Version 2 (FooActivity + BarActivity, DefaultVersion) |
| 177 | +- `3` or `v3` - Version 3 (FooActivity + BarActivity, Version #1) |
| 178 | +- `4` or `v4` - Version 4 (BarActivity only, Version #1) |
0 commit comments