Search in sources :

Example 51 with ApplicationException

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

the class Event method start.

@Override
public void start(Workflow workflow, Task task, WorkflowExecutor provider) {
    Map<String, Object> payload = new HashMap<>(task.getInputData());
    payload.put("workflowInstanceId", workflow.getWorkflowId());
    payload.put("workflowType", workflow.getWorkflowName());
    payload.put("workflowVersion", workflow.getWorkflowVersion());
    payload.put("correlationId", workflow.getCorrelationId());
    String payloadJson;
    try {
        payloadJson = objectMapper.writeValueAsString(payload);
    } catch (JsonProcessingException e) {
        String msg = String.format("Error serializing JSON payload for task: %s, workflow: %s", task.getTaskId(), workflow.getWorkflowId());
        throw new ApplicationException(INTERNAL_ERROR, msg);
    }
    Message message = new Message(task.getTaskId(), payloadJson, task.getTaskId());
    ObservableQueue queue = getQueue(workflow, task);
    if (queue != null) {
        queue.publish(Collections.singletonList(message));
        logger.debug("Published message:{} to queue:{}", message.getId(), queue.getName());
        task.getOutputData().putAll(payload);
        if (isAsyncComplete(task)) {
            task.setStatus(Status.IN_PROGRESS);
        } else {
            task.setStatus(Status.COMPLETED);
        }
    } else {
        task.setReasonForIncompletion("No queue found to publish.");
        task.setStatus(Status.FAILED);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) HashMap(java.util.HashMap) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 52 with ApplicationException

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

the class ExecutionDAOFacade method resetWorkflow.

/**
 * Reset the workflow state by removing from the {@link ExecutionDAO} and
 * removing this workflow from the {@link IndexDAO}.
 *
 * @param workflowId the workflow id to be reset
 */
public void resetWorkflow(String workflowId) {
    try {
        getWorkflowById(workflowId, true);
        executionDAO.removeWorkflow(workflowId);
        if (config.enableAsyncIndexing()) {
            indexDAO.asyncRemoveWorkflow(workflowId);
        } else {
            indexDAO.removeWorkflow(workflowId);
        }
    } catch (ApplicationException ae) {
        throw ae;
    } catch (Exception e) {
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, "Error resetting workflow state: " + workflowId, e);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 53 with ApplicationException

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

the class ExecutionDAOFacade method removeWorkflow.

/**
 * Removes the workflow from the data store.
 *
 * @param workflowId      the id of the workflow to be removed
 * @param archiveWorkflow if true, the workflow will be archived in the {@link IndexDAO} after removal from  {@link ExecutionDAO}
 */
public void removeWorkflow(String workflowId, boolean archiveWorkflow) {
    try {
        Workflow workflow = getWorkflowById(workflowId, true);
        removeWorkflowIndex(workflow, archiveWorkflow);
        // remove workflow from DAO
        try {
            executionDAO.removeWorkflow(workflowId);
        } catch (Exception ex) {
            Monitors.recordDaoError("executionDao", "removeWorkflow");
            throw ex;
        }
    } catch (ApplicationException ae) {
        throw ae;
    } catch (Exception e) {
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, "Error removing workflow: " + workflowId, e);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Workflow(com.netflix.conductor.common.run.Workflow) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 54 with ApplicationException

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

the class ExternalPayloadStorageUtils method verifyAndUpload.

/**
 * Verify the payload size and upload to external storage if necessary.
 *
 * @param entity      the task or workflow for which the payload is to be verified and uploaded
 * @param payloadType the {@link PayloadType} of the payload
 * @param <T>         {@link Task} or {@link Workflow}
 * @throws ApplicationException       in case of JSON parsing errors or upload errors
 * @throws TerminateWorkflowException if the payload size is bigger than permissible limit as per {@link Configuration}
 */
public <T> void verifyAndUpload(T entity, PayloadType payloadType) {
    long threshold = 0L;
    long maxThreshold = 0L;
    Map<String, Object> payload = new HashMap<>();
    String workflowId = "";
    switch(payloadType) {
        case TASK_INPUT:
            threshold = configuration.getTaskInputPayloadSizeThresholdKB();
            maxThreshold = configuration.getMaxTaskInputPayloadSizeThresholdKB();
            payload = ((Task) entity).getInputData();
            workflowId = ((Task) entity).getWorkflowInstanceId();
            break;
        case TASK_OUTPUT:
            threshold = configuration.getTaskOutputPayloadSizeThresholdKB();
            maxThreshold = configuration.getMaxTaskOutputPayloadSizeThresholdKB();
            payload = ((Task) entity).getOutputData();
            workflowId = ((Task) entity).getWorkflowInstanceId();
            break;
        case WORKFLOW_INPUT:
            threshold = configuration.getWorkflowInputPayloadSizeThresholdKB();
            maxThreshold = configuration.getMaxWorkflowInputPayloadSizeThresholdKB();
            payload = ((Workflow) entity).getInput();
            workflowId = ((Workflow) entity).getWorkflowId();
            break;
        case WORKFLOW_OUTPUT:
            threshold = configuration.getWorkflowOutputPayloadSizeThresholdKB();
            maxThreshold = configuration.getMaxWorkflowOutputPayloadSizeThresholdKB();
            payload = ((Workflow) entity).getOutput();
            workflowId = ((Workflow) entity).getWorkflowId();
            break;
    }
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        objectMapper.writeValue(byteArrayOutputStream, payload);
        byte[] payloadBytes = byteArrayOutputStream.toByteArray();
        long payloadSize = payloadBytes.length;
        if (payloadSize > maxThreshold * 1024) {
            if (entity instanceof Task) {
                String errorMsg = String.format("The payload size: %dB of task: %s in workflow: %s  is greater than the permissible limit: %dKB", payloadSize, ((Task) entity).getTaskId(), ((Task) entity).getWorkflowInstanceId(), maxThreshold);
                failTask(((Task) entity), payloadType, errorMsg);
            } else {
                String errorMsg = String.format("The output payload size: %dB of workflow: %s is greater than the permissible limit: %dKB", payloadSize, ((Workflow) entity).getWorkflowId(), maxThreshold);
                failWorkflow(errorMsg);
            }
        } else if (payloadSize > threshold * 1024) {
            switch(payloadType) {
                case TASK_INPUT:
                    ((Task) entity).setInputData(null);
                    ((Task) entity).setExternalInputPayloadStoragePath(uploadHelper(payloadBytes, payloadSize, PayloadType.TASK_INPUT));
                    Monitors.recordExternalPayloadStorageUsage(((Task) entity).getTaskDefName(), ExternalPayloadStorage.Operation.WRITE.toString(), PayloadType.TASK_INPUT.toString());
                    break;
                case TASK_OUTPUT:
                    ((Task) entity).setOutputData(null);
                    ((Task) entity).setExternalOutputPayloadStoragePath(uploadHelper(payloadBytes, payloadSize, PayloadType.TASK_OUTPUT));
                    Monitors.recordExternalPayloadStorageUsage(((Task) entity).getTaskDefName(), ExternalPayloadStorage.Operation.WRITE.toString(), PayloadType.TASK_OUTPUT.toString());
                    break;
                case WORKFLOW_INPUT:
                    ((Workflow) entity).setInput(null);
                    ((Workflow) entity).setExternalInputPayloadStoragePath(uploadHelper(payloadBytes, payloadSize, PayloadType.WORKFLOW_INPUT));
                    Monitors.recordExternalPayloadStorageUsage(((Workflow) entity).getWorkflowName(), ExternalPayloadStorage.Operation.WRITE.toString(), PayloadType.WORKFLOW_INPUT.toString());
                    break;
                case WORKFLOW_OUTPUT:
                    ((Workflow) entity).setOutput(null);
                    ((Workflow) entity).setExternalOutputPayloadStoragePath(uploadHelper(payloadBytes, payloadSize, PayloadType.WORKFLOW_OUTPUT));
                    Monitors.recordExternalPayloadStorageUsage(((Workflow) entity).getWorkflowName(), ExternalPayloadStorage.Operation.WRITE.toString(), PayloadType.WORKFLOW_OUTPUT.toString());
                    break;
            }
        }
    } catch (IOException e) {
        logger.error("Unable to upload payload to external storage for workflow: {}", workflowId, e);
        throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, e);
    }
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 55 with ApplicationException

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

the class S3PayloadStorage method upload.

/**
 * Uploads the payload to the given s3 object key.
 * It is expected that the caller retrieves the object key using {@link #getLocation(Operation, PayloadType, String)} before making this call.
 *
 * @param path        the s3 key of the object to be uploaded
 * @param payload     an {@link InputStream} containing the json payload which is to be uploaded
 * @param payloadSize the size of the json payload in bytes
 */
@Override
public void upload(String path, InputStream payload, long payloadSize) {
    try {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(CONTENT_TYPE);
        objectMetadata.setContentLength(payloadSize);
        PutObjectRequest request = new PutObjectRequest(bucketName, path, payload, objectMetadata);
        s3Client.putObject(request);
    } catch (SdkClientException e) {
        String msg = "Error communicating with S3";
        logger.error(msg, e);
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, msg, e);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SdkClientException(com.amazonaws.SdkClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

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