use of org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent in project asynchronous-search by opensearch-project.
the class AsynchronousSearchStateMachineTests method testStateMachine.
public void testStateMachine() throws InterruptedException, BrokenBarrierException {
DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
TestThreadPool threadPool = null;
try {
threadPool = new TestThreadPool("test", executorBuilder);
ClusterService mockClusterService = createClusterService(settings, threadPool, discoveryNode, clusterSettings);
FakeClient fakeClient = new FakeClient(threadPool);
AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(fakeClient, mockClusterService, threadPool);
CustomContextListener customContextListener = new CustomContextListener();
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, fakeClient, mockClusterService, threadPool, customContextListener, new NamedWriteableRegistry(emptyList()));
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(threadPool);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
TimeValue keepAlive = TimeValue.timeValueDays(randomInt(100));
AsynchronousSearchActiveContext context = new AsynchronousSearchActiveContext(asContextId, discoveryNode.getId(), keepAlive, true, threadPool, threadPool::absoluteTimeInMillis, asProgressListener, null, () -> true);
assertNull(context.getTask());
assertEquals(context.getAsynchronousSearchState(), INIT);
AsynchronousSearchStateMachine stateMachine = asService.getStateMachine();
AtomicInteger numCompleted = new AtomicInteger();
AtomicInteger numFailure = new AtomicInteger();
doConcurrentStateMachineTrigger(stateMachine, new SearchStartedEvent(context, new AsynchronousSearchTask(randomNonNegativeLong(), "transport", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap(), context, null, (a) -> {
})), RUNNING, IllegalStateException.class, Optional.empty());
boolean success = randomBoolean();
assertNotNull(context.getTask());
if (randomBoolean()) {
// delete running context
doConcurrentStateMachineTrigger(stateMachine, new SearchDeletedEvent(context), CLOSED, AsynchronousSearchStateMachineClosedException.class, Optional.empty());
} else {
if (success) {
doConcurrentStateMachineTrigger(stateMachine, new SearchFailureEvent(context, new RuntimeException("test")), FAILED, IllegalStateException.class, Optional.empty());
numFailure.getAndIncrement();
} else {
// success or failure
doConcurrentStateMachineTrigger(stateMachine, new SearchSuccessfulEvent(context, getMockSearchResponse()), SUCCEEDED, IllegalStateException.class, Optional.empty());
numCompleted.getAndIncrement();
}
doConcurrentStateMachineTrigger(stateMachine, new BeginPersistEvent(context), PERSISTING, IllegalStateException.class, Optional.of(AsynchronousSearchStateMachineClosedException.class));
waitUntil(() -> context.getAsynchronousSearchState().equals(CLOSED), 1, TimeUnit.MINUTES);
assertTrue(context.getAsynchronousSearchState().toString() + " numFailure : " + numFailure.get() + " numSuccess : " + numCompleted.get(), context.getAsynchronousSearchState().equals(CLOSED));
assertEquals(1, customContextListener.getPersistedCount() + customContextListener.getPersistFailedCount());
}
assertEquals(numCompleted.get(), customContextListener.getCompletedCount());
assertEquals(numFailure.get(), customContextListener.getFailedCount());
assertEquals("success:" + success, 0, customContextListener.getRunningCount());
assertEquals(1, customContextListener.getDeletedCount());
} finally {
ThreadPool.terminate(threadPool, 100, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent in project asynchronous-search by opensearch-project.
the class AsynchronousSearchPostProcessor method processSearchFailure.
public AsynchronousSearchResponse processSearchFailure(Exception exception, AsynchronousSearchContextId asynchronousSearchContextId) {
final Optional<AsynchronousSearchActiveContext> asynchronousSearchContextOptional = asynchronousSearchActiveStore.getContext(asynchronousSearchContextId);
try {
if (asynchronousSearchContextOptional.isPresent()) {
AsynchronousSearchActiveContext asynchronousSearchContext = asynchronousSearchContextOptional.get();
asynchronousSearchStateMachine.trigger(new SearchFailureEvent(asynchronousSearchContext, exception));
handlePersist(asynchronousSearchContext);
return asynchronousSearchContext.getAsynchronousSearchResponse();
}
// Best effort to return the response.
return new AsynchronousSearchResponse(AsynchronousSearchState.FAILED, -1L, -1L, null, ExceptionsHelper.convertToOpenSearchException(exception));
} catch (AsynchronousSearchStateMachineClosedException ex) {
// Best effort to return the response.
return new AsynchronousSearchResponse(AsynchronousSearchState.FAILED, -1L, -1L, null, ExceptionsHelper.convertToOpenSearchException(exception));
}
}
Aggregations