|
34 | 34 | import com.uber.cadence.WorkflowExecution;
|
35 | 35 | import com.uber.cadence.WorkflowExecutionAlreadyStartedError;
|
36 | 36 | import com.uber.cadence.WorkflowQuery;
|
| 37 | +import com.uber.cadence.common.RetryOptions; |
37 | 38 | import com.uber.cadence.internal.common.*;
|
38 | 39 | import com.uber.cadence.internal.metrics.MetricsTag;
|
39 | 40 | import com.uber.cadence.internal.metrics.MetricsType;
|
|
43 | 44 | import com.uber.m3.tally.Scope;
|
44 | 45 | import com.uber.m3.util.ImmutableMap;
|
45 | 46 | import java.nio.ByteBuffer;
|
| 47 | +import java.time.Duration; |
46 | 48 | import java.util.HashMap;
|
47 | 49 | import java.util.Map;
|
48 | 50 | import java.util.UUID;
|
| 51 | +import java.util.concurrent.CompletableFuture; |
49 | 52 | import org.apache.thrift.TException;
|
| 53 | +import org.apache.thrift.async.AsyncMethodCallback; |
50 | 54 |
|
51 | 55 | public final class GenericWorkflowClientExternalImpl implements GenericWorkflowClientExternal {
|
52 | 56 |
|
@@ -77,19 +81,104 @@ public WorkflowExecution startWorkflow(StartWorkflowExecutionParameters startPar
|
77 | 81 | try {
|
78 | 82 | return startWorkflowInternal(startParameters);
|
79 | 83 | } finally {
|
80 |
| - // TODO: can probably cache this |
81 |
| - Map<String, String> tags = |
82 |
| - new ImmutableMap.Builder<String, String>(3) |
83 |
| - .put(MetricsTag.WORKFLOW_TYPE, startParameters.getWorkflowType().getName()) |
84 |
| - .put(MetricsTag.TASK_LIST, startParameters.getTaskList()) |
85 |
| - .put(MetricsTag.DOMAIN, domain) |
86 |
| - .build(); |
87 |
| - metricsScope.tagged(tags).counter(MetricsType.WORKFLOW_START_COUNTER).inc(1); |
| 84 | + emitMetricsForStartWorkflow(startParameters); |
88 | 85 | }
|
89 | 86 | }
|
90 | 87 |
|
| 88 | + @Override |
| 89 | + public CompletableFuture<WorkflowExecution> startWorkflowAsync( |
| 90 | + StartWorkflowExecutionParameters startParameters) { |
| 91 | + |
| 92 | + return startWorkflowAsync(startParameters, Long.MAX_VALUE); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public CompletableFuture<WorkflowExecution> startWorkflowAsync( |
| 97 | + StartWorkflowExecutionParameters startParameters, Long timeoutInMillis) { |
| 98 | + |
| 99 | + emitMetricsForStartWorkflow(startParameters); |
| 100 | + return startWorkflowAsyncInternal(startParameters, timeoutInMillis); |
| 101 | + } |
| 102 | + |
| 103 | + private void emitMetricsForStartWorkflow(StartWorkflowExecutionParameters startParameters) { |
| 104 | + // TODO: can probably cache this |
| 105 | + Map<String, String> tags = |
| 106 | + new ImmutableMap.Builder<String, String>(3) |
| 107 | + .put(MetricsTag.WORKFLOW_TYPE, startParameters.getWorkflowType().getName()) |
| 108 | + .put(MetricsTag.TASK_LIST, startParameters.getTaskList()) |
| 109 | + .put(MetricsTag.DOMAIN, domain) |
| 110 | + .build(); |
| 111 | + metricsScope.tagged(tags).counter(MetricsType.WORKFLOW_START_COUNTER).inc(1); |
| 112 | + } |
| 113 | + |
91 | 114 | private WorkflowExecution startWorkflowInternal(StartWorkflowExecutionParameters startParameters)
|
92 | 115 | throws WorkflowExecutionAlreadyStartedError {
|
| 116 | + |
| 117 | + StartWorkflowExecutionRequest request = getStartRequest(startParameters); |
| 118 | + StartWorkflowExecutionResponse result; |
| 119 | + try { |
| 120 | + result = |
| 121 | + Retryer.retryWithResult( |
| 122 | + Retryer.DEFAULT_SERVICE_OPERATION_RETRY_OPTIONS, |
| 123 | + () -> service.StartWorkflowExecution(request)); |
| 124 | + } catch (WorkflowExecutionAlreadyStartedError e) { |
| 125 | + throw e; |
| 126 | + } catch (TException e) { |
| 127 | + throw CheckedExceptionWrapper.wrap(e); |
| 128 | + } |
| 129 | + WorkflowExecution execution = new WorkflowExecution(); |
| 130 | + execution.setRunId(result.getRunId()); |
| 131 | + execution.setWorkflowId(request.getWorkflowId()); |
| 132 | + |
| 133 | + return execution; |
| 134 | + } |
| 135 | + |
| 136 | + private RetryOptions getRetryOptionsWithExpiration(RetryOptions o, Long timeoutInMillis) { |
| 137 | + if (timeoutInMillis == null || timeoutInMillis <= 0 || timeoutInMillis == Long.MAX_VALUE) { |
| 138 | + return o; |
| 139 | + } |
| 140 | + return new RetryOptions.Builder(Retryer.DEFAULT_SERVICE_OPERATION_RETRY_OPTIONS) |
| 141 | + .setExpiration(Duration.ofMillis((timeoutInMillis))) |
| 142 | + .build(); |
| 143 | + } |
| 144 | + |
| 145 | + private CompletableFuture<WorkflowExecution> startWorkflowAsyncInternal( |
| 146 | + StartWorkflowExecutionParameters startParameters, Long timeoutInMillis) { |
| 147 | + StartWorkflowExecutionRequest request = getStartRequest(startParameters); |
| 148 | + |
| 149 | + return Retryer.retryWithResultAsync( |
| 150 | + getRetryOptionsWithExpiration( |
| 151 | + Retryer.DEFAULT_SERVICE_OPERATION_RETRY_OPTIONS, timeoutInMillis), |
| 152 | + () -> { |
| 153 | + CompletableFuture<WorkflowExecution> result = new CompletableFuture<>(); |
| 154 | + try { |
| 155 | + |
| 156 | + service.StartWorkflowExecutionWithTimeout( |
| 157 | + request, |
| 158 | + new AsyncMethodCallback<StartWorkflowExecutionResponse>() { |
| 159 | + @Override |
| 160 | + public void onComplete(StartWorkflowExecutionResponse response) { |
| 161 | + WorkflowExecution execution = new WorkflowExecution(); |
| 162 | + execution.setRunId(response.getRunId()); |
| 163 | + execution.setWorkflowId(request.getWorkflowId()); |
| 164 | + result.complete(execution); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void onError(Exception exception) { |
| 169 | + result.completeExceptionally(exception); |
| 170 | + } |
| 171 | + }, |
| 172 | + timeoutInMillis); |
| 173 | + } catch (TException e) { |
| 174 | + result.completeExceptionally(e); |
| 175 | + } |
| 176 | + return result; |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + private StartWorkflowExecutionRequest getStartRequest( |
| 181 | + StartWorkflowExecutionParameters startParameters) { |
93 | 182 | StartWorkflowExecutionRequest request = new StartWorkflowExecutionRequest();
|
94 | 183 | request.setDomain(domain);
|
95 | 184 | if (startParameters.getInput() != null) {
|
@@ -124,26 +213,7 @@ private WorkflowExecution startWorkflowInternal(StartWorkflowExecutionParameters
|
124 | 213 | request.setSearchAttributes(toSearchAttributesThrift(startParameters.getSearchAttributes()));
|
125 | 214 | request.setHeader(toHeaderThrift(startParameters.getContext()));
|
126 | 215 |
|
127 |
| - // if(startParameters.getChildPolicy() != null) { |
128 |
| - // request.setChildPolicy(startParameters.getChildPolicy()); |
129 |
| - // } |
130 |
| - |
131 |
| - StartWorkflowExecutionResponse result; |
132 |
| - try { |
133 |
| - result = |
134 |
| - Retryer.retryWithResult( |
135 |
| - Retryer.DEFAULT_SERVICE_OPERATION_RETRY_OPTIONS, |
136 |
| - () -> service.StartWorkflowExecution(request)); |
137 |
| - } catch (WorkflowExecutionAlreadyStartedError e) { |
138 |
| - throw e; |
139 |
| - } catch (TException e) { |
140 |
| - throw CheckedExceptionWrapper.wrap(e); |
141 |
| - } |
142 |
| - WorkflowExecution execution = new WorkflowExecution(); |
143 |
| - execution.setRunId(result.getRunId()); |
144 |
| - execution.setWorkflowId(request.getWorkflowId()); |
145 |
| - |
146 |
| - return execution; |
| 216 | + return request; |
147 | 217 | }
|
148 | 218 |
|
149 | 219 | private Memo toMemoThrift(Map<String, byte[]> memo) {
|
|
0 commit comments