@Override public void updateWorkflow(String workflowInstanceId, String[] keys, Object[] values) { if (keys.length != values.length) { throw new ApplicationException(ApplicationException.Code.INVALID_INPUT, "Number of keys and values do not match"); } UpdateRequest request = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId); Map<String, Object> source = IntStream.range(0, keys.length).boxed() .collect(Collectors.toMap(i -> keys[i], i -> values[i])); request.doc(source); logger.debug("Updating workflow {} with {}", workflowInstanceId, source); new RetryUtil<UpdateResponse>().retryOnException(() -> { try { return elasticSearchClient.update(request); } catch (IOException e) { throw new RuntimeException(e); } }, null, null, RETRY_COUNT, "Updating index for doc_type workflow", "updateWorkflow"); }
@Override public void updateWorkflow(String workflowInstanceId, String[] keys, Object[] values) { if (keys.length != values.length) { throw new ApplicationException(Code.INVALID_INPUT, "Number of keys and values do not match"); } UpdateRequest request = new UpdateRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId); Map<String, Object> source = IntStream.range(0, keys.length) .boxed() .collect(Collectors.toMap(i -> keys[i], i -> values[i])); request.doc(source); logger.debug("Updating workflow {} with {}", workflowInstanceId, source); new RetryUtil<>().retryOnException( () -> elasticSearchClient.update(request), null, null, RETRY_COUNT, "Updating index for doc_type workflow", "updateWorkflow" ); }
/** * Performs an index operation with a retry. * @param request The index request that we want to perform. * @param operationDescription The type of operation that we are performing. */ private void indexWithRetry(final IndexRequest request, final String operationDescription) { try { new RetryUtil<IndexResponse>().retryOnException(() -> { try { return elasticSearchClient.index(request); } catch (IOException e) { throw new RuntimeException(e); } }, null, null, RETRY_COUNT, operationDescription, "indexWithRetry"); } catch (Exception e) { Monitors.error(className, "index"); logger.error("Failed to index {} for request type: {}", request.id(), request.type(), e); } }
@Override public void addTaskExecutionLogs(List<TaskExecLog> taskExecLogs) { if (taskExecLogs.isEmpty()) { return; } try { BulkRequestBuilder bulkRequestBuilder = elasticSearchClient.prepareBulk(); for (TaskExecLog log : taskExecLogs) { IndexRequest request = new IndexRequest(logIndexName, LOG_DOC_TYPE); request.source(objectMapper.writeValueAsBytes(log), XContentType.JSON); bulkRequestBuilder.add(request); } new RetryUtil<BulkResponse>().retryOnException( () -> bulkRequestBuilder.execute().actionGet(), null, BulkResponse::hasFailures, RETRY_COUNT, "Indexing all execution logs into doc_type task", "addTaskExecutionLogs" ); } catch (Exception e) { List<String> taskIds = taskExecLogs.stream() .map(TaskExecLog::getTaskId) .collect(Collectors.toList()); logger.error("Failed to index task execution logs for tasks: ", taskIds, e); } }
private void updateWithRetry(UpdateRequest request, String operationDescription) { try { new RetryUtil<UpdateResponse>().retryOnException( () -> elasticSearchClient.update(request).actionGet(), null, null, RETRY_COUNT, operationDescription, "updateWithRetry" ); } catch (Exception e) { Monitors.error(className, "index"); logger.error("Failed to index {} for request type: {}", request.index(), request.type(), e); } }
@Override public void addMessage(String queue, Message message) { Map<String, Object> doc = new HashMap<>(); doc.put("messageId", message.getId()); doc.put("payload", message.getPayload()); doc.put("queue", queue); doc.put("created", System.currentTimeMillis()); IndexRequest request = new IndexRequest(logIndexName, MSG_DOC_TYPE); request.source(doc); try { new RetryUtil<>().retryOnException( () -> elasticSearchClient.index(request).actionGet(), null, null, RETRY_COUNT, "Indexing document in for docType: message", "addMessage" ); } catch (Exception e) { logger.error("Failed to index message: {}", message.getId(), e); } }
new RetryUtil<BulkResponse>().retryOnException(() -> { try { return elasticSearchClient.bulk(bulkRequest);
private void updateWithRetry(int count, Task task, TaskResult result, Worker worker) { try { String description = String.format("Retry updating task result: %s for task: %s in worker: %s", result.toString(), task.getTaskDefName(), worker.getIdentity()); String methodName = "updateWithRetry"; new RetryUtil<>().retryOnException(() -> { taskClient.updateTask(result, task.getTaskType()); return null; }, null, null, count, description, methodName); } catch (Exception e) { worker.onErrorUpdate(task); WorkflowTaskMetrics.incrementTaskUpdateErrorCount(worker.getTaskDefName(), e); logger.error(String.format("Failed to update result: %s for task: %s in worker: %s", result.toString(), task.getTaskDefName(), worker.getIdentity()), e); } }
/** * @param eventExecution the instance of {@link EventExecution} * @param action the {@link Action} to be executed for the event * @param payload the {@link Message#payload} * @return the event execution updated with execution output, if the execution is completed/failed with non-transient error * the input event execution, if the execution failed due to transient error */ @SuppressWarnings("Guava") @VisibleForTesting EventExecution execute(EventExecution eventExecution, Action action, Object payload) { try { String methodName = "executeEventAction"; String description = String.format("Executing action: %s for event: %s with messageId: %s with payload: %s", action.getAction(), eventExecution.getId(), eventExecution.getMessageId(), payload); logger.debug(description); Map<String, Object> output = new RetryUtil<Map<String, Object>>().retryOnException(() -> actionProcessor.execute(action, payload, eventExecution.getEvent(), eventExecution.getMessageId()), this::isTransientException, null, RETRY_COUNT, description, methodName); if (output != null) { eventExecution.getOutput().putAll(output); } eventExecution.setStatus(Status.COMPLETED); } catch (RuntimeException e) { logger.error("Error executing action: {} for event: {} with messageId: {}", action.getAction(), eventExecution.getEvent(), eventExecution.getMessageId(), e); if (!isTransientException(e.getCause())) { // not a transient error, fail the event execution eventExecution.setStatus(Status.FAILED); eventExecution.getOutput().put("exception", e.getMessage()); } } return eventExecution; }
private void updateWithRetry(int count, Task task, TaskResult result, Worker worker) { try { String description = String.format("Retry updating task result: %s for task: %s in worker: %s", result.toString(), task.getTaskDefName(), worker.getIdentity()); String methodName = "updateWithRetry"; new RetryUtil<>().retryOnException(() -> { taskClient.updateTask(result, task.getTaskType()); return null; }, null, null, count, description, methodName); } catch (Exception e) { worker.onErrorUpdate(task); WorkflowTaskMetrics.incrementTaskUpdateErrorCount(worker.getTaskDefName(), e); logger.error(String.format("Failed to update result: %s for task: %s in worker: %s", result.toString(), task.getTaskDefName(), worker.getIdentity()), e); } }
/** * @param eventExecution the instance of {@link EventExecution} * @param action the {@link Action} to be executed for the event * @param payload the {@link Message#payload} * @return the event execution updated with execution output, if the execution is completed/failed with non-transient error * the input event execution, if the execution failed due to transient error */ @SuppressWarnings("Guava") @VisibleForTesting EventExecution execute(EventExecution eventExecution, Action action, Object payload) { try { String methodName = "executeEventAction"; String description = String.format("Executing action: %s for event: %s with messageId: %s with payload: %s", action.getAction(), eventExecution.getId(), eventExecution.getMessageId(), payload); logger.debug(description); Map<String, Object> output = new RetryUtil<Map<String, Object>>().retryOnException(() -> actionProcessor.execute(action, payload, eventExecution.getEvent(), eventExecution.getMessageId()), this::isTransientException, null, RETRY_COUNT, description, methodName); if (output != null) { eventExecution.getOutput().putAll(output); } eventExecution.setStatus(Status.COMPLETED); } catch (RuntimeException e) { logger.error("Error executing action: {} for event: {} with messageId: {}", action.getAction(), eventExecution.getEvent(), eventExecution.getMessageId(), e); if (!isTransientException(e.getCause())) { // not a transient error, fail the event execution eventExecution.setStatus(Status.FAILED); eventExecution.getOutput().put("exception", e.getMessage()); } } return eventExecution; }