use of io.temporal.api.history.v1.HistoryEvent in project sdk-java by temporalio.
the class ReplayWorkflowTaskHandler method createStatefulHandler.
// TODO(maxim): Consider refactoring that avoids mutating workflow task.
private WorkflowRunTaskHandler createStatefulHandler(PollWorkflowTaskQueueResponse.Builder workflowTask, Scope metricsScope) throws Exception {
WorkflowType workflowType = workflowTask.getWorkflowType();
List<HistoryEvent> events = workflowTask.getHistory().getEventsList();
// Sticky workflow task with partial history.
if (events.isEmpty() || events.get(0).getEventId() > 1) {
GetWorkflowExecutionHistoryRequest getHistoryRequest = GetWorkflowExecutionHistoryRequest.newBuilder().setNamespace(namespace).setExecution(workflowTask.getWorkflowExecution()).build();
GetWorkflowExecutionHistoryResponse getHistoryResponse = service.blockingStub().withOption(METRICS_TAGS_CALL_OPTIONS_KEY, metricsScope).getWorkflowExecutionHistory(getHistoryRequest);
workflowTask.setHistory(getHistoryResponse.getHistory()).setNextPageToken(getHistoryResponse.getNextPageToken());
}
ReplayWorkflow workflow = workflowFactory.getWorkflow(workflowType);
return new ReplayWorkflowRunTaskHandler(service, namespace, workflow, workflowTask, options, metricsScope, localActivityTaskPoller);
}
use of io.temporal.api.history.v1.HistoryEvent in project sdk-java by temporalio.
the class WorkflowClientLongPollAsyncHelper method getInstanceCloseEventAsync.
/**
* Returns an instance closing event, potentially waiting for workflow to complete.
*/
private static CompletableFuture<HistoryEvent> getInstanceCloseEventAsync(WorkflowServiceStubs service, RootWorkflowClientHelper workflowClientHelper, final WorkflowExecution workflowExecution, ByteString pageToken, long timeout, TimeUnit unit) {
// TODO: Interrupt service long poll call on timeout and on interrupt
long start = System.currentTimeMillis();
GetWorkflowExecutionHistoryRequest request = workflowClientHelper.newHistoryLongPollRequest(workflowExecution, pageToken);
CompletableFuture<GetWorkflowExecutionHistoryResponse> response = getWorkflowExecutionHistoryAsync(service, request, timeout, unit);
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.getEventsCount() == 0) {
// Empty poll returned
return getInstanceCloseEventAsync(service, workflowClientHelper, workflowExecution, pageToken, timeout, unit);
}
HistoryEvent event = history.getEvents(0);
if (!WorkflowExecutionUtils.isWorkflowExecutionClosedEvent(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.EVENT_TYPE_WORKFLOW_EXECUTION_CONTINUED_AS_NEW) {
WorkflowExecution nextWorkflowExecution = WorkflowExecution.newBuilder().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId()).build();
return getInstanceCloseEventAsync(service, workflowClientHelper, nextWorkflowExecution, r.getNextPageToken(), timeout, unit);
}
return CompletableFuture.completedFuture(event);
});
}
use of io.temporal.api.history.v1.HistoryEvent in project sdk-java by temporalio.
the class WorkflowExecutionHistory method checkHistory.
private static void checkHistory(History history) {
List<HistoryEvent> events = history.getEventsList();
if (events == null || events.size() == 0) {
throw new IllegalArgumentException("Empty history");
}
HistoryEvent startedEvent = events.get(0);
if (startedEvent.getEventType() != EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED) {
throw new IllegalArgumentException("First event is not WorkflowExecutionStarted but " + startedEvent);
}
if (!startedEvent.hasWorkflowExecutionStartedEventAttributes()) {
throw new IllegalArgumentException("First event is corrupted");
}
}
use of io.temporal.api.history.v1.HistoryEvent in project sdk-java by temporalio.
the class WorkflowExecutionUtils method getResultFromCloseEvent.
public static Optional<Payloads> getResultFromCloseEvent(WorkflowExecution workflowExecution, Optional<String> workflowType, HistoryEvent closeEvent, DataConverter converter) {
if (closeEvent == null) {
throw new IllegalStateException("Workflow is still running");
}
switch(closeEvent.getEventType()) {
case EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED:
WorkflowExecutionCompletedEventAttributes completedEventAttributes = closeEvent.getWorkflowExecutionCompletedEventAttributes();
if (completedEventAttributes.hasResult()) {
return Optional.of(completedEventAttributes.getResult());
}
return Optional.empty();
case EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED:
String message = null;
WorkflowExecutionCanceledEventAttributes attributes = closeEvent.getWorkflowExecutionCanceledEventAttributes();
Optional<Payloads> details = attributes.hasDetails() ? Optional.of(attributes.getDetails()) : Optional.empty();
throw new WorkflowFailedException(workflowExecution, workflowType.orElse(null), 0, RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE, new CanceledFailure("Workflow canceled", new EncodedValues(details, converter), null));
case EVENT_TYPE_WORKFLOW_EXECUTION_FAILED:
WorkflowExecutionFailedEventAttributes failed = closeEvent.getWorkflowExecutionFailedEventAttributes();
throw new WorkflowExecutionFailedException(failed.getFailure(), failed.getWorkflowTaskCompletedEventId(), failed.getRetryState());
case EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED:
WorkflowExecutionTerminatedEventAttributes terminated = closeEvent.getWorkflowExecutionTerminatedEventAttributes();
throw new WorkflowFailedException(workflowExecution, workflowType.orElse(null), 0, RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE, new TerminatedFailure(terminated.getReason(), null));
case EVENT_TYPE_WORKFLOW_EXECUTION_TIMED_OUT:
WorkflowExecutionTimedOutEventAttributes timedOut = closeEvent.getWorkflowExecutionTimedOutEventAttributes();
throw new WorkflowFailedException(workflowExecution, workflowType.orElse(null), 0, timedOut.getRetryState(), new TimeoutFailure(null, null, TimeoutType.TIMEOUT_TYPE_START_TO_CLOSE));
default:
throw new RuntimeException("Workflow end state is not completed: " + WorkflowExecutionUtils.prettyPrintObject(closeEvent));
}
}
use of io.temporal.api.history.v1.HistoryEvent in project sdk-java by temporalio.
the class ReplayWorkflowExecutor method handleWorkflowExecutionSignaled.
public void handleWorkflowExecutionSignaled(HistoryEvent event) {
WorkflowExecutionSignaledEventAttributes signalAttributes = event.getWorkflowExecutionSignaledEventAttributes();
if (completed) {
throw new IllegalStateException("Signal received after workflow is closed.");
}
Optional<Payloads> input = signalAttributes.hasInput() ? Optional.of(signalAttributes.getInput()) : Optional.empty();
this.workflow.handleSignal(signalAttributes.getSignalName(), input, event.getEventId());
}
Aggregations