Search in sources :

Example 1 with ML_TASK_INDEX

use of org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX in project ml-commons by opensearch-project.

the class GetTaskTransportAction method doExecute.

@Override
protected void doExecute(Task task, ActionRequest request, ActionListener<MLTaskGetResponse> actionListener) {
    MLTaskGetRequest mlTaskGetRequest = MLTaskGetRequest.fromActionRequest(request);
    String taskId = mlTaskGetRequest.getTaskId();
    GetRequest getRequest = new GetRequest(ML_TASK_INDEX).id(taskId);
    try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
        client.get(getRequest, ActionListener.wrap(r -> {
            log.info("Completed Get Task Request, id:{}", taskId);
            if (r != null && r.isExists()) {
                try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, r.getSourceAsBytesRef())) {
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                    MLTask mlTask = MLTask.parse(parser);
                    actionListener.onResponse(MLTaskGetResponse.builder().mlTask(mlTask).build());
                } catch (Exception e) {
                    log.error("Failed to parse ml task" + r.getId(), e);
                    actionListener.onFailure(e);
                }
            } else {
                actionListener.onFailure(new MLResourceNotFoundException("Fail to find task"));
            }
        }, e -> {
            if (e instanceof IndexNotFoundException) {
                actionListener.onFailure(new MLResourceNotFoundException("Fail to find task"));
            } else {
                log.error("Failed to get ML task " + taskId, e);
                actionListener.onFailure(e);
            }
        }));
    } catch (Exception e) {
        log.error("Failed to get ML task " + taskId, e);
        actionListener.onFailure(e);
    }
}
Also used : Client(org.opensearch.client.Client) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) MLTaskGetAction(org.opensearch.ml.common.transport.task.MLTaskGetAction) MLTaskGetResponse(org.opensearch.ml.common.transport.task.MLTaskGetResponse) 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) MLTask(org.opensearch.ml.common.parameter.MLTask) TransportService(org.opensearch.transport.TransportService) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) XContentParser(org.opensearch.common.xcontent.XContentParser) ActionFilters(org.opensearch.action.support.ActionFilters) MLTaskGetRequest(org.opensearch.ml.common.transport.task.MLTaskGetRequest) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Log4j2(lombok.extern.log4j.Log4j2) Inject(org.opensearch.common.inject.Inject) ML_TASK_INDEX(org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX) ActionListener(org.opensearch.action.ActionListener) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) MLTaskGetRequest(org.opensearch.ml.common.transport.task.MLTaskGetRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) MLTaskGetRequest(org.opensearch.ml.common.transport.task.MLTaskGetRequest) XContentParser(org.opensearch.common.xcontent.XContentParser) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) MLResourceNotFoundException(org.opensearch.ml.common.exception.MLResourceNotFoundException) MLTask(org.opensearch.ml.common.parameter.MLTask)

Example 2 with ML_TASK_INDEX

use of org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX in project ml-commons by opensearch-project.

the class MLTaskManager method createMLTask.

/**
 * Create ML task. Will init ML task index first if absent.
 * @param mlTask ML task
 * @param listener action listener
 */
public void createMLTask(MLTask mlTask, ActionListener<IndexResponse> listener) {
    mlIndicesHandler.initMLTaskIndex(ActionListener.wrap(indexCreated -> {
        if (!indexCreated) {
            listener.onFailure(new RuntimeException("No response to create ML task index"));
            return;
        }
        IndexRequest request = new IndexRequest(ML_TASK_INDEX);
        try (XContentBuilder builder = XContentFactory.jsonBuilder();
            ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
            request.source(mlTask.toXContent(builder, ToXContent.EMPTY_PARAMS)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
            client.index(request, ActionListener.runBefore(listener, () -> context.restore()));
        } catch (Exception e) {
            log.error("Failed to create AD task for " + mlTask.getFunctionName() + ", " + mlTask.getTaskType(), e);
            listener.onFailure(e);
        }
    }, e -> {
        log.error("Failed to create ML index", e);
        listener.onFailure(e);
    }));
}
Also used : Client(org.opensearch.client.Client) Semaphore(java.util.concurrent.Semaphore) IndexResponse(org.opensearch.action.index.IndexResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ToXContent(org.opensearch.common.xcontent.ToXContent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MLTaskState(org.opensearch.ml.common.parameter.MLTaskState) LAST_UPDATE_TIME_FIELD(org.opensearch.ml.common.parameter.MLTask.LAST_UPDATE_TIME_FIELD) HashMap(java.util.HashMap) Instant(java.time.Instant) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) MLTask(org.opensearch.ml.common.parameter.MLTask) RestStatus(org.opensearch.rest.RestStatus) TimeUnit(java.util.concurrent.TimeUnit) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) WriteRequest(org.opensearch.action.support.WriteRequest) Map(java.util.Map) Log4j2(lombok.extern.log4j.Log4j2) XContentFactory(org.opensearch.common.xcontent.XContentFactory) UpdateRequest(org.opensearch.action.update.UpdateRequest) ML_TASK_INDEX(org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX) ActionListener(org.opensearch.action.ActionListener) IndexRequest(org.opensearch.action.index.IndexRequest) MLIndicesHandler(org.opensearch.ml.indices.MLIndicesHandler) IndexRequest(org.opensearch.action.index.IndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

Log4j2 (lombok.extern.log4j.Log4j2)2 ActionListener (org.opensearch.action.ActionListener)2 Client (org.opensearch.client.Client)2 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)2 MLTask (org.opensearch.ml.common.parameter.MLTask)2 ML_TASK_INDEX (org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX)2 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Semaphore (java.util.concurrent.Semaphore)1 TimeUnit (java.util.concurrent.TimeUnit)1 ActionRequest (org.opensearch.action.ActionRequest)1 GetRequest (org.opensearch.action.get.GetRequest)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 HandledTransportAction (org.opensearch.action.support.HandledTransportAction)1 WriteRequest (org.opensearch.action.support.WriteRequest)1 UpdateRequest (org.opensearch.action.update.UpdateRequest)1