Search in sources :

Example 1 with AsynchronousSearchId

use of org.opensearch.search.asynchronous.id.AsynchronousSearchId in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceContextTests method testSerializationRoundTripWithSearchResponse.

/**
 * asynchronous search persistence context serializes search response into {@linkplain BytesReference}. We verify that de-serializing
 * the{@linkplain BytesReference} yields the same object.@throws IOException when there is a serialization issue
 */
public void testSerializationRoundTripWithSearchResponse() throws IOException {
    AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
    String id = AsynchronousSearchIdConverter.buildAsyncId(new AsynchronousSearchId(UUID.randomUUID().toString(), randomNonNegativeLong(), asContextId));
    long expirationTimeMillis = randomNonNegativeLong();
    long startTimeMillis = randomNonNegativeLong();
    SearchResponse searchResponse = getMockSearchResponse();
    User user = TestClientUtils.randomUserOrNull();
    SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
    AsynchronousSearchPersistenceContext asPersistenceContext = new AsynchronousSearchPersistenceContext(id, asContextId, new AsynchronousSearchPersistenceModel(startTimeMillis, expirationTimeMillis, searchResponse, null, user), System::currentTimeMillis, new NamedWriteableRegistry(searchModule.getNamedWriteables()));
    assertEquals(asPersistenceContext, new AsynchronousSearchPersistenceContext(id, asContextId, new AsynchronousSearchPersistenceModel(startTimeMillis, expirationTimeMillis, searchResponse, null, user), System::currentTimeMillis, new NamedWriteableRegistry(Collections.emptyList())));
    assertEquals(asPersistenceContext.getAsynchronousSearchResponse(), new AsynchronousSearchResponse(id, asPersistenceContext.getAsynchronousSearchState(), startTimeMillis, expirationTimeMillis, searchResponse, null));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) User(org.opensearch.commons.authuser.User) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) SearchModule(org.opensearch.search.SearchModule) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 2 with AsynchronousSearchId

use of org.opensearch.search.asynchronous.id.AsynchronousSearchId in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceContextTests method testSerializationRoundTripWithError.

/**
 * asynchronous search persistence model serializes exception into {@linkplain BytesReference}. We verify that de-serializing the
 * {@linkplain BytesReference} yields the same object.
 *
 * @throws IOException when there is a serialization issue
 */
public void testSerializationRoundTripWithError() throws IOException {
    AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
    String id = AsynchronousSearchIdConverter.buildAsyncId(new AsynchronousSearchId(UUID.randomUUID().toString(), randomNonNegativeLong(), asContextId));
    long expirationTimeMillis = randomNonNegativeLong();
    long startTimeMillis = randomNonNegativeLong();
    ShardSearchFailure shardSearchFailure = new ShardSearchFailure(new RuntimeException("runtime-exception"));
    SearchPhaseExecutionException exception = new SearchPhaseExecutionException("phase", "msg", new NullPointerException(), new ShardSearchFailure[] { shardSearchFailure });
    User user = TestClientUtils.randomUser();
    SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
    AsynchronousSearchPersistenceContext asPersistenceContext = new AsynchronousSearchPersistenceContext(id, asContextId, new AsynchronousSearchPersistenceModel(startTimeMillis, expirationTimeMillis, null, exception, user), System::currentTimeMillis, new NamedWriteableRegistry(searchModule.getNamedWriteables()));
    OpenSearchException deserializedException = asPersistenceContext.getAsynchronousSearchResponse().getError();
    assertTrue(deserializedException instanceof SearchPhaseExecutionException);
    assertEquals("phase", ((SearchPhaseExecutionException) deserializedException).getPhaseName());
    assertEquals("msg", deserializedException.getMessage());
    assertTrue(deserializedException.getCause() instanceof NullPointerException);
    assertEquals(1, ((SearchPhaseExecutionException) deserializedException).shardFailures().length);
    assertTrue(((SearchPhaseExecutionException) deserializedException).shardFailures()[0].getCause() instanceof RuntimeException);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) User(org.opensearch.commons.authuser.User) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) SearchModule(org.opensearch.search.SearchModule) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) OpenSearchException(org.opensearch.OpenSearchException) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 3 with AsynchronousSearchId

use of org.opensearch.search.asynchronous.id.AsynchronousSearchId in project asynchronous-search by opensearch-project.

the class AsynchronousSearchServiceFreeContextTests method testFreePersistedContextUserNotMatches.

public void testFreePersistedContextUserNotMatches() throws InterruptedException {
    DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    ThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, executorBuilder);
        ClusterService mockClusterService = getClusterService(discoveryNode, testThreadPool);
        MockClient mockClient = new MockClient(testThreadPool);
        AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
        AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(mockClient, mockClusterService, testThreadPool);
        AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, mockClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
        AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
        AsynchronousSearchId asId = new AsynchronousSearchId(discoveryNode.getId(), randomNonNegativeLong(), asContextId);
        persisted = true;
        userMatches = false;
        CountDownLatch latch = new CountDownLatch(1);
        asService.freeContext(AsynchronousSearchIdConverter.buildAsyncId(asId), asContextId, randomUser(), new LatchedActionListener<>(ActionListener.wrap(r -> {
            fail("Expected resource_not_found_exception due to user mismatch security exception. received delete " + "acknowledgement : " + r);
        }, e -> assertTrue("expected resource_not_found_exception got " + e.getClass().getName(), e instanceof ResourceNotFoundException)), latch));
        latch.await();
        mockClusterService.stop();
    } finally {
        ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) ClusterService(org.opensearch.cluster.service.ClusterService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) ResourceNotFoundException(org.opensearch.ResourceNotFoundException)

