Search in sources :

Example 1 with BeginPersistEvent

use of org.opensearch.search.asynchronous.context.state.event.BeginPersistEvent 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);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchSuccessfulEvent(org.opensearch.search.asynchronous.context.state.event.SearchSuccessfulEvent) SearchFailureEvent(org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent) TestThreadPool(org.opensearch.threadpool.TestThreadPool) SearchStartedEvent(org.opensearch.search.asynchronous.context.state.event.SearchStartedEvent) AsynchronousSearchStateMachine(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachine) BeginPersistEvent(org.opensearch.search.asynchronous.context.state.event.BeginPersistEvent) AsynchronousSearchStateMachineClosedException(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachineClosedException) TimeValue(org.opensearch.common.unit.TimeValue) SearchDeletedEvent(org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) AsynchronousSearchActiveContext(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchTask(org.opensearch.search.asynchronous.task.AsynchronousSearchTask) TestUtils.createClusterService(org.opensearch.search.asynchronous.utils.TestUtils.createClusterService) ClusterService(org.opensearch.cluster.service.ClusterService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 ClusterService (org.opensearch.cluster.service.ClusterService)1 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)1 TimeValue (org.opensearch.common.unit.TimeValue)1 AsynchronousSearchContextId (org.opensearch.search.asynchronous.context.AsynchronousSearchContextId)1 AsynchronousSearchActiveContext (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext)1 AsynchronousSearchActiveStore (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore)1 AsynchronousSearchStateMachine (org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachine)1 AsynchronousSearchStateMachineClosedException (org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachineClosedException)1 BeginPersistEvent (org.opensearch.search.asynchronous.context.state.event.BeginPersistEvent)1 SearchDeletedEvent (org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent)1 SearchFailureEvent (org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent)1 SearchStartedEvent (org.opensearch.search.asynchronous.context.state.event.SearchStartedEvent)1 SearchSuccessfulEvent (org.opensearch.search.asynchronous.context.state.event.SearchSuccessfulEvent)1 AsynchronousSearchProgressListener (org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener)1 AsynchronousSearchTask (org.opensearch.search.asynchronous.task.AsynchronousSearchTask)1 TestUtils.createClusterService (org.opensearch.search.asynchronous.utils.TestUtils.createClusterService)1 TestThreadPool (org.opensearch.threadpool.TestThreadPool)1