Search in sources :

Example 1 with Cancellable

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);
    }
}
Also used : Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestThreadPool(org.opensearch.threadpool.TestThreadPool) TimeValue(org.opensearch.common.unit.TimeValue)

Example 2 with Cancellable

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());
}
Also used : Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable) OpenSearchException(org.opensearch.OpenSearchException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with Cancellable

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();
}
Also used : Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) Term(org.apache.lucene.index.Term) Document(org.opensearch.index.mapper.ParseContext.Document) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IOException(java.io.IOException) InternalEngine(org.opensearch.index.engine.InternalEngine) Engine(org.opensearch.index.engine.Engine)

Example 4 with Cancellable

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());
}
Also used : Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable)

Example 5 with Cancellable

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);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(org.opensearch.threadpool.Scheduler.Cancellable) ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

Cancellable (org.opensearch.threadpool.Scheduler.Cancellable)7 ReschedulingRunnable (org.opensearch.threadpool.Scheduler.ReschedulingRunnable)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TimeValue (org.opensearch.common.unit.TimeValue)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Term (org.apache.lucene.index.Term)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 BaseFuture (org.opensearch.common.util.concurrent.BaseFuture)1 Engine (org.opensearch.index.engine.Engine)1 InternalEngine (org.opensearch.index.engine.InternalEngine)1 Document (org.opensearch.index.mapper.ParseContext.Document)1 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)1 TestThreadPool (org.opensearch.threadpool.TestThreadPool)1 ThreadPool (org.opensearch.threadpool.ThreadPool)1