Search in sources :

Example 56 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project cassandra by apache.

the class StreamTransferTask method scheduleTimeout.

/**
     * Schedule timeout task to release reference for file sent.
     * When not receiving ACK after sending to receiver in given time,
     * the task will release reference.
     *
     * @param sequenceNumber sequence number of file sent.
     * @param time time to timeout
     * @param unit unit of given time
     * @return scheduled future for timeout task
     */
public synchronized ScheduledFuture scheduleTimeout(final int sequenceNumber, long time, TimeUnit unit) {
    if (!files.containsKey(sequenceNumber))
        return null;
    ScheduledFuture future = timeoutExecutor.schedule(new Runnable() {

        public void run() {
            synchronized (StreamTransferTask.this) {
                // remove so we don't cancel ourselves
                timeoutTasks.remove(sequenceNumber);
                StreamTransferTask.this.complete(sequenceNumber);
            }
        }
    }, time, unit);
    ScheduledFuture prev = timeoutTasks.put(sequenceNumber, future);
    assert prev == null;
    return future;
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 57 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project hbase by apache.

the class JmxCacheBuster method clearJmxCache.

/**
   * For JMX to forget about all previously exported metrics.
   */
public static void clearJmxCache() {
    if (LOG.isTraceEnabled()) {
        LOG.trace("clearing JMX Cache" + StringUtils.stringifyException(new Exception()));
    }
    //If there are more then 100 ms before the executor will run then everything should be merged.
    ScheduledFuture future = fut.get();
    if ((future != null && (!future.isDone() && future.getDelay(TimeUnit.MILLISECONDS) > 100))) {
        // BAIL OUT
        return;
    }
    if (stopped.get()) {
        return;
    }
    future = executor.getExecutor().schedule(new JmxCacheBusterRunnable(), 5, TimeUnit.SECONDS);
    fut.set(future);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 58 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project kafka by apache.

the class SourceTaskOffsetCommitterTest method testSchedule.

@Test
public void testSchedule() throws Exception {
    Capture<Runnable> taskWrapper = EasyMock.newCapture();
    ScheduledFuture commitFuture = PowerMock.createMock(ScheduledFuture.class);
    EasyMock.expect(executor.scheduleWithFixedDelay(EasyMock.capture(taskWrapper), eq(DEFAULT_OFFSET_COMMIT_INTERVAL_MS), eq(DEFAULT_OFFSET_COMMIT_INTERVAL_MS), eq(TimeUnit.MILLISECONDS))).andReturn(commitFuture);
    ConnectorTaskId taskId = PowerMock.createMock(ConnectorTaskId.class);
    WorkerSourceTask task = PowerMock.createMock(WorkerSourceTask.class);
    EasyMock.expect(committers.put(taskId, commitFuture)).andReturn(null);
    PowerMock.replayAll();
    committer.schedule(taskId, task);
    assertTrue(taskWrapper.hasCaptured());
    assertNotNull(taskWrapper.getValue());
    PowerMock.verifyAll();
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) ScheduledFuture(java.util.concurrent.ScheduledFuture) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 59 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project kafka by apache.

the class SourceTaskOffsetCommitterTest method testRemove.

@Test
public void testRemove() throws Exception {
    ConnectorTaskId taskId = PowerMock.createMock(ConnectorTaskId.class);
    ScheduledFuture task = PowerMock.createMock(ScheduledFuture.class);
    // Try to remove a non-existing task
    EasyMock.expect(committers.remove(taskId)).andReturn(null);
    PowerMock.replayAll();
    committer.remove(taskId);
    PowerMock.verifyAll();
    PowerMock.resetAll();
    // Try to remove an existing task
    EasyMock.expect(committers.remove(taskId)).andReturn(task);
    EasyMock.expect(task.cancel(eq(false))).andReturn(false);
    EasyMock.expect(task.isDone()).andReturn(false);
    EasyMock.expect(task.get()).andReturn(null);
    PowerMock.replayAll();
    committer.remove(taskId);
    PowerMock.verifyAll();
    PowerMock.resetAll();
    // Try to remove a cancelled task
    EasyMock.expect(committers.remove(taskId)).andReturn(task);
    EasyMock.expect(task.cancel(eq(false))).andReturn(false);
    EasyMock.expect(task.isDone()).andReturn(false);
    EasyMock.expect(task.get()).andThrow(new CancellationException());
    mockLog.trace(EasyMock.anyString(), EasyMock.<Object>anyObject());
    PowerMock.expectLastCall();
    PowerMock.replayAll();
    committer.remove(taskId);
    PowerMock.verifyAll();
    PowerMock.resetAll();
    // Try to remove an interrupted task
    EasyMock.expect(committers.remove(taskId)).andReturn(task);
    EasyMock.expect(task.cancel(eq(false))).andReturn(false);
    EasyMock.expect(task.isDone()).andReturn(false);
    EasyMock.expect(task.get()).andThrow(new InterruptedException());
    PowerMock.replayAll();
    try {
        committer.remove(taskId);
        fail("Expected ConnectException to be raised");
    } catch (ConnectException e) {
    //ignore
    }
    PowerMock.verifyAll();
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) CancellationException(java.util.concurrent.CancellationException) ScheduledFuture(java.util.concurrent.ScheduledFuture) ConnectException(org.apache.kafka.connect.errors.ConnectException) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 60 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project cassandra by apache.

the class DebuggableScheduledThreadPoolExecutorTest method testShutdown.

@Test
public void testShutdown() throws ExecutionException, InterruptedException, IOException {
    DebuggableScheduledThreadPoolExecutor testPool = new DebuggableScheduledThreadPoolExecutor("testpool");
    final AtomicInteger value = new AtomicInteger(0);
    //Normal scheduled task
    ScheduledFuture future = testPool.schedule(new Runnable() {

        public void run() {
            value.incrementAndGet();
        }
    }, 1, TimeUnit.SECONDS);
    future.get();
    assert value.get() == 1;
    //Shut down before schedule
    future = testPool.schedule(new Runnable() {

        public void run() {
            value.incrementAndGet();
        }
    }, 10, TimeUnit.SECONDS);
    StorageService.instance.drain();
    testPool.shutdown();
    future.get();
    assert value.get() == 2;
    //Now shut down verify task isn't just swallowed
    future = testPool.schedule(new Runnable() {

        public void run() {
            value.incrementAndGet();
        }
    }, 1, TimeUnit.SECONDS);
    try {
        future.get(2, TimeUnit.SECONDS);
        Assert.fail("Task should be cancelled");
    } catch (CancellationException e) {
    } catch (TimeoutException e) {
        Assert.fail("Task should be cancelled");
    }
    assert future.isCancelled();
    assert value.get() == 2;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) ScheduledFuture(java.util.concurrent.ScheduledFuture) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

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