Skip to content

Commit 47787b3

Browse files
committed
[ML] Adjust finalize job action to work with documents (#34226)
1 parent afb238b commit 47787b3

File tree

13 files changed

+94
-114
lines changed

13 files changed

+94
-114
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static Set<String> openJobIds(PersistentTasksCustomMetaData tasks) {
9797
* Is there an ml anomaly detector job task for the job {@code jobId}?
9898
* @param jobId The job id
9999
* @param tasks Persistent tasks
100-
* @return
100+
* @return True if the job has a task
101101
*/
102102
public static boolean taskExistsForJob(String jobId, PersistentTasksCustomMetaData tasks) {
103103
return openJobIds(tasks).contains(jobId);

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public static UpdateJobAction.Request parseRequest(String jobId, XContentParser
5454

5555
/** Indicates an update that was not triggered by a user */
5656
private boolean isInternal;
57-
private boolean waitForAck = true;
5857

5958
public Request(String jobId, JobUpdate update) {
6059
this(jobId, update, false);
@@ -88,14 +87,6 @@ public boolean isInternal() {
8887
return isInternal;
8988
}
9089

91-
public boolean isWaitForAck() {
92-
return waitForAck;
93-
}
94-
95-
public void setWaitForAck(boolean waitForAck) {
96-
this.waitForAck = waitForAck;
97-
}
98-
9990
@Override
10091
public ActionRequestValidationException validate() {
10192
return null;
@@ -111,10 +102,9 @@ public void readFrom(StreamInput in) throws IOException {
111102
} else {
112103
isInternal = false;
113104
}
114-
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
115-
waitForAck = in.readBoolean();
116-
} else {
117-
waitForAck = true;
105+
// TODO jindex change CURRENT to specific version when feature branch is merged
106+
if (in.getVersion().onOrAfter(Version.V_6_3_0) && in.getVersion().before(Version.CURRENT)) {
107+
in.readBoolean(); // was waitForAck
118108
}
119109
}
120110

@@ -126,8 +116,9 @@ public void writeTo(StreamOutput out) throws IOException {
126116
if (out.getVersion().onOrAfter(Version.V_6_2_2)) {
127117
out.writeBoolean(isInternal);
128118
}
129-
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
130-
out.writeBoolean(waitForAck);
119+
// TODO jindex change CURRENT to specific version when feature branch is merged
120+
if (out.getVersion().onOrAfter(Version.V_6_3_0) && out.getVersion().before(Version.CURRENT)) {
121+
out.writeBoolean(false); // was waitForAck
131122
}
132123
}
133124

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class JobUpdate implements Writeable, ToXContentObject {
5959
INTERNAL_PARSER.declareString(Builder::setModelSnapshotId, Job.MODEL_SNAPSHOT_ID);
6060
INTERNAL_PARSER.declareLong(Builder::setEstablishedModelMemory, Job.ESTABLISHED_MODEL_MEMORY);
6161
INTERNAL_PARSER.declareString(Builder::setJobVersion, Job.JOB_VERSION);
62-
INTERNAL_PARSER.declareBoolean(Builder::setClearJobFinishTime, CLEAR_JOB_FINISH_TIME);
62+
INTERNAL_PARSER.declareBoolean(Builder::setClearFinishTime, CLEAR_JOB_FINISH_TIME);
6363
}
6464

6565
private final String jobId;
@@ -710,7 +710,7 @@ public Builder setJobVersion(String version) {
710710
return this;
711711
}
712712

713-
public Builder setClearJobFinishTime(boolean clearJobFinishTime) {
713+
public Builder setClearFinishTime(boolean clearJobFinishTime) {
714714
this.clearJobFinishTime = clearJobFinishTime;
715715
return this;
716716
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateJobActionRequestTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ protected UpdateJobAction.Request createTestInstance() {
1818
// no need to randomize JobUpdate this is already tested in: JobUpdateTests
1919
JobUpdate.Builder jobUpdate = new JobUpdate.Builder(jobId);
2020
jobUpdate.setAnalysisLimits(new AnalysisLimits(100L, 100L));
21-
UpdateJobAction.Request request = new UpdateJobAction.Request(jobId, jobUpdate.build());
22-
request.setWaitForAck(randomBoolean());
21+
UpdateJobAction.Request request;
22+
if (randomBoolean()) {
23+
request = new UpdateJobAction.Request(jobId, jobUpdate.build());
24+
} else {
25+
// this call sets isInternal = true
26+
request = UpdateJobAction.Request.internal(jobId, jobUpdate.build());
27+
}
28+
2329
return request;
2430
}
2531

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public JobUpdate createRandom(String jobId, @Nullable Job job) {
9494
update.setJobVersion(randomFrom(Version.CURRENT, Version.V_6_2_0, Version.V_6_1_0));
9595
}
9696
if (useInternalParser) {
97-
update.setClearJobFinishTime(randomBoolean());
97+
update.setClearFinishTime(randomBoolean());
9898
}
9999

100100
return update.build();

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.elasticsearch.transport.TransportService;
3131
import org.elasticsearch.xpack.core.ml.MlTasks;
3232
import org.elasticsearch.xpack.core.ml.action.CloseJobAction;
33-
import org.elasticsearch.xpack.core.ml.action.FinalizeJobExecutionAction;
3433
import org.elasticsearch.xpack.core.ml.datafeed.DatafeedState;
3534
import org.elasticsearch.xpack.core.ml.job.config.JobState;
3635
import org.elasticsearch.xpack.core.ml.job.config.JobTaskState;
@@ -50,9 +49,6 @@
5049
import java.util.concurrent.atomic.AtomicInteger;
5150
import java.util.stream.Collectors;
5251

53-
import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN;
54-
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
55-
5652
public class TransportCloseJobAction extends TransportTasksAction<TransportOpenJobAction.JobTask, CloseJobAction.Request,
5753
CloseJobAction.Response, CloseJobAction.Response> {
5854

@@ -427,10 +423,7 @@ void waitForJobClosed(CloseJobAction.Request request, WaitForCloseRequest waitFo
427423
}, request.getCloseTimeout(), new ActionListener<Boolean>() {
428424
@Override
429425
public void onResponse(Boolean result) {
430-
FinalizeJobExecutionAction.Request finalizeRequest = new FinalizeJobExecutionAction.Request(
431-
waitForCloseRequest.jobsToFinalize.toArray(new String[0]));
432-
executeAsyncWithOrigin(client, ML_ORIGIN, FinalizeJobExecutionAction.INSTANCE, finalizeRequest,
433-
ActionListener.wrap(r -> listener.onResponse(response), listener::onFailure));
426+
listener.onResponse(response);
434427
}
435428

436429
@Override

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

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,15 @@
1010
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1111
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
1212
import org.elasticsearch.cluster.ClusterState;
13-
import org.elasticsearch.cluster.ClusterStateUpdateTask;
1413
import org.elasticsearch.cluster.block.ClusterBlockException;
1514
import org.elasticsearch.cluster.block.ClusterBlockLevel;
1615
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
17-
import org.elasticsearch.cluster.metadata.MetaData;
1816
import org.elasticsearch.cluster.service.ClusterService;
1917
import org.elasticsearch.common.inject.Inject;
2018
import org.elasticsearch.common.settings.Settings;
2119
import org.elasticsearch.threadpool.ThreadPool;
2220
import org.elasticsearch.transport.TransportService;
23-
import org.elasticsearch.xpack.core.XPackPlugin;
24-
import org.elasticsearch.xpack.core.ml.MlMetadata;
2521
import org.elasticsearch.xpack.core.ml.action.FinalizeJobExecutionAction;
26-
import org.elasticsearch.xpack.core.ml.job.config.Job;
27-
28-
import java.util.Date;
2922

3023
public class TransportFinalizeJobExecutionAction extends TransportMasterNodeAction<FinalizeJobExecutionAction.Request,
3124
AcknowledgedResponse> {
@@ -51,41 +44,10 @@ protected AcknowledgedResponse newResponse() {
5144

5245
@Override
5346
protected void masterOperation(FinalizeJobExecutionAction.Request request, ClusterState state,
54-
ActionListener<AcknowledgedResponse> listener) throws Exception {
55-
String jobIdString = String.join(",", request.getJobIds());
56-
String source = "finalize_job_execution [" + jobIdString + "]";
57-
logger.debug("finalizing jobs [{}]", jobIdString);
58-
clusterService.submitStateUpdateTask(source, new ClusterStateUpdateTask() {
59-
@Override
60-
public ClusterState execute(ClusterState currentState) {
61-
XPackPlugin.checkReadyForXPackCustomMetadata(currentState);
62-
MlMetadata mlMetadata = MlMetadata.getMlMetadata(currentState);
63-
MlMetadata.Builder mlMetadataBuilder = new MlMetadata.Builder(mlMetadata);
64-
Date finishedTime = new Date();
65-
66-
for (String jobId : request.getJobIds()) {
67-
Job.Builder jobBuilder = new Job.Builder(mlMetadata.getJobs().get(jobId));
68-
jobBuilder.setFinishedTime(finishedTime);
69-
mlMetadataBuilder.putJob(jobBuilder.build(), true);
70-
}
71-
ClusterState.Builder builder = ClusterState.builder(currentState);
72-
return builder.metaData(new MetaData.Builder(currentState.metaData())
73-
.putCustom(MlMetadata.TYPE, mlMetadataBuilder.build()))
74-
.build();
75-
}
76-
77-
@Override
78-
public void onFailure(String source, Exception e) {
79-
listener.onFailure(e);
80-
}
81-
82-
@Override
83-
public void clusterStateProcessed(String source, ClusterState oldState,
84-
ClusterState newState) {
85-
logger.debug("finalized job [{}]", jobIdString);
86-
listener.onResponse(new AcknowledgedResponse(true));
87-
}
88-
});
47+
ActionListener<AcknowledgedResponse> listener) {
48+
// This action is no longer required but needs to be preserved
49+
// in case it is called by an old node in a mixed cluster
50+
listener.onResponse(new AcknowledgedResponse(true));
8951
}
9052

9153
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public void onTimeout(TimeValue timeout) {
564564
}
565565

566566
private void clearJobFinishedTime(String jobId, ActionListener<AcknowledgedResponse> listener) {
567-
JobUpdate update = new JobUpdate.Builder(jobId).setClearJobFinishTime(true).build();
567+
JobUpdate update = new JobUpdate.Builder(jobId).setClearFinishTime(true).build();
568568

569569
jobConfigProvider.updateJob(jobId, update, null, ActionListener.wrap(
570570
job -> listener.onResponse(new AcknowledgedResponse(true)),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ public void onFailure(Exception e) {
237237
*
238238
* @param jobId The Id of the job to update
239239
* @param update The job update
240-
* @param maxModelMemoryLimit The maximum model memory allowed
240+
* @param maxModelMemoryLimit The maximum model memory allowed. This can be {@code null}
241+
* if the job's {@link org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits}
242+
* are not changed.
241243
* @param updatedJobListener Updated job listener
242244
*/
243245
public void updateJob(String jobId, JobUpdate update, ByteSizeValue maxModelMemoryLimit, ActionListener<Job> updatedJobListener) {
@@ -373,7 +375,6 @@ private void indexUpdatedJob(Job updatedJob, long version, ActionListener<Job> u
373375
}
374376
}
375377

376-
377378
/**
378379
* Check a job exists. A job exists if it has a configuration document.
379380
* If the .ml-config index does not exist it is treated as a missing job

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ public void persistQuantiles(Quantiles quantiles, WriteRequest.RefreshPolicy ref
242242
/**
243243
* Persist a model snapshot description
244244
*/
245-
public void persistModelSnapshot(ModelSnapshot modelSnapshot, WriteRequest.RefreshPolicy refreshPolicy) {
245+
public IndexResponse persistModelSnapshot(ModelSnapshot modelSnapshot, WriteRequest.RefreshPolicy refreshPolicy) {
246246
Persistable persistable = new Persistable(modelSnapshot.getJobId(), modelSnapshot, ModelSnapshot.documentId(modelSnapshot));
247247
persistable.setRefreshPolicy(refreshPolicy);
248-
persistable.persist(AnomalyDetectorsIndex.resultsWriteAlias(modelSnapshot.getJobId())).actionGet();
248+
return persistable.persist(AnomalyDetectorsIndex.resultsWriteAlias(modelSnapshot.getJobId())).actionGet();
249249
}
250250

251251
/**

0 commit comments

Comments
 (0)