use of org.opensearch.threadpool.Scheduler.Cancellable in project OpenSearch by opensearch-project.
the class JvmGcMonitorServiceSettingsTests method execute.
private static void execute(Settings settings, TriFunction<Runnable, TimeValue, String, Cancellable> scheduler, Consumer<Throwable> consumer, boolean constructionShouldFail, Runnable asserts) throws InterruptedException {
assert constructionShouldFail == (consumer != null);
assert constructionShouldFail == (asserts == null);
ThreadPool threadPool = null;
try {
threadPool = new TestThreadPool(JvmGcMonitorServiceSettingsTests.class.getCanonicalName()) {
@Override
public Cancellable scheduleWithFixedDelay(Runnable command, TimeValue interval, String name) {
return scheduler.apply(command, interval, name);
}
};
try {
JvmGcMonitorService service = new JvmGcMonitorService(settings, threadPool);
if (constructionShouldFail) {
fail("construction of jvm gc service should have failed");
}
service.doStart();
asserts.run();
service.doStop();
} catch (Exception t) {
consumer.accept(t);
}
} finally {
ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
}
}
use of org.opensearch.threadpool.Scheduler.Cancellable in project OpenSearch by opensearch-project.
the class ScheduleWithFixedDelayTests method testThatRunnableIsRescheduled.
public void testThatRunnableIsRescheduled() throws Exception {
final CountDownLatch latch = new CountDownLatch(scaledRandomIntBetween(2, 16));
final Runnable countingRunnable = () -> {
if (rarely()) {
throw new OpenSearchException("sometimes we throw before counting down");
}
latch.countDown();
if (randomBoolean()) {
throw new OpenSearchException("this shouldn't cause the test to fail!");
}
};
Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, TimeValue.timeValueMillis(10L), Names.GENERIC);
assertNotNull(cancellable);
// wait for the number of successful count down operations
latch.await();
// cancel
cancellable.cancel();
assertTrue(cancellable.isCancelled());
}
use of org.opensearch.threadpool.Scheduler.Cancellable in project OpenSearch by opensearch-project.
the class RefreshListenersTests method testLotsOfThreads.
/**
* Uses a bunch of threads to index, wait for refresh, and non-realtime get documents to validate that they are visible after waiting
* regardless of what crazy sequence of events causes the refresh listener to fire.
*/
public void testLotsOfThreads() throws Exception {
int threadCount = between(3, 10);
maxListeners = between(1, threadCount * 2);
// This thread just refreshes every once in a while to cause trouble.
Cancellable refresher = threadPool.scheduleWithFixedDelay(() -> engine.refresh("because test"), timeValueMillis(100), Names.SAME);
// These threads add and block until the refresh makes the change visible and then do a non-realtime get.
Thread[] indexers = new Thread[threadCount];
for (int thread = 0; thread < threadCount; thread++) {
final String threadId = String.format(Locale.ROOT, "%04d", thread);
indexers[thread] = new Thread(() -> {
for (int iteration = 1; iteration <= 50; iteration++) {
try {
String testFieldValue = String.format(Locale.ROOT, "%s%04d", threadId, iteration);
Engine.IndexResult index = index(threadId, testFieldValue);
assertEquals(iteration, index.getVersion());
DummyRefreshListener listener = new DummyRefreshListener();
listeners.addOrNotify(index.getTranslogLocation(), listener);
assertBusy(() -> assertNotNull("listener never called", listener.forcedRefresh.get()), 1, TimeUnit.MINUTES);
if (threadCount < maxListeners) {
assertFalse(listener.forcedRefresh.get());
}
listener.assertNoError();
Engine.Get get = new Engine.Get(false, false, threadId, new Term(IdFieldMapper.NAME, threadId));
try (Engine.GetResult getResult = engine.get(get, engine::acquireSearcher)) {
assertTrue("document not found", getResult.exists());
assertEquals(iteration, getResult.version());
org.apache.lucene.document.Document document = getResult.docIdAndVersion().reader.document(getResult.docIdAndVersion().docId);
assertThat(document.getValues("test"), arrayContaining(testFieldValue));
}
} catch (Exception t) {
throw new RuntimeException("failure on the [" + iteration + "] iteration of thread [" + threadId + "]", t);
}
}
});
indexers[thread].start();
}
for (Thread indexer : indexers) {
indexer.join();
}
refresher.cancel();
}
use of org.opensearch.threadpool.Scheduler.Cancellable in project OpenSearch by opensearch-project.
the class ScheduleWithFixedDelayTests method testBlockingCallOnNonSchedulerThreadAllowed.
public void testBlockingCallOnNonSchedulerThreadAllowed() throws Exception {
final TestFuture future = new TestFuture();
final TestFuture resultsFuture = new TestFuture();
final boolean rethrow = randomBoolean();
final boolean getWithTimeout = randomBoolean();
final Runnable runnable = () -> {
try {
Object obj;
if (getWithTimeout) {
obj = future.get(1, TimeUnit.MINUTES);
} else {
obj = future.get();
}
resultsFuture.futureDone(obj);
} catch (Throwable t) {
resultsFuture.futureDone(t);
if (rethrow) {
throw new RuntimeException(t);
}
}
};
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(runnable, TimeValue.timeValueMillis(10L), Names.GENERIC);
assertFalse(resultsFuture.isDone());
final Object o = new Object();
future.futureDone(o);
final Object resultingObject = resultsFuture.get();
assertThat(resultingObject, sameInstance(o));
assertFalse(cancellable.isCancelled());
}
use of org.opensearch.threadpool.Scheduler.Cancellable in project OpenSearch by opensearch-project.
the class ScheduleWithFixedDelayTests method testRunnableDoesNotRunAfterCancellation.
public void testRunnableDoesNotRunAfterCancellation() throws Exception {
final int iterations = scaledRandomIntBetween(2, 12);
final AtomicInteger counter = new AtomicInteger();
final CountDownLatch doneLatch = new CountDownLatch(iterations);
final Runnable countingRunnable = () -> {
counter.incrementAndGet();
doneLatch.countDown();
};
final TimeValue interval = TimeValue.timeValueMillis(50L);
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, interval, Names.GENERIC);
doneLatch.await();
cancellable.cancel();
final int counterValue = counter.get();
assertThat(counterValue, equalTo(iterations));
if (rarely()) {
assertBusy(() -> assertThat(counter.get(), equalTo(iterations)), 5 * interval.millis(), TimeUnit.MILLISECONDS);
}
}
Aggregations