Search in sources :

Example 41 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class MySQLBaseDAO method getWithTransaction.

/**
 * Initialize a new transactional {@link Connection} from {@link #dataSource} and pass it to {@literal function}.
 * <p>
 * Successful executions of {@literal function} will result in a commit and return of
 * {@link TransactionalFunction#apply(Connection)}.
 * <p>
 * If any {@link Throwable} thrown from {@code TransactionalFunction#apply(Connection)} will result in a rollback
 * of the transaction
 * and will be wrapped in an {@link ApplicationException} if it is not already one.
 * <p>
 * Generally this is used to wrap multiple {@link #execute(Connection, String, ExecuteFunction)} or
 * {@link #query(Connection, String, QueryFunction)} invocations that produce some expected return value.
 *
 * @param function The function to apply with a new transactional {@link Connection}
 * @param <R>      The return type.
 * @return The result of {@code TransactionalFunction#apply(Connection)}
 * @throws ApplicationException If any errors occur.
 */
private <R> R getWithTransaction(final TransactionalFunction<R> function) {
    final Instant start = Instant.now();
    LazyToString callingMethod = getCallingMethod();
    logger.trace("{} : starting transaction", callingMethod);
    try (Connection tx = dataSource.getConnection()) {
        boolean previousAutoCommitMode = tx.getAutoCommit();
        tx.setAutoCommit(false);
        try {
            R result = function.apply(tx);
            tx.commit();
            return result;
        } catch (Throwable th) {
            tx.rollback();
            if (th instanceof ApplicationException) {
                throw th;
            }
            throw new ApplicationException(BACKEND_ERROR, th.getMessage(), th);
        } finally {
            tx.setAutoCommit(previousAutoCommitMode);
        }
    } catch (SQLException ex) {
        throw new ApplicationException(BACKEND_ERROR, ex.getMessage(), ex);
    } finally {
        logger.trace("{} : took {}ms", callingMethod, Duration.between(start, Instant.now()).toMillis());
    }
}
Also used : BACKEND_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.BACKEND_ERROR) INTERNAL_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.INTERNAL_ERROR) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SQLException(java.sql.SQLException) Instant(java.time.Instant) Connection(java.sql.Connection)

Example 42 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class MySQLBaseDAO method getWithTransactionWithOutErrorPropagation.

protected <R> R getWithTransactionWithOutErrorPropagation(TransactionalFunction<R> function) {
    Instant start = Instant.now();
    LazyToString callingMethod = getCallingMethod();
    logger.trace("{} : starting transaction", callingMethod);
    try (Connection tx = dataSource.getConnection()) {
        boolean previousAutoCommitMode = tx.getAutoCommit();
        tx.setAutoCommit(false);
        try {
            R result = function.apply(tx);
            tx.commit();
            return result;
        } catch (Throwable th) {
            tx.rollback();
            logger.info(CONFLICT + " " + th.getMessage());
            return null;
        } finally {
            tx.setAutoCommit(previousAutoCommitMode);
        }
    } catch (SQLException ex) {
        throw new ApplicationException(BACKEND_ERROR, ex.getMessage(), ex);
    } finally {
        logger.trace("{} : took {}ms", callingMethod, Duration.between(start, Instant.now()).toMillis());
    }
}
Also used : BACKEND_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.BACKEND_ERROR) INTERNAL_ERROR(com.netflix.conductor.core.execution.ApplicationException.Code.INTERNAL_ERROR) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SQLException(java.sql.SQLException) Instant(java.time.Instant) Connection(java.sql.Connection)

Example 43 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class RedisEventHandlerDAO method getEventHandlersForEvent.

@Override
public List<EventHandler> getEventHandlersForEvent(String event, boolean activeOnly) {
    String key = nsKey(EVENT_HANDLERS_BY_EVENT, event);
    Set<String> names = dynoClient.smembers(key);
    List<EventHandler> handlers = new LinkedList<>();
    for (String name : names) {
        try {
            EventHandler eventHandler = getEventHandler(name);
            recordRedisDaoEventRequests("getEventHandler", event);
            if (eventHandler.getEvent().equals(event) && (!activeOnly || eventHandler.isActive())) {
                handlers.add(eventHandler);
            }
        } catch (ApplicationException ae) {
            if (ae.getCode() == Code.NOT_FOUND) {
                LOGGER.info("No matching event handler found for event: {}", event);
            }
            throw ae;
        }
    }
    return handlers;
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) LinkedList(java.util.LinkedList)

