Search in sources :

Example 1 with ActionRequest

use of org.elasticsearch.action.ActionRequest in project camel by apache.

the class BulkRequestAggregationStrategy method aggregate.

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    // Don't use getBody(Class<T>) here as we don't want to coerce the body type using a type converter.
    Object objBody = newExchange.getIn().getBody();
    if (!(objBody instanceof ActionRequest)) {
        throw new InvalidPayloadRuntimeException(newExchange, ActionRequest.class);
    }
    ActionRequest newBody = (ActionRequest) objBody;
    BulkRequest request;
    if (oldExchange == null) {
        request = new BulkRequest();
        request.add(newBody);
        newExchange.getIn().setBody(request);
        return newExchange;
    } else {
        request = oldExchange.getIn().getBody(BulkRequest.class);
        request.add(newBody);
        return oldExchange;
    }
}
Also used : ActionRequest(org.elasticsearch.action.ActionRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Example 2 with ActionRequest

use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.

the class AbstractAsyncBulkByScrollActionScriptTestCase method applyScript.

@SuppressWarnings("unchecked")
protected <T extends ActionRequest> T applyScript(Consumer<Map<String, Object>> scriptBody) {
    IndexRequest index = new IndexRequest("index", "type", "1").source(singletonMap("foo", "bar"));
    ScrollableHitSource.Hit doc = new ScrollableHitSource.BasicHit("test", "type", "id", 0);
    ExecutableScript executableScript = new SimpleExecutableScript(scriptBody);
    when(scriptService.executable(any(CompiledScript.class), Matchers.<Map<String, Object>>any())).thenReturn(executableScript);
    AbstractAsyncBulkByScrollAction<Request> action = action(scriptService, request().setScript(EMPTY_SCRIPT));
    RequestWrapper<?> result = action.buildScriptApplier().apply(AbstractAsyncBulkByScrollAction.wrap(index), doc);
    return (result != null) ? (T) result.self() : null;
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ActionRequest(org.elasticsearch.action.ActionRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) ScrollableHitSource(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 3 with ActionRequest

use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.

the class RestHighLevelClientTests method testRequestValidation.

public void testRequestValidation() {
    ActionRequestValidationException validationException = new ActionRequestValidationException();
    validationException.addValidationError("validation error");
    ActionRequest request = new ActionRequest() {

        @Override
        public ActionRequestValidationException validate() {
            return validationException;
        }
    };
    {
        ActionRequestValidationException actualException = expectThrows(ActionRequestValidationException.class, () -> restHighLevelClient.performRequest(request, null, null, null));
        assertSame(validationException, actualException);
    }
    {
        TrackingActionListener trackingActionListener = new TrackingActionListener();
        restHighLevelClient.performRequestAsync(request, null, null, trackingActionListener, null);
        assertSame(validationException, trackingActionListener.exception.get());
    }
}
Also used : ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) ActionRequest(org.elasticsearch.action.ActionRequest)

Example 4 with ActionRequest

use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.

the class TransportActionFilterChainTests method testTooManyContinueProcessingRequest.

public void testTooManyContinueProcessingRequest() throws ExecutionException, InterruptedException {
    final int additionalContinueCount = randomInt(10);
    RequestTestFilter testFilter = new RequestTestFilter(randomInt(), new RequestCallback() {

        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> void execute(Task task, String action, Request request, ActionListener<Response> listener, ActionFilterChain<Request, Response> actionFilterChain) {
            for (int i = 0; i <= additionalContinueCount; i++) {
                actionFilterChain.proceed(task, action, request, listener);
            }
        }
    });
    Set<ActionFilter> filters = new HashSet<>();
    filters.add(testFilter);
    String actionName = randomAsciiOfLength(randomInt(30));
    ActionFilters actionFilters = new ActionFilters(filters);
    TransportAction<TestRequest, TestResponse> transportAction = new TransportAction<TestRequest, TestResponse>(Settings.EMPTY, actionName, null, actionFilters, null, new TaskManager(Settings.EMPTY)) {

        @Override
        protected void doExecute(TestRequest request, ActionListener<TestResponse> listener) {
            listener.onResponse(new TestResponse());
        }
    };
    final CountDownLatch latch = new CountDownLatch(additionalContinueCount + 1);
    final AtomicInteger responses = new AtomicInteger();
    final List<Throwable> failures = new CopyOnWriteArrayList<>();
    transportAction.execute(new TestRequest(), new ActionListener<TestResponse>() {

        @Override
        public void onResponse(TestResponse testResponse) {
            responses.incrementAndGet();
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            failures.add(e);
            latch.countDown();
        }
    });
    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("timeout waiting for the filter to notify the listener as many times as expected");
    }
    assertThat(testFilter.runs.get(), equalTo(1));
    assertThat(testFilter.lastActionName, equalTo(actionName));
    assertThat(responses.get(), equalTo(1));
    assertThat(failures.size(), equalTo(additionalContinueCount));
    for (Throwable failure : failures) {
        assertThat(failure, instanceOf(IllegalStateException.class));
    }
}
Also used : Task(org.elasticsearch.tasks.Task) HashSet(java.util.HashSet) ActionRequest(org.elasticsearch.action.ActionRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) ActionResponse(org.elasticsearch.action.ActionResponse) TaskManager(org.elasticsearch.tasks.TaskManager) ActionListener(org.elasticsearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with ActionRequest

use of org.elasticsearch.action.ActionRequest in project elasticsearch by elastic.

the class ParentTaskAssigningClientTests method testSetsParentId.

public void testSetsParentId() {
    TaskId[] parentTaskId = new TaskId[] { new TaskId(randomAsciiOfLength(3), randomLong()) };
    // This mock will do nothing but verify that parentTaskId is set on all requests sent to it.
    NoOpClient mock = new NoOpClient(getTestName()) {

        @Override
        protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
            assertEquals(parentTaskId[0], request.getParentTask());
            super.doExecute(action, request, listener);
        }
    };
    try (ParentTaskAssigningClient client = new ParentTaskAssigningClient(mock, parentTaskId[0])) {
        // All of these should have the parentTaskId set
        client.bulk(new BulkRequest());
        client.search(new SearchRequest());
        client.clearScroll(new ClearScrollRequest());
        // Now lets verify that unwrapped calls don't have the parentTaskId set
        parentTaskId[0] = TaskId.EMPTY_TASK_ID;
        client.unwrap().bulk(new BulkRequest());
        client.unwrap().search(new SearchRequest());
        client.unwrap().clearScroll(new ClearScrollRequest());
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Action(org.elasticsearch.action.Action) TaskId(org.elasticsearch.tasks.TaskId) ActionRequest(org.elasticsearch.action.ActionRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ActionResponse(org.elasticsearch.action.ActionResponse) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) ActionListener(org.elasticsearch.action.ActionListener) ActionRequest(org.elasticsearch.action.ActionRequest) NoOpClient(org.elasticsearch.test.client.NoOpClient) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest)

Aggregations

ActionRequest (org.elasticsearch.action.ActionRequest)9 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)4 IOException (java.io.IOException)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)3 InvalidPayloadRuntimeException (org.apache.camel.InvalidPayloadRuntimeException)2 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)2 ActionListener (org.elasticsearch.action.ActionListener)2 ActionResponse (org.elasticsearch.action.ActionResponse)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 MainRequest (org.elasticsearch.action.main.MainRequest)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1