Search in sources :

Example 11 with ConductorClientException

use of com.netflix.conductor.client.exceptions.ConductorClientException in project conductor by Netflix.

the class TaskClient method evaluateAndUploadLargePayload.

public void evaluateAndUploadLargePayload(TaskResult taskResult, String taskType) {
    Preconditions.checkNotNull(taskResult, "Task result cannot be null");
    Preconditions.checkArgument(StringUtils.isBlank(taskResult.getExternalOutputPayloadStoragePath()), "External Storage Path must not be set");
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        objectMapper.writeValue(byteArrayOutputStream, taskResult.getOutputData());
        byte[] taskOutputBytes = byteArrayOutputStream.toByteArray();
        long taskResultSize = taskOutputBytes.length;
        MetricsContainer.recordTaskResultPayloadSize(taskType, taskResultSize);
        long payloadSizeThreshold = conductorClientConfiguration.getTaskOutputPayloadThresholdKB() * 1024;
        if (taskResultSize > payloadSizeThreshold) {
            if (!conductorClientConfiguration.isExternalPayloadStorageEnabled() || taskResultSize > conductorClientConfiguration.getTaskOutputMaxPayloadThresholdKB() * 1024) {
                taskResult.setReasonForIncompletion(String.format("The TaskResult payload size: %d is greater than the permissible %d MB", taskResultSize, payloadSizeThreshold));
                taskResult.setStatus(TaskResult.Status.FAILED_WITH_TERMINAL_ERROR);
                taskResult.setOutputData(null);
            } else {
                MetricsContainer.incrementExternalPayloadUsedCount(taskType, ExternalPayloadStorage.Operation.WRITE.name(), ExternalPayloadStorage.PayloadType.TASK_OUTPUT.name());
                String externalStoragePath = uploadToExternalPayloadStorage(ExternalPayloadStorage.PayloadType.TASK_OUTPUT, taskOutputBytes, taskResultSize);
                taskResult.setExternalOutputPayloadStoragePath(externalStoragePath);
                taskResult.setOutputData(null);
            }
        }
    } catch (IOException e) {
        String errorMsg = String.format("Unable to update task: %s with task result", taskResult.getTaskId());
        logger.error(errorMsg, e);
        throw new ConductorClientException(errorMsg, e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ConductorClientException(com.netflix.conductor.client.exceptions.ConductorClientException)

Example 12 with ConductorClientException

use of com.netflix.conductor.client.exceptions.ConductorClientException in project conductor by Netflix.

the class TaskPollExecutorTest method testLargePayloadCanFailUpdateWithRetry.

@Test
public void testLargePayloadCanFailUpdateWithRetry() {
    Task task = testTask();
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(3000);
    when(worker.getTaskDefName()).thenReturn(TEST_TASK_DEF_NAME);
    when(worker.execute(any())).thenReturn(new TaskResult(task));
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    when(taskClient.pollTask(any(), any(), any())).thenReturn(task);
    when(taskClient.ack(any(), any())).thenReturn(true);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertNull(result.getReasonForIncompletion());
        result.setReasonForIncompletion("some_reason");
        throw new ConductorClientException();
    }).when(taskClient).evaluateAndUploadLargePayload(any(TaskResult.class), any());
    TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 3, new HashMap<>(), "test-worker-");
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(worker).onErrorUpdate(any());
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
    Uninterruptibles.awaitUninterruptibly(latch);
    // When evaluateAndUploadLargePayload fails indefinitely, task update shouldn't be called.
    verify(taskClient, times(0)).updateTask(any());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Task(com.netflix.conductor.common.metadata.tasks.Task) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) ConductorClientException(com.netflix.conductor.client.exceptions.ConductorClientException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with ConductorClientException

use of com.netflix.conductor.client.exceptions.ConductorClientException in project conductor by Netflix.

the class ClientBase method handleUniformInterfaceException.

private void handleUniformInterfaceException(UniformInterfaceException exception, URI uri) {
    ClientResponse clientResponse = exception.getResponse();
    if (clientResponse == null) {
        throw new ConductorClientException(String.format("Unable to invoke Conductor API with uri: %s", uri));
    }
    try {
        if (clientResponse.getStatus() < 300) {
            return;
        }
        String errorMessage = clientResponse.getEntity(String.class);
        logger.error("Unable to invoke Conductor API with uri: {}, unexpected response from server: statusCode={}, responseBody='{}'.", uri, clientResponse.getStatus(), errorMessage);
        ErrorResponse errorResponse;
        try {
            errorResponse = objectMapper.readValue(errorMessage, ErrorResponse.class);
        } catch (IOException e) {
            throw new ConductorClientException(clientResponse.getStatus(), errorMessage);
        }
        throw new ConductorClientException(clientResponse.getStatus(), errorResponse);
    } catch (ConductorClientException e) {
        throw e;
    } catch (ClientHandlerException e) {
        handleClientHandlerException(e, uri);
    } catch (RuntimeException e) {
        handleRuntimeException(e, uri);
    } finally {
        clientResponse.close();
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) IOException(java.io.IOException) ConductorClientException(com.netflix.conductor.client.exceptions.ConductorClientException) ErrorResponse(com.netflix.conductor.common.validation.ErrorResponse)

Example 14 with ConductorClientException

use of com.netflix.conductor.client.exceptions.ConductorClientException in project conductor by Netflix.

the class PayloadStorage method download.

/**
 * Downloads the payload from the given uri.
 *
 * @param uri the location from where the object is to be downloaded
 * @return an inputstream of the payload in the external storage
 * @throws ConductorClientException if the download fails due to an invalid path or an error from external storage
 */
@Override
public InputStream download(String uri) {
    HttpURLConnection connection = null;
    String errorMsg;
    try {
        URL url = new URI(uri).toURL();
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(false);
        // Check the HTTP response code
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            logger.debug("Download completed with HTTP response code: {}", connection.getResponseCode());
            return org.apache.commons.io.IOUtils.toBufferedInputStream(connection.getInputStream());
        }
        errorMsg = String.format("Unable to download. Response code: %d", responseCode);
        logger.error(errorMsg);
        throw new ConductorClientException(errorMsg);
    } catch (URISyntaxException | MalformedURLException e) {
        errorMsg = String.format("Invalid uri specified: %s", uri);
        logger.error(errorMsg, e);
        throw new ConductorClientException(errorMsg, e);
    } catch (IOException e) {
        errorMsg = String.format("Error downloading from uri: %s", uri);
        logger.error(errorMsg, e);
        throw new ConductorClientException(errorMsg, e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ConductorClientException(com.netflix.conductor.client.exceptions.ConductorClientException) URI(java.net.URI) URL(java.net.URL)

Aggregations

ConductorClientException (com.netflix.conductor.client.exceptions.ConductorClientException)14 Test (org.junit.Test)8 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 ValidationError (com.netflix.conductor.common.validation.ValidationError)4 List (java.util.List)4 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 MetadataClient (com.netflix.conductor.client.http.MetadataClient)1 TaskClient (com.netflix.conductor.client.http.TaskClient)1 Worker (com.netflix.conductor.client.worker.Worker)1 Task (com.netflix.conductor.common.metadata.tasks.Task)1 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)1 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)1