Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project hazelcast by hazelcast.

the class DefaultPublisherContext method startRemovalTask.

private void startRemovalTask(final Collection<PartitionAccumulatorRegistry> removalCandidates, String uuid) {
    QueryCacheScheduler queryCacheScheduler = context.getQueryCacheScheduler();
    ScheduledFuture scheduledFuture = queryCacheScheduler.scheduleWithRepetition(new Runnable() {

        @Override
        public void run() {
            for (PartitionAccumulatorRegistry registry : removalCandidates) {
                removePartitionAccumulatorRegistry(registry);
            }
        }
    }, ORPHANED_QUERY_CACHE_REMOVAL_DELAY_SECONDS);
    ScheduledFuture prevFuture = removalCandidateFutures.put(uuid, scheduledFuture);
    if (prevFuture != null) {
        prevFuture.cancel(false);
    }
}
Also used : QueryCacheScheduler(com.hazelcast.map.impl.querycache.QueryCacheScheduler) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)85 Test (org.junit.Test)27 Date (java.util.Date)10 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 IOException (java.io.IOException)8 Map (java.util.Map)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 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 Future (java.util.concurrent.Future)4 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)4