Skip to content

Commit 507ceb6

Browse files
committed
fixed algos to make tolerance 1%
1 parent cf93a6d commit 507ceb6

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class RandomizedTimeSeriesIT extends AbstractEsqlIntegTestCase {
5959
private static final Long TIME_RANGE_SECONDS = 3600L;
6060
private static final String DATASTREAM_NAME = "tsit_ds";
6161
private static final Integer SECONDS_IN_WINDOW = 60;
62-
private List<XContentBuilder> documents = null;
62+
private List<XContentBuilder> documents;
6363
private TSDataGenerationHelper dataGenerationHelper;
6464

6565
List<List<Object>> consumeRows(EsqlQueryResponse resp) {
@@ -208,7 +208,21 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
208208
record RateRange(Double lower, Double upper) implements Comparable<RateRange> {
209209
@Override
210210
public int compareTo(RateRange o) {
211-
return this.lower.compareTo(o.lower);
211+
// Compare first by lower bound, then by upper bound
212+
int cmp = this.lower.compareTo(o.lower);
213+
if (cmp == 0) {
214+
return this.upper.compareTo(o.upper);
215+
}
216+
return cmp;
217+
}
218+
219+
public int compareToFindingMax(RateRange o) {
220+
// Compare first by upper bound, then by lower bound
221+
int cmp = this.upper.compareTo(o.upper);
222+
if (cmp == 0) {
223+
return this.lower.compareTo(o.lower);
224+
}
225+
return cmp;
212226
}
213227
}
214228

@@ -248,16 +262,16 @@ static RateStats calculateRateAggregation(
248262
}
249263
// TODO: Remove tolerances since we are already allowing a min-max range
250264
return new RateRange(
251-
counterGrowth / secondsInWindow * 0.95, // Add 5% tolerance to the lower bound
252-
counterGrowth / (lastTs.toEpochMilli() / 1000 - firstTs.toEpochMilli() / 1000) * 1.2 // Add 20% tolerance to the upper bound
265+
counterGrowth / secondsInWindow * 0.99, // Add 1% tolerance to the lower bound
266+
1000.0 * counterGrowth / (lastTs.toEpochMilli() - firstTs.toEpochMilli()) * 1.01 // Add 1% tolerance to the upper bound
253267
);
254268
}).filter(Objects::nonNull).toList();
255269
if (allRates.isEmpty()) {
256270
return new RateStats(0L, null, null, null, new RateRange(0.0, 0.0));
257271
}
258272
return new RateStats(
259273
(long) allRates.size(),
260-
allRates.stream().max(RateRange::compareTo).orElseThrow(),
274+
allRates.stream().max(RateRange::compareToFindingMax).orElseThrow(),
261275
new RateRange(
262276
allRates.stream().mapToDouble(r -> r.lower).average().orElseThrow(),
263277
allRates.stream().mapToDouble(r -> r.upper).average().orElseThrow()
@@ -319,7 +333,7 @@ void checkWithin(Double actual, RateRange expected) {
319333

320334
void assertNoFailedWindows(List<String> failedWindows, List<List<Object>> rows) {
321335
// TODO: WE have a 15% tolerance for failed windows. Must remove.
322-
if (failedWindows.size() < 0.15 * rows.size()) {
336+
if (failedWindows.size() < 0.01 * rows.size()) {
323337
logger.warn(
324338
"Failed " + failedWindows.size() + " windows out of " + rows.size() + ", failures:\n" + String.join("\n", failedWindows)
325339
);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,21 @@ private static Object randomDimensionValue(String dimensionName) {
5454
// Making a list-to-set-to-list to ensure uniqueness.
5555
this.numDocs = numDocs;
5656
var maxAttributes = (int) Math.sqrt(numDocs);
57-
attributesForMetrics = List.copyOf(
57+
List<String> tempAttributeSet = List.copyOf(
5858
Set.copyOf(ESTestCase.randomList(1, maxAttributes, () -> ESTestCase.randomAlphaOfLengthBetween(2, 30)))
5959
);
6060
var maxTimeSeries = (int) Math.sqrt(numDocs);
6161
var minTimeSeries = Math.max(1, maxTimeSeries / 4);
6262
numTimeSeries = ESTestCase.randomIntBetween(minTimeSeries, maxTimeSeries);
63+
Set<String> usedAttributeNames = new HashSet<>();
6364
// allTimeSeries contains the list of dimension-values for each time series.
6465
List<List<Tuple<String, Object>>> allTimeSeries = IntStream.range(0, numTimeSeries).mapToObj(tsIdx -> {
65-
List<String> dimensionsInMetric = ESTestCase.randomNonEmptySubsetOf(attributesForMetrics);
66+
List<String> dimensionsInMetric = ESTestCase.randomNonEmptySubsetOf(tempAttributeSet);
6667
// TODO: How do we handle the case when there are no dimensions? (i.e. regular randomSubsetof(...)
68+
usedAttributeNames.addAll(dimensionsInMetric);
6769
return dimensionsInMetric.stream().map(attr -> new Tuple<>(attr, randomDimensionValue(attr))).collect(Collectors.toList());
6870
}).toList();
71+
attributesForMetrics = List.copyOf(usedAttributeNames);
6972

7073
// We want to ensure that all documents have different timestamps.
7174
var timeRangeMs = timeRangeSeconds * 1000;

0 commit comments

Comments
 (0)