Search in sources :

Example 1 with TaskSubmissionResponse

use of org.elasticsearch.client.tasks.TaskSubmissionResponse in project Reindex by dbeast-co.

the class ReindexTaskExecutor method run.

@Override
public void run() {
    logger.info("Start to execute Task for index: " + task.getSourceIndex() + " Reindex part: " + task.getQuery());
    try {
        reindexTaskStatus.setInActiveProcess(true);
        TaskSubmissionResponse reindexSubmission = client.submitReindexTask(task.getReindexRequest(), RequestOptions.DEFAULT);
        String taskId = reindexSubmission.getTask();
        reindexTaskStatus.setTaskId(taskId);
        String[] taskForRequest = taskId.split(":");
        GetTaskRequest taskRequest = new GetTaskRequest(taskForRequest[0], Long.parseLong(taskForRequest[1]));
        GetTaskResponse response;
        int numberOfRetries = 0;
        do {
            Thread.sleep(taskRefreshInterval * 1000);
            try {
                response = client.tasks().get(taskRequest, RequestOptions.DEFAULT).orElse(null);
                if (response != null && response.getTaskInfo() != null) {
                    synchronized (this) {
                        reindexTaskStatus.updateStatus(response.getTaskInfo());
                        reindexTaskStatus.setDone(response.isCompleted());
                        this.task.setDone(response.isCompleted());
                        this.reindexTaskPlan.setDone(response.isCompleted());
                    }
                } else {
                    reindexTaskStatus.setDone(true);
                    this.reindexTaskPlan.setDone(true);
                    break;
                }
                synchronized (this) {
                    reindexJobStatus.updateTransferredDocsCount();
                    reindexProjectStatus.updateTransferredDocsCount();
                    dataWarehouse.writeStatusToFile(projectId);
                }
                numberOfRetries = 0;
            } catch (IOException | ElasticsearchException e) {
                if (numberOfRetries < taskRetries) {
                    logger.warn("There is an error in the Task execution loop in the project: " + projectId + ". " + "Task description: " + task.getDescription() + ". " + "We have " + (taskRetries - numberOfRetries) + "  more retries. " + "Error:" + e.getCause());
                    numberOfRetries++;
                } else {
                    throw e;
                }
            }
        } while (!reindexTaskStatus.isDone() && numberOfRetries < taskRetries);
        boolean isError = false;
        Response lowResponse = null;
        numberOfRetries = 0;
        do {
            Thread.sleep(taskRefreshInterval * 1000);
            try {
                lowResponse = lowLevelClient.performRequest(new Request("GET", "_tasks/" + taskId));
            } catch (IOException | ElasticsearchException e) {
                if (numberOfRetries < taskRetries) {
                    logger.warn("There is an error in the Task execution loop in the project: " + projectId + ". " + "Task description: " + task.getDescription() + ". " + "We have " + (taskRetries - numberOfRetries) + "  more retries. " + "Error:" + e.getCause());
                    numberOfRetries++;
                    isError = true;
                } else {
                    throw e;
                }
            }
        } while (isError);
        ObjectMapper mapper = new ObjectMapper();
        JsonNode afterTaskEndResponse = mapper.readTree(EntityUtils.toString(lowResponse.getEntity()));
        synchronized (this) {
            reindexTaskStatus.updateStatus(afterTaskEndResponse);
            reindexTaskStatus.updateStatuses();
            if (reindexTaskStatus.isSucceeded()) {
                reindexJobStatus.incrementSucceededTasks();
                reindexProjectStatus.incrementSucceededTasks();
                this.reindexTaskPlan.setSucceeded(true);
            } else {
                reindexJobStatus.incrementFailedTasks();
                reindexProjectStatus.incrementFailedTasks();
                this.reindexTaskPlan.setFailed(true);
                this.reindexProjectPlan.setFailed(true);
                stopIfFailure();
            }
            reindexJobStatus.updateTransferredDocsCount();
            reindexProjectStatus.updateTransferredDocsCount();
            reindexJobStatus.updateExecutionProgress();
            reindexProjectStatus.updateExecutionProgress();
        }
    } catch (IOException | ElasticsearchException e) {
        reindexTaskStatus.setInActiveProcess(false);
        reindexTaskStatus.setFailed(true);
        reindexTaskStatus.setDone(true);
        this.reindexTaskPlan.setFailed(true);
        this.reindexTaskPlan.setDone(true);
        this.reindexTaskPlan.setInActiveProcess(false);
        this.reindexProjectPlan.setFailed(true);
        reindexTaskStatus.setError(e.getCause());
        synchronized (this) {
            reindexJobStatus.incrementFailedTasks();
            reindexProjectStatus.incrementFailedTasks();
            this.reindexTaskPlan.setFailed(true);
            this.reindexProjectPlan.setFailed(true);
            stopIfFailure();
        }
        logger.warn("There is an error in the Task execution. Error:" + e.getCause());
        logger.warn("Task execution of task for project: " + projectId + " was interrupted. Task description: " + task.getDescription() + " IMPORTANT! MAY BE THE TASK INSIDE THE ELASTICSEARCH DOESN'T STOPPED");
        stopIfFailure();
    } catch (InterruptedException e) {
        if (reindexJobStatus.isInActiveProcess() && e.getCause() != null) {
            reindexTaskStatus.setInActiveProcess(false);
            reindexTaskStatus.setInterrupted(true);
            reindexTaskStatus.setDone(false);
            this.reindexTaskPlan.setInActiveProcess(false);
            this.reindexTaskPlan.setDone(false);
            reindexTaskStatus.setError(e.getCause());
            synchronized (this) {
                reindexJobStatus.incrementFailedTasks();
                reindexProjectStatus.incrementFailedTasks();
                this.reindexTaskPlan.setFailed(true);
                this.reindexProjectPlan.setFailed(true);
                stopIfFailure();
            }
            logger.warn("There is an error in the Task execution. Error:" + e.getCause());
            logger.warn("Task execution of task for project: " + projectId + " was interrupted. Task description: " + task.getDescription() + " IMPORTANT! THE TASK INSIDE ELASTICSEARCH DOESN'T STOPPED");
            stopIfFailure();
        } else {
            logger.warn("Task: " + task.getDescription() + " of the project with id: " + projectId + " was interrupted by user");
        }
    } finally {
        dataWarehouse.writeStatusToFile(projectId);
    }
}
Also used : GetTaskRequest(org.elasticsearch.client.tasks.GetTaskRequest) TaskSubmissionResponse(org.elasticsearch.client.tasks.TaskSubmissionResponse) GetTaskRequest(org.elasticsearch.client.tasks.GetTaskRequest) GetTaskResponse(org.elasticsearch.client.tasks.GetTaskResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) TaskSubmissionResponse(org.elasticsearch.client.tasks.TaskSubmissionResponse) GetTaskResponse(org.elasticsearch.client.tasks.GetTaskResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 GetTaskRequest (org.elasticsearch.client.tasks.GetTaskRequest)1 GetTaskResponse (org.elasticsearch.client.tasks.GetTaskResponse)1 TaskSubmissionResponse (org.elasticsearch.client.tasks.TaskSubmissionResponse)1