Skip to content

Commit 2f36ef4

Browse files
committed
Streams: Adjust max_age for tiny retentions
1 parent 5cca15f commit 2f36ef4

File tree

4 files changed

+113
-4
lines changed

4 files changed

+113
-4
lines changed

modules/data-streams/src/test/java/org/elasticsearch/datastreams/lifecycle/DataStreamLifecycleServiceTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import static org.hamcrest.Matchers.containsString;
125125
import static org.hamcrest.Matchers.empty;
126126
import static org.hamcrest.Matchers.equalTo;
127+
import static org.hamcrest.Matchers.hasSize;
127128
import static org.hamcrest.Matchers.instanceOf;
128129
import static org.hamcrest.Matchers.is;
129130
import static org.hamcrest.Matchers.lessThanOrEqualTo;
@@ -1166,6 +1167,53 @@ public void testMergePolicySettingsAreConfiguredBeforeForcemerge() throws Except
11661167
assertThat(((ForceMergeRequest) clientSeenRequests.get(3)).indices().length, is(1));
11671168
}
11681169

1170+
public void testWithTinyRetentions() {
1171+
final String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
1172+
final int numBackingIndices = 1;
1173+
final int numFailureIndices = 1;
1174+
final ProjectMetadata.Builder metadataBuilder = ProjectMetadata.builder(randomProjectIdOrDefault());
1175+
final DataStreamLifecycle dataLifecycle = DataStreamLifecycle.dataLifecycleBuilder()
1176+
.dataRetention(TimeValue.timeValueDays(1))
1177+
.build();
1178+
final DataStreamLifecycle failuresLifecycle = DataStreamLifecycle.failuresLifecycleBuilder()
1179+
.dataRetention(TimeValue.timeValueHours(12))
1180+
.build();
1181+
final DataStream dataStream = createDataStream(
1182+
metadataBuilder,
1183+
dataStreamName,
1184+
numBackingIndices,
1185+
numFailureIndices,
1186+
settings(IndexVersion.current()),
1187+
dataLifecycle,
1188+
failuresLifecycle,
1189+
now
1190+
);
1191+
metadataBuilder.put(dataStream);
1192+
1193+
final ClusterState state = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(metadataBuilder).build();
1194+
dataStreamLifecycleService.run(state);
1195+
1196+
assertThat(clientSeenRequests, hasSize(2));
1197+
assertThat(clientSeenRequests.get(0), instanceOf(RolloverRequest.class));
1198+
assertThat(clientSeenRequests.get(1), instanceOf(RolloverRequest.class));
1199+
1200+
// test main data stream max_age
1201+
final RolloverRequest rolloverRequest = (RolloverRequest) clientSeenRequests.get(0);
1202+
assertThat(rolloverRequest.getRolloverTarget(), is(dataStreamName));
1203+
final RolloverConditions conditions = rolloverRequest.getConditions();
1204+
assertThat(conditions.getMaxAge(), equalTo(TimeValue.timeValueHours(1))); // 1 day retention -> 1h max_age
1205+
1206+
// test failure data stream max_age
1207+
final RolloverRequest rolloverFailureIndexRequest = (RolloverRequest) clientSeenRequests.get(1);
1208+
assertThat(
1209+
rolloverFailureIndexRequest.getRolloverTarget(),
1210+
is(IndexNameExpressionResolver.combineSelector(dataStreamName, IndexComponentSelector.FAILURES))
1211+
);
1212+
1213+
final RolloverConditions failureStoreConditions = rolloverFailureIndexRequest.getConditions();
1214+
assertThat(failureStoreConditions.getMaxAge(), equalTo(TimeValue.timeValueHours(1))); // 12h retention -> 1h max_age
1215+
}
1216+
11691217
public void testDownsampling() throws Exception {
11701218
String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
11711219
int numBackingIndices = 2;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
teardown:
3+
- do:
4+
indices.delete_data_stream:
5+
name: "tiny-retention-data-stream"
6+
ignore: 404
7+
- do:
8+
indices.delete_index_template:
9+
name: "tiny-retention-template"
10+
ignore: 404
11+
12+
---
13+
"Test failure store rollover with tiny retention":
14+
- requires:
15+
test_runner_features: [ capabilities, allowed_warnings ]
16+
reason: "Exposing failures lifecycle config in templates was added in 9.1+"
17+
capabilities:
18+
- method: GET
19+
path: /_data_stream/{target}
20+
capabilities: [ 'failure_store.lifecycle' ]
21+
- do:
22+
indices.put_index_template:
23+
name: tiny-retention-template
24+
body:
25+
index_patterns: [tiny-retention-*]
26+
template:
27+
settings:
28+
index.number_of_replicas: 0
29+
lifecycle:
30+
data_retention: "1d"
31+
data_stream_options:
32+
failure_store:
33+
enabled: true
34+
lifecycle:
35+
data_retention: "12h"
36+
data_stream: {}
37+
38+
- do:
39+
indices.create_data_stream:
40+
name: tiny-retention-data-stream
41+
42+
- do:
43+
indices.get_data_stream:
44+
name: "tiny-retention-data-stream"
45+
include_defaults: true
46+
- length: { data_streams: 1 }
47+
- match: { data_streams.0.name: tiny-retention-data-stream }
48+
- match: { data_streams.0.lifecycle.data_retention: "1d" }
49+
- match: { data_streams.0.lifecycle.rollover.max_age: "1h [automatic]" }
50+
- match: { data_streams.0.failure_store.enabled: true }
51+
- match: { data_streams.0.failure_store.lifecycle.enabled: true }
52+
- match: { data_streams.0.failure_store.lifecycle.data_retention: "12h" }
53+
- match: { data_streams.0.failure_store.lifecycle.rollover.max_age: "1h [automatic]" }

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverConfiguration.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,18 @@ public static RolloverConfiguration parseSetting(String input, String setting) {
173173
/**
174174
* When max_age is auto we’ll use the following retention dependent heuristics to compute the value of max_age:
175175
* - If retention is null aka infinite (default), max_age will be 30 days
176-
* - If retention is configured to anything lower than 14 days, max_age will be 1 day
177-
* - If retention is configured to anything lower than 90 days, max_age will be 7 days
178-
* - If retention is configured to anything greater than 90 days, max_age will be 30 days
176+
* - If retention is less than or equal to 1 day, max_age will be 1 hour
177+
* - If retention is less than or equal to 14 days, max_age will be 1 day
178+
* - If retention is less than or equal to 90 days, max_age will be 7 days
179+
* - If retention is greater than 90 days, max_age will be 30 days
179180
*/
180181
static TimeValue evaluateMaxAgeCondition(@Nullable TimeValue retention) {
181182
if (retention == null) {
182183
return TimeValue.timeValueDays(30);
183184
}
185+
if (retention.compareTo(TimeValue.timeValueDays(1)) <= 0) {
186+
return TimeValue.timeValueHours(1);
187+
}
184188
if (retention.compareTo(TimeValue.timeValueDays(14)) <= 0) {
185189
return TimeValue.timeValueDays(1);
186190
}

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverConfigurationTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ public void testAutoMaxAgeCalculation() {
268268
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueDays(91)), equalTo(TimeValue.timeValueDays(30)));
269269
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueDays(90)), equalTo(TimeValue.timeValueDays(7)));
270270
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueDays(14)), equalTo(TimeValue.timeValueDays(1)));
271-
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueDays(1)), equalTo(TimeValue.timeValueDays(1)));
271+
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueDays(1)), equalTo(TimeValue.timeValueHours(1)));
272+
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueHours(23)), equalTo(TimeValue.timeValueHours(1)));
273+
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueHours(12)), equalTo(TimeValue.timeValueHours(1)));
274+
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueHours(1)), equalTo(TimeValue.timeValueHours(1)));
275+
assertThat(RolloverConfiguration.evaluateMaxAgeCondition(TimeValue.timeValueHours(0)), equalTo(TimeValue.timeValueHours(1)));
272276
}
273277

274278
public void testToXContent() throws IOException {

0 commit comments

Comments
 (0)