|
11 | 11 |
|
12 | 12 | import org.elasticsearch.action.index.IndexRequestBuilder; |
13 | 13 | import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.util.Maps; |
14 | 15 | import org.elasticsearch.common.util.concurrent.TaskExecutionTimeTrackingEsThreadPoolExecutor; |
15 | 16 | import org.elasticsearch.index.query.QueryBuilders; |
16 | 17 | import org.elasticsearch.plugins.Plugin; |
|
31 | 32 | import java.util.HashSet; |
32 | 33 | import java.util.List; |
33 | 34 | import java.util.Map; |
34 | | -import java.util.Map.Entry; |
35 | 35 | import java.util.Set; |
| 36 | +import java.util.function.BiFunction; |
36 | 37 | import java.util.function.Function; |
37 | 38 | import java.util.regex.Pattern; |
| 39 | +import java.util.stream.Collectors; |
38 | 40 |
|
39 | | -import static java.util.function.Function.identity; |
40 | | -import static org.elasticsearch.common.util.Maps.toUnmodifiableSortedMap; |
41 | 41 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; |
42 | 42 | import static org.elasticsearch.threadpool.ThreadPool.DEFAULT_INDEX_AUTOSCALING_EWMA_ALPHA; |
43 | 43 | import static org.elasticsearch.threadpool.ThreadPool.WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING; |
44 | 44 | import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; |
45 | 45 | import static org.hamcrest.Matchers.contains; |
46 | | -import static org.hamcrest.Matchers.equalTo; |
47 | 46 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
48 | | -import static org.hamcrest.Matchers.hasEntry; |
49 | | -import static org.hamcrest.Matchers.in; |
50 | 47 | import static org.hamcrest.Matchers.instanceOf; |
51 | 48 | import static org.hamcrest.Matchers.matchesRegex; |
52 | 49 |
|
@@ -165,37 +162,78 @@ public void testThreadPoolMetrics() throws Exception { |
165 | 162 | registeredMetrics.addAll(plugin.getRegisteredMetrics(InstrumentType.LONG_ASYNC_COUNTER)); |
166 | 163 |
|
167 | 164 | tps[0].forEach(stats -> { |
168 | | - Map<String, Long> threadPoolStats = List.of( |
169 | | - Map.entry(ThreadPool.THREAD_POOL_METRIC_NAME_COMPLETED, stats.completed()), |
170 | | - Map.entry(ThreadPool.THREAD_POOL_METRIC_NAME_ACTIVE, 0L), |
171 | | - Map.entry(ThreadPool.THREAD_POOL_METRIC_NAME_CURRENT, 0L), |
172 | | - Map.entry(ThreadPool.THREAD_POOL_METRIC_NAME_LARGEST, (long) stats.largest()), |
173 | | - Map.entry(ThreadPool.THREAD_POOL_METRIC_NAME_QUEUE, 0L) |
174 | | - ).stream().collect(toUnmodifiableSortedMap(e -> stats.name() + e.getKey(), Entry::getValue)); |
175 | | - |
176 | | - Function<String, List<Long>> measurementExtractor = name -> { |
177 | | - String metricName = ThreadPool.THREAD_POOL_METRIC_PREFIX + name; |
178 | | - assertThat(metricName, in(registeredMetrics)); |
179 | | - |
180 | | - List<Measurement> measurements = name.endsWith(ThreadPool.THREAD_POOL_METRIC_NAME_COMPLETED) |
181 | | - ? plugin.getLongAsyncCounterMeasurement(metricName) |
182 | | - : plugin.getLongGaugeMeasurement(metricName); |
183 | | - return measurements.stream().map(Measurement::getLong).toList(); |
184 | | - }; |
185 | | - |
186 | | - Map<String, List<Long>> measurements = threadPoolStats.keySet() |
187 | | - .stream() |
188 | | - .collect(toUnmodifiableSortedMap(identity(), measurementExtractor)); |
| 165 | + Map<String, MetricDefinition<?>> metricDefinitions = Map.of( |
| 166 | + ThreadPool.THREAD_POOL_METRIC_NAME_COMPLETED, |
| 167 | + new MetricDefinition<>(stats.completed(), TestTelemetryPlugin::getLongAsyncCounterMeasurement, Measurement::getLong), |
| 168 | + ThreadPool.THREAD_POOL_METRIC_NAME_ACTIVE, |
| 169 | + new MetricDefinition<>(0L, TestTelemetryPlugin::getLongGaugeMeasurement, Measurement::getLong), |
| 170 | + ThreadPool.THREAD_POOL_METRIC_NAME_CURRENT, |
| 171 | + new MetricDefinition<>(0L, TestTelemetryPlugin::getLongGaugeMeasurement, Measurement::getLong), |
| 172 | + ThreadPool.THREAD_POOL_METRIC_NAME_LARGEST, |
| 173 | + new MetricDefinition<>((long) stats.largest(), TestTelemetryPlugin::getLongGaugeMeasurement, Measurement::getLong), |
| 174 | + ThreadPool.THREAD_POOL_METRIC_NAME_QUEUE, |
| 175 | + new MetricDefinition<>(0L, TestTelemetryPlugin::getLongGaugeMeasurement, Measurement::getLong) |
| 176 | + ); |
189 | 177 |
|
190 | | - logger.info("Stats of `{}`: {}", stats.name(), threadPoolStats); |
191 | | - logger.info("Measurements of `{}`: {}", stats.name(), measurements); |
| 178 | + // TaskExecutionTimeTrackingEsThreadPoolExecutor also publishes a utilization metric |
| 179 | + if (tp.executor(stats.name()) instanceof TaskExecutionTimeTrackingEsThreadPoolExecutor) { |
| 180 | + metricDefinitions = Maps.copyMapWithAddedEntry( |
| 181 | + metricDefinitions, |
| 182 | + ThreadPool.THREAD_POOL_METRIC_NAME_UTILIZATION, |
| 183 | + new MetricDefinition<>(0.0d, TestTelemetryPlugin::getDoubleGaugeMeasurement, Measurement::getDouble) |
| 184 | + ); |
| 185 | + } |
192 | 186 |
|
193 | | - threadPoolStats.forEach( |
194 | | - (metric, value) -> assertThat(measurements, hasEntry(equalTo(metric), contains(greaterThanOrEqualTo(value)))) |
| 187 | + metricDefinitions = metricDefinitions.entrySet() |
| 188 | + .stream() |
| 189 | + .collect(Collectors.toUnmodifiableMap(e -> stats.name() + e.getKey(), Map.Entry::getValue)); |
| 190 | + |
| 191 | + logger.info( |
| 192 | + "Measurements of `{}`: {}", |
| 193 | + stats.name(), |
| 194 | + metricDefinitions.entrySet() |
| 195 | + .stream() |
| 196 | + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getMeasurements(plugin, e.getKey()))) |
195 | 197 | ); |
| 198 | + |
| 199 | + // Validate all metrics |
| 200 | + metricDefinitions.forEach((name, md) -> md.assertValid(plugin, name)); |
196 | 201 | }); |
197 | 202 | } |
198 | 203 |
|
| 204 | + private static class MetricDefinition<T extends Comparable<T>> { |
| 205 | + |
| 206 | + private final T minimumValue; |
| 207 | + private final BiFunction<TestTelemetryPlugin, String, List<Measurement>> metricExtractor; |
| 208 | + private final Function<Measurement, T> valueExtractor; |
| 209 | + |
| 210 | + MetricDefinition( |
| 211 | + T minimumValue, |
| 212 | + BiFunction<TestTelemetryPlugin, String, List<Measurement>> metricExtractor, |
| 213 | + Function<Measurement, T> valueExtractor |
| 214 | + ) { |
| 215 | + this.minimumValue = minimumValue; |
| 216 | + this.metricExtractor = metricExtractor; |
| 217 | + this.valueExtractor = valueExtractor; |
| 218 | + } |
| 219 | + |
| 220 | + public List<T> getMeasurements(TestTelemetryPlugin testTelemetryPlugin, String metricSuffix) { |
| 221 | + return metricExtractor.apply(testTelemetryPlugin, ThreadPool.THREAD_POOL_METRIC_PREFIX + metricSuffix) |
| 222 | + .stream() |
| 223 | + .map(valueExtractor) |
| 224 | + .toList(); |
| 225 | + } |
| 226 | + |
| 227 | + public void assertValid(TestTelemetryPlugin testTelemetryPlugin, String metricSuffix) { |
| 228 | + List<T> metrics = getMeasurements(testTelemetryPlugin, metricSuffix); |
| 229 | + assertThat( |
| 230 | + ThreadPool.THREAD_POOL_METRIC_PREFIX + metricSuffix + " is populated", |
| 231 | + metrics, |
| 232 | + contains(greaterThanOrEqualTo(minimumValue)) |
| 233 | + ); |
| 234 | + } |
| 235 | + } |
| 236 | + |
199 | 237 | public void testWriteThreadpoolEwmaAlphaSetting() { |
200 | 238 | Settings settings = Settings.EMPTY; |
201 | 239 | var ewmaAlpha = DEFAULT_INDEX_AUTOSCALING_EWMA_ALPHA; |
|
0 commit comments