Skip to content

Commit f1b1983

Browse files
authored
[ML] Add deprecation warning for flush API (#121667)
The anomaly detection job flush API is deprecated since it is only required for the post data API, which was deprecated since 7.11.0. Closes #121506
1 parent 7b8f7cc commit f1b1983

File tree

9 files changed

+88
-20
lines changed

9 files changed

+88
-20
lines changed

docs/changelog/121667.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pr: 121667
2+
summary: Add deprecation warning for flush API
3+
area: Machine Learning
4+
type: deprecation
5+
issues:
6+
- 121506
7+
deprecation:
8+
title: Add deprecation warning for flush API
9+
area: REST API
10+
details: The anomaly detection job flush API is deprecated since it is only required for the post data API, which was deprecated since 7.11.0.
11+
impact: This should have a minimal impact on users as the flush API is only required for the post data API, which was deprecated since 7.11.0.

x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public class MlBasicMultiNodeIT extends ESRestTestCase {
4141
)
4242
.build();
4343

44+
private static final RequestOptions FLUSH_OPTIONS = RequestOptions.DEFAULT.toBuilder()
45+
.setWarningsHandler(
46+
warnings -> Collections.singletonList(
47+
"Forcing any buffered data to be processed is deprecated, "
48+
+ "in a future major version it will be compulsory to use a datafeed"
49+
).equals(warnings) == false
50+
)
51+
.build();
52+
4453
public void testMachineLearningInstalled() throws Exception {
4554
Response response = client().performRequest(new Request("GET", "/_xpack"));
4655
Map<?, ?> features = (Map<?, ?>) entityAsMap(response).get("features");
@@ -93,7 +102,9 @@ public void testMiniFarequote() throws Exception {
93102
assertEquals(1403481600000L, responseBody.get("earliest_record_timestamp"));
94103
assertEquals(1403481700000L, responseBody.get("latest_record_timestamp"));
95104

96-
Response flushResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush"));
105+
Request flustRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush");
106+
flustRequest.setOptions(FLUSH_OPTIONS);
107+
Response flushResponse = client().performRequest(flustRequest);
97108
assertFlushResponse(flushResponse, true, 1403481600000L);
98109

99110
Request closeRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_close");
@@ -191,7 +202,9 @@ public void testMiniFarequoteReopen() throws Exception {
191202
assertEquals(1403481600000L, responseBody.get("earliest_record_timestamp"));
192203
assertEquals(1403482000000L, responseBody.get("latest_record_timestamp"));
193204

194-
Response flushResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush"));
205+
Request flushRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush");
206+
flushRequest.setOptions(FLUSH_OPTIONS);
207+
Response flushResponse = client().performRequest(flushRequest);
195208
assertFlushResponse(flushResponse, true, 1403481600000L);
196209

197210
Request closeRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_close");

x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public class MlJobIT extends ESRestTestCase {
5858
).equals(warnings) == false
5959
)
6060
.build();
61+
private static final RequestOptions FLUSH_OPTIONS = RequestOptions.DEFAULT.toBuilder()
62+
.setWarningsHandler(
63+
warnings -> Collections.singletonList(
64+
"Forcing any buffered data to be processed is deprecated, "
65+
+ "in a future major version it will be compulsory to use a datafeed"
66+
).equals(warnings) == false
67+
)
68+
.build();
6169

6270
@Override
6371
protected Settings restClientSettings() {
@@ -534,9 +542,9 @@ public void testOutOfOrderData() throws Exception {
534542
postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"responsetime\":100, \"time\":\"2019-07-01 00:10:00Z\" }");
535543
client().performRequest(postDataRequest);
536544

537-
Response flushResponse = client().performRequest(
538-
new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")
539-
);
545+
Request flushRequest = new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush");
546+
flushRequest.setOptions(FLUSH_OPTIONS);
547+
Response flushResponse = client().performRequest(flushRequest);
540548
assertThat(entityAsMap(flushResponse), hasEntry("flushed", true));
541549

542550
closeJob(jobId);
@@ -574,9 +582,9 @@ public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception {
574582
{ "airline":"LOT", "response_time":100, "time":"2019-07-01 02:00:00Z" }""");
575583
client().performRequest(postDataRequest);
576584

577-
Response flushResponse = client().performRequest(
578-
new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")
579-
);
585+
Request flushRequest = new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush");
586+
flushRequest.setOptions(FLUSH_OPTIONS);
587+
Response flushResponse = client().performRequest(flushRequest);
580588
assertThat(entityAsMap(flushResponse), hasEntry("flushed", true));
581589

582590
closeJob(jobId);

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestFlushJobAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public class RestFlushJobAction extends BaseRestHandler {
3434

3535
@Override
3636
public List<Route> routes() {
37-
return List.of(new Route(POST, BASE_PATH + "anomaly_detectors/{" + ID + "}/_flush"));
37+
final String msg = "Forcing any buffered data to be processed is deprecated, "
38+
+ "in a future major version it will be compulsory to use a datafeed";
39+
return List.of(Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + ID + "}/_flush").deprecateAndKeep(msg).build());
3840
}
3941

4042
@Override

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import org.elasticsearch.action.ActionListener;
1010
import org.elasticsearch.client.internal.node.NodeClient;
11-
import org.elasticsearch.core.UpdateForV9;
1211
import org.elasticsearch.rest.BaseRestHandler;
1312
import org.elasticsearch.rest.RestRequest;
1413
import org.elasticsearch.rest.RestStatus;
@@ -27,19 +26,11 @@ public class RestPostDataAction extends BaseRestHandler {
2726
private static final String DEFAULT_RESET_START = "";
2827
private static final String DEFAULT_RESET_END = "";
2928

30-
@UpdateForV9(owner = UpdateForV9.Owner.MACHINE_LEARNING)
31-
// these routes were ".deprecated" in RestApiVersion.V_8 which will require use of REST API compatibility headers to access
32-
// this API in v9. It is unclear if this was intentional for v9, and the code has been updated to ".deprecateAndKeep" which will
33-
// continue to emit deprecations warnings but will not require any special headers to access the API in v9.
34-
// Please review and update the code and tests as needed. The original code remains commented out below for reference.
3529
@Override
3630
public List<Route> routes() {
3731
final String msg = "Posting data directly to anomaly detection jobs is deprecated, "
3832
+ "in a future major version it will be compulsory to use a datafeed";
39-
return List.of(
40-
// Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_data").deprecated(msg, RestApiVersion.V_8).build(),
41-
Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_data").deprecateAndKeep(msg).build()
42-
);
33+
return List.of(Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_data").deprecateAndKeep(msg).build());
4334
}
4435

4536
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ setup:
8787
{"airline":"JZA","responsetime":"244.1276","time":"1403485200"}
8888
8989
- do:
90+
warnings:
91+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
9092
ml.flush_job:
9193
job_id: job-stats-test
9294
- match: { flushed: true }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ setup:
8787
{"airline":"JZA","responsetime":"244.1276","time":"1403485200"}
8888
8989
- do:
90+
warnings:
91+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
9092
ml.flush_job:
9193
job_id: job-stats-test
9294
- match: { flushed: true }
@@ -131,6 +133,8 @@ setup:
131133
{"airline":"JZA","responsetime":"244.1276","time":"1403485200"}
132134
133135
- do:
136+
warnings:
137+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
134138
ml.flush_job:
135139
job_id: job-stats-test
136140
- match: { flushed: true }

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ setup:
7777
- match: { latest_record_timestamp: 1403481700000}
7878

7979
- do:
80+
warnings:
81+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
8082
ml.flush_job:
8183
job_id: post-data-job
8284
- match: { flushed: true }
@@ -110,7 +112,12 @@ setup:
110112

111113
---
112114
"Test flush and close job WITHOUT sending any data":
115+
- skip:
116+
features:
117+
- "warnings"
113118
- do:
119+
warnings:
120+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
114121
ml.flush_job:
115122
job_id: post-data-job
116123
- match: { flushed: true }
@@ -150,6 +157,8 @@ setup:
150157

151158
# Skip a bucket
152159
- do:
160+
warnings:
161+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
153162
ml.flush_job:
154163
job_id: post-data-job
155164
skip_time: 1403488700
@@ -266,26 +275,36 @@ setup:
266275
- skip:
267276
reason: "https://github.com/elastic/elasticsearch/issues/34747"
268277
cluster_features: ["gte_v6.5.0"]
278+
features:
279+
- "warnings"
269280

270281
- do:
271282
catch: missing
283+
warnings:
284+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
272285
ml.flush_job:
273286
job_id: not_a_job
274287

275288
- do:
276289
catch: /parse_exception/
290+
warnings:
291+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
277292
ml.flush_job:
278293
job_id: post-data-job
279294
start: not_a_date
280295

281296
- do:
282297
catch: /parse_exception/
298+
warnings:
299+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
283300
ml.flush_job:
284301
job_id: post-data-job
285302
end: end_not_a_date
286303

287304
- do:
288305
catch: /parse_exception/
306+
warnings:
307+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
289308
ml.flush_job:
290309
job_id: post-data-job
291310
advance_time: advance_time_not_a_date
@@ -311,6 +330,8 @@ setup:
311330

312331
- do:
313332
catch: /status_exception/
333+
warnings:
334+
- 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
314335
ml.flush_job:
315336
job_id: post-data-closed-job
316337

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,23 @@ protected Response postData(String jobId, String data) throws IOException {
338338
}
339339

340340
protected void flushJob(String jobId) throws IOException {
341-
client().performRequest(new Request("POST", "/_ml/anomaly_detectors/" + jobId + "/_flush"));
341+
// Flush job is deprecated, so a deprecation warning is possible (depending on the old version)
342+
RequestOptions flushOptions = RequestOptions.DEFAULT.toBuilder().setWarningsHandler(warnings -> {
343+
if (warnings.isEmpty()) {
344+
// No warning is OK - it means we hit an old node where flush is not deprecated
345+
return false;
346+
} else if (warnings.size() > 1) {
347+
return true;
348+
}
349+
return warnings.get(0)
350+
.equals(
351+
"Forcing any buffered data to be processed is deprecated, "
352+
+ "in a future major version it will be compulsory to use a datafeed"
353+
) == false;
354+
}).build();
355+
Request flushRequest = new Request("POST", "/_ml/anomaly_detectors/" + jobId + "/_flush");
356+
flushRequest.setOptions(flushOptions);
357+
client().performRequest(flushRequest);
342358
}
343359

344360
private void closeJob(String jobId) throws IOException {

0 commit comments

Comments
 (0)