Skip to content

Commit a130929

Browse files
authored
fixing flakiness in RandomizedTimeSeriesIT - generating unique timestamps (elastic#133489)
* fixing flakiness in RandomizedTimeSeriesIT * fixup * unmuting fixed test * comments
1 parent b8963cc commit a130929

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

muted-tests.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,6 @@ tests:
540540
- class: org.elasticsearch.gradle.internal.transport.TransportVersionValidationFuncTest
541541
method: definitions have primary ids which cannot change
542542
issue: https://github.com/elastic/elasticsearch/issues/133131
543-
- class: org.elasticsearch.xpack.esql.action.RandomizedTimeSeriesIT
544-
method: testGroupBySubset
545-
issue: https://github.com/elastic/elasticsearch/issues/133220
546-
- class: org.elasticsearch.xpack.esql.action.RandomizedTimeSeriesIT
547-
method: testGroupByNothing
548-
issue: https://github.com/elastic/elasticsearch/issues/133225
549543
- class: org.elasticsearch.gradle.internal.transport.TransportVersionValidationFuncTest
550544
method: named and unreferenced definitions cannot have the same name
551545
issue: https://github.com/elastic/elasticsearch/issues/133255

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/RandomizedTimeSeriesIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ public void populateIndex() throws IOException {
183183
if (documents == null) {
184184
documents = new ArrayList<>();
185185
}
186-
documents.add(document);
187186
var indexRequest = client().prepareIndex(DATASTREAM_NAME).setOpType(DocWriteRequest.OpType.CREATE).setSource(document);
188187
indexRequest.setRefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE);
189188
indexRequest.get();
189+
documents.add(document);
190190
}
191191
}
192192

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/TSDataGenerationHelper.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.IOException;
2626
import java.time.Instant;
27+
import java.util.HashSet;
2728
import java.util.List;
2829
import java.util.Map;
2930
import java.util.Set;
@@ -65,6 +66,29 @@ private static Object randomDimensionValue(String dimensionName) {
6566
return dimensionsInMetric.stream().map(attr -> new Tuple<>(attr, randomDimensionValue(attr))).collect(Collectors.toList());
6667
}).toList();
6768

69+
// We want to ensure that all documents have different timestamps.
70+
var now = Instant.now();
71+
var timestampSet = new HashSet<Instant>();
72+
var regens = 0;
73+
for (int i = 0; i < numDocs; i++) {
74+
// Random timestamps within the last 90 days.
75+
while (true) {
76+
var randomIns = Instant.ofEpochMilli(
77+
ESTestCase.randomLongBetween(now.minusSeconds(60 * 60 * 2).toEpochMilli(), now.toEpochMilli())
78+
);
79+
if (timestampSet.add(randomIns)) {
80+
break;
81+
}
82+
regens++;
83+
if (regens > numDocs) {
84+
throw new IllegalStateException("Too many collisions when generating timestamps");
85+
}
86+
}
87+
}
88+
// Timestampset should have exactly numDocs entries - this works as long as the random number generator
89+
// does not cycle early.
90+
assert timestampSet.size() == numDocs : "Expected [" + numDocs + "] timestamps but got [" + timestampSet.size() + "]";
91+
var timestampsIter = timestampSet.iterator();
6892
spec = DataGeneratorSpecification.builder()
6993
.withMaxFieldCountPerLevel(0)
7094
.withPredefinedFields(
@@ -73,7 +97,7 @@ private static Object randomDimensionValue(String dimensionName) {
7397
"@timestamp",
7498
FieldType.DATE,
7599
Map.of("type", "date"),
76-
fieldMapping -> ESTestCase.randomInstantBetween(Instant.now().minusSeconds(2 * 60 * 60), Instant.now())
100+
fieldMapping -> timestampsIter.next()
77101
),
78102
new PredefinedField.WithGenerator(
79103
"attributes",

0 commit comments

Comments
 (0)