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));
}
}
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());
}
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));
}
}
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);
}
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());
}
}
Aggregations