Search in sources :

Example 21 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class TasksIT method waitForCompletionTestCase.

/**
     * Test wait for completion.
     * @param storeResult should the task store its results
     * @param wait start waiting for a task. Accepts that id of the task to wait for and returns a future waiting for it.
     * @param validator validate the response and return the task ids that were found
     */
private <T> void waitForCompletionTestCase(boolean storeResult, Function<TaskId, ListenableActionFuture<T>> wait, Consumer<T> validator) throws Exception {
    // Start blocking test task
    ListenableActionFuture<TestTaskPlugin.NodesResponse> future = TestTaskPlugin.TestTaskAction.INSTANCE.newRequestBuilder(client()).setShouldStoreResult(storeResult).execute();
    ListenableActionFuture<T> waitResponseFuture;
    TaskId taskId;
    try {
        taskId = waitForTestTaskStartOnAllNodes();
        // Wait for the task to start
        assertBusy(() -> client().admin().cluster().prepareGetTask(taskId).get());
        // Register listeners so we can be sure the waiting started
        CountDownLatch waitForWaitingToStart = new CountDownLatch(1);
        for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
            ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {

                @Override
                public void waitForTaskCompletion(Task task) {
                    waitForWaitingToStart.countDown();
                }

                @Override
                public void onTaskRegistered(Task task) {
                }

                @Override
                public void onTaskUnregistered(Task task) {
                }
            });
        }
        // Spin up a request to wait for the test task to finish
        waitResponseFuture = wait.apply(taskId);
        /* Wait for the wait to start. This should count down just *before* we wait for completion but after the list/get has got a
             * reference to the running task. Because we unblock immediately after this the task may no longer be running for us to wait
             * on which is fine. */
        waitForWaitingToStart.await();
    } finally {
        // Unblock the request so the wait for completion request can finish
        TestTaskPlugin.UnblockTestTasksAction.INSTANCE.newRequestBuilder(client()).get();
    }
    // Now that the task is unblocked the list response will come back
    T waitResponse = waitResponseFuture.get();
    validator.accept(waitResponse);
    TestTaskPlugin.NodesResponse response = future.get();
    assertEquals(emptyList(), response.failures());
}
Also used : Task(org.elasticsearch.tasks.Task) TaskId(org.elasticsearch.tasks.TaskId) MockTaskManagerListener(org.elasticsearch.test.tasks.MockTaskManagerListener) CountDownLatch(java.util.concurrent.CountDownLatch) MockTaskManager(org.elasticsearch.test.tasks.MockTaskManager) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService)

Example 22 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class AsyncBulkByScrollActionTests method setupForTest.

