Skip to content

Commit 4d41310

Browse files
author
Hendrik Muhs
committed
[ML-DataFrame] fix wire serialization issues in data frame response objects (#39790)
fix wire serialization issues in data frame response objects
1 parent af4e740 commit 4d41310

14 files changed

+241
-14
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/action/DeleteDataFrameTransformAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Request(String id) {
4949
this.id = ExceptionsHelper.requireNonNull(id, DataFrameField.ID.getPreferredName());
5050
}
5151

52-
public Request() {
52+
private Request() {
5353
}
5454

5555
public Request(StreamInput in) throws IOException {
@@ -113,7 +113,7 @@ protected RequestBuilder(ElasticsearchClient client, DeleteDataFrameTransformAct
113113
public static class Response extends BaseTasksResponse implements Writeable, ToXContentObject {
114114
private boolean acknowledged;
115115
public Response(StreamInput in) throws IOException {
116-
super(Collections.emptyList(), Collections.emptyList());
116+
super(in);
117117
readFrom(in);
118118
}
119119

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/action/GetDataFrameTransformsAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Request(String id) {
6363
}
6464
}
6565

66-
public Request() {}
66+
private Request() {}
6767

6868
public Request(StreamInput in) throws IOException {
6969
super(in);
@@ -149,7 +149,7 @@ public Response() {
149149
}
150150

151151
public Response(StreamInput in) throws IOException {
152-
super(Collections.emptyList(), Collections.emptyList());
152+
super(in);
153153
readFrom(in);
154154
}
155155

@@ -173,6 +173,7 @@ public void writeTo(StreamOutput out) throws IOException {
173173
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
174174
List<String> invalidTransforms = new ArrayList<>();
175175
builder.startObject();
176+
toXContentCommon(builder, params);
176177
builder.field(DataFrameField.COUNT.getPreferredName(), transformConfigurations.size());
177178
// XContentBuilder does not support passing the params object for Iterables
178179
builder.field(DataFrameField.TRANSFORMS.getPreferredName());

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/action/GetDataFrameTransformsStatsAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Request(String id) {
5555
}
5656
}
5757

58-
public Request() {}
58+
private Request() {}
5959

6060
public Request(StreamInput in) throws IOException {
6161
super(in);
@@ -138,7 +138,7 @@ public Response() {
138138
}
139139

140140
public Response(StreamInput in) throws IOException {
141-
super(Collections.emptyList(), Collections.emptyList());
141+
super(in);
142142
readFrom(in);
143143
}
144144

@@ -161,6 +161,7 @@ public void writeTo(StreamOutput out) throws IOException {
161161
@Override
162162
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
163163
builder.startObject();
164+
toXContentCommon(builder, params);
164165
builder.field(DataFrameField.COUNT.getPreferredName(), transformsStateAndStats.size());
165166
builder.field(DataFrameField.TRANSFORMS.getPreferredName(), transformsStateAndStats);
166167
builder.endObject();

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/action/StartDataFrameTransformAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Request(String id) {
4646
this.id = ExceptionsHelper.requireNonNull(id, DataFrameField.ID.getPreferredName());
4747
}
4848

49-
public Request() {
49+
private Request() {
5050
}
5151

5252
public Request(StreamInput in) throws IOException {
@@ -108,7 +108,7 @@ public Response() {
108108
}
109109

110110
public Response(StreamInput in) throws IOException {
111-
super(Collections.emptyList(), Collections.emptyList());
111+
super(in);
112112
readFrom(in);
113113
}
114114

@@ -136,6 +136,7 @@ public void writeTo(StreamOutput out) throws IOException {
136136
@Override
137137
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
138138
builder.startObject();
139+
toXContentCommon(builder, params);
139140
builder.field("started", started);
140141
builder.endObject();
141142
return builder;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/action/StopDataFrameTransformAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Request(String id, boolean waitForCompletion, @Nullable TimeValue timeout
5656
this.setTimeout(timeout == null ? DEFAULT_TIMEOUT : timeout);
5757
}
5858

59-
public Request() {
59+
private Request() {
6060
this(null, false, null);
6161
}
6262

@@ -149,7 +149,7 @@ public Response() {
149149
}
150150

151151
public Response(StreamInput in) throws IOException {
152-
super(Collections.emptyList(), Collections.emptyList());
152+
super(in);
153153
readFrom(in);
154154
}
155155

@@ -177,6 +177,7 @@ public void writeTo(StreamOutput out) throws IOException {
177177
@Override
178178
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
179179
builder.startObject();
180+
toXContentCommon(builder, params);
180181
builder.field("stopped", stopped);
181182
builder.endObject();
182183
return builder;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.dataframe.action;
8+
9+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10+
import org.elasticsearch.common.io.stream.Writeable;
11+
import org.elasticsearch.common.settings.Settings;
12+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
13+
import org.elasticsearch.search.SearchModule;
14+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
15+
import org.junit.Before;
16+
17+
import static java.util.Collections.emptyList;
18+
19+
public abstract class AbstractWireSerializingDataFrameTestCase<T extends Writeable> extends AbstractWireSerializingTestCase<T> {
20+
/**
21+
* Test case that ensures aggregation named objects are registered
22+
*/
23+
private NamedWriteableRegistry namedWriteableRegistry;
24+
private NamedXContentRegistry namedXContentRegistry;
25+
26+
@Before
27+
public void registerAggregationNamedObjects() throws Exception {
28+
// register aggregations as NamedWriteable
29+
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
30+
31+
namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
32+
namedXContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
33+
}
34+
35+
@Override
36+
protected NamedWriteableRegistry getNamedWriteableRegistry() {
37+
return namedWriteableRegistry;
38+
}
39+
40+
@Override
41+
protected NamedXContentRegistry xContentRegistry() {
42+
return namedXContentRegistry;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.dataframe.action;
8+
9+
import org.elasticsearch.common.io.stream.Writeable.Reader;
10+
import org.elasticsearch.xpack.core.dataframe.action.DeleteDataFrameTransformAction.Response;
11+
12+
public class DeleteDataFrameTransformActionResponseTests extends AbstractWireSerializingDataFrameTestCase<Response> {
13+
@Override
14+
protected Response createTestInstance() {
15+
return new Response(randomBoolean());
16+
}
17+
18+
@Override
19+
protected Reader<Response> instanceReader() {
20+
return Response::new;
21+
}
22+
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/dataframe/action/GetDataFrameTransformsActionResponseTests.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
package org.elasticsearch.xpack.core.dataframe.action;
88

9+
import org.elasticsearch.common.io.stream.Writeable.Reader;
910
import org.elasticsearch.common.logging.LoggerMessageFormat;
1011
import org.elasticsearch.common.xcontent.XContentBuilder;
1112
import org.elasticsearch.common.xcontent.XContentFactory;
1213
import org.elasticsearch.common.xcontent.XContentType;
1314
import org.elasticsearch.common.xcontent.support.XContentMapValues;
14-
import org.elasticsearch.test.ESTestCase;
15-
import org.elasticsearch.xpack.core.watcher.watch.Payload.XContent;
1615
import org.elasticsearch.xpack.core.dataframe.action.GetDataFrameTransformsAction.Response;
1716
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameTransformConfig;
1817
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameTransformConfigTests;
18+
import org.elasticsearch.xpack.core.watcher.watch.Payload.XContent;
1919

2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.List;
2323
import java.util.Map;
2424

25-
public class GetDataFrameTransformsActionResponseTests extends ESTestCase {
25+
public class GetDataFrameTransformsActionResponseTests extends AbstractWireSerializingDataFrameTestCase<Response> {
2626

2727
public void testInvalidTransforms() throws IOException {
2828
List<DataFrameTransformConfig> transforms = new ArrayList<>();
@@ -66,4 +66,19 @@ public void testNoHeaderInResponse() throws IOException {
6666
assertEquals(null, XContentMapValues.extractValue("headers", transformsResponse.get(i)));
6767
}
6868
}
69+
70+
@Override
71+
protected Response createTestInstance() {
72+
List<DataFrameTransformConfig> configs = new ArrayList<>();
73+
for (int i = 0; i < randomInt(10); ++i) {
74+
configs.add(DataFrameTransformConfigTests.randomDataFrameTransformConfig());
75+
}
76+
77+
return new Response(configs);
78+
}
79+
80+
@Override
81+
protected Reader<Response> instanceReader() {
82+
return Response::new;
83+
}
6984
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.dataframe.action;
8+
9+
import org.elasticsearch.common.io.stream.Writeable.Reader;
10+
import org.elasticsearch.xpack.core.dataframe.action.GetDataFrameTransformsStatsAction.Response;
11+
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameTransformStateAndStats;
12+
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameTransformStateAndStatsTests;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
public class GetDataFrameTransformsStatsActionResponseTests extends AbstractWireSerializingDataFrameTestCase<Response> {
18+
@Override
19+
protected Response createTestInstance() {
20+
List<DataFrameTransformStateAndStats> stats = new ArrayList<>();
21+
for (int i = 0; i < randomInt(10); ++i) {
22+
stats.add(DataFrameTransformStateAndStatsTests.randomDataFrameTransformStateAndStats());
23+
}
24+
25+
return new Response(stats);
26+
}
27+
28+
@Override
29+
protected Reader<Response> instanceReader() {
30+
return Response::new;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.dataframe.action;
8+
9+
import org.elasticsearch.common.io.stream.Writeable.Reader;
10+
import org.elasticsearch.xpack.core.dataframe.action.PreviewDataFrameTransformAction.Response;
11+
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class PreviewDataFrameTransformsActionResponseWireTests extends AbstractWireSerializingDataFrameTestCase<Response> {
18+
19+
@Override
20+
protected Response createTestInstance() {
21+
int size = randomIntBetween(0, 10);
22+
List<Map<String, Object>> data = new ArrayList<>(size);
23+
for (int i = 0; i < size; i++) {
24+
Map<String, Object> datum = new HashMap<>();
25+
Map<String, Object> entry = new HashMap<>();
26+
entry.put("value1", randomIntBetween(1, 100));
27+
datum.put(randomAlphaOfLength(10), entry);
28+
data.add(datum);
29+
}
30+
return new Response(data);
31+
}
32+
33+
@Override
34+
protected Reader<Response> instanceReader() {
35+
return Response::new;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)