com.netflix.spectator.api.patterns.ThreadPoolMonitor Java Examples

The following examples show how to use com.netflix.spectator.api.patterns.ThreadPoolMonitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThreadPoolMetersInitializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
protected void createThreadPoolMeters(String threadPoolName, Executor executor) {
  if (!ThreadPoolExecutor.class.isInstance(executor)) {
    return;
  }

  ThreadPoolMonitor.attach(registry, (ThreadPoolExecutor) executor, threadPoolName);

  if (executor instanceof ThreadPoolExecutorEx) {
    Tag idTag = new BasicTag("id", threadPoolName);

    PolledMeter.using(registry)
        .withName(REJECTED_COUNT)
        .withTag(idTag)
        .monitorMonotonicCounter((ThreadPoolExecutorEx) executor, ThreadPoolExecutorEx::getRejectedCount);
  }
}
 
Example #2
Source File: ExecutorsExt.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
/**
 * Unbounded elastic thread pool that grows and shrinks as necessary.
 */
public static ExecutorService instrumentedCachedThreadPool(Registry registry, String name) {
    ThreadFactory threadFactory = new ThreadFactoryBuilder()
            .setNameFormat(name + "-%d")
            .setDaemon(true)
            .build();
    // similar to Executors.newCachedThreadPool(), but explicitly use the concrete ThreadPoolExecutor type to ensure
    // it can be instrumented by Spectator
    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            0, Integer.MAX_VALUE,
            60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(),
            threadFactory
    );
    ThreadPoolMonitor.attach(registry, executor, name);
    return executor;
}
 
Example #3
Source File: ExecutorsExt.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
/**
 * Fixed size thread pool that pre-allocates all threads. When no more threads are available, requests will block.
 */
public static ExecutorService instrumentedFixedSizeThreadPool(Registry registry, String name, int size) {
    ThreadFactory threadFactory = new ThreadFactoryBuilder()
            .setNameFormat(name + "-%d")
            .setDaemon(true)
            .build();
    // similar to Executors.newFixedSizeThreadPool(), but explicitly use the concrete ThreadPoolExecutor type
    // to ensure it can be instrumented by Spectator
    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            size, size,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            threadFactory
    );
    ThreadPoolMonitor.attach(registry, executor, name);
    return executor;
}
 
Example #4
Source File: EVCacheExecutor.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public EVCacheExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, RejectedExecutionHandler handler, String name) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
            new LinkedBlockingQueue<Runnable>(), 
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat( "EVCacheExecutor-" + name + "-%d").build());
    this.name = name;

    maxAsyncPoolSize = EVCacheConfig.getInstance().getPropertyRepository().get("EVCacheExecutor." + name + ".max.size", Integer.class).orElse(maximumPoolSize);
    setMaximumPoolSize(maxAsyncPoolSize.get());
    coreAsyncPoolSize = EVCacheConfig.getInstance().getPropertyRepository().get("EVCacheExecutor." + name + ".core.size", Integer.class).orElse(corePoolSize);
    setCorePoolSize(coreAsyncPoolSize.get());
    setKeepAliveTime(keepAliveTime, unit);
    maxAsyncPoolSize.subscribe(this::setMaximumPoolSize);
    coreAsyncPoolSize.subscribe(i -> {
        setCorePoolSize(i);
        prestartAllCoreThreads();
    });
    
    setupMonitoring(name);
    ThreadPoolMonitor.attach(EVCacheMetricsFactory.getInstance().getRegistry(), this, EVCacheMetricsFactory.INTERNAL_EXECUTOR + "-" + name);
}
 
Example #5
Source File: EVCacheScheduledExecutor.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public EVCacheScheduledExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, RejectedExecutionHandler handler, String name) {
    super(corePoolSize, handler);
    this.name = name;

    maxAsyncPoolSize = EVCacheConfig.getInstance().getPropertyRepository().get(name + "executor.max.size", Integer.class).orElse(maximumPoolSize);
    setMaximumPoolSize(maxAsyncPoolSize.get());
    coreAsyncPoolSize = EVCacheConfig.getInstance().getPropertyRepository().get(name + "executor.core.size", Integer.class).orElse(corePoolSize);
    setCorePoolSize(coreAsyncPoolSize.get());
    setKeepAliveTime(keepAliveTime, unit);
    final ThreadFactory asyncFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat( "EVCacheScheduledExecutor-" + name + "-%d").build();
    setThreadFactory(asyncFactory);
    maxAsyncPoolSize.subscribe(this::setMaximumPoolSize);
    coreAsyncPoolSize.subscribe(i -> {
            setCorePoolSize(i);
            prestartAllCoreThreads();
    });
    
    setupMonitoring(name);
    ThreadPoolMonitor.attach(EVCacheMetricsFactory.getInstance().getRegistry(), this, EVCacheMetricsFactory.INTERNAL_EXECUTOR_SCHEDULED + "-" + name);
}
 
Example #6
Source File: TestThreadPoolPublishModelFactory.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void createDefaultPublishModel() throws JsonProcessingException {
  new Expectations() {
    {
      threadPoolExecutor.getQueue();
      result = queue;
      queue.size();
      result = 10d;
    }
  };
  new MockUp<ScheduledThreadPoolExecutor>() {
    @Mock
    void delayedExecute(RunnableScheduledFuture<?> task) {

    }
  };
  try {
    ThreadPoolMonitor.attach(registry, threadPoolExecutor, "test");

    PolledMeter.update(registry);
    PublishModelFactory factory = new PublishModelFactory(Lists.newArrayList(registry.iterator()));
    DefaultPublishModel model = factory.createDefaultPublishModel();

    Assert.assertEquals(
        "{\"test\":{\"avgTaskCount\":0.0,\"avgCompletedTaskCount\":0.0,\"currentThreadsBusy\":0,\"maxThreads\":0,\"poolSize\":0,\"corePoolSize\":0,\"queueSize\":10,\"rejected\":\"NaN\"}}",
        JsonUtils.writeValueAsString(model.getThreadPools()));
  } catch (Throwable e) {
    e.printStackTrace();
    Assert.fail("unexpected error happen. " + e.getMessage());
  }
}