use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.CLOSED in project asynchronous-search by opensearch-project.
the class AsynchronousSearchService method findContext.
/**
* Tries to find an {@linkplain AsynchronousSearchActiveContext}. If not found,
* queries the {@linkplain AsynchronousSearchPersistenceService} for a hit. If a response is found, it builds and returns an
* {@linkplain AsynchronousSearchPersistenceContext}, else throws
* {@linkplain ResourceNotFoundException}
*
* @param id The asynchronous search id
* @param asynchronousSearchContextId the Async search context id
* @param user current user
* @param listener to be invoked on finding an {@linkplain AsynchronousSearchContext}
*/
public void findContext(String id, AsynchronousSearchContextId asynchronousSearchContextId, User user, ActionListener<AsynchronousSearchContext> listener) {
ActionListener<AsynchronousSearchContext> exceptionTranslationListener = getExceptionTranslationWrapper(id, listener);
Optional<AsynchronousSearchActiveContext> optionalAsynchronousSearchActiveContext = asynchronousSearchActiveStore.getContext(asynchronousSearchContextId);
// so most likely a CLOSED context is stale
if (optionalAsynchronousSearchActiveContext.isPresent() && optionalAsynchronousSearchActiveContext.get().isAlive()) {
logger.debug("Active context is present for asynchronous search ID [{}]", id);
AsynchronousSearchActiveContext asynchronousSearchActiveContext = optionalAsynchronousSearchActiveContext.get();
if (isUserValid(user, asynchronousSearchActiveContext.getUser()) == false) {
logger.debug("Invalid user requesting GET active context for asynchronous search id {}", id);
exceptionTranslationListener.onFailure(new OpenSearchSecurityException("User doesn't have necessary roles to access the asynchronous search with id " + id, RestStatus.FORBIDDEN));
} else {
exceptionTranslationListener.onResponse(asynchronousSearchActiveContext);
}
} else {
logger.debug("Active context is not present for asynchronous search ID [{}]", id);
persistenceService.getResponse(id, user, wrap((persistenceModel) -> exceptionTranslationListener.onResponse(new AsynchronousSearchPersistenceContext(id, asynchronousSearchContextId, persistenceModel, currentTimeSupplier, namedWriteableRegistry)), ex -> {
logger.debug(() -> new ParameterizedMessage("Context not found for ID in the system index {}", id), ex);
exceptionTranslationListener.onFailure(ex);
}));
}
}
use of org.opensearch.search.asynchronous.context.state.AsynchronousSearchState.CLOSED 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;
}
Aggregations