Search in sources :

Example 1 with TimeoutFailure

use of io.temporal.failure.TimeoutFailure 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 2 with TimeoutFailure

use of io.temporal.failure.TimeoutFailure in project sdk-java by temporalio.

the class NonDeterministicWorkflowPolicyBlockWorkflowTest method testNonDeterministicWorkflowPolicyBlockWorkflow.

@Test
public void testNonDeterministicWorkflowPolicyBlockWorkflow() {
    WorkflowOptions options = WorkflowOptions.newBuilder().setWorkflowRunTimeout(Duration.ofSeconds(5)).setWorkflowTaskTimeout(Duration.ofSeconds(1)).setTaskQueue(testWorkflowRule.getTaskQueue()).build();
    TestWorkflowStringArg workflowStub = testWorkflowRule.getWorkflowClient().newWorkflowStub(TestWorkflowStringArg.class, options);
    try {
        workflowStub.execute(testWorkflowRule.getTaskQueue());
        fail("unreachable");
    } catch (WorkflowFailedException e) {
        // expected to timeout as workflow is going get blocked.
        assertTrue(e.getCause() instanceof TimeoutFailure);
    }
    // these should be only first one failed WFT with non-deterministic error,
    // other WFTs after it should end with WORKFLOW_TASK_TIMED_OUT
    HistoryEvent nonDeterministicExceptionHistoryEvent = testWorkflowRule.getHistoryEvent(WorkflowStub.fromTyped(workflowStub).getExecution(), EventType.EVENT_TYPE_WORKFLOW_TASK_FAILED);
    WorkflowTaskFailedEventAttributes failedWFTEventAttributes = nonDeterministicExceptionHistoryEvent.getWorkflowTaskFailedEventAttributes();
    assertEquals("A correct explicit non deterministic cause should be reported", WorkflowTaskFailedCause.WORKFLOW_TASK_FAILED_CAUSE_NON_DETERMINISTIC_ERROR, failedWFTEventAttributes.getCause());
    assertEquals(NonDeterministicException.class.getName(), failedWFTEventAttributes.getFailure().getApplicationFailureInfo().getType());
}
Also used : WorkflowFailedException(io.temporal.client.WorkflowFailedException) TimeoutFailure(io.temporal.failure.TimeoutFailure) NonDeterministicException(io.temporal.worker.NonDeterministicException) WorkflowTaskFailedEventAttributes(io.temporal.api.history.v1.WorkflowTaskFailedEventAttributes) WorkflowOptions(io.temporal.client.WorkflowOptions) TestWorkflowStringArg(io.temporal.workflow.shared.TestWorkflows.TestWorkflowStringArg) HistoryEvent(io.temporal.api.history.v1.HistoryEvent) Test(org.junit.Test)

Example 3 with TimeoutFailure

use of io.temporal.failure.TimeoutFailure in project sdk-java by temporalio.

the class WorkflowTestingTest method testActivitySimulatedTimeout.

@Test
public void testActivitySimulatedTimeout() {
    Worker worker = testEnvironment.newWorker(TASK_QUEUE);
    worker.registerWorkflowImplementationTypes(ActivityWorkflow.class);
    worker.registerActivitiesImplementations(new SimulatedTimeoutActivityImpl());
    testEnvironment.start();
    WorkflowClient client = testEnvironment.getWorkflowClient();
    WorkflowOptions options = WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).build();
    TestWorkflow1 workflow = client.newWorkflowStub(TestWorkflow1.class, options);
    try {
        workflow.execute("input1");
        fail("unreachable");
    } catch (WorkflowException e) {
        assertTrue(e.getCause() instanceof ActivityFailure);
        TimeoutFailure te = (TimeoutFailure) e.getCause().getCause();
        assertEquals(TimeoutType.TIMEOUT_TYPE_SCHEDULE_TO_CLOSE, te.getTimeoutType());
        assertEquals("progress1", te.getLastHeartbeatDetails().get(String.class));
    }
}
Also used : TimeoutFailure(io.temporal.failure.TimeoutFailure) WorkflowException(io.temporal.client.WorkflowException) Worker(io.temporal.worker.Worker) WorkflowClient(io.temporal.client.WorkflowClient) WorkflowOptions(io.temporal.client.WorkflowOptions) TestWorkflow1(io.temporal.workflow.shared.TestWorkflows.TestWorkflow1) ActivityFailure(io.temporal.failure.ActivityFailure) Test(org.junit.Test)

Example 4 with TimeoutFailure

use of io.temporal.failure.TimeoutFailure in project sdk-java by temporalio.

the class POJOActivityTaskHandler method mapToActivityFailure.

