use of com.google.common.util.concurrent.ListenableScheduledFuture in project sonarqube by SonarSource.
the class CeProcessingSchedulerImplTest method when_workerCount_is_more_than_1_as_many_CeWorkerCallable_are_scheduled.
@Test
public void when_workerCount_is_more_than_1_as_many_CeWorkerCallable_are_scheduled() throws InterruptedException {
int workerCount = Math.abs(new Random().nextInt(10)) + 1;
ceConfiguration.setWorkerCount(workerCount);
ListenableScheduledFuture listenableScheduledFuture = mock(ListenableScheduledFuture.class);
CeProcessingSchedulerExecutorService processingExecutorService = mock(CeProcessingSchedulerExecutorService.class);
CeProcessingSchedulerImpl underTest = new CeProcessingSchedulerImpl(ceConfiguration, processingExecutorService, ceWorkerRunnable);
when(processingExecutorService.schedule(ceWorkerRunnable, ceConfiguration.getQueuePollingDelay(), MILLISECONDS)).thenReturn(listenableScheduledFuture);
underTest.startScheduling();
verify(processingExecutorService, times(workerCount)).schedule(ceWorkerRunnable, ceConfiguration.getQueuePollingDelay(), MILLISECONDS);
verify(listenableScheduledFuture, times(workerCount)).addListener(any(Runnable.class), eq(processingExecutorService));
}
use of com.google.common.util.concurrent.ListenableScheduledFuture in project druid by druid-io.
the class LookupCoordinatorManager method start.
@LifecycleStart
public void start() {
synchronized (startStopSync) {
if (started) {
return;
}
if (executorService.isShutdown()) {
throw new ISE("Cannot restart after stop!");
}
lookupMapConfigRef = configManager.watch(LOOKUP_CONFIG_KEY, new TypeReference<Map<String, Map<String, Map<String, Object>>>>() {
}, null);
final ListenableScheduledFuture backgroundManagerFuture = this.backgroundManagerFuture = executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
final Map<String, Map<String, Map<String, Object>>> allLookupTiers = lookupMapConfigRef.get();
// Sanity check for if we are shutting down
if (Thread.currentThread().isInterrupted()) {
LOG.info("Not updating lookups because process was interrupted");
return;
}
if (!started) {
LOG.info("Not started. Returning");
return;
}
if (allLookupTiers == null) {
LOG.info("Not updating lookups because no data exists");
return;
}
for (final String tier : allLookupTiers.keySet()) {
try {
final Map<String, Map<String, Object>> allLookups = allLookupTiers.get(tier);
final Map<String, Map<String, Object>> oldLookups = prior_update.get(tier);
final Collection<String> drops;
if (oldLookups == null) {
drops = ImmutableList.of();
} else {
drops = Sets.difference(oldLookups.keySet(), allLookups.keySet());
}
if (allLookupTiers == prior_update) {
LOG.debug("No updates");
updateAllNewOnTier(tier, allLookups);
} else {
updateAllOnTier(tier, allLookups);
deleteAllOnTier(tier, drops);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
} catch (Exception e) {
LOG.error(e, "Error updating lookups for tier [%s]. Will try again soon", tier);
}
}
prior_update = allLookupTiers;
}
}, 0, lookupCoordinatorManagerConfig.getPeriod(), TimeUnit.MILLISECONDS);
Futures.addCallback(backgroundManagerFuture, new FutureCallback<Object>() {
@Override
public void onSuccess(@Nullable Object result) {
backgroundManagerExitedLatch.countDown();
LOG.debug("Exited background lookup manager");
}
@Override
public void onFailure(Throwable t) {
backgroundManagerExitedLatch.countDown();
if (backgroundManagerFuture.isCancelled()) {
LOG.info("Background lookup manager exited");
LOG.trace(t, "Background lookup manager exited with throwable");
} else {
LOG.makeAlert(t, "Background lookup manager exited with error!").emit();
}
}
});
started = true;
LOG.debug("Started");
}
}
use of com.google.common.util.concurrent.ListenableScheduledFuture in project druid by druid-io.
the class LookupCoordinatorManager method stop.
@LifecycleStop
public void stop() {
synchronized (startStopSync) {
if (!started) {
LOG.warn("Not started, ignoring stop request");
return;
}
started = false;
executorService.shutdownNow();
final ListenableScheduledFuture backgroundManagerFuture = this.backgroundManagerFuture;
this.backgroundManagerFuture = null;
if (backgroundManagerFuture != null && !backgroundManagerFuture.cancel(true)) {
LOG.warn("Background lookup manager thread could not be cancelled");
}
// NOTE: we can't un-watch the configuration key
LOG.debug("Stopped");
}
}
use of com.google.common.util.concurrent.ListenableScheduledFuture in project sonarqube by SonarSource.
the class CeProcessingSchedulerImplTest method when_workerCount_is_more_than_1_as_many_CeWorkerCallable_are_scheduled.
@Test
public void when_workerCount_is_more_than_1_as_many_CeWorkerCallable_are_scheduled() throws Exception {
int workerCount = Math.abs(new Random().nextInt(10)) + 1;
ceConfiguration.setWorkerThreadCount(workerCount);
CeWorker[] workers = new CeWorker[workerCount];
for (int i = 0; i < workerCount; i++) {
workers[i] = mock(CeWorker.class);
when(workers[i].call()).thenReturn(NO_TASK).thenThrow(ERROR_TO_INTERRUPT_CHAINING);
}
ListenableScheduledFuture listenableScheduledFuture = mock(ListenableScheduledFuture.class);
CeProcessingSchedulerExecutorService processingExecutorService = mock(CeProcessingSchedulerExecutorService.class);
when(processingExecutorService.schedule(any(CeWorker.class), any(Long.class), any(TimeUnit.class))).thenReturn(listenableScheduledFuture);
CeWorkerFactory ceWorkerFactory = spy(new TestCeWorkerFactory(workers));
CeProcessingSchedulerImpl underTest = new CeProcessingSchedulerImpl(ceConfiguration, processingExecutorService, ceWorkerFactory, ceWorkerController);
when(processingExecutorService.schedule(ceWorker, ceConfiguration.getQueuePollingDelay(), MILLISECONDS)).thenReturn(listenableScheduledFuture);
underTest.startScheduling();
// Verify that schedule has been called on all workers
for (int i = 0; i < workerCount; i++) {
verify(processingExecutorService).schedule(workers[i], ceConfiguration.getQueuePollingDelay(), MILLISECONDS);
}
verify(listenableScheduledFuture, times(workerCount)).addListener(any(Runnable.class), eq(MoreExecutors.directExecutor()));
for (int i = 0; i < workerCount; i++) {
verify(ceWorkerFactory).create(i);
}
}
Aggregations