Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Calculate rejection increase and emit the delta increase of rejection as metric #124

Merged
merged 6 commits into from
Jun 23, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.elasticsearch.threadpool.ThreadPoolStats.Stats;

public class ThreadPoolMetricsCollector extends PerformanceAnalyzerMetricsCollector implements MetricsProcessor {
public static final int SAMPLING_TIME_INTERVAL = MetricsConfiguration.CONFIG_MAP.get(ThreadPoolMetricsCollector.class).samplingInterval;
private static final int KEYS_PATH_LENGTH = 0;
private StringBuilder value;
private final Map<String, ThreadPoolStatsRecord> statsRecordMap;

public ThreadPoolMetricsCollector() {
super(SAMPLING_TIME_INTERVAL, "ThreadPoolMetrics");
value = new StringBuilder();
statsRecordMap = new HashMap<>();
}

@Override
Expand All @@ -52,6 +56,24 @@ public void collectMetrics(long startTime) {

while (statsIterator.hasNext()) {
Stats stats = statsIterator.next();
long rejectionDelta = 0;
String name = stats.getName();
khushbr marked this conversation as resolved.
Show resolved Hide resolved
if (statsRecordMap.containsKey(name)) {
ThreadPoolStatsRecord lastRecord = statsRecordMap.get(name);
// if the timestamp in previous record is greater than 15s (3 * intervals),
// then the scheduler might hang or freeze due to long GC etc. We simply drop
// previous record here and set rejectionDelta to 0.
if (startTime - lastRecord.getTimestamp() <= SAMPLING_TIME_INTERVAL * 3) {
khushbr marked this conversation as resolved.
Show resolved Hide resolved
rejectionDelta = stats.getRejected() - lastRecord.getRejected();
// we might not run into this as rejection is a LongAdder which never decrement its count.
// regardless, let's set it to 0 to be safe.
if (rejectionDelta < 0) {
rejectionDelta = 0;
}
}
}
statsRecordMap.put(name, new ThreadPoolStatsRecord(startTime, stats.getRejected()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to clear the statsRecordMap map anywhere ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to clear the map because there are only a fixed amount of types of queue in thread pool and we always compare the timestamp before writing into the map.

final long finalRejectionDelta = rejectionDelta;
ThreadPoolStatus threadPoolStatus = AccessController.doPrivileged((PrivilegedAction<ThreadPoolStatus>) () -> {
try {
//This is for backward compatibility. core ES may or may not emit latency metric
Expand All @@ -64,13 +86,13 @@ public void collectMetrics(long startTime) {
Method getCapacityMethod = Stats.class.getMethod("getCapacity");
int capacity = (Integer) getCapacityMethod.invoke(stats);
return new ThreadPoolStatus(stats.getName(),
stats.getQueue(), stats.getRejected(),
stats.getQueue(), finalRejectionDelta,
stats.getThreads(), stats.getActive(),
latency, capacity);
} catch (Exception e) {
//core ES does not have the latency patch. send the threadpool metrics without adding latency.
return new ThreadPoolStatus(stats.getName(),
stats.getQueue(), stats.getRejected(),
stats.getQueue(), finalRejectionDelta,
stats.getThreads(), stats.getActive());
}
});
Expand All @@ -90,6 +112,24 @@ public String getMetricsPath(long startTime, String... keysPath) {
return PerformanceAnalyzerMetrics.generatePath(startTime, PerformanceAnalyzerMetrics.sThreadPoolPath);
}

private static class ThreadPoolStatsRecord {
private final long timestamp;
private final long rejected;

ThreadPoolStatsRecord(long timestamp, long rejected) {
this.timestamp = timestamp;
this.rejected = rejected;
}

public long getTimestamp() {
return timestamp;
}

public long getRejected() {
return rejected;
}
}

public static class ThreadPoolStatus extends MetricStatus {
public final String type;
public final int queueSize;
Expand Down