Example 4 with AsynchronousSearchId

use of org.opensearch.search.asynchronous.id.AsynchronousSearchId in project asynchronous-search by opensearch-project.

the class AsynchronousSearchServiceFreeContextTests method testFreePersistedContextUserMatches.

public void testFreePersistedContextUserMatches() throws InterruptedException {
    DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    ThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, executorBuilder);
        ClusterService mockClusterService = getClusterService(discoveryNode, testThreadPool);
        MockClient mockClient = new MockClient(testThreadPool);
        AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
        AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(mockClient, mockClusterService, testThreadPool);
        AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, mockClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
        AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
        AsynchronousSearchId asId = new AsynchronousSearchId(discoveryNode.getId(), randomNonNegativeLong(), asContextId);
        persisted = true;
        CountDownLatch latch = new CountDownLatch(1);
        asService.freeContext(AsynchronousSearchIdConverter.buildAsyncId(asId), asContextId, null, new LatchedActionListener<>(ActionListener.wrap(Assert::assertTrue, e -> fail("expected successful delete because persistence is true. but got " + e.getMessage())), latch));
        latch.await();
        mockClusterService.stop();
    } finally {
        ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) ClusterService(org.opensearch.cluster.service.ClusterService) Assert(org.junit.Assert) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId)

Example 5 with AsynchronousSearchId

use of org.opensearch.search.asynchronous.id.AsynchronousSearchId in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testGetAndDeleteNonExistentId.

public void testGetAndDeleteNonExistentId() throws InterruptedException, IOException, ExecutionException {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    TransportService transportService = getInstanceFromNode(TransportService.class);
    SearchResponse searchResponse = client().search(new SearchRequest(TEST_INDEX)).get();
    User user1 = TestClientUtils.randomUser();
    User user2 = TestClientUtils.randomUser();
    for (User originalUser : Arrays.asList(user1, null)) {
        AsynchronousSearchId asId = generateNewAsynchronousSearchId(transportService);
        AsynchronousSearchPersistenceModel model1 = new AsynchronousSearchPersistenceModel(System.currentTimeMillis(), System.currentTimeMillis() + new TimeValue(10, TimeUnit.DAYS).getMillis(), searchResponse, null, originalUser);
        CountDownLatch createLatch = new CountDownLatch(1);
        String id = AsynchronousSearchIdConverter.buildAsyncId(asId);
        persistenceService.storeResponse(id, model1, new LatchedActionListener<>(ActionListener.wrap(r -> assertSuccessfulResponseCreation(id, r), e -> {
            logger.debug("expect successful create, got", e);
            fail("Expected successful create, got " + e.getMessage());
        }), createLatch));
        createLatch.await();
        for (User currentuser : Arrays.asList(originalUser, user2)) {
            CountDownLatch latch = new CountDownLatch(2);
            // assert failure
            persistenceService.getResponse("id", currentuser, new LatchedActionListener<>(ActionListener.wrap((AsynchronousSearchPersistenceModel r) -> fail("Excepted resource_not_found_exception, got " + r), exception -> assertTrue("Expected resource_not_found expection, got " + exception.getClass().toString(), exception instanceof ResourceNotFoundException)), latch));
            // assert failure
            ActionListener<Boolean> wrap = ActionListener.wrap(r -> fail("Expected resource_not_found expection on delete, got acknowledgement " + r), ex -> assertTrue("Expected resource_not_found expection, got " + ex.getClass().toString(), ex instanceof ResourceNotFoundException));
            persistenceService.deleteResponse("id", currentuser, new LatchedActionListener<>(wrap, latch));
            latch.await();
        }
    }
}
Also used : 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) User(org.opensearch.commons.authuser.User) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) TransportService(org.opensearch.transport.TransportService) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) AsynchronousSearchPersistenceModel(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

AsynchronousSearchId (org.opensearch.search.asynchronous.id.AsynchronousSearchId)12 AsynchronousSearchResponse (org.opensearch.search.asynchronous.response.AsynchronousSearchResponse)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AsynchronousSearchContextId (org.opensearch.search.asynchronous.context.AsynchronousSearchContextId)6 SearchRequest (org.opensearch.action.search.SearchRequest)5 ClusterService (org.opensearch.cluster.service.ClusterService)5 DeleteAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)5 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)4 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)4 User (org.opensearch.commons.authuser.User)4 GetAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest)4 AcknowledgedResponse (org.opensearch.search.asynchronous.response.AcknowledgedResponse)4 TestThreadPool (org.opensearch.threadpool.TestThreadPool)4 ExecutionException (java.util.concurrent.ExecutionException)3 Assert (org.junit.Assert)3 ActionListener (org.opensearch.action.ActionListener)3 LatchedActionListener (org.opensearch.action.LatchedActionListener)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 AsynchronousSearchPersistenceService (org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService)3 TransportService (org.opensearch.transport.TransportService)3