Skip to content

Commit 01ec8a9

Browse files
committed
[ML] Hide internal Job update options from the REST API (#30537)
1 parent 42d16c7 commit 01ec8a9

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateJobAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public PutJobAction.Response newResponse() {
4545
public static class Request extends AcknowledgedRequest<UpdateJobAction.Request> implements ToXContentObject {
4646

4747
public static UpdateJobAction.Request parseRequest(String jobId, XContentParser parser) {
48-
JobUpdate update = JobUpdate.PARSER.apply(parser, null).setJobId(jobId).build();
48+
JobUpdate update = JobUpdate.EXTERNAL_PARSER.apply(parser, null).setJobId(jobId).build();
4949
return new UpdateJobAction.Request(jobId, update);
5050
}
5151

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdate.java

+30-22
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,34 @@
3030
public class JobUpdate implements Writeable, ToXContentObject {
3131
public static final ParseField DETECTORS = new ParseField("detectors");
3232

33-
public static final ConstructingObjectParser<Builder, Void> PARSER = new ConstructingObjectParser<>(
33+
// For internal updates
34+
static final ConstructingObjectParser<Builder, Void> INTERNAL_PARSER = new ConstructingObjectParser<>(
35+
"job_update", args -> new Builder((String) args[0]));
36+
37+
// For parsing REST requests
38+
public static final ConstructingObjectParser<Builder, Void> EXTERNAL_PARSER = new ConstructingObjectParser<>(
3439
"job_update", args -> new Builder((String) args[0]));
3540

3641
static {
37-
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), Job.ID);
38-
PARSER.declareStringArray(Builder::setGroups, Job.GROUPS);
39-
PARSER.declareStringOrNull(Builder::setDescription, Job.DESCRIPTION);
40-
PARSER.declareObjectArray(Builder::setDetectorUpdates, DetectorUpdate.PARSER, DETECTORS);
41-
PARSER.declareObject(Builder::setModelPlotConfig, ModelPlotConfig.CONFIG_PARSER, Job.MODEL_PLOT_CONFIG);
42-
PARSER.declareObject(Builder::setAnalysisLimits, AnalysisLimits.CONFIG_PARSER, Job.ANALYSIS_LIMITS);
43-
PARSER.declareString((builder, val) -> builder.setBackgroundPersistInterval(
44-
TimeValue.parseTimeValue(val, Job.BACKGROUND_PERSIST_INTERVAL.getPreferredName())), Job.BACKGROUND_PERSIST_INTERVAL);
45-
PARSER.declareLong(Builder::setRenormalizationWindowDays, Job.RENORMALIZATION_WINDOW_DAYS);
46-
PARSER.declareLong(Builder::setResultsRetentionDays, Job.RESULTS_RETENTION_DAYS);
47-
PARSER.declareLong(Builder::setModelSnapshotRetentionDays, Job.MODEL_SNAPSHOT_RETENTION_DAYS);
48-
PARSER.declareStringArray(Builder::setCategorizationFilters, AnalysisConfig.CATEGORIZATION_FILTERS);
49-
PARSER.declareField(Builder::setCustomSettings, (p, c) -> p.map(), Job.CUSTOM_SETTINGS, ObjectParser.ValueType.OBJECT);
50-
PARSER.declareString(Builder::setModelSnapshotId, Job.MODEL_SNAPSHOT_ID);
51-
PARSER.declareLong(Builder::setEstablishedModelMemory, Job.ESTABLISHED_MODEL_MEMORY);
52-
PARSER.declareString(Builder::setJobVersion, Job.JOB_VERSION);
42+
for (ConstructingObjectParser<Builder, Void> parser : Arrays.asList(INTERNAL_PARSER, EXTERNAL_PARSER)) {
43+
parser.declareString(ConstructingObjectParser.optionalConstructorArg(), Job.ID);
44+
parser.declareStringArray(Builder::setGroups, Job.GROUPS);
45+
parser.declareStringOrNull(Builder::setDescription, Job.DESCRIPTION);
46+
parser.declareObjectArray(Builder::setDetectorUpdates, DetectorUpdate.PARSER, DETECTORS);
47+
parser.declareObject(Builder::setModelPlotConfig, ModelPlotConfig.CONFIG_PARSER, Job.MODEL_PLOT_CONFIG);
48+
parser.declareObject(Builder::setAnalysisLimits, AnalysisLimits.CONFIG_PARSER, Job.ANALYSIS_LIMITS);
49+
parser.declareString((builder, val) -> builder.setBackgroundPersistInterval(
50+
TimeValue.parseTimeValue(val, Job.BACKGROUND_PERSIST_INTERVAL.getPreferredName())), Job.BACKGROUND_PERSIST_INTERVAL);
51+
parser.declareLong(Builder::setRenormalizationWindowDays, Job.RENORMALIZATION_WINDOW_DAYS);
52+
parser.declareLong(Builder::setResultsRetentionDays, Job.RESULTS_RETENTION_DAYS);
53+
parser.declareLong(Builder::setModelSnapshotRetentionDays, Job.MODEL_SNAPSHOT_RETENTION_DAYS);
54+
parser.declareStringArray(Builder::setCategorizationFilters, AnalysisConfig.CATEGORIZATION_FILTERS);
55+
parser.declareField(Builder::setCustomSettings, (p, c) -> p.map(), Job.CUSTOM_SETTINGS, ObjectParser.ValueType.OBJECT);
56+
}
57+
// These fields should not be set by a REST request
58+
INTERNAL_PARSER.declareString(Builder::setModelSnapshotId, Job.MODEL_SNAPSHOT_ID);
59+
INTERNAL_PARSER.declareLong(Builder::setEstablishedModelMemory, Job.ESTABLISHED_MODEL_MEMORY);
60+
INTERNAL_PARSER.declareString(Builder::setJobVersion, Job.JOB_VERSION);
5361
}
5462

5563
private final String jobId;
@@ -224,14 +232,14 @@ public Long getEstablishedModelMemory() {
224232
return establishedModelMemory;
225233
}
226234

227-
public boolean isAutodetectProcessUpdate() {
228-
return modelPlotConfig != null || detectorUpdates != null;
229-
}
230-
231235
public Version getJobVersion() {
232236
return jobVersion;
233237
}
234238

239+
public boolean isAutodetectProcessUpdate() {
240+
return modelPlotConfig != null || detectorUpdates != null;
241+
}
242+
235243
@Override
236244
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
237245
builder.startObject();
@@ -332,7 +340,7 @@ public Set<String> getUpdateFields() {
332340
/**
333341
* Updates {@code source} with the new values in this object returning a new {@link Job}.
334342
*
335-
* @param source Source job to be updated
343+
* @param source Source job to be updated
336344
* @param maxModelMemoryLimit The maximum model memory allowed
337345
* @return A new job equivalent to {@code source} updated.
338346
*/

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdateTests.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
public class JobUpdateTests extends AbstractSerializingTestCase<JobUpdate> {
2828

29+
private boolean useInternalParser = randomBoolean();
30+
2931
@Override
3032
protected JobUpdate createTestInstance() {
3133
JobUpdate.Builder update = new JobUpdate.Builder(randomAlphaOfLength(4));
@@ -84,13 +86,13 @@ protected JobUpdate createTestInstance() {
8486
if (randomBoolean()) {
8587
update.setCustomSettings(Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10)));
8688
}
87-
if (randomBoolean()) {
89+
if (useInternalParser && randomBoolean()) {
8890
update.setModelSnapshotId(randomAlphaOfLength(10));
8991
}
90-
if (randomBoolean()) {
92+
if (useInternalParser && randomBoolean()) {
9193
update.setEstablishedModelMemory(randomNonNegativeLong());
9294
}
93-
if (randomBoolean()) {
95+
if (useInternalParser && randomBoolean()) {
9496
update.setJobVersion(randomFrom(Version.CURRENT, Version.V_6_2_0, Version.V_6_1_0));
9597
}
9698

@@ -104,7 +106,11 @@ protected Writeable.Reader<JobUpdate> instanceReader() {
104106

105107
@Override
106108
protected JobUpdate doParseInstance(XContentParser parser) {
107-
return JobUpdate.PARSER.apply(parser, null).build();
109+
if (useInternalParser) {
110+
return JobUpdate.INTERNAL_PARSER.apply(parser, null).build();
111+
} else {
112+
return JobUpdate.EXTERNAL_PARSER.apply(parser, null).build();
113+
}
108114
}
109115

110116
public void testMergeWithJob() {
@@ -141,7 +147,7 @@ public void testMergeWithJob() {
141147
JobUpdate update = updateBuilder.build();
142148

143149
Job.Builder jobBuilder = new Job.Builder("foo");
144-
jobBuilder.setGroups(Arrays.asList("group-1"));
150+
jobBuilder.setGroups(Collections.singletonList("group-1"));
145151
Detector.Builder d1 = new Detector.Builder("info_content", "domain");
146152
d1.setOverFieldName("mlcategory");
147153
Detector.Builder d2 = new Detector.Builder("min", "field");

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/delete_model_snapshot.yml

+21-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,24 @@ setup:
8888
"description": "second",
8989
"latest_record_time_stamp": "2016-06-01T00:00:00Z",
9090
"latest_result_time_stamp": "2016-06-01T00:00:00Z",
91-
"snapshot_doc_count": 3
91+
"snapshot_doc_count": 3,
92+
"model_size_stats": {
93+
"job_id" : "delete-model-snapshot",
94+
"result_type" : "model_size_stats",
95+
"model_bytes" : 0,
96+
"total_by_field_count" : 101,
97+
"total_over_field_count" : 0,
98+
"total_partition_field_count" : 0,
99+
"bucket_allocation_failures_count" : 0,
100+
"memory_status" : "ok",
101+
"log_time" : 1495808248662,
102+
"timestamp" : 1495808248662
103+
},
104+
"quantiles": {
105+
"job_id": "delete-model-snapshot",
106+
"timestamp": 1495808248662,
107+
"quantile_state": "quantiles-1"
108+
}
92109
}
93110
94111
- do:
@@ -106,12 +123,10 @@ setup:
106123
- do:
107124
headers:
108125
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
109-
xpack.ml.update_job:
126+
xpack.ml.revert_model_snapshot:
110127
job_id: delete-model-snapshot
111-
body: >
112-
{
113-
"model_snapshot_id": "active-snapshot"
114-
}
128+
snapshot_id: "active-snapshot"
129+
115130

116131
---
117132
"Test delete snapshot missing snapshotId":

0 commit comments

Comments
 (0)