-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Add ThreadWatchdog
to ClusterApplierService
#134361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elasticsearchmachine
merged 14 commits into
elastic:main
from
DaveCTurner:2025/09/09/cluster-applier-thread-watchdog
Oct 4, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0e00ab
Add `ThreadWatchdog` to `ClusterApplierService`
DaveCTurner 756e30a
Update docs/changelog/134361.yaml
DaveCTurner b520a39
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner 8bc49e2
Test fixes
DaveCTurner e16c1eb
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner ed9a016
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner ea0fca3
Also test the other path
DaveCTurner 1b14e34
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner b7f6558
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner 5a2d4d8
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner 70ae322
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner 6e69aef
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner 116c063
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner f713035
Merge branch 'main' into 2025/09/09/cluster-applier-thread-watchdog
DaveCTurner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 134361 | ||
summary: Add `ThreadWatchdog` to `ClusterApplierService` | ||
area: Cluster Coordination | ||
type: enhancement | ||
issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...r/src/test/java/org/elasticsearch/cluster/service/ClusterApplierServiceWatchdogTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.cluster.service; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.cluster.ClusterChangedEvent; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.NodeConnectionsService; | ||
import org.elasticsearch.cluster.TimeoutClusterStateListener; | ||
import org.elasticsearch.common.Priority; | ||
import org.elasticsearch.common.settings.ClusterSettings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue; | ||
import org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.MockLog; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static org.elasticsearch.cluster.service.ClusterApplierService.CLUSTER_APPLIER_THREAD_WATCHDOG_INTERVAL; | ||
import static org.elasticsearch.cluster.service.ClusterApplierService.CLUSTER_APPLIER_THREAD_WATCHDOG_QUIET_TIME; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ClusterApplierServiceWatchdogTests extends ESTestCase { | ||
|
||
public void testThreadWatchdogLogging() { | ||
final var deterministicTaskQueue = new DeterministicTaskQueue(); | ||
|
||
final var settingsBuilder = Settings.builder(); | ||
|
||
final long intervalMillis; | ||
if (randomBoolean()) { | ||
intervalMillis = randomLongBetween(1, 1_000_000); | ||
settingsBuilder.put(CLUSTER_APPLIER_THREAD_WATCHDOG_INTERVAL.getKey(), TimeValue.timeValueMillis(intervalMillis)); | ||
} else { | ||
intervalMillis = CLUSTER_APPLIER_THREAD_WATCHDOG_INTERVAL.get(Settings.EMPTY).millis(); | ||
} | ||
|
||
final long quietTimeMillis; | ||
if (randomBoolean()) { | ||
quietTimeMillis = randomLongBetween(intervalMillis, 3 * intervalMillis); | ||
settingsBuilder.put(CLUSTER_APPLIER_THREAD_WATCHDOG_QUIET_TIME.getKey(), TimeValue.timeValueMillis(quietTimeMillis)); | ||
} else { | ||
quietTimeMillis = CLUSTER_APPLIER_THREAD_WATCHDOG_QUIET_TIME.get(Settings.EMPTY).millis(); | ||
} | ||
|
||
final var settings = settingsBuilder.build(); | ||
|
||
try ( | ||
var clusterApplierService = new ClusterApplierService( | ||
randomIdentifier(), | ||
settings, | ||
new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), | ||
deterministicTaskQueue.getThreadPool() | ||
) { | ||
@Override | ||
protected PrioritizedEsThreadPoolExecutor createThreadPoolExecutor() { | ||
return deterministicTaskQueue.getPrioritizedEsThreadPoolExecutor(); | ||
} | ||
}; | ||
var mockLog = MockLog.capture(ClusterApplierService.class) | ||
) { | ||
clusterApplierService.setNodeConnectionsService(mock(NodeConnectionsService.class)); | ||
clusterApplierService.setInitialState(ClusterState.EMPTY_STATE); | ||
clusterApplierService.start(); | ||
|
||
final AtomicBoolean completedTask = new AtomicBoolean(); | ||
|
||
final Runnable hotThreadsDumpsAsserter = () -> { | ||
final var startMillis = deterministicTaskQueue.getCurrentTimeMillis(); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
mockLog.addExpectation( | ||
new MockLog.SeenEventExpectation( | ||
"hot threads dump [" + i + "]", | ||
ClusterApplierService.class.getCanonicalName(), | ||
Level.WARN, | ||
"hot threads dump due to active threads not making progress" | ||
) | ||
); | ||
|
||
while (deterministicTaskQueue.getCurrentTimeMillis() < startMillis + 2 * intervalMillis + i * quietTimeMillis) { | ||
deterministicTaskQueue.advanceTime(); | ||
deterministicTaskQueue.runAllRunnableTasks(); | ||
} | ||
|
||
mockLog.assertAllExpectationsMatched(); | ||
} | ||
}; | ||
|
||
if (randomBoolean()) { | ||
clusterApplierService.runOnApplierThread( | ||
"slow task", | ||
randomFrom(Priority.values()), | ||
ignored -> hotThreadsDumpsAsserter.run(), | ||
ActionListener.running(() -> assertTrue(completedTask.compareAndSet(false, true))) | ||
); | ||
} else { | ||
class TestListener implements TimeoutClusterStateListener { | ||
@Override | ||
public void postAdded() { | ||
hotThreadsDumpsAsserter.run(); | ||
} | ||
|
||
@Override | ||
public void onClose() { | ||
fail("should time out before closing"); | ||
} | ||
|
||
@Override | ||
public void onTimeout(TimeValue timeout) { | ||
assertTrue(completedTask.compareAndSet(false, true)); | ||
clusterApplierService.removeTimeoutListener(TestListener.this); | ||
} | ||
|
||
@Override | ||
public void clusterChanged(ClusterChangedEvent event) { | ||
fail("no cluster state updates expected"); | ||
} | ||
} | ||
|
||
clusterApplierService.addTimeoutListener( | ||
// timeout sufficiently short that it elapses while postAdded() is still running | ||
TimeValue.timeValueMillis(randomLongBetween(0, 2 * intervalMillis + 2 * quietTimeMillis)), | ||
new TestListener() | ||
); | ||
} | ||
|
||
deterministicTaskQueue.runAllRunnableTasks(); | ||
|
||
assertTrue(completedTask.get()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hot-threads dumper still links the doc for
ReferenceDocs.NETWORK_THREADING_MODEL
which is not applicable in the new case. Do we care?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes you're right. I can't think of a great place to document this. I mean it's kind of the same principle, this thread should be frequently idle just like the
transport_worker
threads. I think I'm going to say we don't care.