use of org.opensearch.threadpool.ThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchRejectionIT method testSearchFailures.
public void testSearchFailures() throws Exception {
for (int i = 0; i < 10; i++) {
client().prepareIndex("test").setId(Integer.toString(i)).setSource("field", "1").get();
}
int numberOfAsyncOps = randomIntBetween(100, 200);
final CountDownLatch latch = new CountDownLatch(numberOfAsyncOps);
final CopyOnWriteArrayList<Object> responses = new CopyOnWriteArrayList<>();
TestThreadPool threadPool = null;
try {
threadPool = new TestThreadPool(AsynchronousSearchProgressListenerIT.class.getName());
for (int i = 0; i < numberOfAsyncOps; i++) {
SearchRequest request = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field", "1")).request();
AtomicReference<SearchResponse> responseRef = new AtomicReference<>();
AtomicInteger reduceContextInvocation = new AtomicInteger();
AsynchronousSearchProgressListener listener;
SearchService service = internalCluster().getInstance(SearchService.class);
InternalAggregation.ReduceContextBuilder reduceContextBuilder = service.aggReduceContextBuilder(request);
AtomicReference<Exception> exceptionRef = new AtomicReference<>();
Function<SearchResponse, AsynchronousSearchResponse> responseFunction = (r) -> null;
Function<Exception, AsynchronousSearchResponse> failureFunction = (e) -> null;
listener = new AsynchronousSearchProgressListener(threadPool.relativeTimeInMillis(), responseFunction, failureFunction, threadPool.generic(), threadPool::relativeTimeInMillis, () -> reduceContextBuilder) {
@Override
public void onResponse(SearchResponse searchResponse) {
assertTrue(responseRef.compareAndSet(null, searchResponse));
AsynchronousSearchAssertions.assertSearchResponses(searchResponse, this.partialResponse());
latch.countDown();
}
@Override
public void onFailure(Exception exception) {
assertTrue(exceptionRef.compareAndSet(null, exception));
latch.countDown();
}
};
client().execute(SearchAction.INSTANCE, new SearchRequest(request) {
@Override
public SearchTask createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
SearchTask task = super.createTask(id, type, action, parentTaskId, headers);
task.setProgressListener(listener);
return task;
}
}, listener);
}
latch.await();
} finally {
ThreadPool.terminate(threadPool, 100, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.threadpool.ThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchPartialResponseIT method testCase.
private void testCase(Client client, SearchRequest request) throws Exception {
AtomicReference<SearchResponse> responseRef = new AtomicReference<>();
AtomicInteger reduceContextInvocation = new AtomicInteger();
TestThreadPool threadPool = null;
AsynchronousSearchProgressListener listener;
try {
threadPool = new TestThreadPool(AsynchronousSearchProgressListenerIT.class.getName());
SearchService service = internalCluster().getInstance(SearchService.class);
InternalAggregation.ReduceContextBuilder reduceContextBuilder = service.aggReduceContextBuilder(request);
AtomicReference<Exception> exceptionRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
Function<SearchResponse, AsynchronousSearchResponse> responseFunction = (r) -> null;
Function<Exception, AsynchronousSearchResponse> failureFunction = (e) -> null;
listener = new AsynchronousSearchProgressListener(threadPool.relativeTimeInMillis(), responseFunction, failureFunction, threadPool.generic(), threadPool::relativeTimeInMillis, () -> reduceContextBuilder) {
@Override
public void onResponse(SearchResponse searchResponse) {
assertTrue(responseRef.compareAndSet(null, searchResponse));
AsynchronousSearchAssertions.assertSearchResponses(responseRef.get(), this.partialResponse());
latch.countDown();
}
@Override
protected void onPartialReduce(List<SearchShard> shards, TotalHits totalHits, InternalAggregations aggs, int reducePhase) {
super.onPartialReduce(shards, totalHits, aggs, reducePhase);
Terms terms = this.partialResponse().getAggregations().get("keys");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), lessThanOrEqualTo(aggregationSize));
reduceContextInvocation.incrementAndGet();
}
@Override
protected void onFinalReduce(List<SearchShard> shards, TotalHits totalHits, InternalAggregations aggs, int reducePhase) {
super.onFinalReduce(shards, totalHits, aggs, reducePhase);
Terms terms = this.partialResponse().getAggregations().get("keys");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(aggregationSize));
}
@Override
public void onFailure(Exception exception) {
assertTrue(exceptionRef.compareAndSet(null, exception));
latch.countDown();
}
};
client.execute(SearchAction.INSTANCE, new SearchRequest(request) {
@Override
public SearchTask createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
SearchTask task = super.createTask(id, type, action, parentTaskId, headers);
task.setProgressListener(listener);
return task;
}
}, listener);
latch.await();
Terms terms = responseRef.get().getAggregations().get("keys");
assertThat(reduceContextInvocation.get(), equalTo(responseRef.get().getNumReducePhases() - 1));
List<? extends Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(aggregationSize));
} finally {
ThreadPool.terminate(threadPool, 100, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.threadpool.ThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchActiveStoreTests method testGetNonExistentContext.
public void testGetNonExistentContext() {
DiscoveryNode node = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
ThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(this.getClass().getName(), executorBuilder);
ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, node, clusterSettings);
AsynchronousSearchActiveStore activeStore = new AsynchronousSearchActiveStore(mockClusterService);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
Optional<AsynchronousSearchActiveContext> optional = activeStore.getContext(asContextId);
assertFalse(optional.isPresent());
assertEquals(activeStore.getAllContexts().size(), 0);
} finally {
ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.ThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchServiceFreeContextTests method testFreeContextPermitAcquisitionFailure.
public void testFreeContextPermitAcquisitionFailure() 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 mockStore = mock(AsynchronousSearchActiveStore.class);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(mockClient, mockClusterService, testThreadPool);
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, mockStore, mockClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
TimeValue keepAlive = timeValueDays(9);
boolean keepOnCompletion = true;
User user1 = randomBoolean() ? randomUser() : null;
SearchRequest searchRequest = new SearchRequest();
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
submitAsynchronousSearchRequest.keepAlive(keepAlive);
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(testThreadPool);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
MockAsynchronousSearchActiveContext asActiveContext = new MockAsynchronousSearchActiveContext(asContextId, discoveryNode.getId(), keepAlive, true, testThreadPool, testThreadPool::absoluteTimeInMillis, asProgressListener, user1);
// bootstrap search
AsynchronousSearchTask task = new AsynchronousSearchTask(randomNonNegativeLong(), "transport", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap(), asActiveContext, null, (c) -> {
});
asActiveContext.setTask(task);
simulateUncheckedException = true;
persisted = false;
when(mockStore.getContext(asContextId)).thenReturn(Optional.of(asActiveContext));
CountDownLatch latch = new CountDownLatch(1);
asService.freeContext(asActiveContext.getAsynchronousSearchId(), asActiveContext.getContextId(), null, new LatchedActionListener<>(wrap(r -> fail("Expected resource_not_found_exception. Got acknowledgement " + r), e -> {
assertTrue(e.getClass().getName(), e instanceof ResourceNotFoundException);
}), latch));
latch.await();
assertEquals(1, (int) mockClient.deleteCount);
mockClusterService.stop();
} finally {
ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.ThreadPool 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);
}
}
Aggregations