Skip to content

Commit 8d99d11

Browse files
author
Liang Mei
authored
Add saga class that helps user to implement saga pattern in Cadence (#340)
1 parent d56612f commit 8d99d11

File tree

2 files changed

+423
-0
lines changed

2 files changed

+423
-0
lines changed
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.workflow;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
public final class Saga {
24+
private final Options options;
25+
private final List<Functions.Func<Promise>> compensationOps = new ArrayList<>();
26+
27+
public static final class Options {
28+
private final boolean parallelCompensation;
29+
private final boolean continueWithError;
30+
31+
private Options(boolean parallelCompensation, boolean continueWithError) {
32+
this.parallelCompensation = parallelCompensation;
33+
this.continueWithError = continueWithError;
34+
}
35+
36+
public static final class Builder {
37+
private boolean parallelCompensation = true;
38+
private boolean continueWithError = true;
39+
40+
/**
41+
* This decides if the compensation operations are run in parallel. If parallelCompensation is
42+
* false, then the compensation operations will be run the reverse order as they are added.
43+
*
44+
* @param parallelCompensation
45+
* @return option builder
46+
*/
47+
public Builder setParallelCompensation(boolean parallelCompensation) {
48+
this.parallelCompensation = parallelCompensation;
49+
return this;
50+
}
51+
52+
/**
53+
* continueWithError gives user the option to bail out of compensation operations if exception
54+
* is thrown while running them. This is useful only when parallelCompensation is false. If
55+
* parallel compensation is set to true, then all the compensation operations will be fired no
56+
* matter what and caller will receive exceptions back if there's any.
57+
*
58+
* @param continueWithError whether to proceed with the next compensation operation if the
59+
* previous throws exception. This only applies to sequential compensation.
60+
* @return option builder
61+
*/
62+
public Builder setContinueWithError(boolean continueWithError) {
63+
this.continueWithError = continueWithError;
64+
return this;
65+
}
66+
67+
public Options build() {
68+
return new Options(parallelCompensation, continueWithError);
69+
}
70+
}
71+
}
72+
73+
public static class CompensationException extends RuntimeException {
74+
public CompensationException(Throwable cause) {
75+
super("Exception from saga compensate", cause);
76+
}
77+
}
78+
79+
public Saga(Options options) {
80+
this.options = options;
81+
}
82+
83+
public void compensate() {
84+
if (options.parallelCompensation) {
85+
List<Promise> results = new ArrayList<>();
86+
for (Functions.Func<Promise> f : compensationOps) {
87+
results.add(f.apply());
88+
}
89+
90+
CompensationException sagaException = null;
91+
for (Promise p : results) {
92+
try {
93+
p.get();
94+
} catch (Exception e) {
95+
if (sagaException == null) {
96+
sagaException = new CompensationException(e);
97+
} else {
98+
sagaException.addSuppressed(e);
99+
}
100+
}
101+
}
102+
103+
if (sagaException != null) {
104+
throw sagaException;
105+
}
106+
} else {
107+
for (int i = compensationOps.size() - 1; i >= 0; i--) {
108+
Functions.Func<Promise> f = compensationOps.get(i);
109+
try {
110+
Promise result = f.apply();
111+
result.get();
112+
} catch (Exception e) {
113+
if (!options.continueWithError) {
114+
throw e;
115+
}
116+
}
117+
}
118+
}
119+
}
120+
121+
/**
122+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
123+
* Saga#compensate()} is called.
124+
*
125+
* @param operation to be executed during compensation.
126+
*/
127+
public void addCompensation(Functions.Proc operation) {
128+
compensationOps.add(() -> Async.procedure(operation));
129+
}
130+
131+
/**
132+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
133+
* Saga#compensate()} is called.
134+
*
135+
* @param operation to be executed during compensation.
136+
* @param arg1 first operation function parameter
137+
*/
138+
public <A1> void addCompensation(Functions.Proc1<A1> operation, A1 arg1) {
139+
compensationOps.add(() -> Async.procedure(operation, arg1));
140+
}
141+
142+
/**
143+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
144+
* Saga#compensate()} is called.
145+
*
146+
* @param operation to be executed during compensation.
147+
* @param arg1 first operation function parameter
148+
* @param arg2 second operation function parameter
149+
*/
150+
public <A1, A2> void addCompensation(Functions.Proc2<A1, A2> operation, A1 arg1, A2 arg2) {
151+
compensationOps.add(() -> Async.procedure(operation, arg1, arg2));
152+
}
153+
154+
/**
155+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
156+
* Saga#compensate()} is called.
157+
*
158+
* @param operation to be executed during compensation.
159+
* @param arg1 first operation function parameter
160+
* @param arg2 second operation function parameter
161+
* @param arg3 third operation function parameter
162+
*/
163+
public <A1, A2, A3> void addCompensation(
164+
Functions.Proc3<A1, A2, A3> operation, A1 arg1, A2 arg2, A3 arg3) {
165+
compensationOps.add(() -> Async.procedure(operation, arg1, arg2, arg3));
166+
}
167+
168+
/**
169+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
170+
* Saga#compensate()} is called.
171+
*
172+
* @param operation to be executed during compensation.
173+
* @param arg1 first operation function parameter
174+
* @param arg2 second operation function parameter
175+
* @param arg3 third operation function parameter
176+
* @param arg4 fourth operation function parameter
177+
*/
178+
public <A1, A2, A3, A4> void addCompensation(
179+
Functions.Proc4<A1, A2, A3, A4> operation, A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
180+
compensationOps.add(() -> Async.procedure(operation, arg1, arg2, arg3, arg4));
181+
}
182+
183+
/**
184+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
185+
* Saga#compensate()} is called.
186+
*
187+
* @param operation to be executed during compensation.
188+
* @param arg1 first operation function parameter
189+
* @param arg2 second operation function parameter
190+
* @param arg3 third operation function parameter
191+
* @param arg4 fourth operation function parameter
192+
* @param arg5 fifth operation function parameter
193+
*/
194+
public <A1, A2, A3, A4, A5> void addCompensation(
195+
Functions.Proc5<A1, A2, A3, A4, A5> operation, A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5) {
196+
compensationOps.add(() -> Async.procedure(operation, arg1, arg2, arg3, arg4, arg5));
197+
}
198+
199+
/**
200+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
201+
* Saga#compensate()} is called.
202+
*
203+
* @param operation to be executed during compensation.
204+
* @param arg1 first operation function parameter
205+
* @param arg2 second operation function parameter
206+
* @param arg3 third operation function parameter
207+
* @param arg4 fourth operation function parameter
208+
* @param arg5 fifth operation function parameter
209+
* @param arg6 sixth operation function parameter
210+
*/
211+
public <A1, A2, A3, A4, A5, A6> void addCompensation(
212+
Functions.Proc6<A1, A2, A3, A4, A5, A6> operation,
213+
A1 arg1,
214+
A2 arg2,
215+
A3 arg3,
216+
A4 arg4,
217+
A5 arg5,
218+
A6 arg6) {
219+
compensationOps.add(() -> Async.procedure(operation, arg1, arg2, arg3, arg4, arg5, arg6));
220+
}
221+
222+
/**
223+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
224+
* Saga#compensate()} is called.
225+
*
226+
* @param operation to be executed during compensation.
227+
*/
228+
public void addCompensation(Functions.Func<?> operation) {
229+
compensationOps.add(() -> Async.function(operation));
230+
}
231+
232+
/**
233+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
234+
* Saga#compensate()} is called.
235+
*
236+
* @param operation to be executed during compensation.
237+
* @param arg1 first operation function parameter
238+
*/
239+
public <A1> void addCompensation(Functions.Func1<A1, ?> operation, A1 arg1) {
240+
compensationOps.add(() -> Async.function(operation, arg1));
241+
}
242+
243+
/**
244+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
245+
* Saga#compensate()} is called.
246+
*
247+
* @param operation to be executed during compensation.
248+
* @param arg1 first operation function parameter
249+
* @param arg2 second operation function parameter
250+
*/
251+
public <A1, A2> void addCompensation(Functions.Func2<A1, A2, ?> operation, A1 arg1, A2 arg2) {
252+
compensationOps.add(() -> Async.function(operation, arg1, arg2));
253+
}
254+
255+
/**
256+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
257+
* Saga#compensate()} is called.
258+
*
259+
* @param operation to be executed during compensation.
260+
* @param operation to be executed during compensation.
261+
* @param arg1 first operation function parameter
262+
* @param arg2 second operation function parameter
263+
* @param arg3 third operation function parameter
264+
*/
265+
public <A1, A2, A3> void addCompensation(
266+
Functions.Func3<A1, A2, A3, ?> operation, A1 arg1, A2 arg2, A3 arg3) {
267+
compensationOps.add(() -> Async.function(operation, arg1, arg2, arg3));
268+
}
269+
270+
/**
271+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
272+
* Saga#compensate()} is called.
273+
*
274+
* @param operation to be executed during compensation.
275+
* @param operation to be executed during compensation.
276+
* @param arg1 first operation function parameter
277+
* @param arg2 second operation function parameter
278+
* @param arg3 third operation function parameter
279+
* @param arg4 fourth operation function parameter
280+
*/
281+
public <A1, A2, A3, A4> void addCompensation(
282+
Functions.Func4<A1, A2, A3, A4, ?> operation, A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
283+
compensationOps.add(() -> Async.function(operation, arg1, arg2, arg3, arg4));
284+
}
285+
286+
/**
287+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
288+
* Saga#compensate()} is called.
289+
*
290+
* @param operation to be executed during compensation.
291+
* @param operation to be executed during compensation.
292+
* @param arg1 first operation function parameter
293+
* @param arg2 second operation function parameter
294+
* @param arg3 third operation function parameter
295+
* @param arg4 fourth operation function parameter
296+
* @param arg5 fifth operation function parameter
297+
*/
298+
public <A1, A2, A3, A4, A5> void addCompensation(
299+
Functions.Func5<A1, A2, A3, A4, A5, ?> operation,
300+
A1 arg1,
301+
A2 arg2,
302+
A3 arg3,
303+
A4 arg4,
304+
A5 arg5) {
305+
compensationOps.add(() -> Async.function(operation, arg1, arg2, arg3, arg4, arg5));
306+
}
307+
308+
/**
309+
* Add compensation operation for saga, which will be executed in the reverse order if {@link
310+
* Saga#compensate()} is called.
311+
*
312+
* @param operation to be executed during compensation.
313+
* @param arg1 first operation function parameter
314+
* @param arg2 second operation function parameter
315+
* @param arg3 third operation function parameter
316+
* @param arg4 fourth operation function parameter
317+
* @param arg5 fifth operation function parameter
318+
* @param arg6 sixth operation function parameter
319+
*/
320+
public <A1, A2, A3, A4, A5, A6> void addCompensation(
321+
Functions.Func6<A1, A2, A3, A4, A5, A6, ?> operation,
322+
A1 arg1,
323+
A2 arg2,
324+
A3 arg3,
325+
A4 arg4,
326+
A5 arg5,
327+
A6 arg6) {
328+
compensationOps.add(() -> Async.function(operation, arg1, arg2, arg3, arg4, arg5, arg6));
329+
}
330+
}

0 commit comments

Comments
 (0)