Search in sources :

Example 1 with ActionResponse

use of org.opensearch.action.ActionResponse in project OpenSearch by opensearch-project.

the class TransportBulkActionTookTests method createAction.

private TransportBulkAction createAction(boolean controlled, AtomicLong expected) {
    CapturingTransport capturingTransport = new CapturingTransport();
    TransportService transportService = capturingTransport.createTransportService(clusterService.getSettings(), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    IndexNameExpressionResolver resolver = new Resolver();
    ActionFilters actionFilters = new ActionFilters(new HashSet<>());
    NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {

        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
            listener.onResponse((Response) new CreateIndexResponse(false, false, null));
        }
    };
    if (controlled) {
        return new TestTransportBulkAction(threadPool, transportService, clusterService, null, client, actionFilters, resolver, null, expected::get) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
                expected.set(1000000);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses, indicesThatCannotBeCreated);
            }
        };
    } else {
        return new TestTransportBulkAction(threadPool, transportService, clusterService, null, client, actionFilters, resolver, null, System::nanoTime) {

            @Override
            void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
                long elapsed = spinForAtLeastOneMillisecond();
                expected.set(elapsed);
                super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses, indicesThatCannotBeCreated);
            }
        };
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) Task(org.opensearch.tasks.Task) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ActionType(org.opensearch.action.ActionType) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CapturingTransport(org.opensearch.test.transport.CapturingTransport) ActionRequest(org.opensearch.action.ActionRequest) IndicesRequest(org.opensearch.action.IndicesRequest) ActionFilters(org.opensearch.action.support.ActionFilters) ActionResponse(org.opensearch.action.ActionResponse) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ActionRequest(org.opensearch.action.ActionRequest) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 2 with ActionResponse

use of org.opensearch.action.ActionResponse in project ml-commons by opensearch-project.

the class MLPredictionTaskResponseTest method fromActionResponse_IOException.

@Test(expected = UncheckedIOException.class)
public void fromActionResponse_IOException() {
    ActionResponse actionResponse = new ActionResponse() {

        @Override
        public void writeTo(StreamOutput out) throws IOException {
            throw new IOException("test");
        }
    };
    MLTaskResponse.fromActionResponse(actionResponse);
}
Also used : IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ActionResponse(org.opensearch.action.ActionResponse) Test(org.junit.Test)

Example 3 with ActionResponse

use of org.opensearch.action.ActionResponse in project ml-commons by opensearch-project.

the class MLTrainingTaskResponseTest method fromActionResponse_Exception.

@Test(expected = UncheckedIOException.class)
public void fromActionResponse_Exception() {
    ActionResponse actionResponse = new ActionResponse() {

        @Override
        public void writeTo(StreamOutput out) throws IOException {
            throw new IOException("test");
        }
    };
    MLTaskResponse.fromActionResponse(actionResponse);
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ActionResponse(org.opensearch.action.ActionResponse) Test(org.junit.Test)

Example 4 with ActionResponse

use of org.opensearch.action.ActionResponse in project OpenSearch by opensearch-project.

the class RestCancellableNodeClient method doExecute.

@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
    CloseListener closeListener = httpChannels.computeIfAbsent(httpChannel, channel -> new CloseListener());
    TaskHolder taskHolder = new TaskHolder();
    Task task = client.executeLocally(action, request, new ActionListener<Response>() {

        @Override
        public void onResponse(Response response) {
            try {
                closeListener.unregisterTask(taskHolder);
            } finally {
                listener.onResponse(response);
            }
        }

        @Override
        public void onFailure(Exception e) {
            try {
                closeListener.unregisterTask(taskHolder);
            } finally {
                listener.onFailure(e);
            }
        }
    });
    final TaskId taskId = new TaskId(client.getLocalNodeId(), task.getId());
    closeListener.registerTask(taskHolder, taskId);
    closeListener.maybeRegisterChannel(httpChannel);
}
Also used : ActionResponse(org.opensearch.action.ActionResponse) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId)

Example 5 with ActionResponse

use of org.opensearch.action.ActionResponse in project OpenSearch by opensearch-project.

the class ParentTaskAssigningClientTests method testSetsParentId.

public void testSetsParentId() {
    TaskId[] parentTaskId = new TaskId[] { new TaskId(randomAlphaOfLength(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> void doExecute(ActionType<Response> 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.opensearch.action.search.SearchRequest) TaskId(org.opensearch.tasks.TaskId) ActionType(org.opensearch.action.ActionType) ActionListener(org.opensearch.action.ActionListener) ActionRequest(org.opensearch.action.ActionRequest) NoOpClient(org.opensearch.test.client.NoOpClient) BulkRequest(org.opensearch.action.bulk.BulkRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) SearchRequest(org.opensearch.action.search.SearchRequest) ActionRequest(org.opensearch.action.ActionRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) ActionResponse(org.opensearch.action.ActionResponse)

Aggregations

ActionResponse (org.opensearch.action.ActionResponse)10 ActionListener (org.opensearch.action.ActionListener)5 Test (org.junit.Test)4 ActionRequest (org.opensearch.action.ActionRequest)4 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)4 StreamOutput (org.opensearch.common.io.stream.StreamOutput)4 IOException (java.io.IOException)3 ActionType (org.opensearch.action.ActionType)3 Task (org.opensearch.tasks.Task)3 UncheckedIOException (java.io.UncheckedIOException)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BulkRequest (org.opensearch.action.bulk.BulkRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 MLTaskResponse (org.opensearch.ml.common.transport.MLTaskResponse)2 TaskId (org.opensearch.tasks.TaskId)2 NoOpClient (org.opensearch.test.client.NoOpClient)2 Instant (java.time.Instant)1 ZoneOffset (java.time.ZoneOffset)1