@Before
public void setupForTest() {
    // Fill the context with something random so we can make sure we inherited it appropriately.
    expectedHeaders.clear();
    expectedHeaders.put(randomSimpleString(random()), randomSimpleString(random()));
    setupClient(new TestThreadPool(getTestName()));
    firstSearchRequest = new SearchRequest();
    testRequest = new DummyAbstractBulkByScrollRequest(firstSearchRequest);
    listener = new PlainActionFuture<>();
    scrollId = null;
    taskManager = new TaskManager(Settings.EMPTY);
    testTask = (WorkingBulkByScrollTask) taskManager.register("don'tcare", "hereeither", testRequest);
    localNode = new DiscoveryNode("thenode", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    taskId = new TaskId(localNode.getId(), testTask.getId());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TaskManager(org.elasticsearch.tasks.TaskManager) TaskId(org.elasticsearch.tasks.TaskId) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Before(org.junit.Before)

Example 23 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class RethrottleTests method testCase.

private void testCase(AbstractBulkByScrollRequestBuilder<?, ?> request, String actionName) throws Exception {
    logger.info("Starting test for [{}] with [{}] slices", actionName, request.request().getSlices());
    /* Add ten documents per slice so most slices will have many documents to process, having to go to multiple batches.
         * we can't rely on all of them doing so, but 
         */
    List<IndexRequestBuilder> docs = new ArrayList<>();
    for (int i = 0; i < request.request().getSlices() * 10; i++) {
        docs.add(client().prepareIndex("test", "test", Integer.toString(i)).setSource("foo", "bar"));
    }
    indexRandom(true, docs);
    // Start a request that will never finish unless we rethrottle it
    // Throttle "forever"
    request.setRequestsPerSecond(.000001f);
    // Make sure we use multiple batches
    request.source().setSize(1);
    ListenableActionFuture<? extends BulkByScrollResponse> responseListener = request.execute();
    TaskGroup taskGroupToRethrottle = findTaskToRethrottle(actionName, request.request().getSlices());
    TaskId taskToRethrottle = taskGroupToRethrottle.getTaskInfo().getTaskId();
    if (request.request().getSlices() == 1) {
        assertThat(taskGroupToRethrottle.getChildTasks(), empty());
    } else {
        // There should be a sane number of child tasks running
        assertThat(taskGroupToRethrottle.getChildTasks(), hasSize(allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(request.request().getSlices()))));
        // Wait for all of the sub tasks to start (or finish, some might finish early, all that matters is that not all do)
        assertBusy(() -> {
            BulkByScrollTask.Status parent = (BulkByScrollTask.Status) client().admin().cluster().prepareGetTask(taskToRethrottle).get().getTask().getTask().getStatus();
            long finishedSubTasks = parent.getSliceStatuses().stream().filter(s -> s != null).count();
            ListTasksResponse list = client().admin().cluster().prepareListTasks().setParentTaskId(taskToRethrottle).get();
            list.rethrowFailures("subtasks");
            assertThat(finishedSubTasks + list.getTasks().size(), greaterThanOrEqualTo((long) request.request().getSlices()));
            assertThat(list.getTasks().size(), greaterThan(0));
        });
    }
    // Now rethrottle it so it'll finish
    // No throttle or "very fast"
    float newRequestsPerSecond = randomBoolean() ? Float.POSITIVE_INFINITY : between(1, 1000) * 100000;
    ListTasksResponse rethrottleResponse = rethrottle().setTaskId(taskToRethrottle).setRequestsPerSecond(newRequestsPerSecond).get();
    rethrottleResponse.rethrowFailures("Rethrottle");
    assertThat(rethrottleResponse.getTasks(), hasSize(1));
    BulkByScrollTask.Status status = (BulkByScrollTask.Status) rethrottleResponse.getTasks().get(0).getStatus();
    // Now check the resulting requests per second.
    if (request.request().getSlices() == 1) {
        // If there is a single slice it should match perfectly
        assertEquals(newRequestsPerSecond, status.getRequestsPerSecond(), Float.MIN_NORMAL);
    } else {
        /* Check that at least one slice was rethrottled. We won't always rethrottle all of them because they might have completed.
             * With multiple slices these numbers might not add up perfectly, thus the 1.01F. */
        long unfinished = status.getSliceStatuses().stream().filter(slice -> slice != null).filter(slice -> slice.getStatus().getTotal() > slice.getStatus().getSuccessfullyProcessed()).count();
        float maxExpectedSliceRequestsPerSecond = newRequestsPerSecond == Float.POSITIVE_INFINITY ? Float.POSITIVE_INFINITY : (newRequestsPerSecond / unfinished) * 1.01F;
        float minExpectedSliceRequestsPerSecond = newRequestsPerSecond == Float.POSITIVE_INFINITY ? Float.POSITIVE_INFINITY : (newRequestsPerSecond / request.request().getSlices()) * 0.99F;
        boolean oneSliceRethrottled = false;
        float totalRequestsPerSecond = 0;
        for (BulkByScrollTask.StatusOrException statusOrException : status.getSliceStatuses()) {
            if (statusOrException == null) {
                /* The slice can be null here because it was completed but hadn't reported its success back to the task when the
                     * rethrottle request came through. */
                continue;
            }
            assertNull(statusOrException.getException());
            BulkByScrollTask.Status slice = statusOrException.getStatus();
            if (slice.getTotal() > slice.getSuccessfullyProcessed()) {
                // This slice reports as not having completed so it should have been processed.
                assertThat(slice.getRequestsPerSecond(), both(greaterThanOrEqualTo(minExpectedSliceRequestsPerSecond)).and(lessThanOrEqualTo(maxExpectedSliceRequestsPerSecond)));
            }
            if (minExpectedSliceRequestsPerSecond <= slice.getRequestsPerSecond() && slice.getRequestsPerSecond() <= maxExpectedSliceRequestsPerSecond) {
                oneSliceRethrottled = true;
            }
            totalRequestsPerSecond += slice.getRequestsPerSecond();
        }
        assertTrue("At least one slice must be rethrottled", oneSliceRethrottled);
        /* Now assert that the parent request has the total requests per second. This is a much weaker assertion than that the parent
             * actually has the newRequestsPerSecond. For the most part it will. Sometimes it'll be greater because only unfinished requests
             * are rethrottled, the finished ones just keep whatever requests per second they had while they were running. But it might
             * also be less than newRequestsPerSecond because the newRequestsPerSecond is divided among running sub-requests and then the
             * requests are rethrottled. If one request finishes in between the division and the application of the new throttle then it
             * won't be rethrottled, thus only contributing its lower total. */
        assertEquals(totalRequestsPerSecond, status.getRequestsPerSecond(), totalRequestsPerSecond * 0.0001f);
    }
    // Now the response should come back quickly because we've rethrottled the request
    BulkByScrollResponse response = responseListener.get();
    assertThat("Entire request completed in a single batch. This may invalidate the test as throttling is done between batches.", response.getBatches(), greaterThanOrEqualTo(request.request().getSlices()));
}
Also used : BulkByScrollResponse(org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Matchers.allOf(org.hamcrest.Matchers.allOf) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) AbstractBulkByScrollRequestBuilder(org.elasticsearch.action.bulk.byscroll.AbstractBulkByScrollRequestBuilder) ListenableActionFuture(org.elasticsearch.action.ListenableActionFuture) TaskId(org.elasticsearch.tasks.TaskId) ArrayList(java.util.ArrayList) Matchers.both(org.hamcrest.Matchers.both) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) TaskGroup(org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup) BulkByScrollTask(org.elasticsearch.action.bulk.byscroll.BulkByScrollTask) TaskId(org.elasticsearch.tasks.TaskId) ArrayList(java.util.ArrayList) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) BulkByScrollResponse(org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkByScrollTask(org.elasticsearch.action.bulk.byscroll.BulkByScrollTask) TaskGroup(org.elasticsearch.action.admin.cluster.node.tasks.list.TaskGroup)

Example 24 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class RoundTripTests method testRethrottleRequest.

public void testRethrottleRequest() throws IOException {
    RethrottleRequest request = new RethrottleRequest();
    request.setRequestsPerSecond((float) randomDoubleBetween(0, Float.POSITIVE_INFINITY, false));
    if (randomBoolean()) {
        request.setActions(randomFrom(UpdateByQueryAction.NAME, ReindexAction.NAME));
    } else {
        request.setTaskId(new TaskId(randomAsciiOfLength(5), randomLong()));
    }
    RethrottleRequest tripped = new RethrottleRequest();
    roundTrip(request, tripped);
    assertEquals(request.getRequestsPerSecond(), tripped.getRequestsPerSecond(), 0.00001);
    assertArrayEquals(request.getActions(), tripped.getActions());
    assertEquals(request.getTaskId(), tripped.getTaskId());
}
Also used : TaskId(org.elasticsearch.tasks.TaskId)

Example 25 with TaskId

use of org.elasticsearch.tasks.TaskId in project elasticsearch by elastic.

the class TransportRethrottleActionTests method testRethrottleWithSomeSucceeded.

public void testRethrottleWithSomeSucceeded() {
    int succeeded = between(1, slices - 1);
    List<BulkByScrollTask.StatusOrException> sliceStatuses = new ArrayList<>(slices);
    for (int i = 0; i < succeeded; i++) {
        BulkByScrollTask.Status status = believeableCompletedStatus(i);
        task.onSliceResponse(neverCalled(), i, new BulkByScrollResponse(timeValueMillis(10), status, emptyList(), emptyList(), false));
        sliceStatuses.add(new BulkByScrollTask.StatusOrException(status));
    }
    List<TaskInfo> tasks = new ArrayList<>();
    for (int i = succeeded; i < slices; i++) {
        BulkByScrollTask.Status status = believeableInProgressStatus(i);
        tasks.add(new TaskInfo(new TaskId("test", 123), "test", "test", "test", status, 0, 0, true, new TaskId("test", task.getId())));
        sliceStatuses.add(new BulkByScrollTask.StatusOrException(status));
    }
    rethrottleTestCase(slices - succeeded, listener -> listener.onResponse(new ListTasksResponse(tasks, emptyList(), emptyList())), expectSuccessfulRethrottleWithStatuses(sliceStatuses));
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) ArrayList(java.util.ArrayList) ParentBulkByScrollTask(org.elasticsearch.action.bulk.byscroll.ParentBulkByScrollTask) BulkByScrollTask(org.elasticsearch.action.bulk.byscroll.BulkByScrollTask) ListTasksResponse(org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse) BulkByScrollResponse(org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse)

Aggregations

TaskId (org.elasticsearch.tasks.TaskId)28 TaskInfo (org.elasticsearch.tasks.TaskInfo)13 IOException (java.io.IOException)10 ListTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 ListTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest)6 Task (org.elasticsearch.tasks.Task)6 CancelTasksRequest (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest)5 Settings (org.elasticsearch.common.settings.Settings)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 ActionListener (org.elasticsearch.action.ActionListener)4 CancelTasksResponse (org.elasticsearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse)4 BulkByScrollTask (org.elasticsearch.action.bulk.byscroll.BulkByScrollTask)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 HashMap (java.util.HashMap)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 FailedNodeException (org.elasticsearch.action.FailedNodeException)3 GetTaskResponse (org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse)3