Skip to content

Commit 76aba8a

Browse files
HLRC: Move ML request converters into their own class (#32906)
1 parent ae38cfb commit 76aba8a

File tree

8 files changed

+188
-80
lines changed

8 files changed

+188
-80
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPost;
24+
import org.apache.http.client.methods.HttpPut;
25+
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
26+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
27+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
28+
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
29+
30+
import java.io.IOException;
31+
32+
import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE;
33+
import static org.elasticsearch.client.RequestConverters.createEntity;
34+
35+
final class MLRequestConverters {
36+
37+
private MLRequestConverters() {}
38+
39+
static Request putJob(PutJobRequest putJobRequest) throws IOException {
40+
String endpoint = new EndpointBuilder()
41+
.addPathPartAsIs("_xpack")
42+
.addPathPartAsIs("ml")
43+
.addPathPartAsIs("anomaly_detectors")
44+
.addPathPart(putJobRequest.getJob().getId())
45+
.build();
46+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
47+
request.setEntity(createEntity(putJobRequest, REQUEST_BODY_CONTENT_TYPE));
48+
return request;
49+
}
50+
51+
static Request openJob(OpenJobRequest openJobRequest) throws IOException {
52+
String endpoint = new EndpointBuilder()
53+
.addPathPartAsIs("_xpack")
54+
.addPathPartAsIs("ml")
55+
.addPathPartAsIs("anomaly_detectors")
56+
.addPathPart(openJobRequest.getJobId())
57+
.addPathPartAsIs("_open")
58+
.build();
59+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
60+
request.setJsonEntity(openJobRequest.toString());
61+
return request;
62+
}
63+
64+
static Request deleteJob(DeleteJobRequest deleteJobRequest) {
65+
String endpoint = new EndpointBuilder()
66+
.addPathPartAsIs("_xpack")
67+
.addPathPartAsIs("ml")
68+
.addPathPartAsIs("anomaly_detectors")
69+
.addPathPart(deleteJobRequest.getJobId())
70+
.build();
71+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
72+
73+
RequestConverters.Params params = new RequestConverters.Params(request);
74+
params.putParam("force", Boolean.toString(deleteJobRequest.isForce()));
75+
76+
return request;
77+
}
78+
}

client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public final class MachineLearningClient {
5757
*/
5858
public PutJobResponse putJob(PutJobRequest request, RequestOptions options) throws IOException {
5959
return restHighLevelClient.performRequestAndParseEntity(request,
60-
RequestConverters::putMachineLearningJob,
60+
MLRequestConverters::putJob,
6161
options,
6262
PutJobResponse::fromXContent,
6363
Collections.emptySet());
@@ -75,7 +75,7 @@ public PutJobResponse putJob(PutJobRequest request, RequestOptions options) thro
7575
*/
7676
public void putJobAsync(PutJobRequest request, RequestOptions options, ActionListener<PutJobResponse> listener) {
7777
restHighLevelClient.performRequestAsyncAndParseEntity(request,
78-
RequestConverters::putMachineLearningJob,
78+
MLRequestConverters::putJob,
7979
options,
8080
PutJobResponse::fromXContent,
8181
listener,
@@ -95,7 +95,7 @@ public void putJobAsync(PutJobRequest request, RequestOptions options, ActionLis
9595
*/
9696
public DeleteJobResponse deleteJob(DeleteJobRequest request, RequestOptions options) throws IOException {
9797
return restHighLevelClient.performRequestAndParseEntity(request,
98-
RequestConverters::deleteMachineLearningJob,
98+
MLRequestConverters::deleteJob,
9999
options,
100100
DeleteJobResponse::fromXContent,
101101
Collections.emptySet());
@@ -113,7 +113,7 @@ public DeleteJobResponse deleteJob(DeleteJobRequest request, RequestOptions opti
113113
*/
114114
public void deleteJobAsync(DeleteJobRequest request, RequestOptions options, ActionListener<DeleteJobResponse> listener) {
115115
restHighLevelClient.performRequestAsyncAndParseEntity(request,
116-
RequestConverters::deleteMachineLearningJob,
116+
MLRequestConverters::deleteJob,
117117
options,
118118
DeleteJobResponse::fromXContent,
119119
listener,
@@ -138,7 +138,7 @@ public void deleteJobAsync(DeleteJobRequest request, RequestOptions options, Act
138138
*/
139139
public OpenJobResponse openJob(OpenJobRequest request, RequestOptions options) throws IOException {
140140
return restHighLevelClient.performRequestAndParseEntity(request,
141-
RequestConverters::machineLearningOpenJob,
141+
MLRequestConverters::openJob,
142142
options,
143143
OpenJobResponse::fromXContent,
144144
Collections.emptySet());
@@ -160,7 +160,7 @@ public OpenJobResponse openJob(OpenJobRequest request, RequestOptions options) t
160160
*/
161161
public void openJobAsync(OpenJobRequest request, RequestOptions options, ActionListener<OpenJobResponse> listener) {
162162
restHighLevelClient.performRequestAsyncAndParseEntity(request,
163-
RequestConverters::machineLearningOpenJob,
163+
MLRequestConverters::openJob,
164164
options,
165165
OpenJobResponse::fromXContent,
166166
listener,

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@
112112
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
113113
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
114114
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest;
115-
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
116-
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
117-
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
118115
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
119116
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
120117
import org.elasticsearch.rest.action.search.RestSearchAction;
@@ -1200,46 +1197,6 @@ static Request deleteLicense(DeleteLicenseRequest deleteLicenseRequest) {
12001197
return request;
12011198
}
12021199

1203-
static Request putMachineLearningJob(PutJobRequest putJobRequest) throws IOException {
1204-
String endpoint = new EndpointBuilder()
1205-
.addPathPartAsIs("_xpack")
1206-
.addPathPartAsIs("ml")
1207-
.addPathPartAsIs("anomaly_detectors")
1208-
.addPathPart(putJobRequest.getJob().getId())
1209-
.build();
1210-
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
1211-
request.setEntity(createEntity(putJobRequest, REQUEST_BODY_CONTENT_TYPE));
1212-
return request;
1213-
}
1214-
1215-
static Request deleteMachineLearningJob(DeleteJobRequest deleteJobRequest) {
1216-
String endpoint = new EndpointBuilder()
1217-
.addPathPartAsIs("_xpack")
1218-
.addPathPartAsIs("ml")
1219-
.addPathPartAsIs("anomaly_detectors")
1220-
.addPathPart(deleteJobRequest.getJobId())
1221-
.build();
1222-
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
1223-
1224-
Params params = new Params(request);
1225-
params.putParam("force", Boolean.toString(deleteJobRequest.isForce()));
1226-
1227-
return request;
1228-
}
1229-
1230-
static Request machineLearningOpenJob(OpenJobRequest openJobRequest) throws IOException {
1231-
String endpoint = new EndpointBuilder()
1232-
.addPathPartAsIs("_xpack")
1233-
.addPathPartAsIs("ml")
1234-
.addPathPartAsIs("anomaly_detectors")
1235-
.addPathPart(openJobRequest.getJobId())
1236-
.addPathPartAsIs("_open")
1237-
.build();
1238-
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
1239-
request.setJsonEntity(openJobRequest.toString());
1240-
return request;
1241-
}
1242-
12431200
static Request getMigrationAssistance(IndexUpgradeInfoRequest indexUpgradeInfoRequest) {
12441201
EndpointBuilder endpointBuilder = new EndpointBuilder()
12451202
.addPathPartAsIs("_xpack/migration/assistance")
@@ -1251,7 +1208,7 @@ static Request getMigrationAssistance(IndexUpgradeInfoRequest indexUpgradeInfoRe
12511208
return request;
12521209
}
12531210

1254-
private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
1211+
static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
12551212
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef();
12561213
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));
12571214
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPost;
24+
import org.elasticsearch.common.unit.TimeValue;
25+
import org.elasticsearch.common.xcontent.XContentParser;
26+
import org.elasticsearch.common.xcontent.json.JsonXContent;
27+
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
28+
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
29+
import org.elasticsearch.protocol.xpack.ml.PutJobRequest;
30+
import org.elasticsearch.protocol.xpack.ml.job.config.AnalysisConfig;
31+
import org.elasticsearch.protocol.xpack.ml.job.config.Detector;
32+
import org.elasticsearch.protocol.xpack.ml.job.config.Job;
33+
import org.elasticsearch.test.ESTestCase;
34+
35+
import java.io.ByteArrayOutputStream;
36+
import java.io.IOException;
37+
import java.util.Collections;
38+
39+
import static org.hamcrest.Matchers.equalTo;
40+
41+
public class MLRequestConvertersTests extends ESTestCase {
42+
43+
public void testPutJob() throws IOException {
44+
Job job = createValidJob("foo");
45+
PutJobRequest putJobRequest = new PutJobRequest(job);
46+
47+
Request request = MLRequestConverters.putJob(putJobRequest);
48+
49+
assertThat(request.getEndpoint(), equalTo("/_xpack/ml/anomaly_detectors/foo"));
50+
try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) {
51+
Job parsedJob = Job.PARSER.apply(parser, null).build();
52+
assertThat(parsedJob, equalTo(job));
53+
}
54+
}
55+
56+
public void testOpenJob() throws Exception {
57+
String jobId = "some-job-id";
58+
OpenJobRequest openJobRequest = new OpenJobRequest(jobId);
59+
openJobRequest.setTimeout(TimeValue.timeValueMinutes(10));
60+
61+
Request request = MLRequestConverters.openJob(openJobRequest);
62+
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
63+
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/_open", request.getEndpoint());
64+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
65+
request.getEntity().writeTo(bos);
66+
assertEquals(bos.toString("UTF-8"), "{\"job_id\":\""+ jobId +"\",\"timeout\":\"10m\"}");
67+
}
68+
69+
public void testDeleteJob() {
70+
String jobId = randomAlphaOfLength(10);
71+
DeleteJobRequest deleteJobRequest = new DeleteJobRequest(jobId);
72+
73+
Request request = MLRequestConverters.deleteJob(deleteJobRequest);
74+
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
75+
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId, request.getEndpoint());
76+
assertEquals(Boolean.toString(false), request.getParameters().get("force"));
77+
78+
deleteJobRequest.setForce(true);
79+
request = MLRequestConverters.deleteJob(deleteJobRequest);
80+
assertEquals(Boolean.toString(true), request.getParameters().get("force"));
81+
}
82+
83+
private static Job createValidJob(String jobId) {
84+
AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder(Collections.singletonList(
85+
Detector.builder().setFunction("count").build()));
86+
Job.Builder jobBuilder = Job.builder(jobId);
87+
jobBuilder.setAnalysisConfig(analysisConfig);
88+
return jobBuilder.build();
89+
}
90+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@
127127
import org.elasticsearch.index.rankeval.RestRankEvalAction;
128128
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
129129
import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest;
130-
import org.elasticsearch.protocol.xpack.ml.DeleteJobRequest;
131-
import org.elasticsearch.protocol.xpack.ml.OpenJobRequest;
132130
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
133131
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
134132
import org.elasticsearch.repositories.fs.FsRepository;
@@ -2612,33 +2610,6 @@ public void testXPackDeleteWatch() {
26122610
assertThat(request.getEntity(), nullValue());
26132611
}
26142612

2615-
public void testDeleteMachineLearningJob() {
2616-
String jobId = randomAlphaOfLength(10);
2617-
DeleteJobRequest deleteJobRequest = new DeleteJobRequest(jobId);
2618-
2619-
Request request = RequestConverters.deleteMachineLearningJob(deleteJobRequest);
2620-
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
2621-
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId, request.getEndpoint());
2622-
assertEquals(Boolean.toString(false), request.getParameters().get("force"));
2623-
2624-
deleteJobRequest.setForce(true);
2625-
request = RequestConverters.deleteMachineLearningJob(deleteJobRequest);
2626-
assertEquals(Boolean.toString(true), request.getParameters().get("force"));
2627-
}
2628-
2629-
public void testPostMachineLearningOpenJob() throws Exception {
2630-
String jobId = "some-job-id";
2631-
OpenJobRequest openJobRequest = new OpenJobRequest(jobId);
2632-
openJobRequest.setTimeout(TimeValue.timeValueMinutes(10));
2633-
2634-
Request request = RequestConverters.machineLearningOpenJob(openJobRequest);
2635-
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
2636-
assertEquals("/_xpack/ml/anomaly_detectors/" + jobId + "/_open", request.getEndpoint());
2637-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
2638-
request.getEntity().writeTo(bos);
2639-
assertEquals(bos.toString("UTF-8"), "{\"job_id\":\""+ jobId +"\",\"timeout\":\"10m\"}");
2640-
}
2641-
26422613
/**
26432614
* Randomize the {@link FetchSourceContext} request parameters.
26442615
*/

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/ml/job/config/AnalysisConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ public int hashCode() {
300300
multivariateByFields);
301301
}
302302

303+
public static Builder builder(List<Detector> detectors) {
304+
return new Builder(detectors);
305+
}
306+
303307
public static class Builder {
304308

305309
private List<Detector> detectors;

x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/ml/job/config/Detector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ public int hashCode() {
265265
excludeFrequent, rules, detectorIndex);
266266
}
267267

268+
public static Builder builder() {
269+
return new Builder();
270+
}
271+
268272
public static class Builder {
269273

270274
private String detectorDescription;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ public final String toString() {
412412
return Strings.toString(this);
413413
}
414414

415+
public static Builder builder(String id) {
416+
return new Builder(id);
417+
}
418+
415419
public static class Builder {
416420

417421
private String id;
@@ -435,7 +439,7 @@ public static class Builder {
435439
private String resultsIndexName;
436440
private boolean deleted;
437441

438-
public Builder() {
442+
private Builder() {
439443
}
440444

441445
public Builder(String id) {

0 commit comments

Comments
 (0)