-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance profile API to add model centric result controlled by view pa…
…rameter (#714) * Enhance profile API to add model centric result controled by view paramter Signed-off-by: Zan Niu <[email protected]> * Enhance profile API to add model centric result controled by view parameter Signed-off-by: Zan Niu <[email protected]> * Enhance profile API to add model centric result controled by view parameter Signed-off-by: Zan Niu <[email protected]> --------- Signed-off-by: Zan Niu <[email protected]>
- Loading branch information
Showing
6 changed files
with
315 additions
and
9 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
plugin/src/main/java/org/opensearch/ml/action/profile/MLProfileModelResponse.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,100 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.ml.action.profile; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.io.stream.Writeable; | ||
import org.opensearch.common.xcontent.ToXContentFragment; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.ml.common.MLTask; | ||
import org.opensearch.ml.profile.MLModelProfile; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class MLProfileModelResponse implements ToXContentFragment, Writeable { | ||
@Setter | ||
private String[] targetWorkerNodes; | ||
|
||
@Setter | ||
private String[] workerNodes; | ||
|
||
private Map<String, MLModelProfile> mlModelProfileMap = new HashMap<>(); | ||
|
||
private Map<String, MLTask> mlTaskMap = new HashMap<>(); | ||
|
||
public MLProfileModelResponse(String[] targetWorkerNodes, String[] workerNodes) { | ||
this.targetWorkerNodes = targetWorkerNodes; | ||
this.workerNodes = workerNodes; | ||
} | ||
|
||
public MLProfileModelResponse(StreamInput in) throws IOException { | ||
this.workerNodes = in.readOptionalStringArray(); | ||
this.targetWorkerNodes = in.readOptionalStringArray(); | ||
if (in.readBoolean()) { | ||
this.mlModelProfileMap = in.readMap(StreamInput::readString, MLModelProfile::new); | ||
} | ||
if (in.readBoolean()) { | ||
this.mlTaskMap = in.readMap(StreamInput::readString, MLTask::new); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (targetWorkerNodes != null) { | ||
builder.field("target_worker_nodes", targetWorkerNodes); | ||
} | ||
if (workerNodes != null) { | ||
builder.field("worker_nodes", workerNodes); | ||
} | ||
if (mlModelProfileMap.size() > 0) { | ||
builder.startObject("nodes"); | ||
for (Map.Entry<String, MLModelProfile> entry : mlModelProfileMap.entrySet()) { | ||
builder.field(entry.getKey(), entry.getValue()); | ||
} | ||
builder.endObject(); | ||
} | ||
if (mlTaskMap.size() > 0) { | ||
builder.startObject("tasks"); | ||
for (Map.Entry<String, MLTask> entry : mlTaskMap.entrySet()) { | ||
builder.field(entry.getKey(), entry.getValue()); | ||
} | ||
builder.endObject(); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput streamOutput) throws IOException { | ||
streamOutput.writeOptionalStringArray(workerNodes); | ||
streamOutput.writeOptionalStringArray(targetWorkerNodes); | ||
if (mlModelProfileMap.size() > 0) { | ||
streamOutput.writeBoolean(true); | ||
streamOutput.writeMap(mlModelProfileMap, StreamOutput::writeString, (o, r) -> r.writeTo(o)); | ||
} else { | ||
streamOutput.writeBoolean(false); | ||
} | ||
if (mlTaskMap.size() > 0) { | ||
streamOutput.writeBoolean(true); | ||
streamOutput.writeMap(mlTaskMap, StreamOutput::writeString, (o, r) -> r.writeTo(o)); | ||
} else { | ||
streamOutput.writeBoolean(false); | ||
} | ||
|
||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
plugin/src/test/java/org/opensearch/ml/action/profile/MLProfileModelResponseTests.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,112 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.ml.action.profile; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.common.xcontent.ToXContent; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.commons.authuser.User; | ||
import org.opensearch.ml.common.FunctionName; | ||
import org.opensearch.ml.common.MLTask; | ||
import org.opensearch.ml.common.MLTaskState; | ||
import org.opensearch.ml.common.MLTaskType; | ||
import org.opensearch.ml.common.dataset.MLInputDataType; | ||
import org.opensearch.ml.common.model.MLModelState; | ||
import org.opensearch.ml.profile.MLModelProfile; | ||
import org.opensearch.ml.profile.MLPredictRequestStats; | ||
import org.opensearch.ml.utils.TestHelper; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
public class MLProfileModelResponseTests extends OpenSearchTestCase { | ||
|
||
MLTask mlTask; | ||
MLModelProfile mlModelProfile; | ||
|
||
@Before | ||
public void setup() { | ||
mlTask = MLTask | ||
.builder() | ||
.taskId("test_id") | ||
.modelId("model_id") | ||
.taskType(MLTaskType.TRAINING) | ||
.functionName(FunctionName.AD_LIBSVM) | ||
.state(MLTaskState.CREATED) | ||
.inputType(MLInputDataType.DATA_FRAME) | ||
.progress(0.4f) | ||
.outputIndex("test_index") | ||
.workerNodes(Arrays.asList("test_node")) | ||
.createTime(Instant.ofEpochMilli(123)) | ||
.lastUpdateTime(Instant.ofEpochMilli(123)) | ||
.error("error") | ||
.user(new User()) | ||
.async(false) | ||
.build(); | ||
mlModelProfile = MLModelProfile | ||
.builder() | ||
.predictor("test_predictor") | ||
.workerNodes(new String[] { "node1", "node2" }) | ||
.modelState(MLModelState.LOADED) | ||
.modelInferenceStats(MLPredictRequestStats.builder().count(10L).average(11.0).max(20.0).min(5.0).build()) | ||
.build(); | ||
} | ||
|
||
public void test_create_MLProfileModelResponse_withArgs() throws IOException { | ||
String[] targetWorkerNodes = new String[] { "node1", "node2" }; | ||
String[] workerNodes = new String[] { "node1" }; | ||
Map<String, MLModelProfile> profileMap = new HashMap<>(); | ||
Map<String, MLTask> taskMap = new HashMap<>(); | ||
profileMap.put("node1", mlModelProfile); | ||
taskMap.put("node1", mlTask); | ||
MLProfileModelResponse response = new MLProfileModelResponse(targetWorkerNodes, workerNodes); | ||
response.getMlModelProfileMap().putAll(profileMap); | ||
response.getMlTaskMap().putAll(taskMap); | ||
BytesStreamOutput output = new BytesStreamOutput(); | ||
response.writeTo(output); | ||
MLProfileModelResponse newResponse = new MLProfileModelResponse(output.bytes().streamInput()); | ||
assertNotNull(newResponse.getTargetWorkerNodes()); | ||
assertNotNull(response.getTargetWorkerNodes()); | ||
assertEquals(newResponse.getTargetWorkerNodes().length, response.getTargetWorkerNodes().length); | ||
assertEquals(newResponse.getMlModelProfileMap().size(), response.getMlModelProfileMap().size()); | ||
assertEquals(newResponse.getMlTaskMap().size(), response.getMlTaskMap().size()); | ||
} | ||
|
||
public void test_create_MLProfileModelResponse_NoArgs() throws IOException { | ||
MLProfileModelResponse response = new MLProfileModelResponse(); | ||
BytesStreamOutput output = new BytesStreamOutput(); | ||
response.writeTo(output); | ||
MLProfileModelResponse newResponse = new MLProfileModelResponse(output.bytes().streamInput()); | ||
assertNull(response.getWorkerNodes()); | ||
assertNull(newResponse.getWorkerNodes()); | ||
} | ||
|
||
public void test_toXContent() throws IOException { | ||
String[] targetWorkerNodes = new String[] { "node1", "node2" }; | ||
String[] workerNodes = new String[] { "node1" }; | ||
Map<String, MLModelProfile> profileMap = new HashMap<>(); | ||
Map<String, MLTask> taskMap = new HashMap<>(); | ||
profileMap.put("node1", mlModelProfile); | ||
taskMap.put("node1", mlTask); | ||
MLProfileModelResponse response = new MLProfileModelResponse(targetWorkerNodes, workerNodes); | ||
response.getMlModelProfileMap().putAll(profileMap); | ||
response.getMlTaskMap().putAll(taskMap); | ||
|
||
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); | ||
response.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
String xContentString = TestHelper.xContentBuilderToString(builder); | ||
System.out.println(xContentString); | ||
} | ||
|
||
} |
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
Oops, something went wrong.