Search in sources :

Example 1 with GetTaskResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project elasticsearch by elastic.

the class TasksIT method testNodeNotFoundButTaskFound.

public void testNodeNotFoundButTaskFound() throws Exception {
    // Save a fake task that looks like it is from a node that isn't part of the cluster
    CyclicBarrier b = new CyclicBarrier(2);
    TaskResultsService resultsService = internalCluster().getInstance(TaskResultsService.class);
    resultsService.storeResult(new TaskResult(new TaskInfo(new TaskId("fake", 1), "test", "test", "", null, 0, 0, false, TaskId.EMPTY_TASK_ID), new RuntimeException("test")), new ActionListener<Void>() {

        @Override
        public void onResponse(Void response) {
            try {
                b.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    b.await();
    // Now we can find it!
    GetTaskResponse response = expectFinishedTask(new TaskId("fake:1"));
    assertEquals("test", response.getTask().getTask().getAction());
    assertNotNull(response.getTask().getError());
    assertNull(response.getTask().getResponse());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) TaskResult(org.elasticsearch.tasks.TaskResult) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) TaskResultsService(org.elasticsearch.tasks.TaskResultsService) FailedNodeException(org.elasticsearch.action.FailedNodeException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 2 with GetTaskResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project elasticsearch by elastic.

the class TasksIT method testTaskStoringFailureResult.

public void testTaskStoringFailureResult() throws Exception {
    // we need this to get task id of the process
    registerTaskManageListeners(TestTaskPlugin.TestTaskAction.NAME);
    // Start non-blocking test task that should fail
    assertThrows(TestTaskPlugin.TestTaskAction.INSTANCE.newRequestBuilder(client()).setShouldFail(true).setShouldStoreResult(true).setShouldBlock(false), IllegalStateException.class);
    List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
    assertEquals(1, events.size());
    TaskInfo failedTaskInfo = events.get(0);
    TaskId failedTaskId = failedTaskInfo.getTaskId();
    GetResponse failedResultDoc = client().prepareGet(TaskResultsService.TASK_INDEX, TaskResultsService.TASK_TYPE, failedTaskId.toString()).get();
    assertTrue(failedResultDoc.isExists());
    Map<String, Object> source = failedResultDoc.getSource();
    @SuppressWarnings("unchecked") Map<String, Object> task = (Map<String, Object>) source.get("task");
    assertEquals(failedTaskInfo.getTaskId().getNodeId(), task.get("node"));
    assertEquals(failedTaskInfo.getAction(), task.get("action"));
    assertEquals(Long.toString(failedTaskInfo.getId()), task.get("id").toString());
    @SuppressWarnings("unchecked") Map<String, Object> error = (Map<String, Object>) source.get("error");
    assertEquals("Simulating operation failure", error.get("reason"));
    assertEquals("illegal_state_exception", error.get("type"));
    assertNull(source.get("result"));
    GetTaskResponse getResponse = expectFinishedTask(failedTaskId);
    assertNull(getResponse.getTask().getResponse());
    assertEquals(error, getResponse.getTask().getErrorAsMap());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple)

Example 3 with GetTaskResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project elasticsearch by elastic.

the class TasksIT method testCanFetchIndexStatus.

/**
     * Very basic "is it plugged in" style test that indexes a document and makes sure that you can fetch the status of the process. The
     * goal here is to verify that the large moving parts that make fetching task status work fit together rather than to verify any
     * particular status results from indexing. For that, look at {@link TransportReplicationActionTests}. We intentionally don't use the
     * task recording mechanism used in other places in this test so we can make sure that the status fetching works properly over the wire.
     */
public void testCanFetchIndexStatus() throws Exception {
    // First latch waits for the task to start, second on blocks it from finishing.
    CountDownLatch taskRegistered = new CountDownLatch(1);
    CountDownLatch letTaskFinish = new CountDownLatch(1);
    Thread index = null;
    try {
        for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
            ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {

                @Override
                public void onTaskRegistered(Task task) {
                    if (task.getAction().startsWith(IndexAction.NAME)) {
                        taskRegistered.countDown();
                        logger.debug("Blocking [{}] starting", task);
                        try {
                            assertTrue(letTaskFinish.await(10, TimeUnit.SECONDS));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }

                @Override
                public void onTaskUnregistered(Task task) {
                }

                @Override
                public void waitForTaskCompletion(Task task) {
                }
            });
        }
        // Need to run the task in a separate thread because node client's .execute() is blocked by our task listener
        index = new Thread(() -> {
            IndexResponse indexResponse = client().prepareIndex("test", "test").setSource("test", "test").get();
            assertArrayEquals(ReplicationResponse.EMPTY, indexResponse.getShardInfo().getFailures());
        });
        index.start();
        // waiting for at least one task to be registered
        assertTrue(taskRegistered.await(10, TimeUnit.SECONDS));
        ListTasksResponse listResponse = client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*").setDetailed(true).get();
        assertThat(listResponse.getTasks(), not(empty()));
        for (TaskInfo task : listResponse.getTasks()) {
            assertNotNull(task.getStatus());
            GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get();
            assertFalse("task should still be running", getResponse.getTask().isCompleted());
            TaskInfo fetchedWithGet = getResponse.getTask().getTask();
            assertEquals(task.getId(), fetchedWithGet.getId());
            assertEquals(task.getType(), fetchedWithGet.getType());
            assertEquals(task.getAction(), fetchedWithGet.getAction());
            assertEquals(task.getDescription(), fetchedWithGet.getDescription());
            assertEquals(task.getStatus(), fetchedWithGet.getStatus());
            assertEquals(task.getStartTime(), fetchedWithGet.getStartTime());
            assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos()));
            assertEquals(task.isCancellable(), fetchedWithGet.isCancellable());
            assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId());
        }
    } finally {
        letTaskFinish.countDown();
        if (index != null) {
            index.join();
        }
        assertBusy(() -> {
            assertEquals(emptyList(), client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*").get().getTasks());
        });
    }
}
Also used : Task(org.elasticsearch.tasks.Task) MockTaskManagerListener(org.elasticsearch.test.tasks.MockTaskManagerListener) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) MockTaskManager(org.elasticsearch.test.tasks.MockTaskManager) TaskInfo(org.elasticsearch.tasks.TaskInfo) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 4 with GetTaskResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project elasticsearch by elastic.

the class TasksIT method testTaskStoringSuccesfulResult.

public void testTaskStoringSuccesfulResult() throws Exception {
    // Randomly create an empty index to make sure the type is created automatically
    if (randomBoolean()) {
        logger.info("creating an empty results index with custom settings");
        assertAcked(client().admin().indices().prepareCreate(TaskResultsService.TASK_INDEX));
    }
    // we need this to get task id of the process
    registerTaskManageListeners(TestTaskPlugin.TestTaskAction.NAME);
    // Start non-blocking test task
    TestTaskPlugin.TestTaskAction.INSTANCE.newRequestBuilder(client()).setShouldStoreResult(true).setShouldBlock(false).get();
    List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
    assertEquals(1, events.size());
    TaskInfo taskInfo = events.get(0);
    TaskId taskId = taskInfo.getTaskId();
    GetResponse resultDoc = client().prepareGet(TaskResultsService.TASK_INDEX, TaskResultsService.TASK_TYPE, taskId.toString()).get();
    assertTrue(resultDoc.isExists());
    Map<String, Object> source = resultDoc.getSource();
    @SuppressWarnings("unchecked") Map<String, Object> task = (Map<String, Object>) source.get("task");
    assertEquals(taskInfo.getTaskId().getNodeId(), task.get("node"));
    assertEquals(taskInfo.getAction(), task.get("action"));
    assertEquals(Long.toString(taskInfo.getId()), task.get("id").toString());
    @SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) source.get("response");
    assertEquals("0", result.get("failure_count").toString());
    assertNull(source.get("failure"));
    assertNoFailures(client().admin().indices().prepareRefresh(TaskResultsService.TASK_INDEX).get());
    SearchResponse searchResponse = client().prepareSearch(TaskResultsService.TASK_INDEX).setTypes(TaskResultsService.TASK_TYPE).setSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("task.action", taskInfo.getAction()))).get();
    assertEquals(1L, searchResponse.getHits().getTotalHits());
    searchResponse = client().prepareSearch(TaskResultsService.TASK_INDEX).setTypes(TaskResultsService.TASK_TYPE).setSource(SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("task.node", taskInfo.getTaskId().getNodeId()))).get();
    assertEquals(1L, searchResponse.getHits().getTotalHits());
    GetTaskResponse getResponse = expectFinishedTask(taskId);
    assertEquals(result, getResponse.getTask().getResponseAsMap());
    assertNull(getResponse.getTask().getError());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with GetTaskResponse

use of org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse in project elasticsearch by elastic.

the class TasksIT method expectFinishedTask.

/**
     * Fetch the task status from the list tasks API using it's "fallback to get from the task index" behavior. Asserts some obvious stuff
     * about the fetched task and returns a map of it's status.
     */
private GetTaskResponse expectFinishedTask(TaskId taskId) throws IOException {
    GetTaskResponse response = client().admin().cluster().prepareGetTask(taskId).get();
    assertTrue("the task should have been completed before fetching", response.getTask().isCompleted());
    TaskInfo info = response.getTask().getTask();
    assertEquals(taskId, info.getTaskId());
    // The test task doesn't have any status
    assertNull(info.getStatus());
    return response;
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse)

Aggregations

GetTaskResponse (org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse)5 TaskInfo (org.elasticsearch.tasks.TaskInfo)5 TaskId (org.elasticsearch.tasks.TaskId)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 Tuple (org.elasticsearch.common.collect.Tuple)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IOException (java.io.IOException)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)1 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)1 FailedNodeException (org.elasticsearch.action.FailedNodeException)1 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchTransportService (org.elasticsearch.action.search.SearchTransportService)1 Task (org.elasticsearch.tasks.Task)1