Skip to content

Commit

Permalink
Merge pull request Netflix#1363 from mattrjacobs/fix-semaphore-only-g…
Browse files Browse the repository at this point in the history
…roups-from-showing-up-in-threadpool-output

Filter out all thread pool objects from thread pool metric streams which have not executed a command
  • Loading branch information
mattrjacobs authored Sep 26, 2016
2 parents d31ec7d + 04f33ad commit cdd4dc0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import rx.functions.Func0;
import rx.functions.Func2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -93,7 +95,18 @@ public static HystrixThreadPoolMetrics getInstance(HystrixThreadPoolKey key) {
* @return {@code Collection<HystrixThreadPoolMetrics>}
*/
public static Collection<HystrixThreadPoolMetrics> getInstances() {
return Collections.unmodifiableCollection(metrics.values());
List<HystrixThreadPoolMetrics> threadPoolMetrics = new ArrayList<HystrixThreadPoolMetrics>();
for (HystrixThreadPoolMetrics tpm: metrics.values()) {
if (hasExecutedCommandsOnThread(tpm)) {
threadPoolMetrics.add(tpm);
}
}

return Collections.unmodifiableCollection(threadPoolMetrics);
}

private static boolean hasExecutedCommandsOnThread(HystrixThreadPoolMetrics threadPoolMetrics) {
return threadPoolMetrics.getCurrentCompletedTaskCount().intValue() > 0;
}

public static final Func2<long[], HystrixCommandCompletion, long[]> appendEventToBucket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,50 @@

public class HystrixThreadPoolMetricsTest {

private static final HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("HystrixThreadPoolMetrics-UnitTest");
private static final HystrixThreadPoolKey tpKey = HystrixThreadPoolKey.Factory.asKey("HystrixThreadPoolMetrics-ThreadPool");
private static final HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("HystrixThreadPoolMetrics-UnitTest");
private static final HystrixThreadPoolKey tpKey = HystrixThreadPoolKey.Factory.asKey("HystrixThreadPoolMetrics-ThreadPool");

@Before
public void resetAll() {
HystrixThreadPoolMetrics.reset();
}
HystrixThreadPoolMetrics.reset();
}

@Test
public void shouldYieldNoExecutedTasksOnStartup() throws Exception {
//given
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();
@Test
public void shouldYieldNoExecutedTasksOnStartup() throws Exception {
//given
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();

//then
assertEquals(0, instances.size());
//then
assertEquals(0, instances.size());

}
@Test
public void shouldReturnOneExecutedTask() throws Exception {
//given
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();
RollingThreadPoolEventCounterStream.getInstance(tpKey, 10, 100).startCachingStreamValuesIfUnstarted();
}
@Test
public void shouldReturnOneExecutedTask() throws Exception {
//given
RollingThreadPoolEventCounterStream.getInstance(tpKey, 10, 100).startCachingStreamValuesIfUnstarted();

//when
new NoOpHystrixCommand().execute();
new NoOpHystrixCommand().execute();
Thread.sleep(100);

//then
Thread.sleep(100);
assertEquals(1, instances.size());
assertEquals(1, instances.iterator().next().getRollingCountThreadsExecuted());
}
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();

private static class NoOpHystrixCommand extends HystrixCommand<Void> {
public NoOpHystrixCommand() {
super(Setter.withGroupKey(groupKey)
//then
assertEquals(1, instances.size());
HystrixThreadPoolMetrics metrics = instances.iterator().next();
assertEquals(1, instances.iterator().next().getRollingCountThreadsExecuted());
}

private static class NoOpHystrixCommand extends HystrixCommand<Void> {
public NoOpHystrixCommand() {
super(Setter.withGroupKey(groupKey)
.andThreadPoolKey(tpKey)
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(100)));
}
}

@Override
protected Void run() throws Exception {
System.out.println("Run in thread : " + Thread.currentThread().getName());
@Override
protected Void run() throws Exception {
System.out.println("Run in thread : " + Thread.currentThread().getName());
return null;
}
}


}
}
}

0 comments on commit cdd4dc0

Please sign in to comment.