use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchActiveContextTests method testProcessSearchCompletion.
public void testProcessSearchCompletion() throws InterruptedException {
TestThreadPool threadPool = null;
try {
int writeThreadPoolSize = randomIntBetween(1, 2);
int writeThreadPoolQueueSize = randomIntBetween(1, 2);
Settings settings = Settings.builder().put("thread_pool." + AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME + ".size", writeThreadPoolSize).put("thread_pool." + AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME + ".queue_size", writeThreadPoolQueueSize).build();
final int availableProcessors = OpenSearchExecutors.allocatedProcessors(settings);
ScalingExecutorBuilder scalingExecutorBuilder = new ScalingExecutorBuilder(AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, 1, Math.min(2 * availableProcessors, Math.max(128, 512)), TimeValue.timeValueMinutes(30));
threadPool = new TestThreadPool("Tests", settings, scalingExecutorBuilder);
String node = UUID.randomUUID().toString();
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(threadPool);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
boolean keepOnCompletion = true;
TimeValue keepAlive = TimeValue.timeValueDays(randomInt(100));
AsynchronousSearchActiveContext context = new AsynchronousSearchActiveContext(asContextId, node, keepAlive, keepOnCompletion, threadPool, threadPool::absoluteTimeInMillis, asProgressListener, null, () -> true);
if (randomBoolean()) {
SearchResponse mockSearchResponse = getMockSearchResponse();
try {
context.processSearchResponse(mockSearchResponse);
} catch (Exception ex) {
fail("Unexpected exception " + ex);
}
if (mockSearchResponse.equals(context.getSearchResponse())) {
assertNull(context.getSearchError());
}
} else {
RuntimeException e = new RuntimeException(UUID.randomUUID().toString());
try {
context.processSearchFailure(e);
} catch (Exception ex) {
fail("Unexpected exception " + ex);
}
if (e.equals(context.getSearchError())) {
assertNull(context.getSearchResponse());
}
}
} finally {
ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchActiveStoreTests method testContextFoundWithContextIdMismatch.
public void testContextFoundWithContextIdMismatch() {
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);
long id = randomNonNegativeLong();
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), id);
assertFalse(activeStore.freeContext(new AsynchronousSearchContextId(UUID.randomUUID().toString(), id)));
assertEquals(activeStore.getAllContexts().size(), 0);
} finally {
ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchActiveStoreTests method testFreeNonExistentContext.
public void testFreeNonExistentContext() {
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());
assertFalse(activeStore.freeContext(asContextId));
assertEquals(activeStore.getAllContexts().size(), 0);
} finally {
ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.
the class AsynchronousSearchActiveStoreTests method testPutContextRejection.
public void testPutContextRejection() throws InterruptedException, BrokenBarrierException, TimeoutException {
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);
AtomicInteger runningContexts = new AtomicInteger();
ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, node, clusterSettings);
AsynchronousSearchActiveStore activeStore = new AsynchronousSearchActiveStore(mockClusterService);
List<Runnable> runnables = new ArrayList<>();
AtomicInteger numRejected = new AtomicInteger();
AtomicInteger numFailures = new AtomicInteger();
AtomicInteger numSuccesses = new AtomicInteger();
int numThreads = maxRunningContexts + 1;
CyclicBarrier barrier = new CyclicBarrier(numThreads + 1);
CountDownLatch assertsLatch = new CountDownLatch(1);
for (int i = 0; i < numThreads; i++) {
ThreadPool finalTestThreadPool1 = testThreadPool;
runnables.add(() -> {
try {
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(finalTestThreadPool1);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), runningContexts.incrementAndGet());
boolean keepOnCompletion = randomBoolean();
TimeValue keepAlive = TimeValue.timeValueDays(randomInt(100));
AsynchronousSearchContextEventListener asContextEventListener = new AsynchronousSearchContextEventListener() {
};
AsynchronousSearchActiveContext context = new AsynchronousSearchActiveContext(asContextId, node.getId(), keepAlive, keepOnCompletion, finalTestThreadPool1, finalTestThreadPool1::absoluteTimeInMillis, asProgressListener, null, () -> true);
activeStore.putContext(asContextId, context, asContextEventListener::onContextRejected);
numSuccesses.getAndIncrement();
Optional<AsynchronousSearchActiveContext> optional = activeStore.getContext(asContextId);
assert (optional.isPresent());
assertEquals(optional.get(), context);
barrier.await(5, TimeUnit.SECONDS);
assertsLatch.await();
activeStore.freeContext(context.getContextId());
assertFalse(activeStore.getContext(context.getContextId()).isPresent());
barrier.await();
} catch (OpenSearchRejectedExecutionException e) {
numRejected.getAndIncrement();
try {
barrier.await();
barrier.await();
} catch (InterruptedException | BrokenBarrierException ex) {
numFailures.getAndIncrement();
}
} catch (InterruptedException | BrokenBarrierException e) {
numFailures.getAndIncrement();
} catch (Exception e) {
logger.error(e.getMessage(), e);
numFailures.getAndIncrement();
}
});
}
ThreadPool finalTestThreadPool = testThreadPool;
runnables.forEach(r -> finalTestThreadPool.generic().execute(r));
// create contexts
barrier.await(5, TimeUnit.SECONDS);
assertEquals(activeStore.getAllContexts().size(), maxRunningContexts);
assertEquals(numFailures.get(), 0);
assertEquals(numRejected.get(), 1);
assertEquals(numSuccesses.get(), maxRunningContexts);
assertsLatch.countDown();
// free contexts
barrier.await(5, TimeUnit.SECONDS);
assertEquals(activeStore.getAllContexts().size(), 0);
assertEquals(numFailures.get(), 0);
} finally {
ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.TestThreadPool in project OpenSearch by opensearch-project.
the class Netty4HttpServerTransportTests method setup.
@Before
public void setup() throws Exception {
networkService = new NetworkService(Collections.emptyList());
threadPool = new TestThreadPool("test");
bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
}
Aggregations