Skip to content

Commit 9766da8

Browse files
committed
[ML] Job config document CRUD operations (#32738)
1 parent 8268e0c commit 9766da8

File tree

7 files changed

+1039
-54
lines changed

7 files changed

+1039
-54
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,25 @@ public Job(StreamInput in) throws IOException {
244244
deleted = in.readBoolean();
245245
}
246246

247+
/**
248+
* Get the persisted job document name from the Job Id.
249+
* Throws if {@code jobId} is not a valid job Id.
250+
*
251+
* @param jobId The job id
252+
* @return The id of document the job is persisted in
253+
*/
254+
public static String documentId(String jobId) {
255+
if (!MlStrings.isValidId(jobId)) {
256+
throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_ID, ID.getPreferredName(), jobId));
257+
}
258+
if (!MlStrings.hasValidLengthForId(jobId)) {
259+
throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_ID_TOO_LONG, MlStrings.ID_LENGTH_LIMIT));
260+
}
261+
262+
return "job-" + jobId;
263+
}
264+
265+
247266
/**
248267
* Return the Job Id.
249268
*
@@ -759,6 +778,10 @@ public void setGroups(List<String> groups) {
759778
this.groups = groups == null ? Collections.emptyList() : groups;
760779
}
761780

781+
public List<String> getGroups() {
782+
return groups;
783+
}
784+
762785
public Builder setCustomSettings(Map<String, Object> customSettings) {
763786
this.customSettings = customSettings;
764787
return this;

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java

+541
Large diffs are not rendered by default.

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java

+27
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
*/
66
package org.elasticsearch.xpack.ml;
77

8+
import org.elasticsearch.cluster.ClusterState;
89
import org.elasticsearch.common.settings.Settings;
10+
import org.elasticsearch.common.unit.ByteSizeValue;
911
import org.elasticsearch.license.LicenseService;
12+
import org.elasticsearch.plugins.Plugin;
1013
import org.elasticsearch.test.ESSingleNodeTestCase;
14+
import org.elasticsearch.xpack.core.XPackSettings;
1115
import org.elasticsearch.xpack.core.ml.MachineLearningField;
1216

17+
import java.util.Collection;
18+
1319
/**
1420
* An extention to {@link ESSingleNodeTestCase} that adds node settings specifically needed for ML test cases.
1521
*/
@@ -18,10 +24,31 @@ public abstract class MlSingleNodeTestCase extends ESSingleNodeTestCase {
1824
@Override
1925
protected Settings nodeSettings() {
2026
Settings.Builder newSettings = Settings.builder();
27+
newSettings.put(super.nodeSettings());
28+
2129
// Disable native ML autodetect_process as the c++ controller won't be available
2230
newSettings.put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false);
31+
newSettings.put(MachineLearningField.MAX_MODEL_MEMORY_LIMIT.getKey(), new ByteSizeValue(1024));
2332
newSettings.put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial");
33+
// Disable security otherwise delete-by-query action fails to get authorized
34+
newSettings.put(XPackSettings.SECURITY_ENABLED.getKey(), false);
35+
newSettings.put(XPackSettings.MONITORING_ENABLED.getKey(), false);
36+
newSettings.put(XPackSettings.WATCHER_ENABLED.getKey(), false);
2437
return newSettings.build();
2538
}
2639

40+
@Override
41+
protected Collection<Class<? extends Plugin>> getPlugins() {
42+
return pluginList(LocalStateMachineLearning.class);
43+
}
44+
45+
protected void waitForMlTemplates() throws Exception {
46+
// block until the templates are installed
47+
assertBusy(() -> {
48+
ClusterState state = client().admin().cluster().prepareState().get().getState();
49+
assertTrue("Timed out waiting for the ML templates to be installed",
50+
MachineLearning.allTemplatesInstalled(state));
51+
});
52+
}
53+
2754
}

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java

+1-24
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
*/
66
package org.elasticsearch.xpack.ml.integration;
77

8-
import org.elasticsearch.cluster.ClusterState;
98
import org.elasticsearch.cluster.routing.UnassignedInfo;
109
import org.elasticsearch.common.io.stream.Writeable;
1110
import org.elasticsearch.common.settings.Settings;
1211
import org.elasticsearch.common.unit.TimeValue;
1312
import org.elasticsearch.common.xcontent.ToXContent;
1413
import org.elasticsearch.index.reindex.ReindexPlugin;
1514
import org.elasticsearch.plugins.Plugin;
16-
import org.elasticsearch.xpack.core.XPackSettings;
1715
import org.elasticsearch.xpack.core.ml.action.DeleteJobAction;
1816
import org.elasticsearch.xpack.core.ml.action.PutJobAction;
1917
import org.elasticsearch.xpack.core.ml.action.util.QueryPage;
@@ -32,7 +30,6 @@
3230
import org.elasticsearch.xpack.core.ml.job.results.Influencer;
3331
import org.elasticsearch.xpack.core.ml.job.results.ModelPlot;
3432
import org.elasticsearch.xpack.ml.LocalStateMachineLearning;
35-
import org.elasticsearch.xpack.ml.MachineLearning;
3633
import org.elasticsearch.xpack.ml.MlSingleNodeTestCase;
3734
import org.elasticsearch.xpack.ml.job.persistence.BucketsQueryBuilder;
3835
import org.elasticsearch.xpack.ml.job.persistence.InfluencersQueryBuilder;
@@ -77,17 +74,6 @@ public class AutodetectResultProcessorIT extends MlSingleNodeTestCase {
7774
private AutoDetectResultProcessor resultProcessor;
7875
private Renormalizer renormalizer;
7976

80-
@Override
81-
protected Settings nodeSettings() {
82-
Settings.Builder newSettings = Settings.builder();
83-
newSettings.put(super.nodeSettings());
84-
// Disable security otherwise delete-by-query action fails to get authorized
85-
newSettings.put(XPackSettings.SECURITY_ENABLED.getKey(), false);
86-
newSettings.put(XPackSettings.MONITORING_ENABLED.getKey(), false);
87-
newSettings.put(XPackSettings.WATCHER_ENABLED.getKey(), false);
88-
return newSettings.build();
89-
}
90-
9177
@Override
9278
protected Collection<Class<? extends Plugin>> getPlugins() {
9379
return pluginList(LocalStateMachineLearning.class, ReindexPlugin.class);
@@ -108,7 +94,7 @@ protected void updateModelSnapshotIdOnJob(ModelSnapshot modelSnapshot) {
10894
capturedUpdateModelSnapshotOnJobRequests.add(modelSnapshot);
10995
}
11096
};
111-
putIndexTemplates();
97+
waitForMlTemplates();
11298
putJob();
11399
}
114100

@@ -287,15 +273,6 @@ public void testEndOfStreamTriggersPersisting() throws Exception {
287273
assertResultsAreSame(allRecords, persistedRecords);
288274
}
289275

290-
private void putIndexTemplates() throws Exception {
291-
// block until the templates are installed
292-
assertBusy(() -> {
293-
ClusterState state = client().admin().cluster().prepareState().get().getState();
294-
assertTrue("Timed out waiting for the ML templates to be installed",
295-
MachineLearning.allTemplatesInstalled(state));
296-
});
297-
}
298-
299276
private void putJob() {
300277
Detector detector = new Detector.Builder("dc", "by_instance").build();
301278
Job.Builder jobBuilder = new Job.Builder(JOB_ID);

0 commit comments

Comments
 (0)