use of org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent in project asynchronous-search by opensearch-project.
the class AsynchronousSearchPostProcessorTests method testProcessSearchFailureOnDeletedContext.
public void testProcessSearchFailureOnDeletedContext() throws AsynchronousSearchStateMachineClosedException {
DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
AtomicBoolean activeContextCleanUpConsumerInvocation = new AtomicBoolean();
ThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(this.getClass().getName(), executorBuilder);
ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, discoveryNode, clusterSettings);
FakeClient fakeClient = new FakeClient(testThreadPool);
AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(fakeClient, mockClusterService, testThreadPool);
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, fakeClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
AsynchronousSearchStateMachine asStateMachine = asService.getStateMachine();
ClusterService clusterService = ClusterServiceUtils.createClusterService(testThreadPool, discoveryNode, clusterSettings);
AsynchronousSearchPostProcessor postProcessor = new AsynchronousSearchPostProcessor(persistenceService, asActiveStore, asStateMachine, (context) -> activeContextCleanUpConsumerInvocation.compareAndSet(false, true), testThreadPool, clusterService);
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest());
submitAsynchronousSearchRequest.keepOnCompletion(true);
submitAsynchronousSearchRequest.keepAlive(TimeValue.timeValueHours(1));
AsynchronousSearchActiveContext context = (AsynchronousSearchActiveContext) asService.createAndStoreContext(submitAsynchronousSearchRequest, System.currentTimeMillis(), () -> InternalAggregationTestCase.emptyReduceContextBuilder(), null);
ShardSearchFailure shardSearchFailure = new ShardSearchFailure(new RuntimeException("runtime-exception"));
SearchPhaseExecutionException exception = new SearchPhaseExecutionException("phase", "msg", new NullPointerException(), new ShardSearchFailure[] { shardSearchFailure });
asStateMachine.trigger(new SearchStartedEvent(context, new SearchTask(0, "n/a", "n/a", () -> "test", null, Collections.emptyMap())));
asStateMachine.trigger(new SearchDeletedEvent(context));
AsynchronousSearchResponse asResponse = postProcessor.processSearchFailure(exception, context.getContextId());
assertNull(asResponse.getId());
assertNull(asResponse.getSearchResponse());
assertEquals(-1L, asResponse.getExpirationTimeMillis());
assertEquals(-1L, asResponse.getStartTimeMillis());
assertEquals(AsynchronousSearchState.FAILED, asResponse.getState());
assertThat(asResponse.getError(), instanceOf(SearchPhaseExecutionException.class));
assertFalse(activeContextCleanUpConsumerInvocation.get());
assertEquals(0, fakeClient.persistenceCount);
} finally {
ThreadPool.terminate(testThreadPool, 200, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent 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.SearchDeletedEvent in project asynchronous-search by opensearch-project.
the class AsynchronousSearchPostProcessorTests method testProcessSearchResponseOnClosedContext.
public void testProcessSearchResponseOnClosedContext() throws AsynchronousSearchStateMachineClosedException {
DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
AtomicBoolean activeContextCleanUpConsumerInvocation = new AtomicBoolean();
ThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(this.getClass().getName(), executorBuilder);
ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, discoveryNode, clusterSettings);
FakeClient fakeClient = new FakeClient(testThreadPool);
AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(fakeClient, mockClusterService, testThreadPool);
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, fakeClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
AsynchronousSearchStateMachine asStateMachine = asService.getStateMachine();
ClusterService clusterService = ClusterServiceUtils.createClusterService(testThreadPool, discoveryNode, clusterSettings);
AsynchronousSearchPostProcessor postProcessor = new AsynchronousSearchPostProcessor(persistenceService, asActiveStore, asStateMachine, (context) -> activeContextCleanUpConsumerInvocation.compareAndSet(false, true), testThreadPool, clusterService);
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest());
submitAsynchronousSearchRequest.keepOnCompletion(true);
submitAsynchronousSearchRequest.keepAlive(TimeValue.timeValueHours(1));
AsynchronousSearchActiveContext context = (AsynchronousSearchActiveContext) asService.createAndStoreContext(submitAsynchronousSearchRequest, System.currentTimeMillis(), () -> InternalAggregationTestCase.emptyReduceContextBuilder(), null);
asStateMachine.trigger(new SearchStartedEvent(context, new SearchTask(0, "n/a", "n/a", () -> "test", null, Collections.emptyMap())));
asStateMachine.trigger(new SearchDeletedEvent(context));
SearchResponse mockSearchResponse = getMockSearchResponse();
AsynchronousSearchResponse asResponse = postProcessor.processSearchResponse(mockSearchResponse, context.getContextId());
assertNull(asResponse.getId());
assertNull(asResponse.getError());
assertEquals(-1L, asResponse.getExpirationTimeMillis());
assertEquals(AsynchronousSearchState.SUCCEEDED, asResponse.getState());
assertEquals(-1L, asResponse.getStartTimeMillis());
AsynchronousSearchAssertions.assertSearchResponses(mockSearchResponse, asResponse.getSearchResponse());
assertFalse(activeContextCleanUpConsumerInvocation.get());
assertEquals(0, fakeClient.persistenceCount);
} finally {
ThreadPool.terminate(testThreadPool, 200, TimeUnit.MILLISECONDS);
}
}
Aggregations