use of com.uber.cadence.History in project cadence-client by uber-java.
the class WorkflowTestingTest method testTimerCancellation.
@Test
public void testTimerCancellation() throws TException {
Worker worker = testEnvironment.newWorker(TASK_LIST);
worker.registerWorkflowImplementationTypes(TestTimerCancellationWorkflow.class);
worker.registerActivitiesImplementations(new ActivityImpl());
worker.start();
WorkflowClient client = testEnvironment.newWorkflowClient();
TestWorkflow workflow = client.newWorkflowStub(TestWorkflow.class);
WorkflowExecution execution = WorkflowClient.start(workflow::workflow1, "input1");
WorkflowStub untyped = client.newUntypedWorkflowStub(execution, Optional.empty());
testEnvironment.sleep(Duration.ofHours(1));
untyped.cancel();
try {
untyped.getResult(String.class);
fail("unreacheable");
} catch (CancellationException e) {
}
History history = testEnvironment.getWorkflowService().GetWorkflowExecutionHistory(new GetWorkflowExecutionHistoryRequest().setExecution(execution).setDomain(client.getDomain())).getHistory();
List<HistoryEvent> historyEvents = history.getEvents();
assertTrue(WorkflowExecutionUtils.prettyPrintHistory(history, false), WorkflowExecutionUtils.containsEvent(historyEvents, EventType.TimerCanceled));
}
use of com.uber.cadence.History in project cadence-client by uber-java.
the class TestWorkflowStoreImpl method getWorkflowExecutionHistory.
@Override
public GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory(ExecutionId executionId, GetWorkflowExecutionHistoryRequest getRequest) throws EntityNotExistsError {
HistoryStore history;
// Used to eliminate the race condition on waitForNewEvents
long expectedNextEventId;
lock.lock();
try {
history = getHistoryStore(executionId);
if (!getRequest.isWaitForNewEvent() && getRequest.getHistoryEventFilterType() != HistoryEventFilterType.CLOSE_EVENT) {
List<HistoryEvent> events = history.getEventsLocked();
// Copy the list as it is mutable. Individual events assumed immutable.
ArrayList<HistoryEvent> eventsCopy = new ArrayList<>(events);
return new GetWorkflowExecutionHistoryResponse().setHistory(new History().setEvents(eventsCopy));
}
expectedNextEventId = history.getNextEventIdLocked();
} finally {
lock.unlock();
}
List<HistoryEvent> events = history.waitForNewEvents(expectedNextEventId, getRequest.getHistoryEventFilterType());
GetWorkflowExecutionHistoryResponse result = new GetWorkflowExecutionHistoryResponse();
if (events != null) {
result.setHistory(new History().setEvents(events));
}
return result;
}
use of com.uber.cadence.History in project cadence-client by uber-java.
the class StateMachines method startDecisionTask.
private static void startDecisionTask(RequestContext ctx, DecisionTaskData data, PollForDecisionTaskRequest request, long notUsed) {
DecisionTaskStartedEventAttributes a = new DecisionTaskStartedEventAttributes().setIdentity(request.getIdentity()).setScheduledEventId(data.scheduledEventId);
HistoryEvent event = new HistoryEvent().setEventType(EventType.DecisionTaskStarted).setDecisionTaskStartedEventAttributes(a);
long startedEventId = ctx.addEvent(event);
ctx.onCommit((historySize) -> {
data.decisionTask.setStartedEventId(startedEventId);
DecisionTaskToken taskToken = new DecisionTaskToken(ctx.getExecutionId(), historySize);
data.decisionTask.setTaskToken(taskToken.toBytes());
GetWorkflowExecutionHistoryRequest getRequest = new GetWorkflowExecutionHistoryRequest().setDomain(request.getDomain()).setExecution(ctx.getExecution());
List<HistoryEvent> events;
try {
events = data.store.getWorkflowExecutionHistory(ctx.getExecutionId(), getRequest).getHistory().getEvents();
} catch (EntityNotExistsError entityNotExistsError) {
throw new InternalServiceError(entityNotExistsError.toString());
}
data.decisionTask.setHistory(new History().setEvents(events));
data.previousStartedEventId = startedEventId;
data.attempt++;
});
}
use of com.uber.cadence.History in project cadence-client by uber-java.
the class WorkflowExecutionUtils method getInstanceCloseEventAsync.
private static CompletableFuture<HistoryEvent> getInstanceCloseEventAsync(IWorkflowService service, String domain, final WorkflowExecution workflowExecution, byte[] pageToken, long timeout, TimeUnit unit) {
// TODO: Interrupt service long poll call on timeout and on interrupt
long start = System.currentTimeMillis();
CompletableFuture<HistoryEvent> result = new CompletableFuture<>();
GetWorkflowExecutionHistoryRequest request = new GetWorkflowExecutionHistoryRequest();
request.setDomain(domain);
request.setExecution(workflowExecution);
request.setHistoryEventFilterType(HistoryEventFilterType.CLOSE_EVENT);
request.setNextPageToken(pageToken);
CompletableFuture<GetWorkflowExecutionHistoryResponse> response = getWorkflowExecutionHistoryAsync(service, request);
return response.thenComposeAsync((r) -> {
if (timeout != 0 && System.currentTimeMillis() - start > unit.toMillis(timeout)) {
throw CheckedExceptionWrapper.wrap(new TimeoutException("WorkflowId=" + workflowExecution.getWorkflowId() + ", runId=" + workflowExecution.getRunId() + ", timeout=" + timeout + ", unit=" + unit));
}
History history = r.getHistory();
if (history == null || history.getEvents().size() == 0) {
// Empty poll returned
return getInstanceCloseEventAsync(service, domain, workflowExecution, pageToken, timeout, unit);
}
HistoryEvent event = history.getEvents().get(0);
if (!isWorkflowExecutionCompletedEvent(event)) {
throw new RuntimeException("Last history event is not completion event: " + event);
}
// Workflow called continueAsNew. Start polling the new generation with new runId.
if (event.getEventType() == EventType.WorkflowExecutionContinuedAsNew) {
WorkflowExecution nextWorkflowExecution = new WorkflowExecution().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId());
return getInstanceCloseEventAsync(service, domain, nextWorkflowExecution, r.getNextPageToken(), timeout, unit);
}
return CompletableFuture.completedFuture(event);
});
}
use of com.uber.cadence.History in project cadence-client by uber-java.
the class WorkflowExecutionUtils method getInstanceCloseEvent.
/**
* Returns an instance closing event, potentially waiting for workflow to complete.
*/
public static HistoryEvent getInstanceCloseEvent(IWorkflowService service, String domain, WorkflowExecution workflowExecution, long timeout, TimeUnit unit) throws TimeoutException, EntityNotExistsError {
byte[] pageToken = null;
GetWorkflowExecutionHistoryResponse response;
// TODO: Interrupt service long poll call on timeout and on interrupt
long start = System.currentTimeMillis();
HistoryEvent event;
do {
GetWorkflowExecutionHistoryRequest r = new GetWorkflowExecutionHistoryRequest();
r.setDomain(domain);
r.setExecution(workflowExecution);
r.setHistoryEventFilterType(HistoryEventFilterType.CLOSE_EVENT);
r.setNextPageToken(pageToken);
try {
response = Retryer.retryWithResult(retryParameters, () -> service.GetWorkflowExecutionHistory(r));
} catch (EntityNotExistsError e) {
throw e;
} catch (TException e) {
throw CheckedExceptionWrapper.wrap(e);
}
if (timeout != 0 && System.currentTimeMillis() - start > unit.toMillis(timeout)) {
throw new TimeoutException("WorkflowId=" + workflowExecution.getWorkflowId() + ", runId=" + workflowExecution.getRunId() + ", timeout=" + timeout + ", unit=" + unit);
}
pageToken = response.getNextPageToken();
History history = response.getHistory();
if (history != null && history.getEvents().size() > 0) {
event = history.getEvents().get(0);
if (!isWorkflowExecutionCompletedEvent(event)) {
throw new RuntimeException("Last history event is not completion event: " + event);
}
// Workflow called continueAsNew. Start polling the new generation with new runId.
if (event.getEventType() == EventType.WorkflowExecutionContinuedAsNew) {
pageToken = null;
workflowExecution = new WorkflowExecution().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId());
continue;
}
break;
}
} while (true);
return event;
}
Aggregations