Skip to content
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

Adding MLTask #24

Merged
merged 5 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions plugin/src/main/java/org/opensearch/ml/model/MLTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
*/

package org.opensearch.ml.model;

import lombok.Builder;
import lombok.Data;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;

import java.io.IOException;
import java.time.Instant;

@Data
public class MLTask implements Writeable {
private final String taskId;
private final String taskType;
private String state;
private final Instant createTime;
private final String modelId;
zhanghg08 marked this conversation as resolved.
Show resolved Hide resolved

@Builder
public MLTask(String taskId, String taskType, String state, Instant createTime, String modelId) {
this.taskId = taskId;
this.taskType = taskType;
this.state = state;
this.createTime = createTime;
this.modelId = modelId;
}

public MLTask(StreamInput input) throws IOException {
this.taskId = input.readOptionalString();
this.taskType = input.readOptionalString();
this.state = input.readOptionalString();
this.createTime = input.readOptionalInstant();
this.modelId = input.readOptionalString();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(taskId);
out.writeOptionalString(taskType);
out.writeOptionalString(state);
out.writeOptionalInstant(createTime);
out.writeOptionalString(modelId);
}
}
37 changes: 37 additions & 0 deletions plugin/src/main/java/org/opensearch/ml/model/MLTaskState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
*/

package org.opensearch.ml.model;

/**
* ML task states.
* <ul>
* <li><code>CREATED</code>:
* When user send a machine-learning(ML) request(training/inference), we will create one task to track
* ML task execution and set its state as CREATED
*
* <li><code>RUNNING</code>:
* Once MLTask is dispatched to work node and start to run corresponding ML algorithm, we will set the task state as RUNNING
*
* <li><code>COMPLETED</code>:
* When all training/inference completed, we will set task state as COMPLETED
*
* <li><code>FAILED</code>:
* If any exception happen, we will set task state as FAILED
zhanghg08 marked this conversation as resolved.
Show resolved Hide resolved
* </ul>
*/
public enum MLTaskState {
CREATED,
RUNNING,
COMPLETED,
FAILED
}
18 changes: 18 additions & 0 deletions plugin/src/main/java/org/opensearch/ml/model/MLTaskType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
*/

package org.opensearch.ml.model;

public enum MLTaskType {
TRAINING,
PREDICTION
}
38 changes: 38 additions & 0 deletions plugin/src/test/java/org/opensearch/ml/model/MLTaskTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
*/

package org.opensearch.ml.model;

import org.junit.Assert;
import org.junit.Test;
import org.opensearch.common.io.stream.BytesStreamOutput;

import java.io.IOException;
import java.time.Instant;

public class MLTaskTests {
@Test
public void testWriteTo() throws IOException {
BytesStreamOutput output = new BytesStreamOutput();
Instant now = Instant.now();
MLTask task1 = MLTask.builder()
.taskId("dummy taskId")
.taskType(MLTaskType.PREDICTION.name())
.modelId(null)
.createTime(now)
.state(MLTaskState.RUNNING.name())
.build();
task1.writeTo(output);
MLTask task2 = new MLTask(output.bytes().streamInput());
Assert.assertEquals(task1, task2);
}
}