Skip to content

Commit a29af74

Browse files
committed
[Rollup] Only allow aggregating on multiples of configured interval (#32052)
We need to limit the search request aggregations to whole multiples of the configured interval for both histogram and date_histogram. Otherwise, agg buckets won't overlap with the rolled up buckets and the results will be incorrect. For histogram, the validation is very simple: request must be >= the config, and modulo evenly. Dates are more tricky. - If both request and config are fixed dates, we can convert to millis and treat them just like the histo - If both are calendar, we make sure the request is >= the config with a static lookup map that ranks the calendar values relatively. All calendar units are "singles", so they are evenly divisible already - We disallow any other combination (one fixed, one calendar, etc)
1 parent 7d4895d commit a29af74

File tree

8 files changed

+380
-84
lines changed

8 files changed

+380
-84
lines changed

x-pack/docs/build.gradle

+1-2
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,8 @@ setups['sensor_prefab_data'] = '''
686686
page_size: 1000
687687
groups:
688688
date_histogram:
689-
delay: "7d"
690689
field: "timestamp"
691-
interval: "1h"
690+
interval: "7d"
692691
time_zone: "UTC"
693692
terms:
694693
fields:

x-pack/docs/en/rest-api/rollup/put-job.asciidoc

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ started with the <<rollup-start-job,Start Job API>>.
4343
`metrics`::
4444
(object) Defines the metrics that should be collected for each grouping tuple. See <<rollup-job-config,rollup job config>>.
4545

46+
For more details about the job configuration, see <<rollup-job-config>>.
47+
4648
==== Authorization
4749

4850
You must have `manage` or `manage_rollup` cluster privileges to use this API.

x-pack/docs/en/rest-api/rollup/rollup-job-config.asciidoc

+45-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PUT _xpack/rollup/job/sensor
2323
"groups" : {
2424
"date_histogram": {
2525
"field": "timestamp",
26-
"interval": "1h",
26+
"interval": "60m",
2727
"delay": "7d"
2828
},
2929
"terms": {
@@ -99,7 +99,7 @@ fields will then be available later for aggregating into buckets. For example,
9999
"groups" : {
100100
"date_histogram": {
101101
"field": "timestamp",
102-
"interval": "1h",
102+
"interval": "60m",
103103
"delay": "7d"
104104
},
105105
"terms": {
@@ -133,9 +133,9 @@ The `date_histogram` group has several parameters:
133133
The date field that is to be rolled up.
134134

135135
`interval` (required)::
136-
The interval of time buckets to be generated when rolling up. E.g. `"1h"` will produce hourly rollups. This follows standard time formatting
137-
syntax as used elsewhere in Elasticsearch. The `interval` defines the _minimum_ interval that can be aggregated only. If hourly (`"1h"`)
138-
intervals are configured, <<rollup-search,Rollup Search>> can execute aggregations with 1hr or greater (weekly, monthly, etc) intervals.
136+
The interval of time buckets to be generated when rolling up. E.g. `"60m"` will produce 60 minute (hourly) rollups. This follows standard time formatting
137+
syntax as used elsewhere in Elasticsearch. The `interval` defines the _minimum_ interval that can be aggregated only. If hourly (`"60m"`)
138+
intervals are configured, <<rollup-search,Rollup Search>> can execute aggregations with 60m or greater (weekly, monthly, etc) intervals.
139139
So define the interval as the smallest unit that you wish to later query.
140140

141141
Note: smaller, more granular intervals take up proportionally more space.
@@ -154,6 +154,46 @@ The `date_histogram` group has several parameters:
154154
to be stored with a specific timezone. By default, rollup documents are stored in `UTC`, but this can be changed with the `time_zone`
155155
parameter.
156156

157+
.Calendar vs Fixed time intervals
158+
**********************************
159+
Elasticsearch understands both "calendar" and "fixed" time intervals. Fixed time intervals are fairly easy to understand;
160+
`"60s"` means sixty seconds. But what does `"1M` mean? One month of time depends on which month we are talking about,
161+
some months are longer or shorter than others. This is an example of "calendar" time, and the duration of that unit
162+
depends on context. Calendar units are also affected by leap-seconds, leap-years, etc.
163+
164+
This is important because the buckets generated by Rollup will be in either calendar or fixed intervals, and will limit
165+
how you can query them later (see <<rollup-search-limitations-intervals, Requests must be multiples of the config>>.
166+
167+
We recommend sticking with "fixed" time intervals, since they are easier to understand and are more flexible at query
168+
time. It will introduce some drift in your data during leap-events, and you will have to think about months in a fixed
169+
quantity (30 days) instead of the actual calendar length... but it is often easier than dealing with calendar units
170+
at query time.
171+
172+
Multiples of units are always "fixed" (e.g. `"2h"` is always the fixed quantity `7200` seconds. Single units can be
173+
fixed or calendar depending on the unit:
174+
175+
[options="header"]
176+
|=======
177+
|Unit |Calendar |Fixed
178+
|millisecond |NA |`1ms`, `10ms`, etc
179+
|second |NA |`1s`, `10s`, etc
180+
|minute |`1m` |`2m`, `10m`, etc
181+
|hour |`1h` |`2h`, `10h`, etc
182+
|day |`1d` |`2d`, `10d`, etc
183+
|week |`1w` |NA
184+
|month |`1M` |NA
185+
|quarter |`1q` |NA
186+
|year |`1y` |NA
187+
|=======
188+
189+
For some units where there are both fixed and calendar, you may need to express the quantity in terms of the next
190+
smaller unit. For example, if you want a fixed day (not a calendar day), you should specify `24h` instead of `1d`.
191+
Similarly, if you want fixed hours, specify `60m` instead of `1h`. This is because the single quantity entails
192+
calendar time, and limits you to querying by calendar time in the future.
193+
194+
195+
**********************************
196+
157197
===== Terms
158198

159199
The `terms` group can be used on `keyword` or numeric fields, to allow bucketing via the `terms` aggregation at a later point. The `terms`

x-pack/docs/en/rollup/rollup-getting-started.asciidoc

+62-62
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ PUT _xpack/rollup/job/sensor
3737
"groups" : {
3838
"date_histogram": {
3939
"field": "timestamp",
40-
"interval": "1h",
41-
"delay": "7d"
40+
"interval": "60m"
4241
},
4342
"terms": {
4443
"fields": ["node"]
@@ -66,7 +65,7 @@ The `cron` parameter controls when and how often the job activates. When a roll
6665
from where it left off after the last activation. So if you configure the cron to run every 30 seconds, the job will process the last 30
6766
seconds worth of data that was indexed into the `sensor-*` indices.
6867

69-
If instead the cron was configured to run once a day at midnight, the job would process the last 24hours worth of data. The choice is largely
68+
If instead the cron was configured to run once a day at midnight, the job would process the last 24 hours worth of data. The choice is largely
7069
preference, based on how "realtime" you want the rollups, and if you wish to process continuously or move it to off-peak hours.
7170

7271
Next, we define a set of `groups` and `metrics`. The metrics are fairly straightforward: we want to save the min/max/sum of the `temperature`
@@ -79,7 +78,7 @@ It also allows us to run terms aggregations on the `node` field.
7978
.Date histogram interval vs cron schedule
8079
**********************************
8180
You'll note that the job's cron is configured to run every 30 seconds, but the date_histogram is configured to
82-
rollup at hourly intervals. How do these relate?
81+
rollup at 60 minute intervals. How do these relate?
8382
8483
The date_histogram controls the granularity of the saved data. Data will be rolled up into hourly intervals, and you will be unable
8584
to query with finer granularity. The cron simply controls when the process looks for new data to rollup. Every 30 seconds it will see
@@ -223,70 +222,71 @@ Which returns a corresponding response:
223222
[source,js]
224223
----
225224
{
226-
"took" : 93,
227-
"timed_out" : false,
228-
"terminated_early" : false,
229-
"_shards" : ... ,
230-
"hits" : {
231-
"total" : 0,
232-
"max_score" : 0.0,
233-
"hits" : [ ]
234-
},
235-
"aggregations" : {
236-
"timeline" : {
237-
"meta" : { },
238-
"buckets" : [
239-
{
240-
"key_as_string" : "2018-01-18T00:00:00.000Z",
241-
"key" : 1516233600000,
242-
"doc_count" : 6,
243-
"nodes" : {
244-
"doc_count_error_upper_bound" : 0,
245-
"sum_other_doc_count" : 0,
246-
"buckets" : [
247-
{
248-
"key" : "a",
249-
"doc_count" : 2,
250-
"max_temperature" : {
251-
"value" : 202.0
252-
},
253-
"avg_voltage" : {
254-
"value" : 5.1499998569488525
255-
}
256-
},
257-
{
258-
"key" : "b",
259-
"doc_count" : 2,
260-
"max_temperature" : {
261-
"value" : 201.0
262-
},
263-
"avg_voltage" : {
264-
"value" : 5.700000047683716
265-
}
266-
},
267-
{
268-
"key" : "c",
269-
"doc_count" : 2,
270-
"max_temperature" : {
271-
"value" : 202.0
272-
},
273-
"avg_voltage" : {
274-
"value" : 4.099999904632568
275-
}
276-
}
277-
]
278-
}
279-
}
280-
]
281-
}
282-
}
225+
"took" : 93,
226+
"timed_out" : false,
227+
"terminated_early" : false,
228+
"_shards" : ... ,
229+
"hits" : {
230+
"total" : 0,
231+
"max_score" : 0.0,
232+
"hits" : [ ]
233+
},
234+
"aggregations" : {
235+
"timeline" : {
236+
"meta" : { },
237+
"buckets" : [
238+
{
239+
"key_as_string" : "2018-01-18T00:00:00.000Z",
240+
"key" : 1516233600000,
241+
"doc_count" : 6,
242+
"nodes" : {
243+
"doc_count_error_upper_bound" : 0,
244+
"sum_other_doc_count" : 0,
245+
"buckets" : [
246+
{
247+
"key" : "a",
248+
"doc_count" : 2,
249+
"max_temperature" : {
250+
"value" : 202.0
251+
},
252+
"avg_voltage" : {
253+
"value" : 5.1499998569488525
254+
}
255+
},
256+
{
257+
"key" : "b",
258+
"doc_count" : 2,
259+
"max_temperature" : {
260+
"value" : 201.0
261+
},
262+
"avg_voltage" : {
263+
"value" : 5.700000047683716
264+
}
265+
},
266+
{
267+
"key" : "c",
268+
"doc_count" : 2,
269+
"max_temperature" : {
270+
"value" : 202.0
271+
},
272+
"avg_voltage" : {
273+
"value" : 4.099999904632568
274+
}
275+
}
276+
]
277+
}
278+
}
279+
]
280+
}
281+
}
283282
}
283+
284284
----
285285
// TESTRESPONSE[s/"took" : 93/"took" : $body.$_path/]
286286
// TESTRESPONSE[s/"_shards" : \.\.\. /"_shards" : $body.$_path/]
287287

288288
In addition to being more complicated (date histogram and a terms aggregation, plus an additional average metric), you'll notice
289-
the date_histogram uses a `7d` interval instead of `1h`.
289+
the date_histogram uses a `7d` interval instead of `60m`.
290290

291291
[float]
292292
=== Conclusion

x-pack/docs/en/rollup/rollup-search-limitations.asciidoc

+19-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,25 @@ The response will tell you that the field and aggregation were not possible, bec
8080
[float]
8181
=== Interval Granularity
8282

83-
Rollups are stored at a certain granularity, as defined by the `date_histogram` group in the configuration. If data is rolled up at hourly
84-
intervals, the <<rollup-search>> API can aggregate on any time interval hourly or greater. Intervals that are less than an hour will throw
85-
an exception, since the data simply doesn't exist for finer granularities.
83+
Rollups are stored at a certain granularity, as defined by the `date_histogram` group in the configuration. This means you
84+
can only search/aggregate the rollup data with an interval that is greater-than or equal to the configured rollup interval.
85+
86+
For example, if data is rolled up at hourly intervals, the <<rollup-search>> API can aggregate on any time interval
87+
hourly or greater. Intervals that are less than an hour will throw an exception, since the data simply doesn't
88+
exist for finer granularities.
89+
90+
[[rollup-search-limitations-intervals]]
91+
.Requests must be multiples of the config
92+
**********************************
93+
Perhaps not immediately apparent, but the interval specified in an aggregation request must be a whole
94+
multiple of the configured interval. If the job was configured to rollup on `3d` intervals, you can only
95+
query and aggregate on multiples of three (`3d`, `6d`, `9d`, etc).
96+
97+
A non-multiple wouldn't work, since the rolled up data wouldn't cleanly "overlap" with the buckets generated
98+
by the aggregation, leading to incorrect results.
99+
100+
For that reason, an error is thrown if a whole multiple of the configured interval isn't found.
101+
**********************************
86102

87103
Because the RollupSearch endpoint can "upsample" intervals, there is no need to configure jobs with multiple intervals (hourly, daily, etc).
88104
It's recommended to just configure a single job with the smallest granularity that is needed, and allow the search endpoint to upsample

0 commit comments

Comments
 (0)