Search in sources :

Example 1 with MLModelGetResponse

use of org.opensearch.ml.common.transport.model.MLModelGetResponse in project ml-commons by opensearch-project.

the class GetModelTransportAction method doExecute.

@Override
protected void doExecute(Task task, ActionRequest request, ActionListener<MLModelGetResponse> actionListener) {
    MLModelGetRequest mlModelGetRequest = MLModelGetRequest.fromActionRequest(request);
    String modelId = mlModelGetRequest.getModelId();
    GetRequest getRequest = new GetRequest(ML_MODEL_INDEX).id(modelId);
    try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
        client.get(getRequest, ActionListener.wrap(r -> {
            log.info("Completed Get Model Request, id:{}", modelId);
            if (r != null && r.isExists()) {
                try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, r.getSourceAsBytesRef())) {
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                    MLModel mlModel = MLModel.parse(parser);
                    actionListener.onResponse(MLModelGetResponse.builder().mlModel(mlModel).build());
                } catch (Exception e) {
                    log.error("Failed to parse ml model" + r.getId(), e);
                    actionListener.onFailure(e);
                }
            } else {
                actionListener.onFailure(new MLResourceNotFoundException("Fail to find model"));
            }
        }, e -> {
            if (e instanceof IndexNotFoundException) {
                actionListener.onFailure(new MLResourceNotFoundException("Fail to find model"));
            } else {
                log.error("Failed to get ML model " + modelId, e);
                actionListener.onFailure(e);
            }
        }));
    } catch (Exception e) {
        log.error("Failed to get ML model " + modelId, e);
        actionListener.onFailure(e);
    }
}
Also used : FieldDefaults(lombok.experimental.FieldDefaults) Client(org.opensearch.client.Client) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) MLNodeUtils.createXContentParserFromRegistry(org.opensearch.ml.utils.MLNodeUtils.createXContentParserFromRegistry) ActionRequest(org.opensearch.action.ActionRequest) Task(org.opensearch.tasks.Task) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) TransportService(org.opensearch.transport.TransportService) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) XContentParser(org.opensearch.common.xcontent.XContentParser) ActionFilters(org.opensearch.action.support.ActionFilters) AccessLevel(lombok.AccessLevel) MLModelGetAction(org.opensearch.ml.common.transport.model.MLModelGetAction) ML_MODEL_INDEX(org.opensearch.ml.indices.MLIndicesHandler.ML_MODEL_INDEX) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Log4j2(lombok.extern.log4j.Log4j2) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) MLModel(org.opensearch.ml.common.parameter.MLModel) MLModelGetResponse(org.opensearch.ml.common.transport.model.MLModelGetResponse) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) GetRequest(org.opensearch.action.get.GetRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) MLModel(org.opensearch.ml.common.parameter.MLModel) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) XContentParser(org.opensearch.common.xcontent.XContentParser) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException)

Aggregations

AccessLevel (lombok.AccessLevel)1 FieldDefaults (lombok.experimental.FieldDefaults)1 Log4j2 (lombok.extern.log4j.Log4j2)1 ActionListener (org.opensearch.action.ActionListener)1 ActionRequest (org.opensearch.action.ActionRequest)1 GetRequest (org.opensearch.action.get.GetRequest)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 HandledTransportAction (org.opensearch.action.support.HandledTransportAction)1 Client (org.opensearch.client.Client)1 Inject (org.opensearch.common.inject.Inject)1 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)1 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)1 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)1 MLResourceNotFoundException (org.opensearch.ml.common.exception.MLResourceNotFoundException)1 MLModel (org.opensearch.ml.common.parameter.MLModel)1 MLModelGetAction (org.opensearch.ml.common.transport.model.MLModelGetAction)1 MLModelGetRequest (org.opensearch.ml.common.transport.model.MLModelGetRequest)1 MLModelGetResponse (org.opensearch.ml.common.transport.model.MLModelGetResponse)1