Skip to content

Commit 114e354

Browse files
Compensation example for Workflows (#1333)
* add basic compensation example for wf Signed-off-by: Cassandra Coyle <[email protected]> * update commands to run + wf id Signed-off-by: Cassandra Coyle <[email protected]> * update readme + add mechanical markdown Signed-off-by: Cassandra Coyle <[email protected]> * fix import Signed-off-by: Cassandra Coyle <[email protected]> * fix mechanical markdown + add how to test it locally Signed-off-by: Cassandra Coyle <[email protected]> * move compensation example readme to workflows readme Signed-off-by: Cassandra Coyle <[email protected]> * Update BookCarActivity.java Signed-off-by: artur-ciocanu <[email protected]> * Update BookFlightActivity.java Signed-off-by: artur-ciocanu <[email protected]> * Update BookHotelActivity.java Signed-off-by: artur-ciocanu <[email protected]> * Update BookTripClient.java Signed-off-by: artur-ciocanu <[email protected]> * Update BookTripWorker.java Signed-off-by: artur-ciocanu <[email protected]> * Update BookTripWorkflow.java Signed-off-by: artur-ciocanu <[email protected]> * Update CancelCarActivity.java Signed-off-by: artur-ciocanu <[email protected]> * Update CancelFlightActivity.java Signed-off-by: artur-ciocanu <[email protected]> * Update CancelHotelActivity.java Signed-off-by: artur-ciocanu <[email protected]> * add retry IT tests and catch TaskFailedException Signed-off-by: Cassandra Coyle <[email protected]> * add test for no compensation if successful and assert attempts Signed-off-by: Cassandra Coyle <[email protected]> * update mechanical markdown Signed-off-by: Cassandra Coyle <[email protected]> * add back pubsub... but this should be removed long term Signed-off-by: Cassandra Coyle <[email protected]> * try adding waitforsidecar Signed-off-by: Cassandra Coyle <[email protected]> * rm tests from examples pr Signed-off-by: Cassandra Coyle <[email protected]> * reset unintended changes Signed-off-by: Cassandra Coyle <[email protected]> --------- Signed-off-by: Cassandra Coyle <[email protected]> Signed-off-by: artur-ciocanu <[email protected]> Co-authored-by: artur-ciocanu <[email protected]>
1 parent 1852cc5 commit 114e354

File tree

11 files changed

+584
-4
lines changed

11 files changed

+584
-4
lines changed

CONTRIBUTING.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,48 @@ This section describes the guidelines for contributing code / docs to Dapr.
5454
### Things to consider when adding new API to SDK
5555

5656
1. All the new API's go under [dapr-sdk maven package](https://github.com/dapr/java-sdk/tree/master/sdk)
57-
2. Make sure there is an example talking about how to use the API along with a README. [Example](https://github.com/dapr/java-sdk/pull/1235/files#diff-69ed756c4c01fd5fa884aac030dccb8f3f4d4fefa0dc330862d55a6f87b34a14)
57+
2. Make sure there is an example talking about how to use the API along with a README with mechanical markdown. [Example](https://github.com/dapr/java-sdk/pull/1235/files#diff-69ed756c4c01fd5fa884aac030dccb8f3f4d4fefa0dc330862d55a6f87b34a14)
58+
59+
#### Mechanical Markdown
60+
61+
Mechanical markdown is used to validate example outputs in our CI pipeline. It ensures that the expected output in README files matches the actual output when running the examples. This helps maintain example output, catches any unintended changes in example behavior, and regressions.
62+
63+
To test mechanical markdown locally:
64+
65+
1. Install the package:
66+
```bash
67+
pip3 install mechanical-markdown
68+
```
69+
70+
2. Run the test from the respective examples README directory, for example:
71+
```bash
72+
cd examples
73+
mm.py ./src/main/java/io/dapr/examples/workflows/README.md
74+
```
75+
76+
The test will:
77+
- Parse the STEP markers in the README
78+
- Execute the commands specified in the markers
79+
- Compare the actual output with the expected output
80+
- Report any mismatches
81+
82+
When writing STEP markers:
83+
- Use `output_match_mode: substring` for flexible matching
84+
- Quote strings containing special YAML characters (like `:`, `*`, `'`)
85+
- Set appropriate timeouts for long-running examples
86+
87+
Example STEP marker:
88+
```yaml
89+
<!-- STEP
90+
name: Run example
91+
output_match_mode: substring
92+
expected_stdout_lines:
93+
- "Starting workflow: io.dapr.examples.workflows.compensation.BookTripWorkflow"
94+
...
95+
background: true
96+
timeout_seconds: 60
97+
-->
98+
```
5899

59100
### Pull Requests
60101

examples/src/main/java/io/dapr/examples/workflows/README.md

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Those examples contain the following workflow patterns:
5151
2. [Fan-out/Fan-in Pattern](#fan-outfan-in-pattern)
5252
3. [Continue As New Pattern](#continue-as-new-pattern)
5353
4. [External Event Pattern](#external-event-pattern)
54-
5. [child-workflow Pattern](#child-workflow-pattern)
54+
5. [Child-workflow Pattern](#child-workflow-pattern)
55+
6. [Compensation Pattern](#compensation-pattern)
5556

5657
### Chaining Pattern
5758
In the chaining pattern, a sequence of activities executes in a specific order.
@@ -353,7 +354,7 @@ dapr run --app-id demoworkflowworker --resources-path ./components/workflows --
353354
```
354355
```sh
355356
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.continueasnew.DemoContinueAsNewClient
356-
````
357+
```
357358

358359
You will see the logs from worker showing the `CleanUpActivity` is invoked every 10 seconds after previous one is finished:
359360
```text
@@ -444,7 +445,7 @@ Started a new external-event model workflow with instance ID: 23410d96-1afe-4698
444445
workflow instance with ID: 23410d96-1afe-4698-9fcd-c01c1e0db255 completed.
445446
```
446447

447-
### child-workflow Pattern
448+
### Child-workflow Pattern
448449
The child-workflow pattern allows you to call a workflow from another workflow.
449450

450451
The `DemoWorkflow` class defines the workflow. It calls a child-workflow `DemoChildWorkflow` to do the work. See the code snippet below:
@@ -540,3 +541,115 @@ The log from client:
540541
Started a new child-workflow model workflow with instance ID: c2fb9c83-435b-4b55-bdf1-833b39366cfb
541542
workflow instance with ID: c2fb9c83-435b-4b55-bdf1-833b39366cfb completed with result: !wolfkroW rpaD olleH
542543
```
544+
545+
### Compensation Pattern
546+
The compensation pattern is used to "undo" or "roll back" previously completed steps if a later step fails. This pattern is particularly useful in scenarios where you need to ensure that all resources are properly cleaned up even if the process fails.
547+
548+
The example simulates a trip booking workflow that books a flight, hotel, and car. If any step fails, the workflow will automatically compensate (cancel) the previously completed bookings in reverse order.
549+
550+
The `BookTripWorkflow` class defines the workflow. It orchestrates the booking process and handles compensation if any step fails. See the code snippet below:
551+
```java
552+
public class BookTripWorkflow extends Workflow {
553+
@Override
554+
public WorkflowStub create() {
555+
return ctx -> {
556+
List<String> compensations = new ArrayList<>();
557+
558+
try {
559+
// Book flight
560+
String flightResult = ctx.callActivity(BookFlightActivity.class.getName(), String.class).await();
561+
ctx.getLogger().info("Flight booking completed: " + flightResult);
562+
compensations.add(CancelFlightActivity.class.getName());
563+
564+
// Book hotel
565+
String hotelResult = ctx.callActivity(BookHotelActivity.class.getName(), String.class).await();
566+
ctx.getLogger().info("Hotel booking completed: " + hotelResult);
567+
compensations.add(CancelHotelActivity.class.getName());
568+
569+
// Book car
570+
String carResult = ctx.callActivity(BookCarActivity.class.getName(), String.class).await();
571+
ctx.getLogger().info("Car booking completed: " + carResult);
572+
compensations.add(CancelCarActivity.class.getName());
573+
574+
} catch (Exception e) {
575+
ctx.getLogger().info("******** executing compensation logic ********");
576+
// Execute compensations in reverse order
577+
Collections.reverse(compensations);
578+
for (String compensation : compensations) {
579+
try {
580+
ctx.callActivity(compensation, String.class).await();
581+
} catch (Exception ex) {
582+
ctx.getLogger().error("Error during compensation: " + ex.getMessage());
583+
}
584+
}
585+
ctx.complete("Workflow failed, compensation applied");
586+
return;
587+
}
588+
ctx.complete("All bookings completed successfully");
589+
};
590+
}
591+
}
592+
```
593+
594+
Each activity class (`BookFlightActivity`, `BookHotelActivity`, `BookCarActivity`) implements the booking logic, while their corresponding compensation activities (`CancelFlightActivity`, `CancelHotelActivity`, `CancelCarActivity`) implement the cancellation logic.
595+
596+
<!-- STEP
597+
name: Run Compensation Pattern workflow worker
598+
match_order: none
599+
output_match_mode: substring
600+
expected_stdout_lines:
601+
- "Registered Workflow: BookTripWorkflow"
602+
- "Registered Activity: BookFlightActivity"
603+
- "Registered Activity: CancelFlightActivity"
604+
- "Registered Activity: BookHotelActivity"
605+
- "Registered Activity: CancelHotelActivity"
606+
- "Registered Activity: BookCarActivity"
607+
- "Registered Activity: CancelCarActivity"
608+
- "Successfully built dapr workflow runtime"
609+
- "Start workflow runtime"
610+
- "Durable Task worker is connecting to sidecar at 127.0.0.1:50001."
611+
612+
- "Starting Workflow: io.dapr.examples.workflows.compensation.BookTripWorkflow"
613+
- "Starting Activity: io.dapr.examples.workflows.compensation.BookFlightActivity"
614+
- "Activity completed with result: Flight booked successfully"
615+
- "Flight booking completed: Flight booked successfully"
616+
- "Starting Activity: io.dapr.examples.workflows.compensation.BookHotelActivity"
617+
- "Simulating hotel booking process..."
618+
- "Activity completed with result: Hotel booked successfully"
619+
- "Hotel booking completed: Hotel booked successfully"
620+
- "Starting Activity: io.dapr.examples.workflows.compensation.BookCarActivity"
621+
- "Forcing Failure to trigger compensation for activity: io.dapr.examples.workflows.compensation.BookCarActivity"
622+
- "******** executing compensation logic ********"
623+
- "Activity failed: Task 'io.dapr.examples.workflows.compensation.BookCarActivity' (#2) failed with an unhandled exception: Failed to book car"
624+
- "Starting Activity: io.dapr.examples.workflows.compensation.CancelHotelActivity"
625+
- "Activity completed with result: Hotel canceled successfully"
626+
- "Starting Activity: io.dapr.examples.workflows.compensation.CancelFlightActivity"
627+
- "Activity completed with result: Flight canceled successfully"
628+
background: true
629+
sleep: 60
630+
timeout_seconds: 60
631+
-->
632+
633+
Execute the following script in order to run the BookTripWorker:
634+
```sh
635+
dapr run --app-id book-trip-worker --resources-path ./components/workflows --dapr-grpc-port 50001 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.compensation.BookTripWorker
636+
```
637+
638+
Once running, execute the following script to run the BookTripClient:
639+
```sh
640+
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.compensation.BookTripClient
641+
```
642+
<!-- END_STEP -->
643+
644+
The output demonstrates:
645+
1. The workflow starts and successfully books a flight
646+
2. Then successfully books a hotel
647+
3. When attempting to book a car, it fails (intentionally)
648+
4. The compensation logic triggers, canceling the hotel and flight in reverse order
649+
5. The workflow completes with a status indicating the compensation was applied
650+
651+
Key Points:
652+
1. Each successful booking step adds its compensation action to an ArrayList
653+
2. If an error occurs, the list of compensations is reversed and executed in reverse order
654+
3. The workflow ensures that all resources are properly cleaned up even if the process fails
655+
4. Each activity simulates work with a short delay for demonstration purposes
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.compensation;
15+
16+
import io.dapr.workflows.WorkflowActivity;
17+
import io.dapr.workflows.WorkflowActivityContext;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
public class BookCarActivity implements WorkflowActivity {
24+
private static final Logger logger = LoggerFactory.getLogger(BookCarActivity.class);
25+
26+
@Override
27+
public String run(WorkflowActivityContext ctx) {
28+
logger.info("Starting Activity: " + ctx.getName());
29+
30+
// Simulate work
31+
try {
32+
TimeUnit.SECONDS.sleep(2);
33+
} catch (InterruptedException e) {
34+
throw new RuntimeException(e);
35+
}
36+
37+
logger.info("Forcing Failure to trigger compensation for activity: " + ctx.getName());
38+
39+
// force the compensation
40+
throw new RuntimeException("Failed to book car");
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.compensation;
15+
16+
import io.dapr.workflows.WorkflowActivity;
17+
import io.dapr.workflows.WorkflowActivityContext;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
public class BookFlightActivity implements WorkflowActivity {
24+
private static final Logger logger = LoggerFactory.getLogger(BookFlightActivity.class);
25+
26+
@Override
27+
public String run(WorkflowActivityContext ctx) {
28+
logger.info("Starting Activity: " + ctx.getName());
29+
30+
// Simulate work
31+
try {
32+
TimeUnit.SECONDS.sleep(2);
33+
} catch (InterruptedException e) {
34+
throw new RuntimeException(e);
35+
}
36+
37+
String result = "Flight booked successfully";
38+
logger.info("Activity completed with result: " + result);
39+
return result;
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.compensation;
15+
16+
import io.dapr.workflows.WorkflowActivity;
17+
import io.dapr.workflows.WorkflowActivityContext;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
public class BookHotelActivity implements WorkflowActivity {
22+
private static final Logger logger = LoggerFactory.getLogger(BookHotelActivity.class);
23+
24+
@Override
25+
public String run(WorkflowActivityContext ctx) {
26+
logger.info("Starting Activity: " + ctx.getName());
27+
logger.info("Simulating hotel booking process...");
28+
29+
// Simulate some work
30+
try {
31+
Thread.sleep(2000);
32+
} catch (InterruptedException e) {
33+
Thread.currentThread().interrupt();
34+
}
35+
36+
String result = "Hotel booked successfully";
37+
logger.info("Activity completed with result: " + result);
38+
return result;
39+
}
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.compensation;
15+
16+
import io.dapr.workflows.client.DaprWorkflowClient;
17+
import io.dapr.workflows.client.WorkflowInstanceStatus;
18+
19+
import java.time.Duration;
20+
import java.util.concurrent.TimeoutException;
21+
22+
public class BookTripClient {
23+
public static void main(String[] args) {
24+
try (DaprWorkflowClient client = new DaprWorkflowClient()) {
25+
String instanceId = client.scheduleNewWorkflow(BookTripWorkflow.class);
26+
System.out.printf("Started a new trip booking workflow with instance ID: %s%n", instanceId);
27+
28+
WorkflowInstanceStatus status = client.waitForInstanceCompletion(instanceId, Duration.ofMinutes(30), true);
29+
System.out.printf("Workflow instance with ID: %s completed with status: %s%n", instanceId, status);
30+
System.out.printf("Workflow output: %s%n", status.getSerializedOutput());
31+
} catch (TimeoutException | InterruptedException e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.workflows.compensation;
15+
16+
import io.dapr.workflows.runtime.WorkflowRuntime;
17+
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
18+
19+
public class BookTripWorker {
20+
21+
public static void main(String[] args) throws Exception {
22+
// Register the Workflow with the builder
23+
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder()
24+
.registerWorkflow(BookTripWorkflow.class)
25+
.registerActivity(BookFlightActivity.class)
26+
.registerActivity(CancelFlightActivity.class)
27+
.registerActivity(BookHotelActivity.class)
28+
.registerActivity(CancelHotelActivity.class)
29+
.registerActivity(BookCarActivity.class)
30+
.registerActivity(CancelCarActivity.class);
31+
32+
// Build and start the workflow runtime
33+
try (WorkflowRuntime runtime = builder.build()) {
34+
System.out.println("Start workflow runtime");
35+
runtime.start();
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)