Search in sources :

Example 1 with InstrumentedThreadFactory

use of com.codahale.metrics.InstrumentedThreadFactory in project dropwizard by dropwizard.

the class DefaultHealthFactory method createScheduledExecutorForHealthChecks.

private ScheduledExecutorService createScheduledExecutorForHealthChecks(final int numberOfScheduledHealthChecks, final MetricRegistry metrics, final LifecycleEnvironment lifecycle, final String fullName) {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(fullName + "-%d").setDaemon(true).setUncaughtExceptionHandler((t, e) -> LOGGER.error("Thread={} died due to uncaught exception", t, e)).build();
    final InstrumentedThreadFactory instrumentedThreadFactory = new InstrumentedThreadFactory(threadFactory, metrics);
    final ScheduledExecutorService scheduledExecutorService = lifecycle.scheduledExecutorService(fullName + "-scheduled-executor", instrumentedThreadFactory).threads(numberOfScheduledHealthChecks).build();
    return new InstrumentedScheduledExecutorService(scheduledExecutorService, metrics);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ServletHealthResponderFactory(io.dropwizard.health.response.ServletHealthResponderFactory) Size(javax.validation.constraints.Size) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) LoggerFactory(org.slf4j.LoggerFactory) HealthResponseProviderFactory(io.dropwizard.health.response.HealthResponseProviderFactory) Duration(io.dropwizard.util.Duration) Collections.singletonList(java.util.Collections.singletonList) Valid(javax.validation.Valid) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ServletEnvironment(io.dropwizard.jetty.setup.ServletEnvironment) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ThreadFactory(java.util.concurrent.ThreadFactory) JerseyEnvironment(io.dropwizard.jersey.setup.JerseyEnvironment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HealthResponseProvider(io.dropwizard.health.response.HealthResponseProvider) NotNull(javax.validation.constraints.NotNull) HealthResponderFactory(io.dropwizard.health.response.HealthResponderFactory) List(java.util.List) JsonHealthResponseProviderFactory(io.dropwizard.health.response.JsonHealthResponseProviderFactory) InstrumentedScheduledExecutorService(com.codahale.metrics.InstrumentedScheduledExecutorService) Collections(java.util.Collections) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) InstrumentedScheduledExecutorService(com.codahale.metrics.InstrumentedScheduledExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InstrumentedScheduledExecutorService(com.codahale.metrics.InstrumentedScheduledExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Example 2 with InstrumentedThreadFactory

use of com.codahale.metrics.InstrumentedThreadFactory in project dropwizard by dropwizard.

the class ScheduledExecutorServiceBuilder method build.

public ScheduledExecutorService build() {
    final InstrumentedThreadFactory instrumentedThreadFactory = new InstrumentedThreadFactory(threadFactory, environment.getMetricRegistry(), nameFormat);
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(poolSize, instrumentedThreadFactory, handler);
    executor.setRemoveOnCancelPolicy(removeOnCancel);
    environment.manage(new ExecutorServiceManager(executor, shutdownTime, nameFormat));
    return executor;
}
Also used : InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ExecutorServiceManager(io.dropwizard.lifecycle.ExecutorServiceManager)

Example 3 with InstrumentedThreadFactory

use of com.codahale.metrics.InstrumentedThreadFactory in project janusgraph by JanusGraph.

the class ExecutorServiceInstrumentationTest method shouldCreateInstrumentedThreadFactory.

@Test
public void shouldCreateInstrumentedThreadFactory() throws InterruptedException {
    MetricRegistry registry = new MetricRegistry();
    InstrumentedThreadFactory threadFactory = ExecutorServiceInstrumentation.instrument("org.janusgraph", "test", new ThreadFactoryBuilder().build(), registry);
    ExecutorService executorService = Executors.newSingleThreadExecutor(threadFactory);
    try {
        Lock lock = new ReentrantLock();
        lock.lock();
        try {
            // Spawn a thread that will wait for the lock to become available
            executorService.submit(lock::lock);
            // Wait for the thread to start
            Thread.sleep(100);
            assertEquals(1L, registry.getMeters().get("org.janusgraph.threadpools.test.threadFactory.created").getCount());
            assertEquals(1L, registry.getCounters().get("org.janusgraph.threadpools.test.threadFactory.running").getCount());
            assertEquals(0L, registry.getMeters().get("org.janusgraph.threadpools.test.threadFactory.terminated").getCount());
        } finally {
            lock.unlock();
        }
    } finally {
        executorService.shutdown();
    }
    // Wait for the thread to stop
    Thread.sleep(100);
    assertEquals(1L, registry.getMeters().get("org.janusgraph.threadpools.test.threadFactory.terminated").getCount());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) InstrumentedExecutorService(com.codahale.metrics.InstrumentedExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Test(org.junit.jupiter.api.Test)

Example 4 with InstrumentedThreadFactory

use of com.codahale.metrics.InstrumentedThreadFactory in project dropwizard by dropwizard.

the class ExecutorServiceBuilder method build.

public ExecutorService build() {
    if (corePoolSize != maximumPoolSize && maximumPoolSize > 1 && !isBoundedQueue()) {
        log.warn("Parameter 'maximumPoolSize' is conflicting with unbounded work queues");
    }
    final String nameWithoutFormat = getNameWithoutFormat(nameFormat);
    final ThreadFactory instrumentedThreadFactory = new InstrumentedThreadFactory(threadFactory, environment.getMetricRegistry(), nameWithoutFormat);
    final ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime.getQuantity(), keepAliveTime.getUnit(), workQueue, instrumentedThreadFactory, handler);
    executor.allowCoreThreadTimeOut(allowCoreThreadTimeOut);
    environment.manage(new ExecutorServiceManager(executor, shutdownTime, nameFormat));
    return executor;
}
Also used : InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) ExecutorServiceManager(io.dropwizard.lifecycle.ExecutorServiceManager) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 5 with InstrumentedThreadFactory

use of com.codahale.metrics.InstrumentedThreadFactory in project graylog2-server by Graylog2.

the class JobWorkerPool method buildExecutor.

private static ExecutorService buildExecutor(String name, int poolSize, MetricRegistry metricRegistry) {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(NAME_PREFIX + "[" + name + "]-%d").setUncaughtExceptionHandler((t, e) -> LOG.error("Unhandled exception", e)).build();
    final InstrumentedThreadFactory itf = new InstrumentedThreadFactory(threadFactory, metricRegistry, name(JobWorkerPool.class, name));
    final SynchronousQueue<Runnable> workQueue = new SynchronousQueue<>();
    final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, poolSize, 60L, TimeUnit.SECONDS, workQueue, itf);
    return new InstrumentedExecutorService(executor, metricRegistry, name(EXECUTOR_NAME, name));
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) MetricRegistry(com.codahale.metrics.MetricRegistry) GracefulShutdownService(org.graylog2.system.shutdown.GracefulShutdownService) Logger(org.slf4j.Logger) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Semaphore(java.util.concurrent.Semaphore) LoggerFactory(org.slf4j.LoggerFactory) Assisted(com.google.inject.assistedinject.Assisted) GracefulShutdownHook(org.graylog2.system.shutdown.GracefulShutdownHook) Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) InstrumentedExecutorService(com.codahale.metrics.InstrumentedExecutorService) Gauge(com.codahale.metrics.Gauge) Pattern(java.util.regex.Pattern) ThreadFactory(java.util.concurrent.ThreadFactory) MetricRegistry.name(com.codahale.metrics.MetricRegistry.name) ExecutorService(java.util.concurrent.ExecutorService) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) InstrumentedThreadFactory(com.codahale.metrics.InstrumentedThreadFactory) InstrumentedExecutorService(com.codahale.metrics.InstrumentedExecutorService) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

InstrumentedThreadFactory (com.codahale.metrics.InstrumentedThreadFactory)6 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)4 ThreadFactory (java.util.concurrent.ThreadFactory)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)2 ExecutorServiceManager (io.dropwizard.lifecycle.ExecutorServiceManager)2 ExecutorService (java.util.concurrent.ExecutorService)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Gauge (com.codahale.metrics.Gauge)1 InstrumentedScheduledExecutorService (com.codahale.metrics.InstrumentedScheduledExecutorService)1 MetricRegistry.name (com.codahale.metrics.MetricRegistry.name)1 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Assisted (com.google.inject.assistedinject.Assisted)1 HealthResponderFactory (io.dropwizard.health.response.HealthResponderFactory)1