|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | +package org.elasticsearch.xpack.ml.integration; |
| 8 | + |
| 9 | +import static org.hamcrest.Matchers.containsString; |
| 10 | +import static org.hamcrest.Matchers.equalTo; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.elasticsearch.ElasticsearchStatusException; |
| 19 | +import org.elasticsearch.Version; |
| 20 | +import org.elasticsearch.action.support.WriteRequest; |
| 21 | +import org.elasticsearch.client.OriginSettingClient; |
| 22 | +import org.elasticsearch.cluster.routing.OperationRouting; |
| 23 | +import org.elasticsearch.cluster.routing.allocation.decider.AwarenessAllocationDecider; |
| 24 | +import org.elasticsearch.cluster.service.ClusterApplierService; |
| 25 | +import org.elasticsearch.cluster.service.ClusterService; |
| 26 | +import org.elasticsearch.cluster.service.MasterService; |
| 27 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 28 | +import org.elasticsearch.common.settings.Settings; |
| 29 | +import org.elasticsearch.common.unit.TimeValue; |
| 30 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 31 | +import org.elasticsearch.rest.RestStatus; |
| 32 | +import org.elasticsearch.threadpool.ThreadPool; |
| 33 | +import org.elasticsearch.xpack.core.ClientHelper; |
| 34 | +import org.elasticsearch.xpack.core.ml.action.PutJobAction; |
| 35 | +import org.elasticsearch.xpack.core.ml.action.UpgradeJobModelSnapshotAction; |
| 36 | +import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; |
| 37 | +import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; |
| 38 | +import org.elasticsearch.xpack.core.ml.job.config.DataDescription; |
| 39 | +import org.elasticsearch.xpack.core.ml.job.config.DetectionRule; |
| 40 | +import org.elasticsearch.xpack.core.ml.job.config.Detector; |
| 41 | +import org.elasticsearch.xpack.core.ml.job.config.Job; |
| 42 | +import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; |
| 43 | +import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; |
| 44 | +import org.elasticsearch.xpack.ml.MlSingleNodeTestCase; |
| 45 | +import org.elasticsearch.xpack.ml.inference.ingest.InferenceProcessor; |
| 46 | +import org.elasticsearch.xpack.ml.inference.modelsize.MlModelSizeNamedXContentProvider; |
| 47 | +import org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister; |
| 48 | +import org.elasticsearch.xpack.ml.notifications.AnomalyDetectionAuditor; |
| 49 | +import org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService; |
| 50 | +import org.junit.Before; |
| 51 | + |
| 52 | +public class JobModelSnapshotUpgraderIT extends MlSingleNodeTestCase { |
| 53 | + |
| 54 | + private JobResultsPersister jobResultsPersister; |
| 55 | + |
| 56 | + @Before |
| 57 | + public void createComponents() throws Exception { |
| 58 | + ThreadPool tp = mockThreadPool(); |
| 59 | + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, |
| 60 | + new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, |
| 61 | + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, |
| 62 | + AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING, |
| 63 | + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, |
| 64 | + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, |
| 65 | + ClusterService.USER_DEFINED_METADATA, |
| 66 | + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); |
| 67 | + ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, tp); |
| 68 | + |
| 69 | + OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); |
| 70 | + ResultsPersisterService resultsPersisterService = new ResultsPersisterService( |
| 71 | + tp, |
| 72 | + originSettingClient, |
| 73 | + clusterService, |
| 74 | + Settings.EMPTY |
| 75 | + ); |
| 76 | + AnomalyDetectionAuditor auditor = new AnomalyDetectionAuditor(client(), clusterService); |
| 77 | + jobResultsPersister = new JobResultsPersister(originSettingClient, resultsPersisterService, auditor); |
| 78 | + waitForMlTemplates(); |
| 79 | + } |
| 80 | + |
| 81 | + public void testUpgradeAlreadyUpgradedSnapshot() { |
| 82 | + String jobId = "job-with-current-snapshot"; |
| 83 | + |
| 84 | + createJob(jobId); |
| 85 | + ModelSnapshot snapshot = new ModelSnapshot.Builder(jobId).setMinVersion(Version.CURRENT).setSnapshotId("snap_1").build(); |
| 86 | + indexModelSnapshot(snapshot); |
| 87 | + client().admin().indices().prepareRefresh(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).get(); |
| 88 | + |
| 89 | + ElasticsearchStatusException ex = expectThrows( |
| 90 | + ElasticsearchStatusException.class, |
| 91 | + () -> client().execute( |
| 92 | + UpgradeJobModelSnapshotAction.INSTANCE, |
| 93 | + new UpgradeJobModelSnapshotAction.Request(jobId, "snap_1", TimeValue.timeValueMinutes(10), true) |
| 94 | + ).actionGet()); |
| 95 | + assertThat(ex.status(), equalTo(RestStatus.CONFLICT)); |
| 96 | + assertThat( |
| 97 | + ex.getMessage(), |
| 98 | + containsString( |
| 99 | + "Cannot upgrade job [job-with-current-snapshot] snapshot [snap_1] as it is already compatible with current version" |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private void indexModelSnapshot(ModelSnapshot snapshot) { |
| 105 | + jobResultsPersister.persistModelSnapshot(snapshot, WriteRequest.RefreshPolicy.IMMEDIATE, () -> true); |
| 106 | + } |
| 107 | + |
| 108 | + private Job.Builder createJob(String jobId) { |
| 109 | + Job.Builder builder = new Job.Builder(jobId); |
| 110 | + AnalysisConfig.Builder ac = createAnalysisConfig("by_field"); |
| 111 | + DataDescription.Builder dc = new DataDescription.Builder(); |
| 112 | + builder.setAnalysisConfig(ac); |
| 113 | + builder.setDataDescription(dc); |
| 114 | + |
| 115 | + PutJobAction.Request request = new PutJobAction.Request(builder); |
| 116 | + client().execute(PutJobAction.INSTANCE, request).actionGet(); |
| 117 | + return builder; |
| 118 | + } |
| 119 | + |
| 120 | + private AnalysisConfig.Builder createAnalysisConfig(String byFieldName) { |
| 121 | + Detector.Builder detector = new Detector.Builder("mean", "field"); |
| 122 | + detector.setByFieldName(byFieldName); |
| 123 | + List<DetectionRule> rules = new ArrayList<>(); |
| 124 | + |
| 125 | + detector.setRules(rules); |
| 126 | + |
| 127 | + return new AnalysisConfig.Builder(Collections.singletonList(detector.build())); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public NamedXContentRegistry xContentRegistry() { |
| 132 | + List<NamedXContentRegistry.Entry> namedXContent = new ArrayList<>(); |
| 133 | + namedXContent.addAll(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); |
| 134 | + namedXContent.addAll(new MlModelSizeNamedXContentProvider().getNamedXContentParsers()); |
| 135 | + return new NamedXContentRegistry(namedXContent); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments