Skip to content

Commit 038a4f2

Browse files
authored
Set lower bound on DLM retention in tests (#134536)
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
1 parent 9b41320 commit 038a4f2

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
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
@@ -87,6 +87,7 @@
8787
import org.elasticsearch.common.settings.Settings;
8888
import org.elasticsearch.common.xcontent.XContentHelper;
8989
import org.elasticsearch.core.Nullable;
90+
import org.elasticsearch.core.TimeValue;
9091
import org.elasticsearch.index.Index;
9192
import org.elasticsearch.index.IndexNotFoundException;
9293
import org.elasticsearch.index.mapper.DataStreamTimestampFieldMapper;
@@ -1393,7 +1394,7 @@ public void testSearchAllResolvesDataStreams() throws Exception {
13931394
public void testGetDataStream() throws Exception {
13941395
Settings settings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, maximumNumberOfReplicas() + 2).build();
13951396
DataStreamLifecycle.Template lifecycle = DataStreamLifecycle.dataLifecycleBuilder()
1396-
.dataRetention(randomPositiveTimeValue())
1397+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
13971398
.buildTemplate();
13981399
putComposableIndexTemplate("template_for_foo", null, List.of("metrics-foo*"), settings, null, null, lifecycle, false);
13991400
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
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.common.network.NetworkModule;
3535
import org.elasticsearch.common.settings.Settings;
3636
import org.elasticsearch.common.xcontent.XContentHelper;
37+
import org.elasticsearch.core.TimeValue;
3738
import org.elasticsearch.datastreams.DataStreamsPlugin;
3839
import org.elasticsearch.indices.ExecutorNames;
3940
import org.elasticsearch.indices.SystemDataStreamDescriptor;
@@ -207,7 +208,10 @@ public Collection<SystemDataStreamDescriptor> getSystemDataStreamDescriptors() {
207208
Template.builder()
208209
.settings(Settings.EMPTY)
209210
.mappings(mappings)
210-
.lifecycle(DataStreamLifecycle.dataLifecycleBuilder().dataRetention(randomPositiveTimeValue()))
211+
.lifecycle(
212+
DataStreamLifecycle.dataLifecycleBuilder()
213+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
214+
)
211215
)
212216
.dataStreamTemplate(new DataStreamTemplate())
213217
.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
@@ -1422,6 +1422,18 @@ public static TimeValue randomPositiveTimeValue() {
14221422
return randomTimeValue(1, 1000);
14231423
}
14241424

1425+
/**
1426+
* Generate a random TimeValue that is greater than the provided timeValue.
1427+
* Chooses a random TimeUnit, adds between 1 and 1000 of that unit to {@code timeValue}, and returns a TimeValue in that unit.
1428+
*/
1429+
public static TimeValue randomTimeValueGreaterThan(TimeValue lowerBound) {
1430+
final TimeUnit randomUnit = randomFrom(TimeUnit.values());
1431+
// This conversion might round down, but that's fine since we add at least 1 below, ensuring we still satisfy the "greater than".
1432+
final long lowerBoundDuration = randomUnit.convert(lowerBound.duration(), lowerBound.timeUnit());
1433+
final long duration = lowerBoundDuration + randomLongBetween(1, 1000);
1434+
return new TimeValue(duration, randomUnit);
1435+
}
1436+
14251437
/**
14261438
* generate a random epoch millis in a range 1 to 9999-12-31T23:59:59.999
14271439
*/

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

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

153153
// let's update the index template to remove the ILM configuration and configured data stream lifecycle
154154
// note that this index template change will NOT configure a data stream lifecycle on the data stream, only for **new** data streams
155-
156155
// All existing data streams will fallback to their default data stream lifecycle
157-
158-
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
159-
// data stream has 3 backing indices, two managed by ILM and one will be managed by the data stream lifecycle
160156
DataStreamLifecycle.Template customLifecycle = DataStreamLifecycle.dataLifecycleBuilder()
161-
.dataRetention(randomPositiveTimeValue())
157+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
162158
.buildTemplate();
163159
putComposableIndexTemplate(indexTemplateName, null, List.of(dataStreamName + "*"), Settings.EMPTY, null, customLifecycle);
164160

161+
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
162+
// data stream has 3 backing indices, two managed by ILM and one will be managed by the data stream lifecycle
165163
indexDocs(dataStreamName, 2);
166-
167164
// data stream was rolled over and has 3 indices, two managed by ILM and the write index will be unmanaged
168165
backingIndices = waitForDataStreamBackingIndices(dataStreamName, 3);
169166
String thirdGenerationIndex = backingIndices.get(2);
@@ -482,11 +479,8 @@ public void testUpdateIndexTemplateToDataStreamLifecyclePreference() throws Exce
482479

483480
// let's update the index template to configure the management preference to be data stream lifecycle using the prefer_ilm setting
484481
// note that this index template change will NOT affect existing indices but only the new ones after a rollover.
485-
486-
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
487-
// data stream has 3 backing indices, 2 managed by ILM and 1 by the default data stream lifecycle
488482
DataStreamLifecycle.Template customLifecycle = DataStreamLifecycle.dataLifecycleBuilder()
489-
.dataRetention(randomPositiveTimeValue())
483+
.dataRetention(randomTimeValueGreaterThan(TimeValue.timeValueSeconds(10)))
490484
.buildTemplate();
491485
putComposableIndexTemplate(
492486
indexTemplateName,
@@ -497,8 +491,9 @@ public void testUpdateIndexTemplateToDataStreamLifecyclePreference() throws Exce
497491
customLifecycle
498492
);
499493

494+
// we'll rollover the data stream by indexing 2 documents (like ILM expects) and assert that the rollover happens once so the
495+
// data stream has 3 backing indices, 2 managed by ILM and 1 by the default data stream lifecycle
500496
indexDocs(dataStreamName, 2);
501-
502497
backingIndices = waitForDataStreamBackingIndices(dataStreamName, 3);
503498
String thirdGenerationIndex = backingIndices.get(2);
504499

0 commit comments

Comments
 (0)