Skip to content

Commit 5b8509d

Browse files
Store the blocking task_id along with the reason
1 parent dfc6538 commit 5b8509d

File tree

20 files changed

+400
-236
lines changed

20 files changed

+400
-236
lines changed

docs/reference/ml/anomaly-detection/apis/get-job.asciidoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=exclude-generated]
5959
The API returns an array of {anomaly-job} resources. For the full list of
6060
properties, see <<ml-put-job-request-body,create {anomaly-jobs} API>>.
6161

62+
//Begin blocked
63+
`blocked`::
64+
(object) When present, it explains that a task is executed on the job
65+
that blocks it from opening.
66+
+
67+
.Properties of `blocked`
68+
[%collapsible%open]
69+
====
70+
`reason`:::
71+
(string) The reason the job is blocked. Values may be `delete`, `reset`, `revert`.
72+
Each value means the corresponding action is being executed.
73+
74+
`task_id`:::
75+
(string) The task id of the blocking action. You can use the <<task>> API to
76+
monitor progress.
77+
====
78+
//End blocked
79+
6280
`create_time`::
6381
(string) The time the job was created. For example, `1491007356077`. This
6482
property is informational; you cannot change its value.

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

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
8+
package org.elasticsearch.xpack.core.ml.job.config;
9+
10+
import org.elasticsearch.common.io.stream.StreamInput;
11+
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.common.io.stream.Writeable;
13+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
14+
import org.elasticsearch.common.xcontent.ParseField;
15+
import org.elasticsearch.common.xcontent.ToXContentObject;
16+
import org.elasticsearch.common.xcontent.XContentBuilder;
17+
import org.elasticsearch.core.Nullable;
18+
import org.elasticsearch.tasks.TaskId;
19+
20+
import java.io.IOException;
21+
import java.util.Locale;
22+
import java.util.Objects;
23+
24+
public class Blocked implements ToXContentObject, Writeable {
25+
26+
public enum Reason {
27+
NONE, DELETE, RESET, REVERT;
28+
29+
public static Reason fromString(String value) {
30+
return Reason.valueOf(value.toUpperCase(Locale.ROOT));
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return name().toLowerCase(Locale.ROOT);
36+
}
37+
}
38+
39+
public static final ParseField REASON = new ParseField("reason");
40+
public static final ParseField TASK_ID = new ParseField("task_id");
41+
42+
public static final ConstructingObjectParser<Blocked, Void> LENIENT_PARSER = createParser(true);
43+
public static final ConstructingObjectParser<Blocked, Void> STRICT_PARSER = createParser(false);
44+
45+
private static ConstructingObjectParser<Blocked, Void> createParser(boolean ignoreUnknownFields) {
46+
ConstructingObjectParser<Blocked, Void> parser = new ConstructingObjectParser<>("blocked", ignoreUnknownFields,
47+
a -> new Blocked((Reason) a[0], (TaskId) a[1]));
48+
parser.declareString(ConstructingObjectParser.constructorArg(), Reason::fromString, REASON);
49+
parser.declareString(ConstructingObjectParser.optionalConstructorArg(), TaskId::new, TASK_ID);
50+
return parser;
51+
}
52+
53+
private final Reason reason;
54+
55+
@Nullable
56+
private final TaskId taskId;
57+
58+
public Blocked(Reason reason, @Nullable TaskId taskId) {
59+
this.reason = Objects.requireNonNull(reason);
60+
this.taskId = taskId;
61+
}
62+
63+
public Blocked(StreamInput in) throws IOException {
64+
this.reason = in.readEnum(Reason.class);
65+
this.taskId = in.readOptionalWriteable(TaskId::readFromStream);
66+
}
67+
68+
@Override
69+
public void writeTo(StreamOutput out) throws IOException {
70+
out.writeEnum(reason);
71+
out.writeOptionalWriteable(taskId);
72+
}
73+
74+
public Reason getReason() {
75+
return reason;
76+
}
77+
78+
@Nullable
79+
public TaskId getTaskId() {
80+
return taskId;
81+
}
82+
83+
@Override
84+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
85+
builder.startObject();
86+
builder.field(REASON.getPreferredName(), reason);
87+
if (taskId != null) {
88+
builder.field(TASK_ID.getPreferredName(), taskId.toString());
89+
}
90+
builder.endObject();
91+
return builder;
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return Objects.hash(reason, taskId);
97+
}
98+
99+
@Override
100+
public boolean equals(Object o) {
101+
if (this == o) return true;
102+
if (o == null || getClass() != o.getClass()) return false;
103+
104+
Blocked that = (Blocked) o;
105+
return Objects.equals(reason, that.reason) && Objects.equals(taskId, that.taskId);
106+
}
107+
108+
public static Blocked none() {
109+
return new Blocked(Reason.NONE, null);
110+
}
111+
}

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

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class Job extends AbstractDiffable<Job> implements Writeable, ToXContentO
8383
public static final ParseField RESULTS_INDEX_NAME = new ParseField("results_index_name");
8484
public static final ParseField DELETING = new ParseField("deleting");
8585
public static final ParseField ALLOW_LAZY_OPEN = new ParseField("allow_lazy_open");
86-
public static final ParseField BLOCK_REASON = new ParseField("block_reason");
86+
public static final ParseField BLOCKED = new ParseField("blocked");
8787

