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;
}
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);
}
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();
}
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();
}
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;
}
Aggregations