Skip to content

Commit f444ce6

Browse files
authored
Make write thread pools EWMA configurable (#112283)
Relates #112206.
1 parent b8359db commit f444ce6

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

server/src/internalClusterTest/java/org/elasticsearch/threadpool/SimpleThreadPoolIT.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.elasticsearch.action.index.IndexRequestBuilder;
1212
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.common.util.concurrent.TaskExecutionTimeTrackingEsThreadPoolExecutor;
1314
import org.elasticsearch.index.query.QueryBuilders;
1415
import org.elasticsearch.plugins.Plugin;
1516
import org.elasticsearch.plugins.PluginsService;
@@ -19,6 +20,7 @@
1920
import org.elasticsearch.test.ESIntegTestCase;
2021
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
2122
import org.elasticsearch.test.ESIntegTestCase.Scope;
23+
import org.hamcrest.CoreMatchers;
2224

2325
import java.lang.management.ManagementFactory;
2426
import java.lang.management.ThreadInfo;
@@ -36,12 +38,15 @@
3638
import static java.util.function.Function.identity;
3739
import static org.elasticsearch.common.util.Maps.toUnmodifiableSortedMap;
3840
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
41+
import static org.elasticsearch.threadpool.ThreadPool.DEFAULT_INDEX_AUTOSCALING_EWMA_ALPHA;
42+
import static org.elasticsearch.threadpool.ThreadPool.WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING;
3943
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
4044
import static org.hamcrest.Matchers.contains;
4145
import static org.hamcrest.Matchers.equalTo;
4246
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4347
import static org.hamcrest.Matchers.hasEntry;
4448
import static org.hamcrest.Matchers.in;
49+
import static org.hamcrest.Matchers.instanceOf;
4550
import static org.hamcrest.Matchers.matchesRegex;
4651

4752
@ClusterScope(scope = Scope.TEST, numDataNodes = 0, numClientNodes = 0)
@@ -190,4 +195,19 @@ public void testThreadPoolMetrics() throws Exception {
190195
});
191196
}
192197

198+
public void testWriteThreadpoolEwmaAlphaSetting() {
199+
Settings settings = Settings.EMPTY;
200+
var ewmaAlpha = DEFAULT_INDEX_AUTOSCALING_EWMA_ALPHA;
201+
if (randomBoolean()) {
202+
ewmaAlpha = randomDoubleBetween(0.0, 1.0, true);
203+
settings = Settings.builder().put(WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING.getKey(), ewmaAlpha).build();
204+
}
205+
var nodeName = internalCluster().startNode(settings);
206+
var threadPool = internalCluster().getInstance(ThreadPool.class, nodeName);
207+
for (var name : List.of(ThreadPool.Names.WRITE, ThreadPool.Names.SYSTEM_WRITE, ThreadPool.Names.SYSTEM_CRITICAL_WRITE)) {
208+
assertThat(threadPool.executor(name), instanceOf(TaskExecutionTimeTrackingEsThreadPoolExecutor.class));
209+
final var executor = (TaskExecutionTimeTrackingEsThreadPoolExecutor) threadPool.executor(name);
210+
assertThat(Double.compare(executor.getEwmaAlpha(), ewmaAlpha), CoreMatchers.equalTo(0));
211+
}
212+
}
193213
}

server/src/main/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public void addValue(double newValue) {
4646
successful = averageBits.compareAndSet(currentBits, newBits);
4747
} while (successful == false);
4848
}
49+
50+
// Used for testing
51+
public double getAlpha() {
52+
return alpha;
53+
}
4954
}

server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ public void apply(Settings value, Settings current, Settings previous) {
522522
ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING,
523523
ThreadPool.LATE_TIME_INTERVAL_WARN_THRESHOLD_SETTING,
524524
ThreadPool.SLOW_SCHEDULER_TASK_WARN_THRESHOLD_SETTING,
525+
ThreadPool.WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING,
525526
FastVectorHighlighter.SETTING_TV_HIGHLIGHT_MULTI_VALUE,
526527
Node.BREAKER_TYPE_KEY,
527528
OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING,

server/src/main/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,9 @@ protected void appendThreadPoolExecutorDetails(StringBuilder sb) {
146146
public Map<Runnable, Long> getOngoingTasks() {
147147
return trackOngoingTasks ? Map.copyOf(ongoingTasks) : Map.of();
148148
}
149+
150+
// Used for testing
151+
public double getEwmaAlpha() {
152+
return executionEWMA.getAlpha();
153+
}
149154
}

server/src/main/java/org/elasticsearch/threadpool/DefaultBuiltInExecutorBuilders.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.Map;
1919

2020
import static java.util.Collections.unmodifiableMap;
21-
import static org.elasticsearch.threadpool.ThreadPool.indexAutoscalingEWMA;
21+
import static org.elasticsearch.threadpool.ThreadPool.WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING;
2222
import static org.elasticsearch.threadpool.ThreadPool.searchAutoscalingEWMA;
2323

2424
public class DefaultBuiltInExecutorBuilders implements BuiltInExecutorBuilders {
@@ -29,6 +29,7 @@ public Map<String, ExecutorBuilder> getBuilders(Settings settings, int allocated
2929
final int halfProcMaxAt5 = ThreadPool.halfAllocatedProcessorsMaxFive(allocatedProcessors);
3030
final int halfProcMaxAt10 = ThreadPool.halfAllocatedProcessorsMaxTen(allocatedProcessors);
3131
final int genericThreadPoolMax = ThreadPool.boundedBy(4 * allocatedProcessors, 128, 512);
32+
final double indexAutoscalingEWMA = WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING.get(settings);
3233

3334
Map<String, ExecutorBuilder> result = new HashMap<>();
3435
result.put(

server/src/main/java/org/elasticsearch/threadpool/ThreadPool.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static ThreadPoolType fromType(String type) {
183183
// EWMA value is at least within 90% of the new increased task duration. This value also determines the impact of a single
184184
// long-running task on the moving average and limits it roughly to 2% of the (long) task duration, e.g. if the current
185185
// moving average is 100ms, and we get one task which takes 20s the new EWMA will be ~500ms.
186-
public static final double indexAutoscalingEWMA = 0.02;
186+
public static final double DEFAULT_INDEX_AUTOSCALING_EWMA_ALPHA = 0.02;
187187

188188
private final Map<String, ExecutorHolder> executors;
189189

@@ -230,6 +230,15 @@ public Collection<ExecutorBuilder> builders() {
230230
Setting.Property.NodeScope
231231
);
232232

233+
// A setting to change the alpha parameter of the EWMA used in WRITE, SYSTEM_WRITE and SYSTEM_CRITICAL_WRITE thread pools
234+
public static final Setting<Double> WRITE_THREAD_POOLS_EWMA_ALPHA_SETTING = Setting.doubleSetting(
235+
"thread_pool.write.ewma_alpha",
236+
DEFAULT_INDEX_AUTOSCALING_EWMA_ALPHA,
237+
0.0,
238+
1.0,
239+
Setting.Property.NodeScope
240+
);
241+
233242
/**
234243
* Defines and builds the many thread pools delineated in {@link Names}.
235244
*

0 commit comments

Comments
 (0)