8888
// Used for QueryPage
8989
public static final ParseField RESULTS_FIELD = new ParseField("jobs");
@@ -137,7 +137,7 @@ private static ObjectParser<Builder, Void> createParser(boolean ignoreUnknownFie
137137
parser.declareString(Builder::setResultsIndexName, RESULTS_INDEX_NAME);
138138
parser.declareBoolean(Builder::setDeleting, DELETING);
139139
parser.declareBoolean(Builder::setAllowLazyOpen, ALLOW_LAZY_OPEN);
140-
parser.declareStringOrNull(Builder::setBlockReason, BLOCK_REASON);
140+
parser.declareObject(Builder::setBlocked, ignoreUnknownFields ? Blocked.LENIENT_PARSER : Blocked.STRICT_PARSER, BLOCKED);
141141

142142
return parser;
143143
}
@@ -172,17 +172,15 @@ private static ObjectParser<Builder, Void> createParser(boolean ignoreUnknownFie
172172
private final String resultsIndexName;
173173
private final boolean deleting;
174174
private final boolean allowLazyOpen;
175-
176-
@Nullable
177-
private final BlockReason blockReason;
175+
private final Blocked blocked;
178176

179177
private Job(String jobId, String jobType, Version jobVersion, List<String> groups, String description,
180178
Date createTime, Date finishedTime,
181179
AnalysisConfig analysisConfig, AnalysisLimits analysisLimits, DataDescription dataDescription,
182180
ModelPlotConfig modelPlotConfig, Long renormalizationWindowDays, TimeValue backgroundPersistInterval,
183181
Long modelSnapshotRetentionDays, Long dailyModelSnapshotRetentionAfterDays, Long resultsRetentionDays,
184182
Map<String, Object> customSettings, String modelSnapshotId, Version modelSnapshotMinVersion, String resultsIndexName,
185-
boolean deleting, boolean allowLazyOpen, BlockReason blockReason) {
183+
boolean deleting, boolean allowLazyOpen, Blocked blocked) {
186184

187185
this.jobId = jobId;
188186
this.jobType = jobType;
@@ -206,7 +204,7 @@ private Job(String jobId, String jobType, Version jobVersion, List<String> group
206204
this.resultsIndexName = resultsIndexName;
207205
this.deleting = deleting;
208206
this.allowLazyOpen = allowLazyOpen;
209-
this.blockReason = blockReason;
207+
this.blocked = blocked == null ? Blocked.none() : blocked;
210208
}
211209

212210
public Job(StreamInput in) throws IOException {
@@ -238,9 +236,9 @@ public Job(StreamInput in) throws IOException {
238236
deleting = in.readBoolean();
239237
allowLazyOpen = in.readBoolean();
240238
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
241-
blockReason = in.readOptionalEnum(BlockReason.class);
239+
blocked = new Blocked(in);
242240
} else {
243-
blockReason = deleting ? BlockReason.DELETE : null;
241+
blocked = deleting ? new Blocked(Blocked.Reason.DELETE, null) : Blocked.none();
244242
}
245243
}
246244

@@ -427,9 +425,8 @@ public boolean allowLazyOpen() {
427425
return allowLazyOpen;
428426
}
429427

430-
@Nullable
431-
public BlockReason getBlockReason() {
432-
return blockReason;
428+
public Blocked getBlocked() {
429+
return blocked;
433430
}
434431

435432
/**
@@ -520,7 +517,7 @@ public void writeTo(StreamOutput out) throws IOException {
520517
out.writeBoolean(deleting);
521518
out.writeBoolean(allowLazyOpen);
522519
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
523-
out.writeOptionalEnum(blockReason);
520+
blocked.writeTo(out);
524521
}
525522
}
526523

@@ -597,8 +594,8 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
597594
}
598595
builder.field(RESULTS_INDEX_NAME.getPreferredName(), resultsIndexName);
599596
builder.field(ALLOW_LAZY_OPEN.getPreferredName(), allowLazyOpen);
600-
if (blockReason != null) {
601-
builder.field(BLOCK_REASON.getPreferredName(), blockReason);
597+
if (blocked.getReason() != Blocked.Reason.NONE) {
598+
builder.field(BLOCKED.getPreferredName(), blocked);
602599
}
603600
return builder;
604601
}
@@ -636,15 +633,15 @@ public boolean equals(Object other) {
636633
&& Objects.equals(this.resultsIndexName, that.resultsIndexName)
637634
&& Objects.equals(this.deleting, that.deleting)
638635
&& Objects.equals(this.allowLazyOpen, that.allowLazyOpen)
639-
&& Objects.equals(this.blockReason, that.blockReason);
636+
&& Objects.equals(this.blocked, that.blocked);
640637
}
641638

642639
@Override
643640
public int hashCode() {
644641
return Objects.hash(jobId, jobType, jobVersion, groups, description, createTime, finishedTime,
645642
analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays,
646643
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
647-
customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, allowLazyOpen, blockReason);
644+
customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, allowLazyOpen, blocked);
648645
}
649646

650647
// Class already extends from AbstractDiffable, so copied from ToXContentToBytes#toString()
@@ -694,7 +691,7 @@ public static class Builder implements Writeable {
694691
private String resultsIndexName;
695692
private boolean deleting;
696693
private boolean allowLazyOpen;
697-
private BlockReason blockReason;
694+
private Blocked blocked = Blocked.none();
698695

699696
public Builder() {
700697
}
@@ -726,7 +723,7 @@ public Builder(Job job) {
726723
this.resultsIndexName = job.getResultsIndexNameNoPrefix();
727724
this.deleting = job.isDeleting();
728725
this.allowLazyOpen = job.allowLazyOpen();
729-
this.blockReason = job.getBlockReason();
726+
this.blocked = job.getBlocked();
730727
}
731728

732729
public Builder(StreamInput in) throws IOException {
@@ -757,9 +754,9 @@ public Builder(StreamInput in) throws IOException {
757754
deleting = in.readBoolean();
758755
allowLazyOpen = in.readBoolean();
759756
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
760-
blockReason = in.readOptionalEnum(BlockReason.class);
757+
blocked = new Blocked(in);
761758
} else {
762-
blockReason = null;
759+
blocked = Blocked.none();
763760
}
764761
}
765762

@@ -888,10 +885,10 @@ public Builder setResultsIndexName(String resultsIndexName) {
888885
public Builder setDeleting(boolean deleting) {
889886
this.deleting = deleting;
890887
if (deleting) {
891-
this.blockReason = BlockReason.DELETE;
888+
this.blocked = new Blocked(Blocked.Reason.DELETE, null);
892889
} else {
893-
if (blockReason == BlockReason.DELETE) {
894-
blockReason = null;
890+
if (blocked.getReason() == Blocked.Reason.DELETE) {
891+
blocked = Blocked.none();
895892
}
896893
}
897894
return this;
@@ -902,19 +899,9 @@ public Builder setAllowLazyOpen(boolean allowLazyOpen) {
902899
return this;
903900
}
904901

905-
private Builder setBlockReason(String blockReason) {
906-
if (blockReason == null) {
907-
this.blockReason = null;
908-
this.deleting = false;
909-
return this;
910-
} else {
911-
return setBlockReason(BlockReason.fromString(blockReason));
912-
}
913-
}
914-
915-
public Builder setBlockReason(BlockReason blockReason) {
916-
this.blockReason = blockReason;
917-
this.deleting = (this.blockReason == BlockReason.DELETE);
902+
public Builder setBlocked(Blocked blocked) {
903+
this.blocked = ExceptionsHelper.requireNonNull(blocked, BLOCKED);
904+
this.deleting = (this.blocked.getReason() == Blocked.Reason.DELETE);
918905
return this;
919906
}
920907

@@ -984,7 +971,7 @@ public void writeTo(StreamOutput out) throws IOException {
984971
out.writeBoolean(deleting);
985972
out.writeBoolean(allowLazyOpen);
986973
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
987-
out.writeOptionalEnum(blockReason);
974+
blocked.writeTo(out);
988975
}
989976
}
990977

@@ -1015,15 +1002,15 @@ public boolean equals(Object o) {
10151002
&& Objects.equals(this.resultsIndexName, that.resultsIndexName)
10161003
&& Objects.equals(this.deleting, that.deleting)
10171004
&& Objects.equals(this.allowLazyOpen, that.allowLazyOpen)
1018-
&& Objects.equals(this.blockReason, that.blockReason);
1005+
&& Objects.equals(this.blocked, that.blocked);
10191006
}
10201007

10211008
@Override
10221009
public int hashCode() {
10231010
return Objects.hash(id, jobType, jobVersion, groups, description, analysisConfig, analysisLimits, dataDescription,
10241011
createTime, finishedTime, modelPlotConfig, renormalizationWindowDays,
10251012
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
1026-
customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, allowLazyOpen, blockReason);
1013+
customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, allowLazyOpen, blocked);
10271014
}
10281015

10291016
/**
@@ -1182,7 +1169,7 @@ public Job build() {
11821169
id, jobType, jobVersion, groups, description, createTime, finishedTime,
11831170
analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays,
11841171
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
1185-
customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, allowLazyOpen, blockReason);
1172+
customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, allowLazyOpen, blocked);
11861173
}
11871174

11881175
private void checkValidBackgroundPersistInterval() {

0 commit comments

Comments
 (0)