Skip to content

Commit 04f33ad

Browse files
author
Matt Jacobs
committed
Filter out all thread pool objects from thread pool metric streams which have not executed a command
1 parent d31ec7d commit 04f33ad

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

hystrix-core/src/main/java/com/netflix/hystrix/HystrixThreadPoolMetrics.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import rx.functions.Func0;
2626
import rx.functions.Func2;
2727

28+
import java.util.ArrayList;
2829
import java.util.Collection;
2930
import java.util.Collections;
31+
import java.util.List;
3032
import java.util.concurrent.BlockingQueue;
3133
import java.util.concurrent.ConcurrentHashMap;
3234
import java.util.concurrent.ThreadPoolExecutor;
@@ -93,7 +95,18 @@ public static HystrixThreadPoolMetrics getInstance(HystrixThreadPoolKey key) {
9395
* @return {@code Collection<HystrixThreadPoolMetrics>}
9496
*/
9597
public static Collection<HystrixThreadPoolMetrics> getInstances() {
96-
return Collections.unmodifiableCollection(metrics.values());
98+
List<HystrixThreadPoolMetrics> threadPoolMetrics = new ArrayList<HystrixThreadPoolMetrics>();
99+
for (HystrixThreadPoolMetrics tpm: metrics.values()) {
100+
if (hasExecutedCommandsOnThread(tpm)) {
101+
threadPoolMetrics.add(tpm);
102+
}
103+
}
104+
105+
return Collections.unmodifiableCollection(threadPoolMetrics);
106+
}
107+
108+
private static boolean hasExecutedCommandsOnThread(HystrixThreadPoolMetrics threadPoolMetrics) {
109+
return threadPoolMetrics.getCurrentCompletedTaskCount().intValue() > 0;
97110
}
98111

99112
public static final Func2<long[], HystrixCommandCompletion, long[]> appendEventToBucket

hystrix-core/src/test/java/com/netflix/hystrix/HystrixThreadPoolMetricsTest.java

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,50 @@
2525

2626
public class HystrixThreadPoolMetricsTest {
2727

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

3131
@Before
3232
public void resetAll() {
33-
HystrixThreadPoolMetrics.reset();
34-
}
33+
HystrixThreadPoolMetrics.reset();
34+
}
3535

36-
@Test
37-
public void shouldYieldNoExecutedTasksOnStartup() throws Exception {
38-
//given
39-
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();
36+
@Test
37+
public void shouldYieldNoExecutedTasksOnStartup() throws Exception {
38+
//given
39+
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();
4040

41-
//then
42-
assertEquals(0, instances.size());
41+
//then
42+
assertEquals(0, instances.size());
4343

44-
}
45-
@Test
46-
public void shouldReturnOneExecutedTask() throws Exception {
47-
//given
48-
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();
49-
RollingThreadPoolEventCounterStream.getInstance(tpKey, 10, 100).startCachingStreamValuesIfUnstarted();
44+
}
45+
@Test
46+
public void shouldReturnOneExecutedTask() throws Exception {
47+
//given
48+
RollingThreadPoolEventCounterStream.getInstance(tpKey, 10, 100).startCachingStreamValuesIfUnstarted();
5049

51-
//when
52-
new NoOpHystrixCommand().execute();
50+
new NoOpHystrixCommand().execute();
51+
Thread.sleep(100);
5352

54-
//then
55-
Thread.sleep(100);
56-
assertEquals(1, instances.size());
57-
assertEquals(1, instances.iterator().next().getRollingCountThreadsExecuted());
58-
}
53+
final Collection<HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.getInstances();
5954

60-
private static class NoOpHystrixCommand extends HystrixCommand<Void> {
61-
public NoOpHystrixCommand() {
62-
super(Setter.withGroupKey(groupKey)
55+
//then
56+
assertEquals(1, instances.size());
57+
HystrixThreadPoolMetrics metrics = instances.iterator().next();
58+
assertEquals(1, instances.iterator().next().getRollingCountThreadsExecuted());
59+
}
60+
61+
private static class NoOpHystrixCommand extends HystrixCommand<Void> {
62+
public NoOpHystrixCommand() {
63+
super(Setter.withGroupKey(groupKey)
6364
.andThreadPoolKey(tpKey)
6465
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(100)));
65-
}
66+
}
6667

67-
@Override
68-
protected Void run() throws Exception {
69-
System.out.println("Run in thread : " + Thread.currentThread().getName());
68+
@Override
69+
protected Void run() throws Exception {
70+
System.out.println("Run in thread : " + Thread.currentThread().getName());
7071
return null;
71-
}
72-
}
73-
74-
72+
}
73+
}
7574
}

0 commit comments

Comments
 (0)