Search in sources :

Example 1 with ActionType

use of org.opensearch.action.ActionType 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 ActionType

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

the class RestValidateQueryActionTests method stubValidateQueryAction.

/**
 * Configures {@link NodeClient} to stub {@link ValidateQueryAction} transport action.
 * <p>
 * This lower level of validation is out of the scope of this test.
 */
@BeforeClass
public static void stubValidateQueryAction() {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    final TransportAction transportAction = new TransportAction(ValidateQueryAction.NAME, new ActionFilters(Collections.emptySet()), taskManager) {

        @Override
        protected void doExecute(Task task, ActionRequest request, ActionListener listener) {
        }
    };
    final Map<ActionType, TransportAction> actions = new HashMap<>();
    actions.put(ValidateQueryAction.INSTANCE, transportAction);
    client.initialize(actions, () -> "local", null, new NamedWriteableRegistry(Collections.emptyList()));
    controller.registerHandler(action);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Task(org.opensearch.tasks.Task) TaskManager(org.opensearch.tasks.TaskManager) ActionType(org.opensearch.action.ActionType) ActionListener(org.opensearch.action.ActionListener) ActionRequest(org.opensearch.action.ActionRequest) HashMap(java.util.HashMap) TransportAction(org.opensearch.action.support.TransportAction) ActionFilters(org.opensearch.action.support.ActionFilters) BeforeClass(org.junit.BeforeClass)

Example 3 with ActionType

use of org.opensearch.action.ActionType 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)

Example 4 with ActionType

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

the class OriginSettingClientTests method testSetsParentId.

public void testSetsParentId() {
    String origin = randomAlphaOfLength(7);
    /*
         * This mock will do nothing but verify that origin is set in the
         * thread context before executing the action.
         */
    NoOpClient mock = new NoOpClient(getTestName()) {

        @Override
        protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
            assertEquals(origin, threadPool().getThreadContext().getTransient(ThreadContext.ACTION_ORIGIN_TRANSIENT_NAME));
            super.doExecute(action, request, listener);
        }
    };
    try (OriginSettingClient client = new OriginSettingClient(mock, origin)) {
        // All of these should have the origin set
        client.bulk(new BulkRequest());
        client.search(new SearchRequest());
        client.clearScroll(new ClearScrollRequest());
        ThreadContext threadContext = client.threadPool().getThreadContext();
        client.bulk(new BulkRequest(), listenerThatAssertsOriginNotSet(threadContext));
        client.search(new SearchRequest(), listenerThatAssertsOriginNotSet(threadContext));
        client.clearScroll(new ClearScrollRequest(), listenerThatAssertsOriginNotSet(threadContext));
    }
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) 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) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) ActionResponse(org.opensearch.action.ActionResponse)

Example 5 with ActionType

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

the class AbstractMLSearchAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.parseXContent(request.contentOrSourceParamParser());
    searchSourceBuilder.fetchSource(getSourceContext(request));
    searchSourceBuilder.seqNoAndPrimaryTerm(true).version(true);
    SearchRequest searchRequest = new SearchRequest().source(searchSourceBuilder).indices(index);
    return channel -> client.execute(actionType, searchRequest, search(channel));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) EMPTY_PARAMS(org.opensearch.common.xcontent.ToXContent.EMPTY_PARAMS) RestActionUtils.getSourceContext(org.opensearch.ml.utils.RestActionUtils.getSourceContext) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) ArrayList(java.util.ArrayList) List(java.util.List) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) RestChannel(org.opensearch.rest.RestChannel) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchRequest(org.opensearch.action.search.SearchRequest) ActionType(org.opensearch.action.ActionType) SearchResponse(org.opensearch.action.search.SearchResponse) BaseRestHandler(org.opensearch.rest.BaseRestHandler) RestResponseListener(org.opensearch.rest.action.RestResponseListener) SearchRequest(org.opensearch.action.search.SearchRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

ActionType (org.opensearch.action.ActionType)5 ActionListener (org.opensearch.action.ActionListener)4 ActionRequest (org.opensearch.action.ActionRequest)4 ActionResponse (org.opensearch.action.ActionResponse)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 BulkRequest (org.opensearch.action.bulk.BulkRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 ActionFilters (org.opensearch.action.support.ActionFilters)2 NodeClient (org.opensearch.client.node.NodeClient)2 Task (org.opensearch.tasks.Task)2 NoOpClient (org.opensearch.test.client.NoOpClient)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BeforeClass (org.junit.BeforeClass)1 IndicesRequest (org.opensearch.action.IndicesRequest)1 CreateIndexResponse (org.opensearch.action.admin.indices.create.CreateIndexResponse)1