Skip to content

Commit 3036ef5

Browse files
vancexumeiliang86
authored andcommitted
Support Memo in visibility (#298)
1 parent ce02fea commit 3036ef5

File tree

9 files changed

+241
-12
lines changed

9 files changed

+241
-12
lines changed

src/main/java/com/uber/cadence/client/WorkflowOptions.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.uber.cadence.internal.common.OptionsUtils;
3434
import com.uber.cadence.workflow.WorkflowMethod;
3535
import java.time.Duration;
36+
import java.util.Map;
3637
import java.util.Objects;
3738

3839
public final class WorkflowOptions {
@@ -62,6 +63,7 @@ public static WorkflowOptions merge(
6263
.setChildPolicy(o.getChildPolicy())
6364
.setRetryOptions(RetryOptions.merge(methodRetry, o.getRetryOptions()))
6465
.setCronSchedule(OptionsUtils.merge(cronAnnotation, o.getCronSchedule(), String.class))
66+
.setMemo(o.getMemo())
6567
.validateBuildWithDefaults();
6668
}
6769

@@ -83,6 +85,8 @@ public static final class Builder {
8385

8486
private String cronSchedule;
8587

88+
private Map<String, Object> memo;
89+
8690
public Builder() {}
8791

8892
public Builder(WorkflowOptions o) {
@@ -97,6 +101,7 @@ public Builder(WorkflowOptions o) {
97101
this.childPolicy = o.childPolicy;
98102
this.retryOptions = o.retryOptions;
99103
this.cronSchedule = o.cronSchedule;
104+
this.memo = o.memo;
100105
}
101106

102107
/**
@@ -181,6 +186,12 @@ public Builder setCronSchedule(String cronSchedule) {
181186
return this;
182187
}
183188

189+
/** Specifies additional non-indexed information in result of list workflow. */
190+
public Builder setMemo(Map<String, Object> memo) {
191+
this.memo = memo;
192+
return this;
193+
}
194+
184195
public WorkflowOptions build() {
185196
return new WorkflowOptions(
186197
workflowId,
@@ -190,7 +201,8 @@ public WorkflowOptions build() {
190201
taskList,
191202
childPolicy,
192203
retryOptions,
193-
cronSchedule);
204+
cronSchedule,
205+
memo);
194206
}
195207

196208
/**
@@ -235,7 +247,8 @@ public WorkflowOptions validateBuildWithDefaults() {
235247
taskList,
236248
childPolicy,
237249
retryOptions,
238-
cronSchedule);
250+
cronSchedule,
251+
memo);
239252
}
240253
}
241254

@@ -255,6 +268,8 @@ public WorkflowOptions validateBuildWithDefaults() {
255268

256269
private String cronSchedule;
257270

271+
private Map<String, Object> memo;
272+
258273
private WorkflowOptions(
259274
String workflowId,
260275
WorkflowIdReusePolicy workflowIdReusePolicy,
@@ -263,7 +278,8 @@ private WorkflowOptions(
263278
String taskList,
264279
ChildPolicy childPolicy,
265280
RetryOptions retryOptions,
266-
String cronSchedule) {
281+
String cronSchedule,
282+
Map<String, Object> memo) {
267283
this.workflowId = workflowId;
268284
this.workflowIdReusePolicy = workflowIdReusePolicy;
269285
this.executionStartToCloseTimeout = executionStartToCloseTimeout;
@@ -272,6 +288,7 @@ private WorkflowOptions(
272288
this.childPolicy = childPolicy;
273289
this.retryOptions = retryOptions;
274290
this.cronSchedule = cronSchedule;
291+
this.memo = memo;
275292
}
276293

277294
public String getWorkflowId() {
@@ -306,6 +323,10 @@ public String getCronSchedule() {
306323
return cronSchedule;
307324
}
308325

326+
public Map<String, Object> getMemo() {
327+
return memo;
328+
}
329+
309330
@Override
310331
public boolean equals(Object o) {
311332
if (this == o) return true;
@@ -318,7 +339,8 @@ public boolean equals(Object o) {
318339
&& Objects.equals(taskList, that.taskList)
319340
&& childPolicy == that.childPolicy
320341
&& Objects.equals(retryOptions, that.retryOptions)
321-
&& Objects.equals(cronSchedule, that.cronSchedule);
342+
&& Objects.equals(cronSchedule, that.cronSchedule)
343+
&& Objects.equals(memo, that.memo);
322344
}
323345

324346
@Override
@@ -331,7 +353,8 @@ public int hashCode() {
331353
taskList,
332354
childPolicy,
333355
retryOptions,
334-
cronSchedule);
356+
cronSchedule,
357+
memo);
335358
}
336359

337360
@Override
@@ -356,6 +379,9 @@ public String toString() {
356379
+ ", cronSchedule='"
357380
+ cronSchedule
358381
+ '\''
382+
+ ", memo='"
383+
+ memo
384+
+ '\''
359385
+ '}';
360386
}
361387
}

src/main/java/com/uber/cadence/internal/common/StartWorkflowExecutionParameters.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
2828
import java.util.List;
29+
import java.util.Map;
2930
import java.util.Objects;
3031

3132
public final class StartWorkflowExecutionParameters {
@@ -50,6 +51,8 @@ public final class StartWorkflowExecutionParameters {
5051

5152
private String cronSchedule;
5253

54+
private Map<String, byte[]> memo;
55+
5356
/**
5457
* Returns the value of the WorkflowId property for this object.
5558
*
@@ -292,6 +295,14 @@ public void setCronSchedule(String cronSchedule) {
292295
this.cronSchedule = cronSchedule;
293296
}
294297

298+
public Map<String, byte[]> getMemo() {
299+
return memo;
300+
}
301+
302+
public void setMemo(Map<String, byte[]> memo) {
303+
this.memo = memo;
304+
}
305+
295306
public StartWorkflowExecutionParameters withRetryParameters(RetryParameters retryParameters) {
296307
this.retryParameters = retryParameters;
297308
return this;
@@ -364,6 +375,9 @@ public String toString() {
364375
+ ", cronSchedule='"
365376
+ cronSchedule
366377
+ '\''
378+
+ ", memo='"
379+
+ memo
380+
+ '\''
367381
+ '}';
368382
}
369383

@@ -381,7 +395,8 @@ public boolean equals(Object o) {
381395
&& childPolicy == that.childPolicy
382396
&& workflowIdReusePolicy == that.workflowIdReusePolicy
383397
&& Objects.equals(retryParameters, that.retryParameters)
384-
&& Objects.equals(cronSchedule, that.cronSchedule);
398+
&& Objects.equals(cronSchedule, that.cronSchedule)
399+
&& Objects.equals(memo, that.memo);
385400
}
386401

387402
@Override
@@ -396,7 +411,8 @@ public int hashCode() {
396411
childPolicy,
397412
workflowIdReusePolicy,
398413
retryParameters,
399-
cronSchedule);
414+
cronSchedule,
415+
memo);
400416
result = 31 * result + Arrays.hashCode(input);
401417
return result;
402418
}

src/main/java/com/uber/cadence/internal/external/GenericWorkflowClientExternalImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.uber.cadence.serviceclient.IWorkflowService;
3232
import com.uber.m3.tally.Scope;
3333
import com.uber.m3.util.ImmutableMap;
34+
import java.nio.ByteBuffer;
35+
import java.util.HashMap;
3436
import java.util.Map;
3537
import java.util.UUID;
3638
import org.apache.thrift.TException;
@@ -107,6 +109,7 @@ private WorkflowExecution startWorkflowInternal(StartWorkflowExecutionParameters
107109
if (!Strings.isNullOrEmpty(startParameters.getCronSchedule())) {
108110
request.setCronSchedule(startParameters.getCronSchedule());
109111
}
112+
request.setMemo(toMemoThrift(startParameters.getMemo()));
110113

111114
// if(startParameters.getChildPolicy() != null) {
112115
// request.setChildPolicy(startParameters.getChildPolicy());
@@ -127,6 +130,20 @@ private WorkflowExecution startWorkflowInternal(StartWorkflowExecutionParameters
127130
return execution;
128131
}
129132

133+
private Memo toMemoThrift(Map<String, byte[]> memo) {
134+
if (memo == null || memo.isEmpty()) {
135+
return null;
136+
}
137+
138+
Map<String, ByteBuffer> fields = new HashMap<>();
139+
for (Map.Entry<String, byte[]> item : memo.entrySet()) {
140+
fields.put(item.getKey(), ByteBuffer.wrap(item.getValue()));
141+
}
142+
Memo memoThrift = new Memo();
143+
memoThrift.setFields(fields);
144+
return memoThrift;
145+
}
146+
130147
private RetryPolicy toRetryPolicy(RetryParameters retryParameters) {
131148
return new RetryPolicy()
132149
.setBackoffCoefficient(retryParameters.getBackoffCoefficient())

src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.uber.cadence.client.WorkflowServiceException;
3333
import com.uber.cadence.client.WorkflowStub;
3434
import com.uber.cadence.converter.DataConverter;
35+
import com.uber.cadence.converter.DataConverterException;
3536
import com.uber.cadence.internal.common.CheckedExceptionWrapper;
3637
import com.uber.cadence.internal.common.SignalWithStartWorkflowExecutionParameters;
3738
import com.uber.cadence.internal.common.StartWorkflowExecutionParameters;
@@ -41,6 +42,8 @@
4142
import com.uber.cadence.internal.replay.QueryWorkflowParameters;
4243
import com.uber.cadence.internal.replay.SignalExternalWorkflowParameters;
4344
import java.lang.reflect.Type;
45+
import java.util.HashMap;
46+
import java.util.Map;
4447
import java.util.Optional;
4548
import java.util.UUID;
4649
import java.util.concurrent.CancellationException;
@@ -136,9 +139,26 @@ private StartWorkflowExecutionParameters getStartWorkflowExecutionParameters(
136139
}
137140
p.setInput(dataConverter.toData(args));
138141
p.setWorkflowType(new WorkflowType().setName(workflowType.get()));
142+
p.setMemo(convertMemoFromObjectToBytes(o.getMemo()));
139143
return p;
140144
}
141145

146+
private Map<String, byte[]> convertMemoFromObjectToBytes(Map<String, Object> memoFromOption) {
147+
if (memoFromOption == null) {
148+
return null;
149+
}
150+
Map<String, byte[]> memo = new HashMap<>();
151+
for (Map.Entry<String, Object> item : memoFromOption.entrySet()) {
152+
try {
153+
memo.put(item.getKey(), dataConverter.toData(item.getValue()));
154+
} catch (DataConverterException e) {
155+
throw new DataConverterException(
156+
"Cannot serialize memo for key " + item.getKey(), e.getCause());
157+
}
158+
}
159+
return memo;
160+
}
161+
142162
@Override
143163
public WorkflowExecution start(Object... args) {
144164
if (!options.isPresent()) {

src/main/java/com/uber/cadence/internal/testservice/StateMachines.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ private static void startWorkflow(
538538
a.setAttempt(data.retryState.get().getAttempt());
539539
}
540540
a.setLastCompletionResult(data.lastCompletionResult);
541+
a.setMemo(request.getMemo());
541542
HistoryEvent event =
542543
new HistoryEvent()
543544
.setEventType(EventType.WorkflowExecutionStarted)

0 commit comments

Comments
 (0)