浏览代码

Add downsampling parser support for PUT _lifecycle API (#98910)

Andrei Dan 2 年之前
父节点
当前提交
01686a8093

+ 21 - 3
modules/data-streams/src/main/java/org/elasticsearch/datastreams/lifecycle/action/PutDataStreamLifecycleAction.java

@@ -15,10 +15,12 @@ import org.elasticsearch.action.support.IndicesOptions;
 import org.elasticsearch.action.support.master.AcknowledgedRequest;
 import org.elasticsearch.action.support.master.AcknowledgedResponse;
 import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
+import org.elasticsearch.cluster.metadata.DataStreamLifecycle.Downsampling;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
 import org.elasticsearch.core.Nullable;
 import org.elasticsearch.core.TimeValue;
+import org.elasticsearch.xcontent.AbstractObjectParser;
 import org.elasticsearch.xcontent.ConstructingObjectParser;
 import org.elasticsearch.xcontent.ObjectParser;
 import org.elasticsearch.xcontent.ToXContentObject;
@@ -30,6 +32,7 @@ import java.util.Arrays;
 import java.util.Objects;
 
 import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.DATA_RETENTION_FIELD;
+import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.DOWNSAMPLING_FIELD;
 import static org.elasticsearch.cluster.metadata.DataStreamLifecycle.ENABLED_FIELD;
 
 /**
@@ -48,7 +51,7 @@ public class PutDataStreamLifecycleAction extends ActionType<AcknowledgedRespons
 
         public static final ConstructingObjectParser<Request, Void> PARSER = new ConstructingObjectParser<>(
             "put_data_stream_lifecycle_request",
-            args -> new Request(null, ((TimeValue) args[0]), (Boolean) args[1])
+            args -> new Request(null, ((TimeValue) args[0]), (Boolean) args[1], (Downsampling) args[2])
         );
 
         static {
@@ -59,6 +62,13 @@ public class PutDataStreamLifecycleAction extends ActionType<AcknowledgedRespons
                 ObjectParser.ValueType.STRING_OR_NULL
             );
             PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ENABLED_FIELD);
+            PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> {
+                if (p.currentToken() == XContentParser.Token.VALUE_NULL) {
+                    return Downsampling.NULL;
+                } else {
+                    return new Downsampling(AbstractObjectParser.parseArray(p, c, Downsampling.Round::fromXContent));
+                }
+            }, DOWNSAMPLING_FIELD, ObjectParser.ValueType.OBJECT_ARRAY_OR_NULL);
         }
 
         public static Request parseRequest(XContentParser parser) {
@@ -85,7 +95,7 @@ public class PutDataStreamLifecycleAction extends ActionType<AcknowledgedRespons
         }
 
         public Request(String[] names, @Nullable TimeValue dataRetention) {
-            this(names, dataRetention, null);
+            this(names, dataRetention, null, null);
         }
 
         public Request(String[] names, DataStreamLifecycle lifecycle) {
@@ -94,8 +104,16 @@ public class PutDataStreamLifecycleAction extends ActionType<AcknowledgedRespons
         }
 
         public Request(String[] names, @Nullable TimeValue dataRetention, @Nullable Boolean enabled) {
+            this(names, dataRetention, enabled, null);
+        }
+
+        public Request(String[] names, @Nullable TimeValue dataRetention, @Nullable Boolean enabled, @Nullable Downsampling downsampling) {
             this.names = names;
-            this.lifecycle = DataStreamLifecycle.newBuilder().dataRetention(dataRetention).enabled(enabled == null || enabled).build();
+            this.lifecycle = DataStreamLifecycle.newBuilder()
+                .dataRetention(dataRetention)
+                .enabled(enabled == null || enabled)
+                .downsampling(downsampling)
+                .build();
         }
 
         public String[] getNames() {

+ 26 - 5
modules/data-streams/src/yamlRestTest/resources/rest-api-spec/test/data_stream/lifecycle/20_basic.yml

@@ -1,8 +1,8 @@
 setup:
   - skip:
       features: allowed_warnings
-      version: " - 8.9.99"
-      reason: "Data stream lifecycles only supported in 8.10+"
+      version: " - 8.10.99"
+      reason: "Data stream lifecycles only supported in 8.11+"
   - do:
       allowed_warnings:
         - "index template [my-lifecycle] has index patterns [data-stream-with-lifecycle] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-lifecycle] will take precedence during new index creation"
@@ -65,9 +65,22 @@ setup:
   - do:
       indices.put_data_lifecycle:
         name: "*"
-        body:
-          data_retention: '30d'
-          enabled: false
+        body: >
+          {
+            "downsampling": [
+              {
+                "after": "10d",
+                "fixed_interval": "1h"
+              },
+              {
+                "after": "100d",
+                "fixed_interval": "10h"
+              }
+            ],
+            "data_retention": "30d",
+            "enabled": false
+          }
+
   - is_true: acknowledged
 
   - do:
@@ -77,9 +90,17 @@ setup:
   - match: { data_streams.0.name: data-stream-with-lifecycle }
   - match: { data_streams.0.lifecycle.data_retention: '30d' }
   - match: { data_streams.0.lifecycle.enabled: false}
+  - match: { data_streams.0.lifecycle.downsampling.0.after: '10d'}
+  - match: { data_streams.0.lifecycle.downsampling.0.fixed_interval: '1h'}
+  - match: { data_streams.0.lifecycle.downsampling.1.after: '100d'}
+  - match: { data_streams.0.lifecycle.downsampling.1.fixed_interval: '10h'}
   - match: { data_streams.1.name: simple-data-stream1 }
   - match: { data_streams.1.lifecycle.data_retention: '30d' }
   - match: { data_streams.1.lifecycle.enabled: false}
+  - match: { data_streams.1.lifecycle.downsampling.0.after: '10d'}
+  - match: { data_streams.1.lifecycle.downsampling.0.fixed_interval: '1h'}
+  - match: { data_streams.1.lifecycle.downsampling.1.after: '100d'}
+  - match: { data_streams.1.lifecycle.downsampling.1.fixed_interval: '10h'}
 
 ---
 "Enable lifecycle":