@SuppressWarnings("deprecation")
private ActivityTaskHandler.Result mapToActivityFailure(Throwable exception, String activityId, Scope metricsScope, boolean isLocalActivity) {
    if (exception instanceof ActivityCanceledException) {
        if (isLocalActivity) {
            metricsScope.counter(MetricsType.LOCAL_ACTIVITY_EXEC_CANCELLED_COUNTER).inc(1);
            metricsScope.counter(MetricsType.LOCAL_ACTIVITY_CANCELED_COUNTER).inc(1);
        } else {
            metricsScope.counter(MetricsType.ACTIVITY_EXEC_CANCELLED_COUNTER).inc(1);
            metricsScope.counter(MetricsType.ACTIVITY_CANCELED_COUNTER).inc(1);
        }
        String stackTrace = FailureConverter.serializeStackTrace(exception);
        throw new FailureWrapperException(Failure.newBuilder().setStackTrace(stackTrace).setCanceledFailureInfo(CanceledFailureInfo.newBuilder()).build());
    }
    Scope ms = metricsScope.tagged(ImmutableMap.of(MetricsTag.EXCEPTION, exception.getClass().getSimpleName()));
    if (isLocalActivity) {
        ms.counter(MetricsType.LOCAL_ACTIVITY_EXEC_FAILED_COUNTER).inc(1);
        ms.counter(MetricsType.LOCAL_ACTIVITY_FAILED_COUNTER).inc(1);
    } else {
        ms.counter(MetricsType.ACTIVITY_EXEC_FAILED_COUNTER).inc(1);
    }
    if (exception instanceof TemporalFailure) {
        ((TemporalFailure) exception).setDataConverter(dataConverter);
    }
    if (exception instanceof TimeoutFailure) {
        exception = new SimulatedTimeoutFailure((TimeoutFailure) exception);
    }
    Failure failure = FailureConverter.exceptionToFailure(exception);
    RespondActivityTaskFailedRequest.Builder result = RespondActivityTaskFailedRequest.newBuilder().setFailure(failure);
    return new ActivityTaskHandler.Result(activityId, null, new Result.TaskFailedResult(result.build(), exception), null, null, false);
}
Also used : FailureWrapperException(io.temporal.internal.replay.FailureWrapperException) Scope(com.uber.m3.tally.Scope) TimeoutFailure(io.temporal.failure.TimeoutFailure) SimulatedTimeoutFailure(io.temporal.failure.SimulatedTimeoutFailure) ActivityCanceledException(io.temporal.client.ActivityCanceledException) TemporalFailure(io.temporal.failure.TemporalFailure) SimulatedTimeoutFailure(io.temporal.failure.SimulatedTimeoutFailure) TimeoutFailure(io.temporal.failure.TimeoutFailure) TemporalFailure(io.temporal.failure.TemporalFailure) SimulatedTimeoutFailure(io.temporal.failure.SimulatedTimeoutFailure) Failure(io.temporal.api.failure.v1.Failure) RespondActivityTaskFailedRequest(io.temporal.api.workflowservice.v1.RespondActivityTaskFailedRequest)

Example 5 with TimeoutFailure

use of io.temporal.failure.TimeoutFailure in project sdk-java by temporalio.

the class WorkflowTestingTest method testWorkflowTimeout.

@Test
public void testWorkflowTimeout() {
    Worker worker = testEnvironment.newWorker(TASK_QUEUE);
    worker.registerWorkflowImplementationTypes(TimeoutWorkflow.class);
    testEnvironment.start();
    WorkflowClient client = testEnvironment.getWorkflowClient();
    WorkflowOptions options = WorkflowOptions.newBuilder().setTaskQueue(TASK_QUEUE).setWorkflowRunTimeout(Duration.ofSeconds(1)).build();
    TestWorkflow1 workflow = client.newWorkflowStub(TestWorkflow1.class, options);
    try {
        workflow.execute("bar");
        fail("unreacheable");
    } catch (WorkflowException e) {
        assertTrue(e instanceof WorkflowException);
        assertEquals(TimeoutType.TIMEOUT_TYPE_START_TO_CLOSE, ((TimeoutFailure) e.getCause()).getTimeoutType());
    }
}
Also used : TimeoutFailure(io.temporal.failure.TimeoutFailure) WorkflowException(io.temporal.client.WorkflowException) Worker(io.temporal.worker.Worker) WorkflowClient(io.temporal.client.WorkflowClient) WorkflowOptions(io.temporal.client.WorkflowOptions) TestWorkflow1(io.temporal.workflow.shared.TestWorkflows.TestWorkflow1) Test(org.junit.Test)

Aggregations

TimeoutFailure (io.temporal.failure.TimeoutFailure)17 Test (org.junit.Test)13 WorkflowOptions (io.temporal.client.WorkflowOptions)11 WorkflowException (io.temporal.client.WorkflowException)10 WorkflowClient (io.temporal.client.WorkflowClient)7 Worker (io.temporal.worker.Worker)7 ActivityFailure (io.temporal.failure.ActivityFailure)6 ChildWorkflowFailure (io.temporal.failure.ChildWorkflowFailure)5 WorkflowFailedException (io.temporal.client.WorkflowFailedException)4 TestWorkflow1 (io.temporal.workflow.shared.TestWorkflows.TestWorkflow1)3 GreetingWorkflow (io.temporal.samples.hello.HelloException.GreetingWorkflow)2 TestWorkflowStringArg (io.temporal.workflow.shared.TestWorkflows.TestWorkflowStringArg)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Scope (com.uber.m3.tally.Scope)1 Payloads (io.temporal.api.common.v1.Payloads)1 Failure (io.temporal.api.failure.v1.Failure)1 ChildWorkflowExecutionTimedOutEventAttributes (io.temporal.api.history.v1.ChildWorkflowExecutionTimedOutEventAttributes)1 HistoryEvent (io.temporal.api.history.v1.HistoryEvent)1 WorkflowExecutionCanceledEventAttributes (io.temporal.api.history.v1.WorkflowExecutionCanceledEventAttributes)1 WorkflowExecutionCompletedEventAttributes (io.temporal.api.history.v1.WorkflowExecutionCompletedEventAttributes)1