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