use of com.google.common.util.concurrent.FutureCallback in project druid by druid-io.
the class CachingClusteredClientTest method testOutOfOrderBackgroundCachePopulation.
@Test
public void testOutOfOrderBackgroundCachePopulation() {
// to trigger the actual execution when we are ready to shuffle the order.
abstract class DrainTask implements Runnable {
}
final ForwardingListeningExecutorService randomizingExecutorService = new ForwardingListeningExecutorService() {
final ConcurrentLinkedDeque<Pair<SettableFuture, Object>> taskQueue = new ConcurrentLinkedDeque<>();
final ListeningExecutorService delegate = MoreExecutors.listeningDecorator(// are complete before moving on to the next query run.
Execs.directExecutor());
@Override
protected ListeningExecutorService delegate() {
return delegate;
}
private <T> ListenableFuture<T> maybeSubmitTask(Object task, boolean wait) {
if (wait) {
SettableFuture<T> future = SettableFuture.create();
taskQueue.addFirst(Pair.of(future, task));
return future;
} else {
List<Pair<SettableFuture, Object>> tasks = Lists.newArrayList(taskQueue.iterator());
Collections.shuffle(tasks, new Random(0));
for (final Pair<SettableFuture, Object> pair : tasks) {
ListenableFuture future = pair.rhs instanceof Callable ? delegate.submit((Callable) pair.rhs) : delegate.submit((Runnable) pair.rhs);
Futures.addCallback(future, new FutureCallback() {
@Override
public void onSuccess(@Nullable Object result) {
pair.lhs.set(result);
}
@Override
public void onFailure(Throwable t) {
pair.lhs.setException(t);
}
});
}
}
return task instanceof Callable ? delegate.submit((Callable) task) : (ListenableFuture<T>) delegate.submit((Runnable) task);
}
@SuppressWarnings("ParameterPackage")
@Override
public <T> ListenableFuture<T> submit(Callable<T> task) {
return maybeSubmitTask(task, true);
}
@Override
public ListenableFuture<?> submit(Runnable task) {
if (task instanceof DrainTask) {
return maybeSubmitTask(task, false);
} else {
return maybeSubmitTask(task, true);
}
}
};
client = makeClient(new BackgroundCachePopulator(randomizingExecutorService, JSON_MAPPER, new CachePopulatorStats(), -1));
// callback to be run every time a query run is complete, to ensure all background
// caching tasks are executed, and cache is populated before we move onto the next query
queryCompletedCallback = new Runnable() {
@Override
public void run() {
try {
randomizingExecutorService.submit(new DrainTask() {
@Override
public void run() {
// no-op
}
}).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
final Druids.TimeseriesQueryBuilder builder = Druids.newTimeseriesQueryBuilder().dataSource(DATA_SOURCE).intervals(SEG_SPEC).filters(DIM_FILTER).granularity(GRANULARITY).aggregators(AGGS).postAggregators(POST_AGGS).context(CONTEXT).randomQueryId();
QueryRunner runner = new FinalizeResultsQueryRunner(getDefaultQueryRunner(), new TimeseriesQueryQueryToolChest());
testQueryCaching(runner, builder.build(), Intervals.of("2011-01-05/2011-01-10"), makeTimeResults(DateTimes.of("2011-01-05"), 85, 102, DateTimes.of("2011-01-06"), 412, 521, DateTimes.of("2011-01-07"), 122, 21894, DateTimes.of("2011-01-08"), 5, 20, DateTimes.of("2011-01-09"), 18, 521), Intervals.of("2011-01-10/2011-01-13"), makeTimeResults(DateTimes.of("2011-01-10"), 85, 102, DateTimes.of("2011-01-11"), 412, 521, DateTimes.of("2011-01-12"), 122, 21894));
}
use of com.google.common.util.concurrent.FutureCallback in project druid by druid-io.
the class TestTaskRunner method run.
@Override
public ListenableFuture<TaskStatus> run(final Task task) {
final TaskToolbox toolbox = toolboxFactory.build(task);
final Object taskPriorityObj = task.getContextValue(TaskThreadPriority.CONTEXT_KEY);
int taskPriority = 0;
try {
taskPriority = taskPriorityObj == null ? 0 : Numbers.parseInt(taskPriorityObj);
} catch (NumberFormatException e) {
log.error(e, "Error parsing task priority [%s] for task [%s]", taskPriorityObj, task.getId());
}
final int finalTaskPriority = taskPriority;
final ListenableFuture<TaskStatus> statusFuture = exec.computeIfAbsent(taskPriority, k -> buildExecutorService(finalTaskPriority)).submit(new TestTaskRunnerCallable(task, toolbox));
final TestTaskRunnerWorkItem taskRunnerWorkItem = new TestTaskRunnerWorkItem(task, statusFuture);
runningItems.add(taskRunnerWorkItem);
Futures.addCallback(statusFuture, new FutureCallback<TaskStatus>() {
@Override
public void onSuccess(TaskStatus result) {
runningItems.remove(taskRunnerWorkItem);
}
@Override
public void onFailure(Throwable t) {
runningItems.remove(taskRunnerWorkItem);
}
});
return statusFuture;
}
use of com.google.common.util.concurrent.FutureCallback in project binnavi by google.
the class OpenInLastWindowAndZoomToAddressAction method actionPerformed.
@Override
public void actionPerformed(final ActionEvent event) {
final FutureCallback<Boolean> callBack = new FutureCallback<Boolean>() {
@Override
public void onFailure(final Throwable t) {
CUtilityFunctions.logException(t);
}
@Override
public void onSuccess(final Boolean result) {
ZyGraph graph = null;
final List<CGraphWindow> windows = CWindowManager.instance().getOpenWindows();
for (final CGraphWindow graphContainer : windows) {
for (final IGraphPanel window : graphContainer) {
if (reference.getView().equals(window.getModel().getGraph().getRawView())) {
graph = window.getModel().getGraph();
}
}
}
for (final NaviNode node : graph.getNodes()) {
if (node.getRawNode() instanceof INaviCodeNode) {
final INaviCodeNode codeNode = (INaviCodeNode) node.getRawNode();
for (final INaviInstruction instruction : codeNode.getInstructions()) {
if (instruction.getAddress().equals(reference.getAddress())) {
ZyZoomHelpers.zoomToAddress(graph, reference.getAddress());
CrossReferencePainter.paintCrossReference(node, codeNode, reference, instruction);
}
}
}
}
}
};
CShowViewFunctions.showViewsAndPerformCallBack(m_parent, m_container, m_views, CWindowManager.instance().getLastWindow(), callBack);
}
use of com.google.common.util.concurrent.FutureCallback in project airlift by airlift.
the class CachingServiceSelector method refresh.
@Override
public ListenableFuture<List<ServiceDescriptor>> refresh() {
ServiceDescriptors oldDescriptors = this.serviceDescriptors.get();
ListenableFuture<ServiceDescriptors> future;
if (oldDescriptors == null) {
future = lookupClient.getServices(type, pool);
} else {
future = lookupClient.refreshServices(oldDescriptors);
}
future = chainedCallback(future, new FutureCallback<ServiceDescriptors>() {
@Override
public void onSuccess(ServiceDescriptors newDescriptors) {
serviceDescriptors.set(newDescriptors);
errorBackOff.success();
Duration delay = newDescriptors.getMaxAge();
if (delay == null) {
delay = DEFAULT_DELAY;
}
scheduleRefresh(delay);
}
@Override
public void onFailure(Throwable t) {
Duration duration = errorBackOff.failed(t);
scheduleRefresh(duration);
}
}, executor);
return Futures.transform(future, ServiceDescriptors::getServiceDescriptors, directExecutor());
}
use of com.google.common.util.concurrent.FutureCallback in project spf4j by zolyfarkas.
the class JdbcHeartBeat method scheduleHeartbeat.
public void scheduleHeartbeat(final ListeningScheduledExecutorService scheduler) {
synchronized (jdbc) {
if (isClosed) {
throw new IllegalStateException("Heartbeater is closed " + this);
}
if (scheduledHearbeat == null) {
long lrn = lastRunNanos;
long nanosSincelLastHB = TimeSource.nanoTime() - lrn;
long delayNanos = intervalNanos - nanosSincelLastHB;
if (delayNanos < (-intervalNanos) * missedHBRatio) {
throw new HeartBeatError("Missed heartbeat, last one was " + nanosSincelLastHB + " ns ago");
}
if (delayNanos < 0) {
delayNanos = 0;
}
ListenableScheduledFuture<?> scheduleFut = scheduler.schedule(getHeartBeatRunnable(), delayNanos, TimeUnit.NANOSECONDS);
scheduledHearbeat = scheduleFut;
Futures.addCallback(scheduleFut, new FutureCallback() {
@Override
public void onSuccess(final Object result) {
synchronized (jdbc) {
if (!isClosed) {
scheduledHearbeat = null;
scheduleHeartbeat(scheduler);
}
}
}
@Override
@SuppressFBWarnings("ITC_INHERITANCE_TYPE_CHECKING")
public void onFailure(final Throwable t) {
if (t instanceof Error) {
throw (Error) t;
} else if (!(t instanceof CancellationException)) {
throw new HeartBeatError(t);
}
}
}, DefaultExecutor.INSTANCE);
}
}
}
Aggregations