-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Update the number of allocations per nlp process (#86277)
Adds a method to DeploymentManager to update the number of allocations per process as implemented in elastic/ml-cpp#2258.
- Loading branch information
Showing
28 changed files
with
998 additions
and
433 deletions.
There are no files selected for viewing
This file contains 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 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 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
111 changes: 111 additions & 0 deletions
111
.../src/main/java/org/elasticsearch/xpack/ml/inference/deployment/AbstractPyTorchAction.java
This file contains 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,111 @@ | ||
/* | ||
* 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.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.common.util.concurrent.AbstractRunnable; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.threadpool.Scheduler; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
import org.elasticsearch.xpack.ml.MachineLearning; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
abstract class AbstractPyTorchAction<T> extends AbstractRunnable { | ||
|
||
private final String modelId; | ||
private final long requestId; | ||
private final TimeValue timeout; | ||
private final Scheduler.Cancellable timeoutHandler; | ||
private final DeploymentManager.ProcessContext processContext; | ||
private final AtomicBoolean notified = new AtomicBoolean(); | ||
|
||
private final ActionListener<T> listener; | ||
|
||
protected AbstractPyTorchAction( | ||
String modelId, | ||
long requestId, | ||
TimeValue timeout, | ||
DeploymentManager.ProcessContext processContext, | ||
ThreadPool threadPool, | ||
ActionListener<T> listener | ||
) { | ||
this.modelId = modelId; | ||
this.requestId = requestId; | ||
this.timeout = timeout; | ||
this.timeoutHandler = threadPool.schedule( | ||
this::onTimeout, | ||
ExceptionsHelper.requireNonNull(timeout, "timeout"), | ||
MachineLearning.UTILITY_THREAD_POOL_NAME | ||
); | ||
this.processContext = processContext; | ||
this.listener = listener; | ||
} | ||
|
||
void onTimeout() { | ||
if (notified.compareAndSet(false, true)) { | ||
processContext.getTimeoutCount().incrementAndGet(); | ||
processContext.getResultProcessor().ignoreResponseWithoutNotifying(String.valueOf(requestId)); | ||
listener.onFailure( | ||
new ElasticsearchStatusException("timeout [{}] waiting for inference result", RestStatus.REQUEST_TIMEOUT, timeout) | ||
); | ||
return; | ||
} | ||
getLogger().debug("[{}] request [{}] received timeout after [{}] but listener already alerted", modelId, requestId, timeout); | ||
} | ||
|
||
void onSuccess(T result) { | ||
timeoutHandler.cancel(); | ||
if (notified.compareAndSet(false, true)) { | ||
listener.onResponse(result); | ||
return; | ||
} | ||
getLogger().debug("[{}] request [{}] received inference response but listener already notified", modelId, requestId); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
timeoutHandler.cancel(); | ||
if (notified.compareAndSet(false, true)) { | ||
processContext.getResultProcessor().ignoreResponseWithoutNotifying(String.valueOf(requestId)); | ||
listener.onFailure(e); | ||
return; | ||
} | ||
getLogger().debug( | ||
() -> new ParameterizedMessage("[{}] request [{}] received failure but listener already notified", modelId, requestId), | ||
e | ||
); | ||
} | ||
|
||
protected void onFailure(String errorMessage) { | ||
onFailure(new ElasticsearchStatusException("Error in inference process: [" + errorMessage + "]", RestStatus.INTERNAL_SERVER_ERROR)); | ||
} | ||
|
||
boolean isNotified() { | ||
return notified.get(); | ||
} | ||
|
||
long getRequestId() { | ||
return requestId; | ||
} | ||
|
||
String getModelId() { | ||
return modelId; | ||
} | ||
|
||
DeploymentManager.ProcessContext getProcessContext() { | ||
return processContext; | ||
} | ||
|
||
protected abstract Logger getLogger(); | ||
} |
103 changes: 103 additions & 0 deletions
103
...ain/java/org/elasticsearch/xpack/ml/inference/deployment/ControlMessagePyTorchAction.java
This file contains 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,103 @@ | ||
/* | ||
* 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.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xcontent.XContentFactory; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
import org.elasticsearch.xpack.ml.inference.pytorch.results.PyTorchResult; | ||
import org.elasticsearch.xpack.ml.inference.pytorch.results.ThreadSettings; | ||
|
||
import java.io.IOException; | ||
|
||
class ControlMessagePyTorchAction extends AbstractPyTorchAction<ThreadSettings> { | ||
|
||
private static final Logger logger = LogManager.getLogger(InferencePyTorchAction.class); | ||
|
||
private final int numAllocationThreads; | ||
|
||
private enum ControlMessageTypes { | ||
AllocationThreads | ||
}; | ||
|
||
ControlMessagePyTorchAction( | ||
String modelId, | ||
long requestId, | ||
int numAllocationThreads, | ||
TimeValue timeout, | ||
DeploymentManager.ProcessContext processContext, | ||
ThreadPool threadPool, | ||
ActionListener<ThreadSettings> listener | ||
) { | ||
super(modelId, requestId, timeout, processContext, threadPool, listener); | ||
this.numAllocationThreads = numAllocationThreads; | ||
} | ||
|
||
@Override | ||
protected void doRun() throws Exception { | ||
if (isNotified()) { | ||
// Should not execute request as it has already timed out while waiting in the queue | ||
logger.debug( | ||
() -> new ParameterizedMessage( | ||
"[{}] skipping control message on request [{}] as it has timed out", | ||
getModelId(), | ||
getRequestId() | ||
) | ||
); | ||
return; | ||
} | ||
|
||
final String requestIdStr = String.valueOf(getRequestId()); | ||
try { | ||
var message = buildControlMessage(requestIdStr, numAllocationThreads); | ||
|
||
getProcessContext().getResultProcessor() | ||
.registerRequest(requestIdStr, ActionListener.wrap(this::processResponse, this::onFailure)); | ||
|
||
getProcessContext().getProcess().get().writeInferenceRequest(message); | ||
} catch (IOException e) { | ||
logger.error(new ParameterizedMessage("[{}] error writing control message to the inference process", getModelId()), e); | ||
onFailure(ExceptionsHelper.serverError("Error writing control message to the inference process", e)); | ||
} catch (Exception e) { | ||
onFailure(e); | ||
} | ||
} | ||
|
||
public static BytesReference buildControlMessage(String requestId, int numAllocationThreads) throws IOException { | ||
XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
builder.startObject(); | ||
builder.field("request_id", requestId); | ||
builder.field("control", ControlMessageTypes.AllocationThreads.ordinal()); | ||
builder.field("num_allocations", numAllocationThreads); | ||
builder.endObject(); | ||
|
||
// BytesReference.bytes closes the builder | ||
return BytesReference.bytes(builder); | ||
} | ||
|
||
public void processResponse(PyTorchResult result) { | ||
if (result.isError()) { | ||
onFailure(result.errorResult().error()); | ||
return; | ||
} | ||
onSuccess(result.threadSettings()); | ||
} | ||
|
||
@Override | ||
protected Logger getLogger() { | ||
return logger; | ||
} | ||
|
||
} |
Oops, something went wrong.