Search in sources :

Example 1 with AsynchronousSearchPersistenceService

use of org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPlugin method createComponents.

@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<RepositoriesService> repositoriesServiceSupplier) {
    this.persistenceService = new AsynchronousSearchPersistenceService(client, clusterService, threadPool);
    this.asynchronousSearchActiveStore = new AsynchronousSearchActiveStore(clusterService);
    this.asynchronousSearchService = new AsynchronousSearchService(persistenceService, asynchronousSearchActiveStore, client, clusterService, threadPool, new InternalAsynchronousSearchStats(), namedWriteableRegistry);
    return Arrays.asList(persistenceService, asynchronousSearchService);
}
Also used : AsynchronousSearchService(org.opensearch.search.asynchronous.service.AsynchronousSearchService) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore)

Example 2 with AsynchronousSearchPersistenceService

use of org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testConcurrentUpdatesAndDeletesRace.

public void testConcurrentUpdatesAndDeletesRace() throws InterruptedException {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    AsynchronousSearchResponse asResponse = submitAndGetPersistedAsynchronousSearchResponse();
    int numThreads = 200;
    List<Thread> threads = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(numThreads);
    AtomicInteger numDelete = new AtomicInteger();
    AtomicInteger numFailure = new AtomicInteger();
    AtomicInteger numDeleteAttempts = new AtomicInteger();
    AtomicInteger numDeleteFailedAttempts = new AtomicInteger();
    for (int i = 0; i < numThreads; i++) {
        final int iter = i;
        Thread t = new Thread(() -> {
            if (iter % 2 == 0 || iter < 20) /*letting few updates to queue up before starting to fire deletes*/
            {
                long expirationTimeMillis = System.currentTimeMillis() + timeValueDays(10).millis();
                persistenceService.updateExpirationTime(asResponse.getId(), expirationTimeMillis, null, new LatchedActionListener<>(ActionListener.wrap(r -> {
                    if (r.getExpirationTimeMillis() != expirationTimeMillis && r.getExpirationTimeMillis() != asResponse.getExpirationTimeMillis()) {
                        numFailure.getAndIncrement();
                    }
                }, e -> {
                    // rest all failures are unexpected
                    if (!(e instanceof VersionConflictEngineException) && !(e instanceof ResourceNotFoundException)) {
                        numFailure.getAndIncrement();
                    }
                }), latch));
            } else {
                numDeleteAttempts.getAndIncrement();
                persistenceService.deleteResponse(asResponse.getId(), null, new LatchedActionListener<>(ActionListener.wrap(r -> {
                    if (r) {
                        numDelete.getAndIncrement();
                    } else {
                        numFailure.getAndIncrement();
                    }
                }, e -> {
                    // acceptable. rest all failures are unexpected
                    if (e instanceof ResourceNotFoundException || e instanceof IllegalStateException) {
                        numDeleteFailedAttempts.getAndIncrement();
                    } else {
                        numFailure.getAndIncrement();
                    }
                }), latch));
            }
        });
        threads.add(t);
    }
    threads.forEach(Thread::start);
    latch.await();
    assertEquals(numFailure.get(), 0);
    assertEquals(numDeleteAttempts.get() - 1, numDeleteFailedAttempts.get());
    assertEquals(numDelete.get(), 1);
    for (Thread t : threads) {
        t.join();
    }
    expectThrows(ResourceNotFoundException.class, () -> executeDeleteAsynchronousSearch(client(), new DeleteAsynchronousSearchRequest(asResponse.getId())).actionGet());
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)

Example 3 with AsynchronousSearchPersistenceService

use of org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService 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)

Example 4 with AsynchronousSearchPersistenceService

use of org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testCreateResponseFailureOnClusterBlock.

public void testCreateResponseFailureOnClusterBlock() throws Exception {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUIDs.base64UUID(), randomInt(100));
    AsynchronousSearchId newAsynchronousSearchId = new AsynchronousSearchId(getInstanceFromNode(TransportService.class).getLocalNode().getId(), 1, asContextId);
    String id = AsynchronousSearchIdConverter.buildAsyncId(newAsynchronousSearchId);
    AsynchronousSearchResponse mockResponse = new AsynchronousSearchResponse(id, AsynchronousSearchState.STORE_RESIDENT, randomNonNegativeLong(), randomNonNegativeLong(), getMockSearchResponse(), null);
    createDoc(getInstanceFromNode(AsynchronousSearchPersistenceService.class), mockResponse, null);
    client().admin().indices().prepareUpdateSettings(AsynchronousSearchPersistenceService.ASYNC_SEARCH_RESPONSE_INDEX).setSettings(Settings.builder().put(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE, true).build()).execute().actionGet();
    SearchRequest searchRequest = new SearchRequest().indices("index").source(new SearchSourceBuilder());
    SubmitAsynchronousSearchRequest request = new SubmitAsynchronousSearchRequest(searchRequest);
    request.keepOnCompletion(true);
    request.waitForCompletionTimeout(TimeValue.timeValueMillis(5000));
    AsynchronousSearchResponse asResponse = TestClientUtils.blockingSubmitAsynchronousSearch(client(), request);
    waitUntil(() -> assertRnf(() -> TestClientUtils.blockingGetAsynchronousSearchResponse(client(), new GetAsynchronousSearchRequest(asResponse.getId()))));
    assertRnf(() -> TestClientUtils.blockingGetAsynchronousSearchResponse(client(), new GetAsynchronousSearchRequest(id)));
    client().admin().indices().prepareUpdateSettings(AsynchronousSearchPersistenceService.ASYNC_SEARCH_RESPONSE_INDEX).setSettings(Settings.builder().putNull(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE).build()).execute().actionGet();
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) 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) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) TransportService(org.opensearch.transport.TransportService) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with AsynchronousSearchPersistenceService

