use of org.opensearch.search.asynchronous.stats.AsynchronousSearchStats in project asynchronous-search by opensearch-project.
the class AsynchronousSearchStatsIT method testStatsAcrossNodes.
@TestLogging(value = "_root:DEBUG", reason = "flaky")
public void testStatsAcrossNodes() throws InterruptedException, ExecutionException {
TestThreadPool threadPool = null;
try {
threadPool = new TestThreadPool(AsynchronousSearchStatsIT.class.getName());
String index = "idx";
createIndex(index);
indexRandom(super.ignoreExternalCluster(), client().prepareIndex(index).setId("1").setSource("field1", "the quick brown fox jumps"), client().prepareIndex(index).setId("2").setSource("field1", "quick brown"), client().prepareIndex(index).setId("3").setSource("field1", "quick"));
List<DiscoveryNode> dataNodes = new LinkedList<>();
clusterService().state().nodes().getDataNodes().iterator().forEachRemaining(node -> {
dataNodes.add(node.value);
});
assertFalse(dataNodes.isEmpty());
int numThreads = 20;
List<Runnable> threads = new ArrayList<>();
AtomicLong expectedNumSuccesses = new AtomicLong();
AtomicLong expectedNumFailures = new AtomicLong();
AtomicLong expectedNumPersisted = new AtomicLong();
CountDownLatch latch = new CountDownLatch(numThreads);
for (int i = 0; i < numThreads; i++) {
threads.add(() -> {
try {
boolean success = randomBoolean();
boolean keepOnCompletion = randomBoolean();
if (keepOnCompletion) {
expectedNumPersisted.getAndIncrement();
}
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest;
if (success) {
expectedNumSuccesses.getAndIncrement();
submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest(index));
submitAsynchronousSearchRequest.waitForCompletionTimeout(TimeValue.timeValueSeconds(2));
submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
} else {
expectedNumFailures.getAndIncrement();
submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest("non_existent_index"));
submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
}
AsynchronousSearchResponse asResponse = executeSubmitAsynchronousSearch(client(dataNodes.get(randomInt(1)).getName()), submitAsynchronousSearchRequest);
if (keepOnCompletion) {
TestClientUtils.assertResponsePersistence(client(), asResponse.getId());
}
} catch (Exception e) {
fail(e.getMessage());
} finally {
latch.countDown();
}
});
}
TestThreadPool finalThreadPool = threadPool;
threads.forEach(t -> finalThreadPool.generic().execute(t));
latch.await();
AsynchronousSearchStatsResponse statsResponse = client().execute(AsynchronousSearchStatsAction.INSTANCE, new AsynchronousSearchStatsRequest()).get();
AtomicLong actualNumSuccesses = new AtomicLong();
AtomicLong actualNumFailures = new AtomicLong();
AtomicLong actualNumPersisted = new AtomicLong();
for (AsynchronousSearchStats node : statsResponse.getNodes()) {
AsynchronousSearchCountStats asCountStats = node.getAsynchronousSearchCountStats();
assertEquals(asCountStats.getRunningCount(), 0);
assertThat(expectedNumSuccesses.get(), greaterThanOrEqualTo(asCountStats.getCompletedCount()));
actualNumSuccesses.getAndAdd(asCountStats.getCompletedCount());
assertThat(expectedNumFailures.get(), greaterThanOrEqualTo(asCountStats.getFailedCount()));
actualNumFailures.getAndAdd(asCountStats.getFailedCount());
assertThat(expectedNumPersisted.get(), greaterThanOrEqualTo(asCountStats.getPersistedCount()));
actualNumPersisted.getAndAdd(asCountStats.getPersistedCount());
}
assertEquals(expectedNumPersisted.get(), actualNumPersisted.get());
assertEquals(expectedNumFailures.get(), actualNumFailures.get());
assertEquals(expectedNumSuccesses.get(), actualNumSuccesses.get());
waitForAsyncSearchTasksToComplete();
} finally {
ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
}
}
use of org.opensearch.search.asynchronous.stats.AsynchronousSearchStats in project asynchronous-search by opensearch-project.
the class AsynchronousSearchStatsIT method testRunningAsynchronousSearchCountStat.
public void testRunningAsynchronousSearchCountStat() throws InterruptedException, ExecutionException {
String index = "idx";
createIndex(index);
indexRandom(super.ignoreExternalCluster(), client().prepareIndex(index).setId("1").setSource("field1", "the quick brown fox jumps"), client().prepareIndex(index).setId("2").setSource("field1", "quick brown"), client().prepareIndex(index).setId("3").setSource("field1", "quick"));
List<ScriptedBlockPlugin> plugins = initBlockFactory();
SearchRequest searchRequest = client().prepareSearch(index).setQuery(scriptQuery(new Script(ScriptType.INLINE, "mockscript", SCRIPT_NAME, Collections.emptyMap()))).request();
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
submitAsynchronousSearchRequest.keepOnCompletion(true);
AsynchronousSearchResponse asResponse = executeSubmitAsynchronousSearch(client(), submitAsynchronousSearchRequest);
AsynchronousSearchStatsResponse statsResponse = client().execute(AsynchronousSearchStatsAction.INSTANCE, new AsynchronousSearchStatsRequest()).get();
long runningSearchCount = 0;
for (AsynchronousSearchStats node : statsResponse.getNodes()) {
runningSearchCount += node.getAsynchronousSearchCountStats().getRunningCount();
assertEquals(node.getAsynchronousSearchCountStats().getCompletedCount(), 0L);
assertEquals(node.getAsynchronousSearchCountStats().getFailedCount(), 0L);
assertEquals(node.getAsynchronousSearchCountStats().getPersistedCount(), 0L);
}
assertEquals(runningSearchCount, 1L);
disableBlocks(plugins);
TestClientUtils.assertResponsePersistence(client(), asResponse.getId());
statsResponse = client().execute(AsynchronousSearchStatsAction.INSTANCE, new AsynchronousSearchStatsRequest()).get();
long persistedCount = 0;
long completedCount = 0;
for (AsynchronousSearchStats node : statsResponse.getNodes()) {
persistedCount += node.getAsynchronousSearchCountStats().getPersistedCount();
completedCount += node.getAsynchronousSearchCountStats().getCompletedCount();
assertEquals(node.getAsynchronousSearchCountStats().getRunningCount(), 0L);
assertEquals(node.getAsynchronousSearchCountStats().getFailedCount(), 0L);
}
assertEquals(runningSearchCount, 1L);
}
use of org.opensearch.search.asynchronous.stats.AsynchronousSearchStats in project asynchronous-search by opensearch-project.
the class AsynchronousSearchStatsResponse method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("nodes");
for (AsynchronousSearchStats stats : getNodes()) {
builder.startObject(stats.getNode().getId());
stats.toXContent(builder, params);
builder.endObject();
}
builder.endObject();
return builder;
}
Aggregations