Example 44 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class RedisEventHandlerDAO method updateEventHandler.

@Override
public void updateEventHandler(EventHandler eventHandler) {
    Preconditions.checkNotNull(eventHandler.getName(), "Missing Name");
    EventHandler existing = getEventHandler(eventHandler.getName());
    if (existing == null) {
        throw new ApplicationException(Code.NOT_FOUND, "EventHandler with name " + eventHandler.getName() + " not found!");
    }
    index(eventHandler);
    dynoClient.hset(nsKey(EVENT_HANDLERS), eventHandler.getName(), toJson(eventHandler));
    recordRedisDaoRequests("updateEventHandler");
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler)

Example 45 with ApplicationException

use of com.netflix.conductor.core.execution.ApplicationException in project conductor by Netflix.

the class AbstractWorkflowServiceTest method testKafkaTaskDefTemplateSuccess.

@Test
public void testKafkaTaskDefTemplateSuccess() throws Exception {
    try {
        registerKafkaWorkflow();
    } catch (ApplicationException e) {
    }
    Map<String, Object> input = getKafkaInput();
    String workflowInstanceId = startOrLoadWorkflowExecution("template_kafka_workflow", 1, "testTaskDefTemplate", input, null, null);
    assertNotNull(workflowInstanceId);
    Workflow workflow = workflowExecutionService.getExecutionStatus(workflowInstanceId, true);
    assertNotNull(workflow);
    assertTrue(workflow.getReasonForIncompletion(), !workflow.getStatus().isTerminal());
    assertEquals(1, workflow.getTasks().size());
    Task task = workflow.getTasks().get(0);
    Map<String, Object> taskInput = task.getInputData();
    assertNotNull(taskInput);
    assertTrue(taskInput.containsKey("kafka_request"));
    assertTrue(taskInput.get("kafka_request") instanceof Map);
    String expected = "{\"kafka_request\":{\"topic\":\"test_kafka_topic\",\"bootStrapServers\":\"localhost:9092\",\"value\":{\"requestDetails\":{\"key1\":\"value1\",\"key2\":42},\"outputPath\":\"s3://bucket/outputPath\",\"inputPaths\":[\"file://path1\",\"file://path2\"]}}}";
    assertEquals(expected, objectMapper.writeValueAsString(taskInput));
    TaskResult taskResult = new TaskResult(task);
    taskResult.setStatus(TaskResult.Status.COMPLETED);
    // Polling for the first task
    Task task1 = workflowExecutionService.poll("KAFKA_PUBLISH", "test");
    assertNotNull(task1);
    assertTrue(workflowExecutionService.ackTaskReceived(task1.getTaskId()));
    assertEquals(workflowInstanceId, task1.getWorkflowInstanceId());
    workflowExecutionService.updateTask(taskResult);
    workflowExecutor.decide(workflowInstanceId);
    workflow = workflowExecutionService.getExecutionStatus(workflowInstanceId, true);
    assertNotNull(workflow);
    assertEquals(WorkflowStatus.COMPLETED, workflow.getStatus());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) UserTask(com.netflix.conductor.tests.utils.UserTask) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ApplicationException (com.netflix.conductor.core.execution.ApplicationException)93 Task (com.netflix.conductor.common.metadata.tasks.Task)22 Workflow (com.netflix.conductor.common.run.Workflow)22 HashMap (java.util.HashMap)19 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 IOException (java.io.IOException)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)14 List (java.util.List)14 Inject (javax.inject.Inject)14 Singleton (javax.inject.Singleton)14 Logger (org.slf4j.Logger)14 LoggerFactory (org.slf4j.LoggerFactory)14 Trace (com.netflix.conductor.annotations.Trace)13 EventExecution (com.netflix.conductor.common.metadata.events.EventExecution)13 Monitors (com.netflix.conductor.metrics.Monitors)13 Collectors (java.util.stream.Collectors)13 ResultSet (com.datastax.driver.core.ResultSet)12 EventHandler (com.netflix.conductor.common.metadata.events.EventHandler)12 TimeUnit (java.util.concurrent.TimeUnit)12