Search in sources :

Example 1 with AsynchronousSearchState

use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState in project asynchronous-search by opensearch-project.

the class AsynchronousSearchService method initStateMachine.

private AsynchronousSearchStateMachine initStateMachine() {
    AsynchronousSearchStateMachine stateMachine = new AsynchronousSearchStateMachine(EnumSet.allOf(AsynchronousSearchState.class), INIT, contextEventListener);
    stateMachine.markTerminalStates(EnumSet.of(CLOSED));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(INIT, RUNNING, (s, e) -> ((AsynchronousSearchActiveContext) e.asynchronousSearchContext()).setTask(e.getSearchTask()), (contextId, listener) -> listener.onContextRunning(contextId), SearchStartedEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(RUNNING, SUCCEEDED, (s, e) -> ((AsynchronousSearchActiveContext) e.asynchronousSearchContext()).processSearchResponse(e.getSearchResponse()), (contextId, listener) -> listener.onContextCompleted(contextId), SearchSuccessfulEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(RUNNING, FAILED, (s, e) -> ((AsynchronousSearchActiveContext) e.asynchronousSearchContext()).processSearchFailure(e.getException()), (contextId, listener) -> listener.onContextFailed(contextId), SearchFailureEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(SUCCEEDED, PERSISTING, (s, e) -> asynchronousSearchPostProcessor.persistResponse((AsynchronousSearchActiveContext) e.asynchronousSearchContext(), e.getAsynchronousSearchPersistenceModel()), (contextId, listener) -> {
    }, BeginPersistEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(FAILED, PERSISTING, (s, e) -> asynchronousSearchPostProcessor.persistResponse((AsynchronousSearchActiveContext) e.asynchronousSearchContext(), e.getAsynchronousSearchPersistenceModel()), (contextId, listener) -> {
    }, BeginPersistEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(PERSISTING, PERSIST_SUCCEEDED, (s, e) -> {
    }, (contextId, listener) -> listener.onContextPersisted(contextId), SearchResponsePersistedEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(PERSISTING, PERSIST_FAILED, (s, e) -> {
    }, (contextId, listener) -> listener.onContextPersistFailed(contextId), SearchResponsePersistFailedEvent.class));
    stateMachine.registerTransition(new AsynchronousSearchTransition<>(RUNNING, CLOSED, (s, e) -> asynchronousSearchActiveStore.freeContext(e.asynchronousSearchContext().getContextId()), (contextId, listener) -> listener.onRunningContextDeleted(contextId), SearchDeletedEvent.class));
    for (AsynchronousSearchState state : EnumSet.of(PERSISTING, PERSIST_SUCCEEDED, PERSIST_FAILED, SUCCEEDED, FAILED, INIT)) {
        stateMachine.registerTransition(new AsynchronousSearchTransition<>(state, CLOSED, (s, e) -> asynchronousSearchActiveStore.freeContext(e.asynchronousSearchContext().getContextId()), (contextId, listener) -> listener.onContextDeleted(contextId), SearchDeletedEvent.class));
    }
    return stateMachine;
}
Also used : InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) SearchStartedEvent(org.opensearch.search.asynchronous.context.state.event.SearchStartedEvent) PERSISTING(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.PERSISTING) LongSupplier(java.util.function.LongSupplier) ActionListener.runAfter(org.opensearch.action.ActionListener.runAfter) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) TimeoutException(java.util.concurrent.TimeoutException) SearchDeletedEvent(org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent) GroupedActionListener(org.opensearch.action.support.GroupedActionListener) SUCCEEDED(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.SUCCEEDED) Locale(java.util.Locale) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) SearchSuccessfulEvent(org.opensearch.search.asynchronous.context.state.event.SearchSuccessfulEvent) CancelTasksResponse(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksResponse) EnumSet(java.util.EnumSet) AsynchronousSearchTransition(org.opensearch.search.asynchronous.context.state.AsynchronousSearchTransition) AsynchronousSearchContext(org.opensearch.search.asynchronous.context.AsynchronousSearchContext) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) SearchTask(org.opensearch.action.search.SearchTask) AsynchronousSearchPostProcessor(org.opensearch.search.asynchronous.processor.AsynchronousSearchPostProcessor) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) SearchResponsePersistedEvent(org.opensearch.search.asynchronous.context.state.event.SearchResponsePersistedEvent) RestStatus(org.opensearch.rest.RestStatus) AsynchronousSearchPersistenceContext(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceContext) Collectors(java.util.stream.Collectors) CLOSED(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.CLOSED) Objects(java.util.Objects) AbstractLifecycleComponent(org.opensearch.common.component.AbstractLifecycleComponent) RUNNING(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.RUNNING) Logger(org.apache.logging.log4j.Logger) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) Optional(java.util.Optional) AsynchronousSearchExceptionUtils(org.opensearch.search.asynchronous.utils.AsynchronousSearchExceptionUtils) ActionListener.wrap(org.opensearch.action.ActionListener.wrap) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) AsynchronousSearchActiveContext(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext) AsynchronousSearchContextPermits(org.opensearch.search.asynchronous.context.permits.AsynchronousSearchContextPermits) SearchAction(org.opensearch.action.search.SearchAction) LegacyOpendistroAsynchronousSearchSettings(org.opensearch.search.asynchronous.settings.LegacyOpendistroAsynchronousSearchSettings) PERSIST_SUCCEEDED(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.PERSIST_SUCCEEDED) ThreadPool(org.opensearch.threadpool.ThreadPool) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) AsynchronousSearchPlugin(org.opensearch.search.asynchronous.plugin.AsynchronousSearchPlugin) Releasable(org.opensearch.common.lease.Releasable) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) ClusterStateListener(org.opensearch.cluster.ClusterStateListener) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) UserAuthUtils.isUserValid(org.opensearch.search.asynchronous.utils.UserAuthUtils.isUserValid) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) BeginPersistEvent(org.opensearch.search.asynchronous.context.state.event.BeginPersistEvent) AsynchronousSearchStateMachine(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachine) UUIDs(org.opensearch.common.UUIDs) AsynchronousSearchContextEventListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchContextEventListener) SearchFailureEvent(org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent) AsynchronousSearchStateMachineClosedException(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachineClosedException) Setting(org.opensearch.common.settings.Setting) TaskId(org.opensearch.tasks.TaskId) SearchResponsePersistFailedEvent(org.opensearch.search.asynchronous.context.state.event.SearchResponsePersistFailedEvent) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) AtomicLong(java.util.concurrent.atomic.AtomicLong) FAILED(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.FAILED) INIT(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.INIT) User(org.opensearch.commons.authuser.User) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) ClusterService(org.opensearch.cluster.service.ClusterService) PERSIST_FAILED(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.PERSIST_FAILED) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) AsynchronousSearchStats(org.opensearch.search.asynchronous.stats.AsynchronousSearchStats) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) SearchResponsePersistFailedEvent(org.opensearch.search.asynchronous.context.state.event.SearchResponsePersistFailedEvent) SearchStartedEvent(org.opensearch.search.asynchronous.context.state.event.SearchStartedEvent) SearchSuccessfulEvent(org.opensearch.search.asynchronous.context.state.event.SearchSuccessfulEvent) SearchFailureEvent(org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent) AsynchronousSearchStateMachine(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachine) SearchResponsePersistedEvent(org.opensearch.search.asynchronous.context.state.event.SearchResponsePersistedEvent) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) AsynchronousSearchActiveContext(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext) BeginPersistEvent(org.opensearch.search.asynchronous.context.state.event.BeginPersistEvent) SearchDeletedEvent(org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent)