use of org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testCreateAndGetAndDelete.

public void testCreateAndGetAndDelete() throws IOException, InterruptedException {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    TransportService transportService = getInstanceFromNode(TransportService.class);
    AsynchronousSearchResponse asResponse = submitAndGetPersistedAsynchronousSearchResponse();
    AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUIDs.base64UUID(), randomInt(100));
    AsynchronousSearchId newAsynchronousSearchId = new AsynchronousSearchId(transportService.getLocalNode().getId(), 1, asContextId);
    String id = AsynchronousSearchIdConverter.buildAsyncId(newAsynchronousSearchId);
    User user1 = TestClientUtils.randomUser();
    User user2 = TestClientUtils.randomUser();
    for (User user : Arrays.asList(user1, null)) {
        AsynchronousSearchResponse newAsynchronousSearchResponse = new AsynchronousSearchResponse(id, AsynchronousSearchState.STORE_RESIDENT, asResponse.getStartTimeMillis(), asResponse.getExpirationTimeMillis(), asResponse.getSearchResponse(), asResponse.getError());
        createDoc(persistenceService, newAsynchronousSearchResponse, user);
        if (user != null) {
            CountDownLatch getLatch1 = new CountDownLatch(1);
            ActionListener<AsynchronousSearchPersistenceModel> getListener = ActionListener.wrap(r -> fail("Expected exception. Got " + r), e -> assertTrue(e instanceof OpenSearchSecurityException));
            persistenceService.getResponse(newAsynchronousSearchResponse.getId(), user2, new LatchedActionListener<>(getListener, getLatch1));
            getLatch1.await();
        }
        CountDownLatch getLatch2 = new CountDownLatch(1);
        persistenceService.getResponse(newAsynchronousSearchResponse.getId(), user, new LatchedActionListener<>(ActionListener.wrap(r -> assertEquals(new AsynchronousSearchPersistenceModel(asResponse.getStartTimeMillis(), asResponse.getExpirationTimeMillis(), asResponse.getSearchResponse(), null, user), r), e -> {
            logger.error("Expected get result got ", e);
            fail(e.getMessage());
        }), getLatch2));
        getLatch2.await();
        if (user != null) {
            CountDownLatch deleteLatch1 = new CountDownLatch(1);
            User diffUser = TestClientUtils.randomUser();
            ActionListener<Boolean> deleteListener = ActionListener.wrap(r -> fail("Expected exception on delete. Got acknowledgment" + r), e -> assertTrue(e instanceof OpenSearchSecurityException));
            persistenceService.deleteResponse(newAsynchronousSearchResponse.getId(), diffUser, new LatchedActionListener<>(deleteListener, deleteLatch1));
            deleteLatch1.await();
        }
        CountDownLatch deleteLatch2 = new CountDownLatch(1);
        ActionListener<Boolean> deleteListener = ActionListener.wrap(Assert::assertTrue, e -> {
            logger.debug(() -> new ParameterizedMessage("Delete failed unexpectedly "), e);
            fail("delete failed.expected success");
        });
        persistenceService.deleteResponse(newAsynchronousSearchResponse.getId(), user, new LatchedActionListener<>(deleteListener, deleteLatch2));
        deleteLatch2.await();
        // assert failure
        CountDownLatch getLatch3 = new CountDownLatch(2);
        ActionListener<AsynchronousSearchPersistenceModel> getListener = ActionListener.wrap((r) -> fail("Expected RNF, Got " + r), exception -> assertTrue(exception instanceof ResourceNotFoundException));
        persistenceService.getResponse(newAsynchronousSearchResponse.getId(), null, new LatchedActionListener<>(getListener, getLatch3));
        persistenceService.getResponse(newAsynchronousSearchResponse.getId(), user2, new LatchedActionListener<>(getListener, getLatch3));
        getLatch3.await();
    }
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) User(org.opensearch.commons.authuser.User) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) CountDownLatch(java.util.concurrent.CountDownLatch) Assert(org.junit.Assert) TransportService(org.opensearch.transport.TransportService) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) AsynchronousSearchPersistenceModel(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel)

Aggregations

AsynchronousSearchPersistenceService (org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService)10 AsynchronousSearchResponse (org.opensearch.search.asynchronous.response.AsynchronousSearchResponse)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)6 AsynchronousSearchPersistenceModel (org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel)5 DeleteAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)5 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 OpenSearchSecurityException (org.opensearch.OpenSearchSecurityException)4 TimeValue (org.opensearch.common.unit.TimeValue)4 User (org.opensearch.commons.authuser.User)4 VersionConflictEngineException (org.opensearch.index.engine.VersionConflictEngineException)4 AsynchronousSearchId (org.opensearch.search.asynchronous.id.AsynchronousSearchId)4 SearchRequest (org.opensearch.action.search.SearchRequest)3 AsynchronousSearchContextId (org.opensearch.search.asynchronous.context.AsynchronousSearchContextId)3 TransportService (org.opensearch.transport.TransportService)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 Assert (org.junit.Assert)2