|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.common.util.concurrent; |
| 11 | + |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | + |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import static org.elasticsearch.common.util.concurrent.EsExecutors.TaskTrackingConfig.DEFAULT_EWMA_ALPHA; |
| 18 | +import static org.hamcrest.Matchers.equalTo; |
| 19 | + |
| 20 | +public class TaskExecutionTimeTrackingPerIndexEsThreadPoolExecutorTests extends ESTestCase { |
| 21 | + |
| 22 | + String INDEX_NAME = "index"; |
| 23 | + |
| 24 | + public void testExecutionPerIndexStatistics() throws Exception { |
| 25 | + ThreadContext context = new ThreadContext(Settings.EMPTY); |
| 26 | + |
| 27 | + TaskExecutionTimeTrackingPerIndexEsThreadPoolExecutor executor = new TaskExecutionTimeTrackingPerIndexEsThreadPoolExecutor( |
| 28 | + "test-threadpool", |
| 29 | + 1, |
| 30 | + 1, |
| 31 | + 1000, |
| 32 | + TimeUnit.MILLISECONDS, |
| 33 | + ConcurrentCollections.newBlockingQueue(), |
| 34 | + TimedRunnable::new, |
| 35 | + EsExecutors.daemonThreadFactory("queuetest"), |
| 36 | + new EsAbortPolicy(), |
| 37 | + context, |
| 38 | + new EsExecutors.TaskTrackingConfig(randomBoolean(), DEFAULT_EWMA_ALPHA) |
| 39 | + ); |
| 40 | + executor.prestartAllCoreThreads(); |
| 41 | + |
| 42 | + assertThat((long) executor.getLoadEMWAPerIndex(INDEX_NAME), equalTo(0L)); |
| 43 | + assertThat(executor.getSearchLoadPerIndex(INDEX_NAME), equalTo(0L)); |
| 44 | + |
| 45 | + executeTask(executor, 1); |
| 46 | + assertBusy(() -> { |
| 47 | + assertTrue((long) executor.getLoadEMWAPerIndex(INDEX_NAME) > 0); |
| 48 | + assertTrue(executor.getSearchLoadPerIndex(INDEX_NAME) > 0); |
| 49 | + }); |
| 50 | + |
| 51 | + executor.shutdown(); |
| 52 | + executor.awaitTermination(10, TimeUnit.SECONDS); |
| 53 | + } |
| 54 | + |
| 55 | + /** Execute a blank task {@code times} times for the executor */ |
| 56 | + private void executeTask(TaskExecutionTimeTrackingPerIndexEsThreadPoolExecutor executor, int times) { |
| 57 | + for (int i = 0; i < times; i++) { |
| 58 | + Runnable runnable = () -> {}; |
| 59 | + executor.registerIndexNameForRunnable(INDEX_NAME, runnable); |
| 60 | + executor.execute(runnable); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments