Skip to content

Commit f957556

Browse files
authored
Refactor PUT DLM lifecycle API parsing (#138789)
We currently parse the request body of the `PUT _data_stream/{name}/_lifecycle` API in pretty much the same way as we parse `DataStreamLifecycle` objects. We currently already use the latter in the data stream options API. By reusing that parser in this API too, we can remove some code and we'll automatically be able to parse new fields in this API when they get added to `DataStreamLifecycle` in the future. If there ever comes a point where there's a discrepancy between the two, we can always go back to having separate parsers.
1 parent 6ada429 commit f957556

File tree

2 files changed

+13
-89
lines changed

2 files changed

+13
-89
lines changed

modules/data-streams/src/main/java/org/elasticsearch/datastreams/lifecycle/rest/RestPutDataStreamLifecycleAction.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.action.datastreams.lifecycle.PutDataStreamLifecycleAction;
1212
import org.elasticsearch.action.support.IndicesOptions;
1313
import org.elasticsearch.client.internal.node.NodeClient;
14+
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
1415
import org.elasticsearch.common.Strings;
1516
import org.elasticsearch.rest.BaseRestHandler;
1617
import org.elasticsearch.rest.RestRequest;
@@ -46,17 +47,12 @@ public List<Route> routes() {
4647
@Override
4748
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
4849
try (XContentParser parser = request.contentParser()) {
49-
PutDataStreamLifecycleAction.Request putLifecycleRequest = PutDataStreamLifecycleAction.Request.parseRequest(
50-
parser,
51-
(dataRetention, enabled, downsamplingRounds, downsamplingMethod) -> new PutDataStreamLifecycleAction.Request(
52-
getMasterNodeTimeout(request),
53-
getAckTimeout(request),
54-
Strings.splitStringByCommaToArray(request.param("name")),
55-
dataRetention,
56-
enabled,
57-
downsamplingRounds,
58-
downsamplingMethod
59-
)
50+
final var lifecycle = DataStreamLifecycle.dataLifecycleFromXContent(parser);
51+
PutDataStreamLifecycleAction.Request putLifecycleRequest = new PutDataStreamLifecycleAction.Request(
52+
getMasterNodeTimeout(request),
53+
getAckTimeout(request),
54+
Strings.splitStringByCommaToArray(request.param("name")),
55+
lifecycle
6056
);
6157
putLifecycleRequest.indicesOptions(IndicesOptions.fromRequest(request, putLifecycleRequest.indicesOptions()));
6258
return channel -> client.execute(

server/src/main/java/org/elasticsearch/action/datastreams/lifecycle/PutDataStreamLifecycleAction.java

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import org.elasticsearch.action.ActionType;
1313
import org.elasticsearch.action.IndicesRequest;
14-
import org.elasticsearch.action.downsample.DownsampleConfig;
1514
import org.elasticsearch.action.support.IndicesOptions;
1615
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1716
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -20,23 +19,13 @@
2019
import org.elasticsearch.common.io.stream.StreamOutput;
2120
import org.elasticsearch.core.Nullable;
2221
import org.elasticsearch.core.TimeValue;
23-
import org.elasticsearch.xcontent.AbstractObjectParser;
24-
import org.elasticsearch.xcontent.ConstructingObjectParser;
25-
import org.elasticsearch.xcontent.ObjectParser;
2622
import org.elasticsearch.xcontent.ToXContentObject;
2723
import org.elasticsearch.xcontent.XContentBuilder;
28-
import org.elasticsearch.xcontent.XContentParser;
2924

3025
import java.io.IOException;
3126
import java.util.Arrays;
32-
import java.util.List;
3327
import java.util.Objects;
3428

35-
import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.DATA_RETENTION_FIELD;
36-
import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.DOWNSAMPLING_FIELD;
37-
import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.DOWNSAMPLING_METHOD_FIELD;
38-
import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.ENABLED_FIELD;
39-
4029
/**
4130
* Sets the data stream lifecycle that was provided in the request to the requested data streams.
4231
*/
@@ -48,53 +37,6 @@ private PutDataStreamLifecycleAction() {/* no instances */}
4837

4938
public static final class Request extends AcknowledgedRequest<Request> implements IndicesRequest.Replaceable, ToXContentObject {
5039

51-
public interface Factory {
52-
Request create(
53-
@Nullable TimeValue dataRetention,
54-
@Nullable Boolean enabled,
55-
@Nullable List<DataStreamLifecycle.DownsamplingRound> downsampling,
56-
@Nullable DownsampleConfig.SamplingMethod downsamplingMethod
57-
);
58-
}
59-
60-
@SuppressWarnings("unchecked")
61-
public static final ConstructingObjectParser<Request, Factory> PARSER = new ConstructingObjectParser<>(
62-
"put_data_stream_lifecycle_request",
63-
false,
64-
(args, factory) -> factory.create(
65-
(TimeValue) args[0],
66-
(Boolean) args[1],
67-
(List<DataStreamLifecycle.DownsamplingRound>) args[2],
68-
(DownsampleConfig.SamplingMethod) args[3]
69-
)
70-
);
71-
72-
static {
73-
PARSER.declareField(
74-
ConstructingObjectParser.optionalConstructorArg(),
75-
(p, c) -> TimeValue.parseTimeValue(p.text(), DATA_RETENTION_FIELD.getPreferredName()),
76-
DATA_RETENTION_FIELD,
77-
ObjectParser.ValueType.STRING
78-
);
79-
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ENABLED_FIELD);
80-
PARSER.declareField(
81-
ConstructingObjectParser.optionalConstructorArg(),
82-
(p, c) -> AbstractObjectParser.parseArray(p, null, DataStreamLifecycle.DownsamplingRound::fromXContent),
83-
DOWNSAMPLING_FIELD,
84-
ObjectParser.ValueType.OBJECT_ARRAY
85-
);
86-
PARSER.declareField(
87-
ConstructingObjectParser.optionalConstructorArg(),
88-
(p, c) -> DownsampleConfig.SamplingMethod.fromString(p.text()),
89-
DOWNSAMPLING_METHOD_FIELD,
90-
ObjectParser.ValueType.STRING
91-
);
92-
}
93-
94-
public static Request parseRequest(XContentParser parser, Factory factory) {
95-
return PARSER.apply(parser, factory);
96-
}
97-
9840
private String[] names;
9941
private IndicesOptions indicesOptions = IndicesOptions.builder()
10042
.concreteTargetOptions(IndicesOptions.ConcreteTargetOptions.ERROR_WHEN_UNAVAILABLE_TARGETS)
@@ -150,26 +92,12 @@ public Request(
15092
@Nullable TimeValue dataRetention,
15193
@Nullable Boolean enabled
15294
) {
153-
this(masterNodeTimeout, ackTimeout, names, dataRetention, enabled, null, null);
154-
}
155-
156-
public Request(
157-
TimeValue masterNodeTimeout,
158-
TimeValue ackTimeout,
159-
String[] names,
160-
@Nullable TimeValue dataRetention,
161-
@Nullable Boolean enabled,
162-
@Nullable List<DataStreamLifecycle.DownsamplingRound> downsamplingRounds,
163-
@Nullable DownsampleConfig.SamplingMethod downsamplingMethod
164-
) {
165-
super(masterNodeTimeout, ackTimeout);
166-
this.names = names;
167-
this.lifecycle = DataStreamLifecycle.dataLifecycleBuilder()
168-
.dataRetention(dataRetention)
169-
.enabled(enabled == null || enabled)
170-
.downsamplingRounds(downsamplingRounds)
171-
.downsamplingMethod(downsamplingMethod)
172-
.build();
95+
this(
96+
masterNodeTimeout,
97+
ackTimeout,
98+
names,
99+
DataStreamLifecycle.dataLifecycleBuilder().enabled(enabled == null || enabled).dataRetention(dataRetention).build()
100+
);
173101
}
174102

175103
public String[] getNames() {

0 commit comments

Comments
 (0)