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);
}
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());
}
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();
}
}
}
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();
}
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();
}
}
Aggregations