From 6547bfecc722524e42634827e288c96879662eb7 Mon Sep 17 00:00:00 2001 From: Spencer Judge Date: Tue, 16 Sep 2025 14:28:13 -0700 Subject: [PATCH 1/2] Create worker deployment based versioning sample --- .gitignore | 3 +- README.md | 3 +- .../samples/workerversioning/Activities.java | 42 +++++ .../workerversioning/ActivitiesImpl.java | 25 +++ .../AutoUpgradingWorkflow.java | 15 ++ .../AutoUpgradingWorkflowV1Impl.java | 52 ++++++ .../AutoUpgradingWorkflowV1bImpl.java | 65 +++++++ .../workerversioning/PinnedWorkflow.java | 15 ++ .../PinnedWorkflowV1Impl.java | 49 ++++++ .../PinnedWorkflowV2Impl.java | 51 ++++++ .../samples/workerversioning/README.md | 25 +++ .../samples/workerversioning/Starter.java | 163 ++++++++++++++++++ .../samples/workerversioning/WorkerV1.java | 38 ++++ .../samples/workerversioning/WorkerV1_1.java | 38 ++++ .../samples/workerversioning/WorkerV2.java | 38 ++++ 15 files changed, 620 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/Activities.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/ActivitiesImpl.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflow.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1Impl.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1bImpl.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflow.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV1Impl.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV2Impl.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/README.md create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/Starter.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/WorkerV1.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/WorkerV1_1.java create mode 100644 core/src/main/java/io/temporal/samples/workerversioning/WorkerV2.java diff --git a/.gitignore b/.gitignore index a560c2465..038d5ef43 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ target .project .settings/ bin/ -core/.vscode/ \ No newline at end of file +core/.vscode/ +.claude/ diff --git a/README.md b/README.md index a62ed4c98..15e12b8e2 100644 --- a/README.md +++ b/README.md @@ -108,8 +108,9 @@ See the README.md file in each main sample directory for cut/paste Gradle comman - [**Custom Annotation**](/core/src/main/java/io/temporal/samples/customannotation): Demonstrates how to create a custom annotation using an interceptor. -- [**Asnyc Packet Delivery**](/core/src/main/java/io/temporal/samples/packetdelivery): Demonstrates running multiple execution paths async within single execution. +- [**Async Packet Delivery**](/core/src/main/java/io/temporal/samples/packetdelivery): Demonstrates running multiple execution paths async within single execution. +- [**Worker Versioning**](/core/src/main/java/io/temporal/samples/workerversioning): Demonstrates how to use worker versioning to manage workflow code changes. #### API demonstrations diff --git a/core/src/main/java/io/temporal/samples/workerversioning/Activities.java b/core/src/main/java/io/temporal/samples/workerversioning/Activities.java new file mode 100644 index 000000000..f0f579832 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/Activities.java @@ -0,0 +1,42 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.activity.ActivityInterface; +import io.temporal.activity.ActivityMethod; + +@ActivityInterface +public interface Activities { + + @ActivityMethod + String someActivity(String calledBy); + + @ActivityMethod + String someIncompatibleActivity(IncompatibleActivityInput input); + + class IncompatibleActivityInput { + String calledBy; + String moreData; + + public IncompatibleActivityInput() {} + + public IncompatibleActivityInput(String calledBy, String moreData) { + this.calledBy = calledBy; + this.moreData = moreData; + } + + public String getCalledBy() { + return calledBy; + } + + public String getMoreData() { + return moreData; + } + + public void setCalledBy(String calledBy) { + this.calledBy = calledBy; + } + + public void setMoreData(String moreData) { + this.moreData = moreData; + } + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/ActivitiesImpl.java b/core/src/main/java/io/temporal/samples/workerversioning/ActivitiesImpl.java new file mode 100644 index 000000000..b3e4e2c28 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/ActivitiesImpl.java @@ -0,0 +1,25 @@ +package io.temporal.samples.workerversioning; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ActivitiesImpl implements Activities { + + private static final Logger logger = LoggerFactory.getLogger(ActivitiesImpl.class); + + @Override + public String someActivity(String calledBy) { + logger.info("SomeActivity called by {}", calledBy); + return "SomeActivity called by " + calledBy; + } + + @Override + public String someIncompatibleActivity(IncompatibleActivityInput input) { + logger.info( + "SomeIncompatibleActivity called by {} with {}", input.getCalledBy(), input.getMoreData()); + return "SomeIncompatibleActivity called by " + + input.getCalledBy() + + " with " + + input.getMoreData(); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflow.java b/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflow.java new file mode 100644 index 000000000..8dff5f7cd --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflow.java @@ -0,0 +1,15 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.workflow.SignalMethod; +import io.temporal.workflow.WorkflowInterface; +import io.temporal.workflow.WorkflowMethod; + +@WorkflowInterface +public interface AutoUpgradingWorkflow { + + @WorkflowMethod + void run(); + + @SignalMethod + void doNextSignal(String signal); +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1Impl.java b/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1Impl.java new file mode 100644 index 000000000..645f0f70d --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1Impl.java @@ -0,0 +1,52 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.activity.ActivityOptions; +import io.temporal.common.VersioningBehavior; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.WorkflowVersioningBehavior; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; + +/** + * This workflow will automatically move to the latest worker version. We'll be making changes to + * it, which must be replay safe. Note that generally you won't want or need to include a version + * number in your workflow name if you're using the worker versioning feature. This sample does it + * to illustrate changes to the same code over time - but really what we're demonstrating here is + * the evolution of what would have been one workflow definition. + */ +public class AutoUpgradingWorkflowV1Impl implements AutoUpgradingWorkflow { + + private static final Logger logger = Workflow.getLogger(AutoUpgradingWorkflowV1Impl.class); + + private final List signals = new ArrayList<>(); + private final Activities activities = + Workflow.newActivityStub( + Activities.class, + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build()); + + @Override + @WorkflowVersioningBehavior(VersioningBehavior.AUTO_UPGRADE) + public void run() { + logger.info("Changing workflow v1 started. StartTime: {}", Workflow.currentTimeMillis()); + + while (true) { + Workflow.await(() -> !signals.isEmpty()); + String signal = signals.remove(0); + + if ("do-activity".equals(signal)) { + logger.info("Changing workflow v1 running activity"); + activities.someActivity("v1"); + } else { + logger.info("Concluding workflow v1"); + return; + } + } + } + + @Override + public void doNextSignal(String signal) { + signals.add(signal); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1bImpl.java b/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1bImpl.java new file mode 100644 index 000000000..abedf8517 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/AutoUpgradingWorkflowV1bImpl.java @@ -0,0 +1,65 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.activity.ActivityOptions; +import io.temporal.common.VersioningBehavior; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.WorkflowVersioningBehavior; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; + +/** + * This represents us having made *compatible* changes to AutoUpgradingWorkflowV1Impl. + * + *

