-
Notifications
You must be signed in to change notification settings - Fork 25.5k
[ML] Don't block thread while waiting for work to finish on graceful shutdown #135350
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
+91
−54
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8357159
On graceful shutdown don't block thread while waiting for work to finish
davidkyle 3d4eead
Merge branch 'main' into graceful-stop-listener
davidkyle ca896c5
fix tests after conflict
davidkyle 2ac5031
Use a timeout listener
davidkyle 072e0b6
Merge branch 'main' into graceful-stop-listener
davidkyle e226379
fix delegate
davidkyle 01e8acc
Merge branch 'main' into graceful-stop-listener
davidkyle 8caf55b
Merge branch 'main' into graceful-stop-listener
davidkyle 2d23388
Merge branch 'main' into graceful-stop-listener
davidkyle 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
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
75 changes: 75 additions & 0 deletions
75
...gin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/ShutdownTracker.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,75 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.inference.deployment; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.threadpool.Scheduler; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.xpack.ml.MachineLearning; | ||
import org.elasticsearch.xpack.ml.inference.pytorch.PriorityProcessWorkerExecutorService; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class ShutdownTracker { | ||
|
||
private final ActionListener<AcknowledgedResponse> everythingStoppedListener; | ||
|
||
private final Scheduler.Cancellable timeoutHandler; | ||
private final Runnable onWorkerQueueCompletedCallback; | ||
private final Runnable onTimeoutCallback; | ||
private final Object monitor = new Object(); | ||
private final AtomicBoolean timedOutOrCompleted = new AtomicBoolean(); | ||
|
||
private static final TimeValue COMPLETION_TIMEOUT = TimeValue.timeValueMinutes(5); | ||
|
||
public ShutdownTracker( | ||
Runnable onTimeoutCallback, | ||
Runnable onWorkerQueueCompletedCallback, | ||
ThreadPool threadPool, | ||
PriorityProcessWorkerExecutorService workerQueue, | ||
ActionListener<AcknowledgedResponse> everythingStoppedListener | ||
) { | ||
this.onTimeoutCallback = onTimeoutCallback; | ||
this.onWorkerQueueCompletedCallback = onWorkerQueueCompletedCallback; | ||
this.everythingStoppedListener = ActionListener.notifyOnce(everythingStoppedListener); | ||
|
||
// initiate the worker shutdown and add this as a callback when completed | ||
workerQueue.shutdownWithCallback(this::workerQueueCompleted); | ||
// start the race with the timeout and the worker completing | ||
this.timeoutHandler = threadPool.schedule( | ||
this::onTimeout, | ||
COMPLETION_TIMEOUT, | ||
threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME) | ||
); | ||
} | ||
|
||
private void onTimeout() { | ||
synchronized (monitor) { // TODO remove the lock as the atomic should be sufficient | ||
if (timedOutOrCompleted.compareAndSet(false, true) == false) { | ||
// already completed | ||
return; | ||
} | ||
onTimeoutCallback.run(); | ||
everythingStoppedListener.onResponse(AcknowledgedResponse.FALSE); | ||
} | ||
} | ||
|
||
private void workerQueueCompleted() { | ||
synchronized (monitor) { // TODO remove the lock as the atomic should be sufficient | ||
if (timedOutOrCompleted.compareAndSet(false, true) == false) { | ||
// already completed | ||
return; | ||
} | ||
timeoutHandler.cancel(); | ||
onWorkerQueueCompletedCallback.run(); | ||
everythingStoppedListener.onResponse(AcknowledgedResponse.TRUE); | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.