Skip to content

Commit 421b6f4

Browse files
authored
Set lower bound on DLM retention in tests (#134536) (#135088)
The previous randomization logic in these tests could result in a retention sufficiently short enough for the index to (unexpectedly) be deleted by DLM during the test, resulting in a failure. Fixes #134276 (cherry picked from commit 038a4f2) # Conflicts: # x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/DataStreamAndIndexLifecycleMixingTests.java
1 parent c8dae02 commit 421b6f4

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.elasticsearch.common.compress.CompressedXContent;
8181
import org.elasticsearch.common.settings.Settings;
8282
import org.elasticsearch.core.Nullable;
83+
import org.elasticsearch.core.TimeValue;
8384
import org.elasticsearch.index.Index;
8485
import org.elasticsearch.index.IndexNotFoundException;
8586
import org.elasticsearch.index.mapper.DataStreamTimestampFieldMapper;
@@ -1373,7 +1374,7 @@ public void testSearchAllResolvesDataStreams() throws Exception {
13731374
public void testGetDataStream() throws Exception {
13741375
Settings settings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, maximumNumberOfReplicas() + 2).build();
13751376
DataStreamLifecycle.Template lifecycle = DataStreamLifecycle.dataLifecycleBuilder()
1376-
.dataRetention(randomPositiveTimeValue())
1377+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
13771378
.buildTemplate();
13781379
putComposableIndexTemplate("template_for_foo", null, List.of("metrics-foo*"), settings, null, null, lifecycle, false);
13791380
int numDocsFoo = randomIntBetween(2, 16);

modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/lifecycle/CrudDataStreamLifecycleIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void testPutLifecycle() throws Exception {
227227

228228
public void testDeleteLifecycle() throws Exception {
229229
DataStreamLifecycle.Template lifecycle = DataStreamLifecycle.dataLifecycleBuilder()
230-
.dataRetention(randomPositiveTimeValue())
230+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
231231
.buildTemplate();
232232
putComposableIndexTemplate("id1", null, List.of("with-lifecycle*"), null, null, lifecycle);
233233
putComposableIndexTemplate("id2", null, List.of("without-lifecycle*"), null, null, null);

modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/lifecycle/CrudSystemDataStreamLifecycleIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.common.network.NetworkModule;
3333
import org.elasticsearch.common.settings.Settings;
3434
import org.elasticsearch.common.xcontent.XContentHelper;
35+
import org.elasticsearch.core.TimeValue;
3536
import org.elasticsearch.datastreams.DataStreamsPlugin;
3637
import org.elasticsearch.indices.ExecutorNames;
3738
import org.elasticsearch.indices.SystemDataStreamDescriptor;
@@ -204,7 +205,10 @@ public Collection<SystemDataStreamDescriptor> getSystemDataStreamDescriptors() {
204205
Template.builder()
205206
.settings(Settings.EMPTY)
206207
.mappings(mappings)
207-
.lifecycle(DataStreamLifecycle.dataLifecycleBuilder().dataRetention(randomPositiveTimeValue()))
208+
.lifecycle(
209+
DataStreamLifecycle.dataLifecycleBuilder()
210+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
211+
)
208212
)
209213
.dataStreamTemplate(new DataStreamTemplate())
210214
.build(),

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,18 @@ public static TimeValue randomPositiveTimeValue() {
13861386
return randomTimeValue(1, 1000);
13871387
}
13881388

1389+
/**
1390+
* Generate a random TimeValue that is greater than the provided timeValue.
1391+
* Chooses a random TimeUnit, adds between 1 and 1000 of that unit to {@code timeValue}, and returns a TimeValue in that unit.
1392+
*/
1393+
public static TimeValue randomTimeValueGreaterThan(TimeValue lowerBound) {
1394+
final TimeUnit randomUnit = randomFrom(TimeUnit.values());
1395+
// This conversion might round down, but that's fine since we add at least 1 below, ensuring we still satisfy the "greater than".
1396+
final long lowerBoundDuration = randomUnit.convert(lowerBound.duration(), lowerBound.timeUnit());
1397+
final long duration = lowerBoundDuration + randomLongBetween(1, 1000);
1398+
return new TimeValue(duration, randomUnit);
1399+
}
1400+
13891401
/**
13901402
* generate a random epoch millis in a range 1 to 9999-12-31T23:59:59.999
13911403
*/

x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/DataStreamAndIndexLifecycleMixingTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,15 @@ public void testIndexTemplateSwapsILMForDataStreamLifecycle() throws Exception {
165165

166166
// let's update the index template to remove the ILM configuration and configured data stream lifecycle
167167
// note that this index template change will NOT configure a data stream lifecycle on the data stream, only for **new** data streams
168-
169168
// All existing data streams will fallback to their default data stream lifecycle
170-
171-
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
172-
// data stream has 3 backing indices, two managed by ILM and one will be managed by the data stream lifecycle
173169
DataStreamLifecycle.Template customLifecycle = DataStreamLifecycle.dataLifecycleBuilder()
174-
.dataRetention(randomPositiveTimeValue())
170+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
175171
.buildTemplate();
176172
putComposableIndexTemplate(indexTemplateName, null, List.of(dataStreamName + "*"), Settings.EMPTY, null, customLifecycle);
177173

174+
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
175+
// data stream has 3 backing indices, two managed by ILM and one will be managed by the data stream lifecycle
178176
indexDocs(dataStreamName, 2);
179-
180177
// data stream was rolled over and has 3 indices, two managed by ILM and the write index will be unmanaged
181178
assertBusy(() -> {
182179
GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request(
@@ -561,11 +558,8 @@ public void testUpdateIndexTemplateToDataStreamLifecyclePreference() throws Exce
561558

562559
// let's update the index template to configure the management preference to be data stream lifecycle using the prefer_ilm setting
563560
// note that this index template change will NOT affect existing indices but only the new ones after a rollover.
564-
565-
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
566-
// data stream has 3 backing indices, 2 managed by ILM and 1 by the default data stream lifecycle
567561
DataStreamLifecycle.Template customLifecycle = DataStreamLifecycle.dataLifecycleBuilder()
568-
.dataRetention(randomPositiveTimeValue())
562+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
569563
.buildTemplate();
570564
putComposableIndexTemplate(
571565
indexTemplateName,
@@ -576,6 +570,8 @@ public void testUpdateIndexTemplateToDataStreamLifecyclePreference() throws Exce
576570
customLifecycle
577571
);
578572

573+
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
574+
// data stream has 3 backing indices, 2 managed by ILM and 1 by the default data stream lifecycle
579575
indexDocs(dataStreamName, 2);
580576

581577
assertBusy(() -> {

0 commit comments

Comments
 (0)