Skip to content

Commit 73b323e

Browse files
authored
Fixing random_sampler tests (#83549)
Fixes smoke tests my enabling the random_sampler agg Fixes flakey sampler test closes #83523 closes #83357
1 parent 2282cf7 commit 73b323e

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

qa/smoke-test-multinode/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ testClusters.configureEach {
2929
setting 'xpack.security.enabled', 'false'
3030
if (BuildParams.isSnapshotBuild() == false) {
3131
systemProperty 'es.index_mode_feature_flag_registered', 'true'
32+
systemProperty 'es.random_sampler_feature_flag_registered', 'true'
3233
}
3334
}
3435

server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/random/RandomSamplerAggregatorTests.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,51 @@
1717

1818
import java.io.IOException;
1919
import java.util.List;
20+
import java.util.concurrent.atomic.AtomicInteger;
21+
import java.util.stream.DoubleStream;
22+
import java.util.stream.LongStream;
2023

2124
import static org.hamcrest.Matchers.allOf;
2225
import static org.hamcrest.Matchers.closeTo;
23-
import static org.hamcrest.Matchers.greaterThan;
24-
import static org.hamcrest.Matchers.lessThan;
26+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
27+
import static org.hamcrest.Matchers.lessThanOrEqualTo;
2528

2629
public class RandomSamplerAggregatorTests extends AggregatorTestCase {
2730

2831
private static final String NUMERIC_FIELD_NAME = "value";
2932

3033
public void testAggregationSampling() throws IOException {
31-
testCase(
32-
new RandomSamplerAggregationBuilder("my_agg").subAggregation(AggregationBuilders.avg("avg").field(NUMERIC_FIELD_NAME))
33-
.setProbability(0.25),
34-
new MatchAllDocsQuery(),
35-
RandomSamplerAggregatorTests::writeTestDocs,
36-
(InternalRandomSampler result) -> {
37-
assertThat(result.getDocCount(), allOf(greaterThan(20L), lessThan(60L)));
38-
Avg agg = result.getAggregations().get("avg");
39-
assertThat(agg.getValue(), closeTo(2, 1));
40-
},
41-
longField(NUMERIC_FIELD_NAME)
42-
);
34+
double[] avgs = new double[5];
35+
long[] counts = new long[5];
36+
AtomicInteger integer = new AtomicInteger();
37+
do {
38+
testCase(
39+
new RandomSamplerAggregationBuilder("my_agg").subAggregation(AggregationBuilders.avg("avg").field(NUMERIC_FIELD_NAME))
40+
.setProbability(0.25),
41+
new MatchAllDocsQuery(),
42+
RandomSamplerAggregatorTests::writeTestDocs,
43+
(InternalRandomSampler result) -> {
44+
counts[integer.get()] = result.getDocCount();
45+
Avg agg = result.getAggregations().get("avg");
46+
assertTrue(Double.isNaN(agg.getValue()) == false && Double.isFinite(agg.getValue()));
47+
avgs[integer.get()] = agg.getValue();
48+
},
49+
longField(NUMERIC_FIELD_NAME)
50+
);
51+
} while (integer.incrementAndGet() < 5);
52+
long avgCount = LongStream.of(counts).sum() / integer.get();
53+
double avgAvg = DoubleStream.of(avgs).sum() / integer.get();
54+
assertThat(avgCount, allOf(greaterThanOrEqualTo(20L), lessThanOrEqualTo(70L)));
55+
assertThat(avgAvg, closeTo(1.5, 0.5));
4356
}
4457

4558
private static void writeTestDocs(RandomIndexWriter w) throws IOException {
46-
for (int i = 0; i < 50; i++) {
59+
for (int i = 0; i < 75; i++) {
4760
w.addDocument(List.of(new SortedNumericDocValuesField(NUMERIC_FIELD_NAME, 1)));
4861
}
49-
for (int i = 0; i < 50; i++) {
62+
for (int i = 0; i < 75; i++) {
5063
w.addDocument(List.of(new SortedNumericDocValuesField(NUMERIC_FIELD_NAME, 2)));
5164
}
52-
for (int i = 0; i < 25; i++) {
53-
w.addDocument(List.of(new SortedNumericDocValuesField(NUMERIC_FIELD_NAME, 4)));
54-
}
5565
}
5666

5767
}

x-pack/plugin/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ testClusters.configureEach {
153153

154154
if (BuildParams.isSnapshotBuild() == false) {
155155
systemProperty 'es.index_mode_feature_flag_registered', 'true'
156+
systemProperty 'es.random_sampler_feature_flag_registered', 'true'
156157
systemProperty 'es.user_profile_feature_flag_enabled', 'true'
157158
}
158159
}

0 commit comments

Comments
 (0)