Search in sources :

Example 1 with ListenableScheduledFuture

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));
}
Also used : Random(java.util.Random) ListenableScheduledFuture(com.google.common.util.concurrent.ListenableScheduledFuture) Test(org.junit.Test)

Example 2 with ListenableScheduledFuture

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");
    }
}
Also used : TimeoutException(java.util.concurrent.TimeoutException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ListenableScheduledFuture(com.google.common.util.concurrent.ListenableScheduledFuture) ISE(io.druid.java.util.common.ISE) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LifecycleStart(io.druid.java.util.common.lifecycle.LifecycleStart)

Example 3 with ListenableScheduledFuture

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");
    }
}
Also used : ListenableScheduledFuture(com.google.common.util.concurrent.ListenableScheduledFuture) LifecycleStop(io.druid.java.util.common.lifecycle.LifecycleStop)

Example 4 with ListenableScheduledFuture

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);
    }
}
Also used : Random(java.util.Random) ListenableScheduledFuture(com.google.common.util.concurrent.ListenableScheduledFuture) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test)

Aggregations

ListenableScheduledFuture (com.google.common.util.concurrent.ListenableScheduledFuture)4 Random (java.util.Random)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ISE (io.druid.java.util.common.ISE)1 LifecycleStart (io.druid.java.util.common.lifecycle.LifecycleStart)1 LifecycleStop (io.druid.java.util.common.lifecycle.LifecycleStop)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1