Search in sources :

Example 51 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project spring-boot-admin by codecentric.

the class RegistrationApplicationListenerTest method test_start_stop.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test_start_stop() throws Exception {
    ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
    TaskScheduler scheduler = mock(TaskScheduler.class);
    RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
    ScheduledFuture task = mock(ScheduledFuture.class);
    when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
    listener.startRegisterTask();
    verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
    listener.stopRegisterTask();
    verify(task).cancel(true);
}
Also used : RegistrationApplicationListener(de.codecentric.boot.admin.client.registration.RegistrationApplicationListener) ApplicationRegistrator(de.codecentric.boot.admin.client.registration.ApplicationRegistrator) TaskScheduler(org.springframework.scheduling.TaskScheduler) ScheduledFuture(java.util.concurrent.ScheduledFuture) Test(org.junit.Test)

Example 52 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project druid by druid-io.

the class RemoteTaskRunner method cancelWorkerCleanup.

private boolean cancelWorkerCleanup(String workerHost) {
    ScheduledFuture previousCleanup = removedWorkerCleanups.remove(workerHost);
    if (previousCleanup != null) {
        log.info("Cancelling Worker[%s] scheduled task cleanup", workerHost);
        previousCleanup.cancel(false);
    }
    return previousCleanup != null;
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) ListenableScheduledFuture(com.google.common.util.concurrent.ListenableScheduledFuture)

Example 53 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project elasticsearch by elastic.

the class AsyncBulkByScrollActionTests method testCancelWhileDelayedAfterScrollResponse.

/**
     * Tests that we can cancel the request during its throttling delay. This can't use {@link #cancelTaskCase(Consumer)} because it needs
     * to send the request un-canceled and cancel it at a specific time.
     */
public void testCancelWhileDelayedAfterScrollResponse() throws Exception {
    String reason = randomSimpleString(random());
    /*
         * Replace the thread pool with one that will cancel the task as soon as anything is scheduled, which reindex tries to do when there
         * is a delay.
         */
    setupClient(new TestThreadPool(getTestName()) {

        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
            /*
                 * This is called twice:
                 * 1. To schedule the throttling. When that happens we immediately cancel the task.
                 * 2. After the task is canceled.
                 * Both times we use delegate to the standard behavior so the task is scheduled as expected so it can be cancelled and all
                 * that good stuff.
                 */
            if (delay.nanos() > 0) {
                generic().execute(() -> taskManager.cancel(testTask, reason, () -> {
                }));
            }
            return super.schedule(delay, name, command);
        }
    });
    // Send the scroll response which will trigger the custom thread pool above, canceling the request before running the response
    DummyAsyncBulkByScrollAction action = new DummyAsyncBulkByScrollAction();
    boolean previousScrollSet = usually();
    if (previousScrollSet) {
        action.setScroll(scrollId());
    }
    long total = randomIntBetween(0, Integer.MAX_VALUE);
    ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), total, emptyList(), null);
    // Use a long delay here so the test will time out if the cancellation doesn't reschedule the throttled task
    testTask.rethrottle(1);
    simulateScrollResponse(action, timeValueNanos(System.nanoTime()), 1000, response);
    // Now that we've got our cancel we'll just verify that it all came through all right
    assertEquals(reason, listener.get(10, TimeUnit.SECONDS).getReasonCancelled());
    if (previousScrollSet) {
        // Canceled tasks always start to clear the scroll before they die.
        assertThat(client.scrollsCleared, contains(scrollId));
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) ActionResponse(org.elasticsearch.action.ActionResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Matchers.containsString(org.hamcrest.Matchers.containsString) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 54 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project elasticsearch by elastic.

the class RemoteScrollableHitSourceTests method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    final ExecutorService directExecutor = EsExecutors.newDirectExecutorService();
    threadPool = new TestThreadPool(getTestName()) {

        @Override
        public ExecutorService executor(String name) {
            return directExecutor;
        }

        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
            command.run();
            return null;
        }
    };
    retries = 0;
    searchRequest = new SearchRequest();
    searchRequest.scroll(timeValueMinutes(5));
    searchRequest.source(new SearchSourceBuilder().size(10).version(true).sort("_doc").size(123));
    retriesAllowed = 0;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ExecutorService(java.util.concurrent.ExecutorService) Matchers.containsString(org.hamcrest.Matchers.containsString) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Before(org.junit.Before)

Example 55 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project elasticsearch by elastic.

the class ScheduleWithFixedDelayTests method testOnRejectionCausesCancellation.

public void testOnRejectionCausesCancellation() throws Exception {
    final TimeValue delay = TimeValue.timeValueMillis(10L);
    terminate(threadPool);
    threadPool = new ThreadPool(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "fixed delay tests").build()) {

        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String executor, Runnable command) {
            if (command instanceof ReschedulingRunnable) {
                ((ReschedulingRunnable) command).onRejection(new EsRejectedExecutionException());
            } else {
                fail("this should only be called with a rescheduling runnable in this test");
            }
            return null;
        }
    };
    Runnable runnable = () -> {
    };
    ReschedulingRunnable reschedulingRunnable = new ReschedulingRunnable(runnable, delay, Names.GENERIC, threadPool);
    assertTrue(reschedulingRunnable.isCancelled());
}
Also used : ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable) ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)83 Test (org.junit.Test)27 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Date (java.util.Date)9 IOException (java.io.IOException)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 Map (java.util.Map)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 None (com.linkedin.common.util.None)5 D2Client (com.linkedin.d2.balancer.D2Client)5 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 JSONObject (org.json.simple.JSONObject)5 IdleReaderException (com.twitter.distributedlog.exceptions.IdleReaderException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4