Search in sources :

Example 1 with HistoryEvent

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);
}
Also used : WorkflowType(io.temporal.api.common.v1.WorkflowType) HistoryEvent(io.temporal.api.history.v1.HistoryEvent)

Example 2 with HistoryEvent

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);
    });
}
Also used : GetWorkflowExecutionHistoryRequest(io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest) WorkflowExecution(io.temporal.api.common.v1.WorkflowExecution) GetWorkflowExecutionHistoryResponse(io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryResponse) History(io.temporal.api.history.v1.History) HistoryEvent(io.temporal.api.history.v1.HistoryEvent)

Example 3 with HistoryEvent

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");
    }
}
Also used : HistoryEvent(io.temporal.api.history.v1.HistoryEvent)

Example 4 with HistoryEvent

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));
    }
}
Also used : WorkflowExecutionFailedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionFailedEventAttributes) WorkflowExecutionCanceledEventAttributes(io.temporal.api.history.v1.WorkflowExecutionCanceledEventAttributes) CanceledFailure(io.temporal.failure.CanceledFailure) EncodedValues(io.temporal.common.converter.EncodedValues) WorkflowExecutionTimedOutEventAttributes(io.temporal.api.history.v1.WorkflowExecutionTimedOutEventAttributes) WorkflowFailedException(io.temporal.client.WorkflowFailedException) TimeoutFailure(io.temporal.failure.TimeoutFailure) WorkflowExecutionCompletedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionCompletedEventAttributes) WorkflowExecutionTerminatedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionTerminatedEventAttributes) TerminatedFailure(io.temporal.failure.TerminatedFailure) Payloads(io.temporal.api.common.v1.Payloads)

Example 5 with HistoryEvent

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());
}
Also used : WorkflowExecutionSignaledEventAttributes(io.temporal.api.history.v1.WorkflowExecutionSignaledEventAttributes) Payloads(io.temporal.api.common.v1.Payloads)

Aggregations

HistoryEvent (io.temporal.api.history.v1.HistoryEvent)88 Test (org.junit.Test)23 Payloads (io.temporal.api.common.v1.Payloads)19 ArrayList (java.util.ArrayList)15 WorkflowExecution (io.temporal.api.common.v1.WorkflowExecution)13 Failure (io.temporal.api.failure.v1.Failure)13 WorkflowExecutionSignaledEventAttributes (io.temporal.api.history.v1.WorkflowExecutionSignaledEventAttributes)13 ActivityTaskCompletedEventAttributes (io.temporal.api.history.v1.ActivityTaskCompletedEventAttributes)12 ApplicationFailureInfo (io.temporal.api.failure.v1.ApplicationFailureInfo)11 ActivityTaskCancelRequestedEventAttributes (io.temporal.api.history.v1.ActivityTaskCancelRequestedEventAttributes)11 ActivityTaskCanceledEventAttributes (io.temporal.api.history.v1.ActivityTaskCanceledEventAttributes)11 ActivityTaskFailedEventAttributes (io.temporal.api.history.v1.ActivityTaskFailedEventAttributes)11 ActivityTaskScheduledEventAttributes (io.temporal.api.history.v1.ActivityTaskScheduledEventAttributes)11 ActivityTaskStartedEventAttributes (io.temporal.api.history.v1.ActivityTaskStartedEventAttributes)11 ActivityTaskTimedOutEventAttributes (io.temporal.api.history.v1.ActivityTaskTimedOutEventAttributes)11 DataConverter (io.temporal.common.converter.DataConverter)11 Functions (io.temporal.workflow.Functions)11 ActivityCancellationType (io.temporal.activity.ActivityCancellationType)10 Command (io.temporal.api.command.v1.Command)10 ScheduleActivityTaskCommandAttributes (io.temporal.api.command.v1.ScheduleActivityTaskCommandAttributes)10