The compatible changes we've made are: + * + *

+ */ +public class AutoUpgradingWorkflowV1bImpl implements AutoUpgradingWorkflow { + + private static final Logger logger = Workflow.getLogger(AutoUpgradingWorkflowV1bImpl.class); + + private final List signals = new ArrayList<>(); + private final Activities activities = + Workflow.newActivityStub( + Activities.class, + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build()); + + @Override + @WorkflowVersioningBehavior(VersioningBehavior.AUTO_UPGRADE) + public void run() { + logger.info("Changing workflow v1b started. StartTime: {}", Workflow.currentTimeMillis()); + + while (true) { + Workflow.await(() -> !signals.isEmpty()); + String signal = signals.remove(0); + + if ("do-activity".equals(signal)) { + logger.info("Changing workflow v1b running activity"); + int version = Workflow.getVersion("DifferentActivity", Workflow.DEFAULT_VERSION, 1); + if (version == 1) { + activities.someIncompatibleActivity( + new Activities.IncompatibleActivityInput("v1b", "hello!")); + } else { + // Note it is a valid compatible change to alter the input to an activity. + // However, because we're using the getVersion API, this branch will never be + // taken. + activities.someActivity("v1b"); + } + } else { + logger.info("Concluding workflow v1b"); + break; + } + } + } + + @Override + public void doNextSignal(String signal) { + signals.add(signal); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflow.java b/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflow.java new file mode 100644 index 000000000..1d930a40e --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflow.java @@ -0,0 +1,15 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.workflow.SignalMethod; +import io.temporal.workflow.WorkflowInterface; +import io.temporal.workflow.WorkflowMethod; + +@WorkflowInterface +public interface PinnedWorkflow { + + @WorkflowMethod + void run(); + + @SignalMethod + void doNextSignal(String signal); +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV1Impl.java b/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV1Impl.java new file mode 100644 index 000000000..4826f715c --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV1Impl.java @@ -0,0 +1,49 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.activity.ActivityOptions; +import io.temporal.common.VersioningBehavior; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.WorkflowVersioningBehavior; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; + +/** + * This workflow represents one that likely has a short lifetime, and we want to always stay pinned + * to the same version it began on. Note that generally you won't want or need to include a version + * number in your workflow name if you're using the worker versioning feature. This sample does it + * to illustrate changes to the same code over time - but really what we're demonstrating here is + * the evolution of what would have been one workflow definition. + */ +public class PinnedWorkflowV1Impl implements PinnedWorkflow { + + private static final Logger logger = Workflow.getLogger(PinnedWorkflowV1Impl.class); + + private final List signals = new ArrayList<>(); + private final Activities activities = + Workflow.newActivityStub( + Activities.class, + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build()); + + @Override + @WorkflowVersioningBehavior(VersioningBehavior.PINNED) + public void run() { + logger.info("Pinned Workflow v1 started. StartTime: {}", Workflow.currentTimeMillis()); + + while (true) { + Workflow.await(() -> !signals.isEmpty()); + String signal = signals.remove(0); + if ("conclude".equals(signal)) { + break; + } + } + + activities.someActivity("Pinned-v1"); + } + + @Override + public void doNextSignal(String signal) { + signals.add(signal); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV2Impl.java b/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV2Impl.java new file mode 100644 index 000000000..a880b2dc1 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/PinnedWorkflowV2Impl.java @@ -0,0 +1,51 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.activity.ActivityOptions; +import io.temporal.common.VersioningBehavior; +import io.temporal.workflow.Workflow; +import io.temporal.workflow.WorkflowVersioningBehavior; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; + +/** + * This workflow has changes that would make it incompatible with v1, and aren't protected by a + * patch. + */ +public class PinnedWorkflowV2Impl implements PinnedWorkflow { + + private static final Logger logger = Workflow.getLogger(PinnedWorkflowV2Impl.class); + + private final List signals = new ArrayList<>(); + private final Activities activities = + Workflow.newActivityStub( + Activities.class, + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build()); + + @Override + @WorkflowVersioningBehavior(VersioningBehavior.PINNED) + public void run() { + logger.info("Pinned Workflow v2 started. StartTime: {}", Workflow.currentTimeMillis()); + + // Here we call an activity where we didn't before, which is an incompatible change. + activities.someActivity("Pinned-v2"); + + while (true) { + Workflow.await(() -> !signals.isEmpty()); + String signal = signals.remove(0); + if ("conclude".equals(signal)) { + break; + } + } + + // We've also changed the activity type here, another incompatible change + activities.someIncompatibleActivity( + new Activities.IncompatibleActivityInput("Pinned-v2", "hi")); + } + + @Override + public void doNextSignal(String signal) { + signals.add(signal); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/README.md b/core/src/main/java/io/temporal/samples/workerversioning/README.md new file mode 100644 index 000000000..bfe82c568 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/README.md @@ -0,0 +1,25 @@ +# Worker Versioning + +This sample demonstrates how to use Temporal's Worker Versioning feature to safely deploy updates to workflow and activity code. It shows the difference between auto-upgrading and pinned workflows, and how to manage worker deployments with different build IDs. + +The sample creates multiple worker versions (1.0, 1.1, and 2.0) within one deployment and demonstrates: +- **Auto-upgrading workflows**: Automatically and controllably migrate to newer worker versions +- **Pinned workflows**: Stay on the original worker version throughout their lifecycle +- **Compatible vs incompatible changes**: How to make safe updates using `Workflow.getVersion` + +## Steps to run this sample: + +1) Run a [Temporal service](https://github.com/temporalio/samples-java/tree/main/#how-to-use). + +2) Start the main application (this will guide you through the sample): + ```bash + ./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.Starter + ``` +3) Follow the prompts to start workers in separate terminals: + - When prompted, run: `./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.WorkerV1` + - When prompted, run: `./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.WorkerV1_1` + - When prompted, run: `./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.WorkerV2` + +Follow the prompts in the example to observe auto-upgrading workflows migrating to newer workers +while pinned workflows remain on their original versions. + diff --git a/core/src/main/java/io/temporal/samples/workerversioning/Starter.java b/core/src/main/java/io/temporal/samples/workerversioning/Starter.java new file mode 100644 index 000000000..a48902557 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/Starter.java @@ -0,0 +1,163 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.api.workflowservice.v1.DescribeWorkerDeploymentRequest; +import io.temporal.api.workflowservice.v1.DescribeWorkerDeploymentResponse; +import io.temporal.api.workflowservice.v1.SetWorkerDeploymentCurrentVersionRequest; +import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowOptions; +import io.temporal.client.WorkflowStub; +import io.temporal.common.WorkerDeploymentVersion; +import io.temporal.serviceclient.WorkflowServiceStubs; +import java.util.UUID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Starter { + public static final String TASK_QUEUE = "worker-versioning"; + public static final String DEPLOYMENT_NAME = "my-deployment"; + + private static final Logger logger = LoggerFactory.getLogger(Starter.class); + + public static void main(String[] args) throws Exception { + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + WorkflowClient client = WorkflowClient.newInstance(service); + + // Wait for v1 worker and set as current version + logger.info( + "Waiting for v1 worker to appear. Run `./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.WorkerV1` in another terminal"); + waitForWorkerAndMakeCurrent(client, service, "1.0"); + + // Start auto-upgrading and pinned workflows. Importantly, note that when we start the + // workflows, + // we are using a workflow type name which does *not* include the version number. We defined + // them + // with versioned names so we could show changes to the code, but here when the client invokes + // them, we're demonstrating that the client remains version-agnostic. + String autoUpgradeWorkflowId = "worker-versioning-versioning-autoupgrade_" + UUID.randomUUID(); + WorkflowStub autoUpgradeExecution = + client.newUntypedWorkflowStub( + "AutoUpgradingWorkflow", + WorkflowOptions.newBuilder() + .setWorkflowId(autoUpgradeWorkflowId) + .setTaskQueue(TASK_QUEUE) + .build()); + + String pinnedWorkflowId = "worker-versioning-versioning-pinned_" + UUID.randomUUID(); + WorkflowStub pinnedExecution = + client.newUntypedWorkflowStub( + "PinnedWorkflow", + WorkflowOptions.newBuilder() + .setWorkflowId(pinnedWorkflowId) + .setTaskQueue(TASK_QUEUE) + .build()); + + // Start workflows asynchronously + autoUpgradeExecution.start(); + pinnedExecution.start(); + + logger.info( + "Started auto-upgrading workflow: {}", autoUpgradeExecution.getExecution().getWorkflowId()); + logger.info("Started pinned workflow: {}", pinnedExecution.getExecution().getWorkflowId()); + + // Signal both workflows a few times to drive them + advanceWorkflows(autoUpgradeExecution, pinnedExecution); + + // Now wait for the v1.1 worker to appear and become current + logger.info( + "Waiting for v1.1 worker to appear. Run `./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.WorkerV1_1` in another terminal"); + waitForWorkerAndMakeCurrent(client, service, "1.1"); + + // Once it has, we will continue to advance the workflows. + // The auto-upgrade workflow will now make progress on the new worker, while the pinned one will + // keep progressing on the old worker. + advanceWorkflows(autoUpgradeExecution, pinnedExecution); + + // Finally we'll start the v2 worker, and again it'll become the new current version + logger.info( + "Waiting for v2 worker to appear. Run `./gradlew -q execute -PmainClass=io.temporal.samples.workerversioning.WorkerV2` in another terminal"); + waitForWorkerAndMakeCurrent(client, service, "2.0"); + + // Once it has we'll start one more new workflow, another pinned one, to demonstrate that new + // pinned workflows start on the current version. + String pinnedWorkflow2Id = "worker-versioning-versioning-pinned-2_" + UUID.randomUUID(); + WorkflowStub pinnedExecution2 = + client.newUntypedWorkflowStub( + "PinnedWorkflow", + WorkflowOptions.newBuilder() + .setWorkflowId(pinnedWorkflow2Id) + .setTaskQueue(TASK_QUEUE) + .build()); + pinnedExecution2.start(); + logger.info("Started pinned workflow v2: {}", pinnedExecution2.getExecution().getWorkflowId()); + + // Now we'll conclude all workflows. You should be able to see in your server UI that the pinned + // workflow always stayed on 1.0, while the auto-upgrading workflow migrated. + autoUpgradeExecution.signal("doNextSignal", "conclude"); + pinnedExecution.signal("doNextSignal", "conclude"); + pinnedExecution2.signal("doNextSignal", "conclude"); + + // Wait for all workflows to complete + autoUpgradeExecution.getResult(Void.class); + pinnedExecution.getResult(Void.class); + pinnedExecution2.getResult(Void.class); + + logger.info("All workflows completed"); + } + + private static void advanceWorkflows( + WorkflowStub autoUpgradeExecution, WorkflowStub pinnedExecution) { + // Signal both workflows a few times to drive them + for (int i = 0; i < 3; i++) { + autoUpgradeExecution.signal("doNextSignal", "do-activity"); + pinnedExecution.signal("doNextSignal", "some-signal"); + } + } + + private static void waitForWorkerAndMakeCurrent( + WorkflowClient client, WorkflowServiceStubs service, String buildId) + throws InterruptedException { + WorkerDeploymentVersion targetVersion = new WorkerDeploymentVersion(DEPLOYMENT_NAME, buildId); + + // Wait for the worker to appear + while (true) { + try { + DescribeWorkerDeploymentRequest describeRequest = + DescribeWorkerDeploymentRequest.newBuilder() + .setNamespace(client.getOptions().getNamespace()) + .setDeploymentName(DEPLOYMENT_NAME) + .build(); + + DescribeWorkerDeploymentResponse response = + service.blockingStub().describeWorkerDeployment(describeRequest); + + // Check if our version is present in the version summaries + boolean found = + response.getWorkerDeploymentInfo().getVersionSummariesList().stream() + .anyMatch( + versionSummary -> + targetVersion + .getDeploymentName() + .equals(versionSummary.getDeploymentVersion().getDeploymentName()) + && targetVersion + .getBuildId() + .equals(versionSummary.getDeploymentVersion().getBuildId())); + + if (found) { + break; + } + } catch (Exception ignored) { + } + Thread.sleep(1000); + } + + // Once the version is available, set it as current + SetWorkerDeploymentCurrentVersionRequest setRequest = + SetWorkerDeploymentCurrentVersionRequest.newBuilder() + .setNamespace(client.getOptions().getNamespace()) + .setDeploymentName(DEPLOYMENT_NAME) + .setBuildId(targetVersion.getBuildId()) + .build(); + + service.blockingStub().setWorkerDeploymentCurrentVersion(setRequest); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/WorkerV1.java b/core/src/main/java/io/temporal/samples/workerversioning/WorkerV1.java new file mode 100644 index 000000000..6d90b82fd --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/WorkerV1.java @@ -0,0 +1,38 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.client.WorkflowClient; +import io.temporal.common.WorkerDeploymentVersion; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerDeploymentOptions; +import io.temporal.worker.WorkerFactory; +import io.temporal.worker.WorkerOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WorkerV1 { + + private static final Logger logger = LoggerFactory.getLogger(WorkerV1.class); + + public static void main(String[] args) { + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkerDeploymentVersion version = new WorkerDeploymentVersion(Starter.DEPLOYMENT_NAME, "1.0"); + WorkerDeploymentOptions deploymentOptions = + WorkerDeploymentOptions.newBuilder().setUseVersioning(true).setVersion(version).build(); + + WorkerOptions workerOptions = + WorkerOptions.newBuilder().setDeploymentOptions(deploymentOptions).build(); + + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(Starter.TASK_QUEUE, workerOptions); + + worker.registerWorkflowImplementationTypes( + AutoUpgradingWorkflowV1Impl.class, PinnedWorkflowV1Impl.class); + worker.registerActivitiesImplementations(new ActivitiesImpl()); + + logger.info("Starting worker v1 (build 1.0)"); + factory.start(); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/WorkerV1_1.java b/core/src/main/java/io/temporal/samples/workerversioning/WorkerV1_1.java new file mode 100644 index 000000000..5b1235460 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/WorkerV1_1.java @@ -0,0 +1,38 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.client.WorkflowClient; +import io.temporal.common.WorkerDeploymentVersion; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerDeploymentOptions; +import io.temporal.worker.WorkerFactory; +import io.temporal.worker.WorkerOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WorkerV1_1 { + + private static final Logger logger = LoggerFactory.getLogger(WorkerV1_1.class); + + public static void main(String[] args) { + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkerDeploymentVersion version = new WorkerDeploymentVersion(Starter.DEPLOYMENT_NAME, "1.1"); + WorkerDeploymentOptions deploymentOptions = + WorkerDeploymentOptions.newBuilder().setUseVersioning(true).setVersion(version).build(); + + WorkerOptions workerOptions = + WorkerOptions.newBuilder().setDeploymentOptions(deploymentOptions).build(); + + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(Starter.TASK_QUEUE, workerOptions); + + worker.registerWorkflowImplementationTypes( + AutoUpgradingWorkflowV1bImpl.class, PinnedWorkflowV1Impl.class); + worker.registerActivitiesImplementations(new ActivitiesImpl()); + + logger.info("Starting worker v1.1 (build 1.1)"); + factory.start(); + } +} diff --git a/core/src/main/java/io/temporal/samples/workerversioning/WorkerV2.java b/core/src/main/java/io/temporal/samples/workerversioning/WorkerV2.java new file mode 100644 index 000000000..57edd0ed3 --- /dev/null +++ b/core/src/main/java/io/temporal/samples/workerversioning/WorkerV2.java @@ -0,0 +1,38 @@ +package io.temporal.samples.workerversioning; + +import io.temporal.client.WorkflowClient; +import io.temporal.common.WorkerDeploymentVersion; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerDeploymentOptions; +import io.temporal.worker.WorkerFactory; +import io.temporal.worker.WorkerOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WorkerV2 { + + private static final Logger logger = LoggerFactory.getLogger(WorkerV2.class); + + public static void main(String[] args) { + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); + WorkflowClient client = WorkflowClient.newInstance(service); + + WorkerDeploymentVersion version = new WorkerDeploymentVersion(Starter.DEPLOYMENT_NAME, "2.0"); + WorkerDeploymentOptions deploymentOptions = + WorkerDeploymentOptions.newBuilder().setUseVersioning(true).setVersion(version).build(); + + WorkerOptions workerOptions = + WorkerOptions.newBuilder().setDeploymentOptions(deploymentOptions).build(); + + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(Starter.TASK_QUEUE, workerOptions); + + worker.registerWorkflowImplementationTypes( + AutoUpgradingWorkflowV1bImpl.class, PinnedWorkflowV2Impl.class); + worker.registerActivitiesImplementations(new ActivitiesImpl()); + + logger.info("Starting worker v2 (build 2.0)"); + factory.start(); + } +} From ae2167e3a06f36957823b218f73c1f2160183659 Mon Sep 17 00:00:00 2001 From: Spencer Judge Date: Wed, 17 Sep 2025 14:14:09 -0700 Subject: [PATCH 2/2] Review fixes --- .../samples/workerversioning/Activities.java | 22 ++++++++----------- .../samples/workerversioning/README.md | 3 ++- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/io/temporal/samples/workerversioning/Activities.java b/core/src/main/java/io/temporal/samples/workerversioning/Activities.java index f0f579832..b41189d2a 100644 --- a/core/src/main/java/io/temporal/samples/workerversioning/Activities.java +++ b/core/src/main/java/io/temporal/samples/workerversioning/Activities.java @@ -1,5 +1,7 @@ package io.temporal.samples.workerversioning; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import io.temporal.activity.ActivityInterface; import io.temporal.activity.ActivityMethod; @@ -13,30 +15,24 @@ public interface Activities { String someIncompatibleActivity(IncompatibleActivityInput input); class IncompatibleActivityInput { - String calledBy; - String moreData; + private final String calledBy; + private final String moreData; - public IncompatibleActivityInput() {} - - public IncompatibleActivityInput(String calledBy, String moreData) { + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public IncompatibleActivityInput( + @JsonProperty("calledBy") String calledBy, @JsonProperty("moreData") String moreData) { this.calledBy = calledBy; this.moreData = moreData; } + @JsonProperty("calledBy") public String getCalledBy() { return calledBy; } + @JsonProperty("moreData") public String getMoreData() { return moreData; } - - public void setCalledBy(String calledBy) { - this.calledBy = calledBy; - } - - public void setMoreData(String moreData) { - this.moreData = moreData; - } } } diff --git a/core/src/main/java/io/temporal/samples/workerversioning/README.md b/core/src/main/java/io/temporal/samples/workerversioning/README.md index bfe82c568..c74b10488 100644 --- a/core/src/main/java/io/temporal/samples/workerversioning/README.md +++ b/core/src/main/java/io/temporal/samples/workerversioning/README.md @@ -9,7 +9,8 @@ The sample creates multiple worker versions (1.0, 1.1, and 2.0) within one deplo ## Steps to run this sample: -1) Run a [Temporal service](https://github.com/temporalio/samples-java/tree/main/#how-to-use). +1) Run a [Temporal service](https://github.com/temporalio/samples-java/tree/main/#how-to-use). And + ensure that you're using at least Server version 1.28.0 (CLI version 1.4.0). 2) Start the main application (this will guide you through the sample): ```bash