Example 2 with AsynchronousSearchState

use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState in project asynchronous-search by opensearch-project.

the class AsyncSearchBackwardsCompatibilityIT method testSubmitWithRetainedResponse.

public void testSubmitWithRetainedResponse(boolean shouldUseLegacyApi) throws IOException {
    SearchRequest searchRequest = new SearchRequest("test");
    searchRequest.source(new SearchSourceBuilder());
    SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
    submitAsynchronousSearchRequest.keepOnCompletion(true);
    submitAsynchronousSearchRequest.waitForCompletionTimeout(TimeValue.timeValueMillis(randomLongBetween(1, 500)));
    AsynchronousSearchResponse submitResponse = executeSubmitAsynchronousSearch(submitAsynchronousSearchRequest, shouldUseLegacyApi);
    List<AsynchronousSearchState> legalStates = Arrays.asList(AsynchronousSearchState.RUNNING, AsynchronousSearchState.SUCCEEDED, AsynchronousSearchState.PERSIST_SUCCEEDED, AsynchronousSearchState.PERSISTING, AsynchronousSearchState.CLOSED, AsynchronousSearchState.STORE_RESIDENT);
    assertNotNull(submitResponse.getId());
    assertTrue(submitResponse.getState().name(), legalStates.contains(submitResponse.getState()));
    GetAsynchronousSearchRequest getAsynchronousSearchRequest = new GetAsynchronousSearchRequest(submitResponse.getId());
    AsynchronousSearchResponse getResponse;
    do {
        getResponse = getAssertedAsynchronousSearchResponse(submitResponse, getAsynchronousSearchRequest, shouldUseLegacyApi);
        if (getResponse.getState() == AsynchronousSearchState.RUNNING && getResponse.getSearchResponse() != null) {
            assertEquals(getResponse.getSearchResponse().getHits().getHits().length, 0);
        } else {
            assertNotNull(getResponse.getSearchResponse());
            assertNotEquals(getResponse.getSearchResponse().getTook(), -1L);
        }
    } while (AsynchronousSearchState.STORE_RESIDENT.equals(getResponse.getState()) == false);
    getResponse = getAssertedAsynchronousSearchResponse(submitResponse, getAsynchronousSearchRequest, shouldUseLegacyApi);
    assertNotNull(getResponse.getSearchResponse());
    assertEquals(AsynchronousSearchState.STORE_RESIDENT, getResponse.getState());
    assertHitCount(getResponse.getSearchResponse(), 5);
    executeDeleteAsynchronousSearch(new DeleteAsynchronousSearchRequest(submitResponse.getId()), shouldUseLegacyApi);
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)

