|
| 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 | +## Running the Example |
| 51 | + |
| 52 | +### Prerequisites |
| 53 | + |
| 54 | +Make sure you have Cadence server running and the sample compiled: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Build the sample |
| 58 | +go build -o bin/versioning cmd/samples/recipes/versioning/*.go |
| 59 | +``` |
| 60 | + |
| 61 | +### Step-by-Step Deployment Simulation |
| 62 | + |
| 63 | +#### 1. Start Worker V1 |
| 64 | +```bash |
| 65 | +./bin/versioning -m worker -v 1 |
| 66 | +``` |
| 67 | + |
| 68 | +#### 2. Trigger a Workflow (Executed by Worker V1) |
| 69 | +```bash |
| 70 | +./bin/versioning -m trigger |
| 71 | +``` |
| 72 | + |
| 73 | +**Deployment started** - You now have a workflow running on V1. |
| 74 | + |
| 75 | +#### 3. Deploy Worker V2 (Safe Deployment) |
| 76 | +```bash |
| 77 | +./bin/versioning -m worker -v 2 |
| 78 | +``` |
| 79 | + |
| 80 | +#### 4. Test V2 Compatibility |
| 81 | +* Kill the process of worker V1 (Ctrl+C), then wait 5 seconds to see workflow rescheduling to worker V2 without errors. |
| 82 | +* Verify logs of V2, V2 should handle workflows started by V1. |
| 83 | + |
| 84 | +#### 5. Upgrade to Version V3 |
| 85 | +```bash |
| 86 | +./bin/versioning -m worker -v 3 |
| 87 | +``` |
| 88 | + |
| 89 | +#### 6. Test V3 Compatibility |
| 90 | +* Kill the process of worker V2, then wait 5 seconds to see workflow rescheduling to worker V3 without errors. |
| 91 | +* Verify logs of V3 worker, V3 worker should handle workflows started by V1. |
| 92 | + |
| 93 | +#### 7. Test Breaking Change with V4 |
| 94 | +```bash |
| 95 | +./bin/versioning -m worker -v 4 |
| 96 | +``` |
| 97 | + |
| 98 | +Kill the process of worker V3. You'll notice that workflows initiated by V1 cannot be executed by version V4 - this simulates a breaking change. |
| 99 | + |
| 100 | +#### 8. Gracefully Stop the Workflow |
| 101 | +```bash |
| 102 | +./bin/versioning -m stop |
| 103 | +``` |
| 104 | + |
| 105 | +#### 9. Start a New Workflow (Will Use V4 Logic) |
| 106 | +```bash |
| 107 | +./bin/versioning -m trigger |
| 108 | +``` |
| 109 | + |
| 110 | +The workflow will use version 1 of the change ID (V4's default). |
| 111 | + |
| 112 | +#### 10. Rollback to Worker V2 |
| 113 | +```bash |
| 114 | +./bin/versioning -m worker -v 2 |
| 115 | +``` |
| 116 | + |
| 117 | +* Kill the process of worker V4, then wait for workflow rescheduling. |
| 118 | +* Verify logs of V2 worker, V2 worker should handle workflows started by V4. |
| 119 | + |
| 120 | +#### 11. Aggressive Upgrade: V2 to V4 (Breaking Change) |
| 121 | +Since V3 worked fine, we decide to combine getting rid of support for V1 and make an upgrade straightforward to V4: |
| 122 | + |
| 123 | +```bash |
| 124 | +./bin/versioning -m worker -v 4 |
| 125 | +``` |
| 126 | + |
| 127 | +* Kill the process of worker V2, then wait for workflow rescheduling. |
| 128 | +* Verify logs of V4 worker, V4 worker should handle workflows started by V4. |
| 129 | + |
| 130 | +## Important Notes |
| 131 | + |
| 132 | +- **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. |
| 133 | +- **Signal Method**: The workflow uses a simple signal method to stop gracefully, keeping the implementation straightforward. |
| 134 | +- **Version Compatibility**: Each version is designed to handle workflows started by compatible previous versions. |
| 135 | +- **Breaking Changes**: V4 demonstrates what happens when you introduce a breaking change - workflows started by V1 cannot be executed. |
| 136 | + |
| 137 | +## Version Compatibility Matrix |
| 138 | + |
| 139 | +| Started By | V1 Worker | V2 Worker | V3 Worker | V4 Worker | |
| 140 | +|------------|-----------|-----------|-----------|-----------| |
| 141 | +| V1 | ✅ | ✅ | ✅ | ❌ | |
| 142 | +| V2 | ❌ | ✅ | ✅ | ✅ | |
| 143 | +| V3 | ❌ | ✅ | ✅ | ✅ | |
| 144 | +| V4 | ❌ | ✅ | ✅ | ✅ | |
| 145 | + |
| 146 | +## Command Reference |
| 147 | + |
| 148 | +```bash |
| 149 | +# Start a worker with specific version |
| 150 | +./bin/versioning -m worker -v <version> |
| 151 | + |
| 152 | +# Start a new workflow |
| 153 | +./bin/versioning -m trigger |
| 154 | + |
| 155 | +# Stop the running workflow |
| 156 | +./bin/versioning -m stop |
| 157 | +``` |
| 158 | + |
| 159 | +Where `<version>` can be: |
| 160 | +- `1` or `v1` - Version 1 (FooActivity only, DefaultVersion) |
| 161 | +- `2` or `v2` - Version 2 (FooActivity + BarActivity, DefaultVersion) |
| 162 | +- `3` or `v3` - Version 3 (FooActivity + BarActivity, Version #1) |
| 163 | +- `4` or `v4` - Version 4 (BarActivity only, Version #1) |
0 commit comments