-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[ML][Inference] Adding read/del trained models #47882
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
Merged
benwtrent
merged 6 commits into
elastic:feature/ml-inference
from
benwtrent:feature/ml-inference-model-read-del
Oct 18, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f3a8453
[ML][Inference] Adding read/del trained models
benwtrent 829f3c6
Merge branch 'feature/ml-inference' into feature/ml-inference-model-r…
benwtrent c34977f
addressing PR comments and fixing tests
benwtrent ee758ee
adding error tests to ml_security blacklist
benwtrent c900846
Merge branch 'feature/ml-inference' into feature/ml-inference-model-r…
benwtrent 841da05
fixing tests
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...n/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAction.java
This file contains hidden or 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,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class DeleteTrainedModelAction extends ActionType<AcknowledgedResponse> { | ||
|
||
public static final DeleteTrainedModelAction INSTANCE = new DeleteTrainedModelAction(); | ||
public static final String NAME = "cluster:admin/xpack/ml/inference/delete"; | ||
|
||
private DeleteTrainedModelAction() { | ||
super(NAME, AcknowledgedResponse::new); | ||
} | ||
|
||
public static class Request extends AcknowledgedRequest<Request> implements ToXContentFragment { | ||
|
||
private String id; | ||
|
||
public Request(StreamInput in) throws IOException { | ||
super(in); | ||
id = in.readString(); | ||
} | ||
|
||
public Request() {} | ||
|
||
public Request(String id) { | ||
this.id = ExceptionsHelper.requireNonNull(id, TrainedModelConfig.MODEL_ID); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.field(TrainedModelConfig.MODEL_ID.getPreferredName(), id); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DeleteTrainedModelAction.Request request = (DeleteTrainedModelAction.Request) o; | ||
return Objects.equals(id, request.id); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...gin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsAction.java
This file contains hidden or 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,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.action.ActionRequestBuilder; | ||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.client.ElasticsearchClient; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; | ||
import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; | ||
import org.elasticsearch.xpack.core.action.util.QueryPage; | ||
import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; | ||
|
||
import java.io.IOException; | ||
|
||
public class GetTrainedModelsAction extends ActionType<GetTrainedModelsAction.Response> { | ||
|
||
public static final GetTrainedModelsAction INSTANCE = new GetTrainedModelsAction(); | ||
public static final String NAME = "cluster:monitor/xpack/ml/inference/get"; | ||
|
||
private GetTrainedModelsAction() { | ||
super(NAME, Response::new); | ||
} | ||
|
||
public static class Request extends AbstractGetResourcesRequest { | ||
|
||
public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match"); | ||
|
||
public Request() { | ||
setAllowNoResources(true); | ||
} | ||
|
||
public Request(String id) { | ||
setResourceId(id); | ||
setAllowNoResources(true); | ||
} | ||
|
||
public Request(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public String getResourceIdField() { | ||
return TrainedModelConfig.MODEL_ID.getPreferredName(); | ||
} | ||
|
||
} | ||
|
||
public static class Response extends AbstractGetResourcesResponse<TrainedModelConfig> { | ||
|
||
public static final ParseField RESULTS_FIELD = new ParseField("trained_model_configs"); | ||
|
||
public Response(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
public Response(QueryPage<TrainedModelConfig> trainedModels) { | ||
super(trainedModels); | ||
} | ||
|
||
@Override | ||
protected Reader<TrainedModelConfig> getReader() { | ||
return TrainedModelConfig::new; | ||
} | ||
} | ||
|
||
public static class RequestBuilder extends ActionRequestBuilder<Request, Response> { | ||
|
||
public RequestBuilder(ElasticsearchClient client) { | ||
super(client, INSTANCE, new Request()); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...re/src/main/java/org/elasticsearch/xpack/core/ml/notifications/InferenceAuditMessage.java
This file contains hidden or 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,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.notifications; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; | ||
import org.elasticsearch.xpack.core.common.notifications.Level; | ||
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
|
||
import java.util.Date; | ||
|
||
|
||
public class InferenceAuditMessage extends AbstractAuditMessage { | ||
|
||
//TODO this should be MODEL_ID... | ||
private static final ParseField JOB_ID = Job.ID; | ||
public static final ConstructingObjectParser<InferenceAuditMessage, Void> PARSER = | ||
createParser("ml_inference_audit_message", InferenceAuditMessage::new, JOB_ID); | ||
|
||
public InferenceAuditMessage(String resourceId, String message, Level level, Date timestamp, String nodeName) { | ||
super(resourceId, message, level, timestamp, nodeName); | ||
} | ||
|
||
@Override | ||
public final String getJobType() { | ||
return "inference"; | ||
} | ||
|
||
@Override | ||
protected String getResourceField() { | ||
return JOB_ID.getPreferredName(); | ||
} | ||
} |
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
...src/test/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelsRequestTests.java
This file contains hidden or 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,23 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
import org.elasticsearch.xpack.core.ml.action.DeleteTrainedModelAction.Request; | ||
|
||
public class DeleteTrainedModelsRequestTests extends AbstractWireSerializingTestCase<Request> { | ||
|
||
@Override | ||
protected Request createTestInstance() { | ||
return new Request(randomAlphaOfLengthBetween(1, 20)); | ||
} | ||
|
||
@Override | ||
protected Writeable.Reader<Request> instanceReader() { | ||
return Request::new; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...re/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsRequestTests.java
This file contains hidden or 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,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
import org.elasticsearch.xpack.core.action.util.PageParams; | ||
import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction.Request; | ||
|
||
public class GetTrainedModelsRequestTests extends AbstractWireSerializingTestCase<Request> { | ||
|
||
@Override | ||
protected Request createTestInstance() { | ||
Request request = new Request(randomAlphaOfLength(20)); | ||
request.setPageParams(new PageParams(randomIntBetween(0, 100), randomIntBetween(0, 100))); | ||
return request; | ||
} | ||
|
||
@Override | ||
protected Writeable.Reader<Request> instanceReader() { | ||
return Request::new; | ||
} | ||
} |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
...n/core/src/test/java/org/elasticsearch/xpack/core/ml/notifications/AuditMessageTests.java
This file contains hidden or 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,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.notifications; | ||
|
||
import org.elasticsearch.test.AbstractXContentTestCase; | ||
import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; | ||
|
||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public abstract class AuditMessageTests<T extends AbstractAuditMessage> extends AbstractXContentTestCase<T> { | ||
|
||
public abstract String getJobType(); | ||
|
||
public void testGetJobType() { | ||
AbstractAuditMessage message = createTestInstance(); | ||
assertThat(message.getJobType(), equalTo(getJobType())); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return true; | ||
} | ||
} |
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
...c/test/java/org/elasticsearch/xpack/core/ml/notifications/InferenceAuditMessageTests.java
This file contains hidden or 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,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.notifications; | ||
|
||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.xpack.core.common.notifications.Level; | ||
|
||
import java.util.Date; | ||
|
||
public class InferenceAuditMessageTests extends AuditMessageTests<InferenceAuditMessage> { | ||
|
||
@Override | ||
public String getJobType() { | ||
return "inference"; | ||
} | ||
|
||
@Override | ||
protected InferenceAuditMessage doParseInstance(XContentParser parser) { | ||
return InferenceAuditMessage.PARSER.apply(parser, null); | ||
} | ||
|
||
@Override | ||
protected InferenceAuditMessage createTestInstance() { | ||
return new InferenceAuditMessage( | ||
randomBoolean() ? null : randomAlphaOfLength(10), | ||
randomAlphaOfLengthBetween(1, 20), | ||
randomFrom(Level.values()), | ||
new Date(), | ||
randomBoolean() ? null : randomAlphaOfLengthBetween(1, 20) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a unit test, similar to
AnomalyDetectionAuditMessageTests
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will, but it seems like overkill to me as this class essentially does nothing.