Skip to content

Commit b29f54f

Browse files
authored
Fixed race condition on workflow start in TestWorkflowService (#164)
1 parent 21400a7 commit b29f54f

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,27 @@ StartWorkflowExecutionResponse startWorkflowExecutionImpl(
188188
lock.lock();
189189
try {
190190
existing = executionsByWorkflowId.get(workflowId);
191+
if (existing != null) {
192+
Optional<WorkflowExecutionCloseStatus> statusOptional = existing.getCloseStatus();
193+
WorkflowIdReusePolicy policy =
194+
startRequest.isSetWorkflowIdReusePolicy()
195+
? startRequest.getWorkflowIdReusePolicy()
196+
: WorkflowIdReusePolicy.AllowDuplicateFailedOnly;
197+
if (!statusOptional.isPresent() || policy == WorkflowIdReusePolicy.RejectDuplicate) {
198+
return throwDuplicatedWorkflow(startRequest, existing);
199+
}
200+
WorkflowExecutionCloseStatus status = statusOptional.get();
201+
if (policy == WorkflowIdReusePolicy.AllowDuplicateFailedOnly
202+
&& (status == WorkflowExecutionCloseStatus.COMPLETED
203+
|| status == WorkflowExecutionCloseStatus.CONTINUED_AS_NEW)) {
204+
return throwDuplicatedWorkflow(startRequest, existing);
205+
}
206+
}
207+
return startWorkflowExecutionNoRunningCheckLocked(
208+
startRequest, parent, parentChildInitiatedEventId, workflowId);
191209
} finally {
192210
lock.unlock();
193211
}
194-
if (existing != null) {
195-
Optional<WorkflowExecutionCloseStatus> statusOptional = existing.getCloseStatus();
196-
WorkflowIdReusePolicy policy =
197-
startRequest.isSetWorkflowIdReusePolicy()
198-
? startRequest.getWorkflowIdReusePolicy()
199-
: WorkflowIdReusePolicy.AllowDuplicateFailedOnly;
200-
if (!statusOptional.isPresent() || policy == WorkflowIdReusePolicy.RejectDuplicate) {
201-
return throwDuplicatedWorkflow(startRequest, existing);
202-
}
203-
WorkflowExecutionCloseStatus status = statusOptional.get();
204-
if (policy == WorkflowIdReusePolicy.AllowDuplicateFailedOnly
205-
&& (status == WorkflowExecutionCloseStatus.COMPLETED
206-
|| status == WorkflowExecutionCloseStatus.CONTINUED_AS_NEW)) {
207-
return throwDuplicatedWorkflow(startRequest, existing);
208-
}
209-
}
210-
return startWorkflowExecutionNoRunningCheck(
211-
startRequest, parent, parentChildInitiatedEventId, workflowId);
212212
}
213213

214214
private StartWorkflowExecutionResponse throwDuplicatedWorkflow(
@@ -224,7 +224,7 @@ private StartWorkflowExecutionResponse throwDuplicatedWorkflow(
224224
throw error;
225225
}
226226

227-
private StartWorkflowExecutionResponse startWorkflowExecutionNoRunningCheck(
227+
private StartWorkflowExecutionResponse startWorkflowExecutionNoRunningCheckLocked(
228228
StartWorkflowExecutionRequest startRequest,
229229
Optional<TestWorkflowMutableState> parent,
230230
OptionalLong parentChildInitiatedEventId,
@@ -236,13 +236,8 @@ private StartWorkflowExecutionResponse startWorkflowExecutionNoRunningCheck(
236236
startRequest, parent, parentChildInitiatedEventId, this, store);
237237
WorkflowExecution execution = result.getExecutionId().getExecution();
238238
ExecutionId executionId = new ExecutionId(domain, execution);
239-
lock.lock();
240-
try {
241-
executionsByWorkflowId.put(workflowId, result);
242-
executions.put(executionId, result);
243-
} finally {
244-
lock.unlock();
245-
}
239+
executionsByWorkflowId.put(workflowId, result);
240+
executions.put(executionId, result);
246241
result.startWorkflow();
247242
return new StartWorkflowExecutionResponse().setRunId(execution.getRunId());
248243
}
@@ -464,10 +459,15 @@ public String continueAsNew(
464459
.setWorkflowId(executionId.getWorkflowId().getWorkflowId())
465460
.setWorkflowIdReusePolicy(previousRunStartRequest.getWorkflowIdReusePolicy())
466461
.setIdentity(identity);
467-
StartWorkflowExecutionResponse response =
468-
startWorkflowExecutionNoRunningCheck(
469-
startRequest, parent, parentChildInitiatedEventId, executionId.getWorkflowId());
470-
return response.getRunId();
462+
lock.lock();
463+
try {
464+
StartWorkflowExecutionResponse response =
465+
startWorkflowExecutionNoRunningCheckLocked(
466+
startRequest, parent, parentChildInitiatedEventId, executionId.getWorkflowId());
467+
return response.getRunId();
468+
} finally {
469+
lock.unlock();
470+
}
471471
}
472472

473473
@Override

0 commit comments

Comments
 (0)