Example 3 with AsynchronousSearchState

use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState in project asynchronous-search by opensearch-project.

the class AsynchronousSearchResponse method innerFromXContent.

public static AsynchronousSearchResponse innerFromXContent(XContentParser parser) throws IOException {
    ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser);
    String id = null;
    AsynchronousSearchState status = null;
    long startTimeMillis = -1;
    long expirationTimeMillis = -1;
    SearchResponse searchResponse = null;
    OpenSearchException error = null;
    String currentFieldName = null;
    for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) {
        currentFieldName = parser.currentName();
        if (RESPONSE.match(currentFieldName, parser.getDeprecationHandler())) {
            if (token == XContentParser.Token.START_OBJECT) {
                ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
                searchResponse = SearchResponse.innerFromXContent(parser);
            }
        } else if (ERROR.match(currentFieldName, parser.getDeprecationHandler())) {
            parser.nextToken();
            error = OpenSearchException.fromXContent(parser);
        } else if (token.isValue()) {
            if (ID.match(currentFieldName, parser.getDeprecationHandler())) {
                id = parser.text();
            } else if (START_TIME_IN_MILLIS.match(currentFieldName, parser.getDeprecationHandler())) {
                startTimeMillis = parser.longValue();
            } else if (EXPIRATION_TIME_IN_MILLIS.match(currentFieldName, parser.getDeprecationHandler())) {
                expirationTimeMillis = parser.longValue();
            } else if (STATE.match(currentFieldName, parser.getDeprecationHandler())) {
                status = AsynchronousSearchState.valueOf(parser.text());
            } else {
                parser.skipChildren();
            }
        }
    }
    return new AsynchronousSearchResponse(id, status, startTimeMillis, expirationTimeMillis, searchResponse, error);
}
Also used : OpenSearchException(org.opensearch.OpenSearchException) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) XContentParser(org.opensearch.common.xcontent.XContentParser) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with AsynchronousSearchState

use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState in project asynchronous-search by opensearch-project.

the class AsynchronousSearchStateMachineTests method doConcurrentStateMachineTrigger.

private <T extends Exception, R extends Exception> void doConcurrentStateMachineTrigger(AsynchronousSearchStateMachine asStateMachine, AsynchronousSearchContextEvent event, AsynchronousSearchState finalState, Class<T> throwable, Optional<Class<R>> terminalStateException) throws InterruptedException, BrokenBarrierException {
    int numThreads = 10;
    List<Thread> operationThreads = new ArrayList<>();
    AtomicInteger numTriggerSuccess = new AtomicInteger();
    CyclicBarrier barrier = new CyclicBarrier(numThreads + 1);
    for (int i = 0; i < numThreads; i++) {
        Thread thread = new Thread(() -> {
            try {
                AsynchronousSearchState state = asStateMachine.trigger(event);
                assertEquals(state, finalState);
                numTriggerSuccess.getAndIncrement();
            } catch (Exception e) {
                if (terminalStateException.isPresent()) {
                    assertTrue(terminalStateException.get().isInstance(e) || throwable.isInstance(e));
                } else {
                    assertTrue(throwable.isInstance(e));
                }
            } finally {
                try {
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    fail("stage advancement failure");
                }
            }
        });
        operationThreads.add(thread);
    }
    operationThreads.forEach(Thread::start);
    barrier.await();
    for (Thread t : operationThreads) {
        t.join();
    }
    assertEquals(1, numTriggerSuccess.get());
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) OpenSearchException(org.opensearch.OpenSearchException) AsynchronousSearchStateMachineClosedException(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachineClosedException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 5 with AsynchronousSearchState

use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState in project asynchronous-search by opensearch-project.

the class ApiParamsValidationIT method testSubmitSearchOnInvalidIndex.

public void testSubmitSearchOnInvalidIndex() throws IOException {
    SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest("invalid-index"));
    AsynchronousSearchResponse submitResponse = executeSubmitAsynchronousSearch(submitAsynchronousSearchRequest);
    List<AsynchronousSearchState> legalStates = Arrays.asList(AsynchronousSearchState.FAILED, AsynchronousSearchState.CLOSED);
    assertNull(submitResponse.getSearchResponse());
    assertNotNull(submitResponse.getError());
    assertThat(submitResponse.getError().getDetailedMessage(), containsString("index_not_found"));
    assertTrue(legalStates.contains(submitResponse.getState()));
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState)

Aggregations

AsynchronousSearchState (org.opensearch.search.asynchronous.context.state.AsynchronousSearchState)10 SubmitAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest)8 SearchRequest (org.opensearch.action.search.SearchRequest)7 DeleteAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)7 GetAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest)7 AsynchronousSearchResponse (org.opensearch.search.asynchronous.response.AsynchronousSearchResponse)7 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)4 OpenSearchException (org.opensearch.OpenSearchException)2 AsynchronousSearchStateMachineClosedException (org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachineClosedException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1