Skip to content

Commit ea2145a

Browse files
talevyjrodewig
andauthored
move rollup_index param out of RollupActionConfig (#66139)
This commit moves the ownership of tracking the rollup_index from the RollupActionConfig to the RollupAction.Request. This is cleaner since the config should not be concerned with the source and rollup indices. relates #42720. Co-authored-by: James Rodewig <[email protected]>
1 parent 852f6a4 commit ea2145a

File tree

10 files changed

+54
-80
lines changed

10 files changed

+54
-80
lines changed

docs/reference/rollup/apis/rollup-api.asciidoc

+9-10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ For example, you can roll up hourly data into daily or weekly summaries.
1111

1212
[source,console]
1313
----
14-
POST /my-index-000001/_rollup
14+
POST /my-index-000001/_rollup/my-rollup-index
1515
{
16-
"rollup_index": "my-rollup-index",
1716
"groups": {
1817
"date_histogram": {
1918
"field": "@timestamp",
@@ -44,7 +43,7 @@ POST /my-index-000001/_rollup
4443
[[rollup-api-request]]
4544
==== {api-request-title}
4645

47-
`PUT /<index>/_rollup`
46+
`PUT /<index>/_rollup/<rollup-index>`
4847

4948
[[rollup-api-prereqs]]
5049
==== {api-prereq-title}
@@ -61,11 +60,7 @@ Index to roll up. Cannot be a <<data-streams,data stream>> or
6160
<<indices-aliases,index alias>>. Does not support <<multi-index,multi-target
6261
syntax>> or wildcards (`*`).
6362

64-
[role="child_attributes"]
65-
[[rollup-api-request-body]]
66-
==== {api-request-body-title}
67-
68-
`rollup_index`::
63+
`<rollup-index>`::
6964
(Required, string)
7065
New index that stores the rollup results. Cannot be an existing index,
7166
a <<data-streams,data stream>>, or an <<indices-aliases,index alias>>.
@@ -75,6 +70,10 @@ The request creates this index with
7570
`<index>` is a backing index for a data stream, this index is a backing index
7671
for the same stream.
7772

73+
[role="child_attributes"]
74+
[[rollup-api-request-body]]
75+
==== {api-request-body-title}
76+
7877
`groups`::
7978
(Required, object)
8079
Aggregates and stores fields in the rollup.
@@ -105,7 +104,7 @@ Time interval used to group documents. For differences between
105104
TIP: Choose this value carefully. You won't be able to use a smaller interval
106105
later. For example, you can't aggregate daily rollups into hourly
107106
summaries. However, smaller time intervals can greatly increase the size of your
108-
`rollup_index`.
107+
`<rollup-index>`.
109108

110109
`time_zone`::
111110
(Optional, string)
@@ -147,7 +146,7 @@ Array of <<keyword,keyword family>> and <<number,numeric>> fields to store. If
147146
you specify a `terms` object, this property is required.
148147
+
149148
TIP: Avoid storing high-cardinality fields. High-cardinality fields can greatly
150-
increase the size of your `rollup_index`.
149+
increase the size of your `<rollup-index>`.
151150
=====
152151
====
153152

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/v2/RollupAction.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ private RollupAction() {
3535

3636
public static class Request extends ActionRequest implements ToXContentObject {
3737
private String sourceIndex;
38+
private String rollupIndex;
3839
private RollupActionConfig rollupConfig;
3940

40-
public Request(String sourceIndex, RollupActionConfig rollupConfig) {
41+
public Request(String sourceIndex, String rollupIndex, RollupActionConfig rollupConfig) {
4142
this.sourceIndex = sourceIndex;
43+
this.rollupIndex = rollupIndex;
4244
this.rollupConfig = rollupConfig;
4345
}
4446

@@ -47,25 +49,31 @@ public Request() {}
4749
public Request(StreamInput in) throws IOException {
4850
super(in);
4951
sourceIndex = in.readString();
52+
rollupIndex = in.readString();
5053
rollupConfig = new RollupActionConfig(in);
5154
}
5255

5356
@Override
5457
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
55-
return new RollupTask(id, type, action, parentTaskId, rollupConfig, headers);
58+
return new RollupTask(id, type, action, parentTaskId, rollupIndex, rollupConfig, headers);
5659
}
5760

5861
@Override
5962
public void writeTo(StreamOutput out) throws IOException {
6063
super.writeTo(out);
6164
out.writeString(sourceIndex);
65+
out.writeString(rollupIndex);
6266
rollupConfig.writeTo(out);
6367
}
6468

6569
public String getSourceIndex() {
6670
return sourceIndex;
6771
}
6872

73+
public String getRollupIndex() {
74+
return rollupIndex;
75+
}
76+
6977
public RollupActionConfig getRollupConfig() {
7078
return rollupConfig;
7179
}
@@ -79,14 +87,15 @@ public ActionRequestValidationException validate() {
7987
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
8088
builder.startObject();
8189
builder.field("source_index", sourceIndex);
90+
builder.field("rollup_index", rollupIndex);
8291
rollupConfig.toXContent(builder, params);
8392
builder.endObject();
8493
return builder;
8594
}
8695

8796
@Override
8897
public int hashCode() {
89-
return Objects.hash(sourceIndex, rollupConfig);
98+
return Objects.hash(sourceIndex, rollupIndex, rollupConfig);
9099
}
91100

92101
@Override
@@ -99,6 +108,7 @@ public boolean equals(Object obj) {
99108
}
100109
Request other = (Request) obj;
101110
return Objects.equals(sourceIndex, other.sourceIndex)
111+
&& Objects.equals(rollupIndex, other.rollupIndex)
102112
&& Objects.equals(rollupConfig, other.rollupConfig);
103113
}
104114
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/v2/RollupActionConfig.java

+7-41
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.common.xcontent.ToXContentObject;
2020
import org.elasticsearch.common.xcontent.XContentBuilder;
2121
import org.elasticsearch.common.xcontent.XContentParser;
22-
import org.elasticsearch.xpack.core.rollup.RollupField;
2322
import org.elasticsearch.xpack.core.rollup.job.GroupConfig;
2423
import org.elasticsearch.xpack.core.rollup.job.MetricConfig;
2524

@@ -31,7 +30,6 @@
3130
import java.util.Objects;
3231
import java.util.Set;
3332

34-
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3533
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
3634

3735
/**
@@ -43,59 +41,41 @@ public class RollupActionConfig implements NamedWriteable, ToXContentObject {
4341
private static final String NAME = "xpack/rollup/action/config";
4442
private static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(20);
4543
private static final String TIMEOUT = "timeout";
46-
private static final String ROLLUP_INDEX = "rollup_index";
4744

4845
private final GroupConfig groupConfig;
4946
private final List<MetricConfig> metricsConfig;
5047
private final TimeValue timeout;
51-
private String rollupIndex;
5248

5349
private static final ConstructingObjectParser<RollupActionConfig, Void> PARSER;
5450
static {
5551
PARSER = new ConstructingObjectParser<>(NAME, false, (args) -> {
56-
String rollupIndex = (String) args[0];
57-
GroupConfig groupConfig = (GroupConfig) args[1];
52+
GroupConfig groupConfig = (GroupConfig) args[0];
5853
@SuppressWarnings("unchecked")
59-
List<MetricConfig> metricsConfig = (List<MetricConfig>) args[2];
60-
TimeValue timeout = (TimeValue) args[3];
61-
return new RollupActionConfig(groupConfig, metricsConfig, timeout, rollupIndex);
54+
List<MetricConfig> metricsConfig = (List<MetricConfig>) args[1];
55+
TimeValue timeout = (TimeValue) args[2];
56+
return new RollupActionConfig(groupConfig, metricsConfig, timeout);
6257
});
63-
PARSER.declareString(constructorArg(), new ParseField(ROLLUP_INDEX));
6458
PARSER.declareObject(optionalConstructorArg(), (p, c) -> GroupConfig.fromXContent(p), new ParseField(GroupConfig.NAME));
6559
PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> MetricConfig.fromXContent(p), new ParseField(MetricConfig.NAME));
6660
PARSER.declareField(optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), TIMEOUT),
6761
new ParseField(TIMEOUT), ObjectParser.ValueType.STRING_OR_NULL);
6862
}
6963

70-
public RollupActionConfig(final GroupConfig groupConfig, final List<MetricConfig> metricsConfig,
71-
final @Nullable TimeValue timeout, final String rollupIndex) {
72-
if (rollupIndex == null || rollupIndex.isEmpty()) {
73-
throw new IllegalArgumentException("Rollup index must be a non-null, non-empty string");
74-
}
64+
public RollupActionConfig(final GroupConfig groupConfig, final List<MetricConfig> metricsConfig, final @Nullable TimeValue timeout) {
7565
if (groupConfig == null && (metricsConfig == null || metricsConfig.isEmpty())) {
7666
throw new IllegalArgumentException("At least one grouping or metric must be configured");
7767
}
78-
this.rollupIndex = rollupIndex;
7968
this.groupConfig = groupConfig;
8069
this.metricsConfig = metricsConfig != null ? metricsConfig : Collections.emptyList();
8170
this.timeout = timeout != null ? timeout : DEFAULT_TIMEOUT;
8271
}
8372

8473
public RollupActionConfig(final StreamInput in) throws IOException {
85-
rollupIndex = in.readString();
8674
groupConfig = in.readOptionalWriteable(GroupConfig::new);
8775
metricsConfig = in.readList(MetricConfig::new);
8876
timeout = in.readTimeValue();
8977
}
9078

91-
public String getId() {
92-
return RollupField.NAME + "_" + rollupIndex;
93-
}
94-
95-
public void setRollupIndex(String rollupIndex) {
96-
this.rollupIndex = rollupIndex;
97-
}
98-
9979
public GroupConfig getGroupConfig() {
10080
return groupConfig;
10181
}
@@ -108,10 +88,6 @@ public TimeValue getTimeout() {
10888
return timeout;
10989
}
11090

111-
public String getRollupIndex() {
112-
return rollupIndex;
113-
}
114-
11591
@Override
11692
public String getWriteableName() {
11793
return NAME;
@@ -142,7 +118,6 @@ public void validateMappings(final Map<String, Map<String, FieldCapabilities>> f
142118
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
143119
builder.startObject();
144120
{
145-
builder.field(ROLLUP_INDEX, rollupIndex);
146121
if (groupConfig != null) {
147122
builder.field(GroupConfig.NAME, groupConfig);
148123
}
@@ -163,7 +138,6 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa
163138

164139
@Override
165140
public void writeTo(final StreamOutput out) throws IOException {
166-
out.writeString(rollupIndex);
167141
out.writeOptionalWriteable(groupConfig);
168142
out.writeList(metricsConfig);
169143
out.writeTimeValue(timeout);
@@ -179,29 +153,21 @@ public boolean equals(Object other) {
179153
}
180154

181155
final RollupActionConfig that = (RollupActionConfig) other;
182-
return Objects.equals(this.rollupIndex, that.rollupIndex)
183-
&& Objects.equals(this.groupConfig, that.groupConfig)
156+
return Objects.equals(this.groupConfig, that.groupConfig)
184157
&& Objects.equals(this.metricsConfig, that.metricsConfig)
185158
&& Objects.equals(this.timeout, that.timeout);
186159
}
187160

188161
@Override
189162
public int hashCode() {
190-
return Objects.hash(rollupIndex, groupConfig, metricsConfig, timeout);
163+
return Objects.hash(groupConfig, metricsConfig, timeout);
191164
}
192165

193166
@Override
194167
public String toString() {
195168
return Strings.toString(this, true, true);
196169
}
197170

198-
/**
199-
* Same as toString() but more explicitly named so the caller knows this is turned into JSON
200-
*/
201-
public String toJSONString() {
202-
return toString();
203-
}
204-
205171
public static RollupActionConfig fromXContent(final XContentParser parser) throws IOException {
206172
return PARSER.parse(parser, null);
207173
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/v2/RollupTask.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
package org.elasticsearch.xpack.core.rollup.v2;
77

8-
import org.apache.logging.log4j.LogManager;
9-
import org.apache.logging.log4j.Logger;
108
import org.elasticsearch.tasks.CancellableTask;
119
import org.elasticsearch.tasks.TaskId;
1210
import org.elasticsearch.xpack.core.rollup.RollupField;
@@ -19,16 +17,21 @@
1917
* which drives the indexing, and periodically updates it's parent PersistentTask with the indexing's current position.
2018
*/
2119
public class RollupTask extends CancellableTask {
22-
private static final Logger logger = LogManager.getLogger(RollupTask.class.getName());
23-
20+
private String rollupIndex;
2421
private RollupActionConfig config;
2522
private RollupJobStatus status;
2623

27-
RollupTask(long id, String type, String action, TaskId parentTask, RollupActionConfig config, Map<String, String> headers) {
28-
super(id, type, action, RollupField.NAME + "_" + config.getRollupIndex(), parentTask, headers);
24+
RollupTask(long id, String type, String action, TaskId parentTask, String rollupIndex, RollupActionConfig config,
25+
Map<String, String> headers) {
26+
super(id, type, action, RollupField.NAME + "_" + rollupIndex, parentTask, headers);
27+
this.rollupIndex = rollupIndex;
2928
this.config = config;
3029
}
3130

31+
public String getRollupIndex() {
32+
return rollupIndex;
33+
}
34+
3235
public RollupActionConfig config() {
3336
return config;
3437
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/v2/RollupActionConfigTests.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ protected RollupActionConfig createTestInstance() {
2929
}
3030

3131
public static RollupActionConfig randomConfig(Random random) {
32-
final String rollupIndex = randomAlphaOfLength(5);
3332
final TimeValue timeout = random.nextBoolean() ? null : ConfigTestHelpers.randomTimeout(random);
3433
final GroupConfig groupConfig = ConfigTestHelpers.randomGroupConfig(random);
3534
final List<MetricConfig> metricConfigs = ConfigTestHelpers.randomMetricsConfigs(random);
36-
return new RollupActionConfig(groupConfig, metricConfigs, timeout, rollupIndex);
35+
return new RollupActionConfig(groupConfig, metricConfigs, timeout);
3736
}
3837

3938
@Override
@@ -46,19 +45,10 @@ protected RollupActionConfig doParseInstance(final XContentParser parser) throws
4645
return RollupActionConfig.fromXContent(parser);
4746
}
4847

49-
public void testEmptyRollupIndex() {
50-
final RollupActionConfig sample = createTestInstance();
51-
Exception e = expectThrows(IllegalArgumentException.class, () ->
52-
new RollupActionConfig(sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout(),
53-
randomBoolean() ? null : ""));
54-
assertThat(e.getMessage(), equalTo("Rollup index must be a non-null, non-empty string"));
55-
}
56-
5748
public void testEmptyGroupAndMetrics() {
5849
final RollupActionConfig sample = createTestInstance();
5950
Exception e = expectThrows(IllegalArgumentException.class, () ->
60-
new RollupActionConfig(null, randomBoolean() ? null : emptyList(), sample.getTimeout(),
61-
sample.getRollupIndex()));
51+
new RollupActionConfig(null, randomBoolean() ? null : emptyList(), sample.getTimeout()));
6252
assertThat(e.getMessage(), equalTo("At least one grouping or metric must be configured"));
6353
}
6454
}

x-pack/plugin/rollup/qa/rest/src/test/resources/rest-api-spec/test/rollup/10_basic.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ setup:
5454
- do:
5555
rollup.rollup:
5656
index: docs
57+
rollup_index: rollup_docs
5758
body: >
5859
{
59-
"rollup_index": "rollup_docs",
6060
"groups" : {
6161
"date_histogram": {
6262
"field": "timestamp",

x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/RestRollupAction.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ public class RestRollupAction extends BaseRestHandler {
2222

2323
@Override
2424
public List<Route> routes() {
25-
return List.of(new Route(POST, "/{index}/_rollup"));
25+
return List.of(new Route(POST, "/{index}/_rollup/{rollup_index}"));
2626
}
2727

2828
@Override
2929
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
3030
String index = restRequest.param("index");
31+
String rollupIndex = restRequest.param("rollup_index");
3132
RollupActionConfig config = RollupActionConfig.fromXContent(restRequest.contentParser());
32-
RollupAction.Request request = new RollupAction.Request(index, config);
33+
RollupAction.Request request = new RollupAction.Request(index, rollupIndex, config);
3334
return channel -> client.execute(RollupAction.INSTANCE, request, new RestToXContentListener<>(channel));
3435
}
3536

x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/v2/RollupV2Indexer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ public class RollupV2Indexer extends AsyncTwoPhaseIndexer<Map<String, Object>, R
106106
this.request = request;
107107
this.headers = headers;
108108
this.compositeBuilder = createCompositeBuilder(this.request.getRollupConfig());
109-
this.tmpIndex = ".rolluptmp-" + this.request.getRollupConfig().getRollupIndex();
109+
this.tmpIndex = ".rolluptmp-" + this.request.getRollupIndex();
110110
this.completionListener = completionListener;
111111
}
112112

113113
@Override
114114
protected String getJobId() {
115-
return request.getRollupConfig().getId();
115+
return "rollup_" + request.getRollupIndex();
116116
}
117117

118118
@Override
@@ -196,7 +196,7 @@ protected void onFailure(Exception exc) {
196196
@Override
197197
protected void onFinish(ActionListener<Void> listener) {
198198
// "shrink index"
199-
ResizeRequest resizeRequest = new ResizeRequest(request.getRollupConfig().getRollupIndex(), tmpIndex);
199+
ResizeRequest resizeRequest = new ResizeRequest(request.getRollupIndex(), tmpIndex);
200200
resizeRequest.setResizeType(ResizeType.CLONE);
201201
resizeRequest.getTargetIndexRequest()
202202
.settings(Settings.builder().put(IndexMetadata.SETTING_INDEX_HIDDEN, false).build());

0 commit comments

Comments
 (0)