Skip to content

Commit 58a4994

Browse files
committed
Make random sampling method more dynamic
1 parent 67099d3 commit 58a4994

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

modules/data-streams/src/test/java/org/elasticsearch/datastreams/lifecycle/DataStreamLifecycleFixtures.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.elasticsearch.test.ESIntegTestCase.client;
4141
import static org.elasticsearch.test.ESTestCase.between;
4242
import static org.elasticsearch.test.ESTestCase.frequently;
43+
import static org.elasticsearch.test.ESTestCase.randomFrom;
4344
import static org.elasticsearch.test.ESTestCase.randomIntBetween;
4445
import static org.junit.Assert.assertTrue;
4546

@@ -184,11 +185,10 @@ private static DataStreamLifecycle.DownsamplingRound nextRound(DataStreamLifecyc
184185
}
185186

186187
public static DownsampleConfig.SamplingMethod randomSamplingMethod() {
187-
return switch (between(0, 2)) {
188-
case 0 -> null;
189-
case 1 -> DownsampleConfig.SamplingMethod.AGGREGATE;
190-
case 2 -> DownsampleConfig.SamplingMethod.LAST_VALUE;
191-
default -> throw new IllegalStateException("Unknown randomisation path");
192-
};
188+
if (between(0, DownsampleConfig.SamplingMethod.values().length) == 0) {
189+
return null;
190+
} else {
191+
return randomFrom(DownsampleConfig.SamplingMethod.values());
192+
}
193193
}
194194
}

server/src/test/java/org/elasticsearch/action/downsample/DownsampleConfigTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ public static DownsampleConfig randomConfig() {
5050
}
5151

5252
public static DownsampleConfig.SamplingMethod randomSamplingMethod() {
53-
return switch (between(0, 2)) {
54-
case 0 -> null;
55-
case 1 -> DownsampleConfig.SamplingMethod.AGGREGATE;
56-
case 2 -> DownsampleConfig.SamplingMethod.LAST_VALUE;
57-
default -> throw new AssertionError("Illegal randomisation branch");
58-
};
53+
if (between(0, DownsampleConfig.SamplingMethod.values().length) == 0) {
54+
return null;
55+
} else {
56+
return randomFrom(DownsampleConfig.SamplingMethod.values());
57+
}
5958
}
6059

6160
@Override

x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DownsamplingIntegTestCase.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,10 @@ private static boolean hasTimeSeriesDimensionTrue(Map<String, ?> fieldMapping) {
336336
}
337337

338338
public static DownsampleConfig.SamplingMethod randomSamplingMethod() {
339-
return switch (between(0, 2)) {
340-
case 0 -> null;
341-
case 1 -> DownsampleConfig.SamplingMethod.AGGREGATE;
342-
case 2 -> DownsampleConfig.SamplingMethod.LAST_VALUE;
343-
default -> throw new IllegalStateException("Unexpected randomisation branch");
344-
};
339+
if (between(0, DownsampleConfig.SamplingMethod.values().length) == 0) {
340+
return null;
341+
} else {
342+
return randomFrom(DownsampleConfig.SamplingMethod.values());
343+
}
345344
}
346345
}

x-pack/plugin/downsample/src/test/java/org/elasticsearch/xpack/downsample/DownsampleActionSingleNodeTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,11 +1824,10 @@ public void testDuplicateDownsampleRequest() throws Exception {
18241824
}
18251825

18261826
static DownsampleConfig.SamplingMethod randomSamplingMethod() {
1827-
return switch (between(0, 2)) {
1828-
case 0 -> null;
1829-
case 1 -> DownsampleConfig.SamplingMethod.AGGREGATE;
1830-
case 2 -> DownsampleConfig.SamplingMethod.LAST_VALUE;
1831-
default -> throw new IllegalStateException("Unexpected randomisation branch");
1832-
};
1827+
if (between(0, DownsampleConfig.SamplingMethod.values().length) == 0) {
1828+
return null;
1829+
} else {
1830+
return randomFrom(DownsampleConfig.SamplingMethod.values());
1831+
}
18331832
}
18341833
}

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DataStreamLifecycleDownsamplingSecurityIT.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.action.bulk.BulkResponse;
2020
import org.elasticsearch.action.datastreams.GetDataStreamAction;
2121
import org.elasticsearch.action.datastreams.lifecycle.ErrorEntry;
22+
import org.elasticsearch.action.downsample.DownsampleConfig;
2223
import org.elasticsearch.action.index.IndexRequest;
2324
import org.elasticsearch.action.support.WriteRequest;
2425
import org.elasticsearch.client.internal.Client;
@@ -120,6 +121,7 @@ public void testDownsamplingAuthorized() throws Exception {
120121
String dataStreamName = "metrics-foo";
121122

122123
DataStreamLifecycle.Template lifecycle = DataStreamLifecycle.dataLifecycleBuilder()
124+
.downsamplingMethod(randomSamplingMethod())
123125
.downsamplingRounds(
124126
List.of(
125127
new DataStreamLifecycle.DownsamplingRound(TimeValue.timeValueMillis(0), new DateHistogramInterval("5m")),
@@ -401,9 +403,18 @@ private void bulkIndex(Client client, String dataStreamName, Supplier<XContentBu
401403
logger.info("-> Indexed [{}] documents. Dropped [{}] duplicates.", docsIndexed, duplicates);
402404
}
403405

406+
private static DownsampleConfig.SamplingMethod randomSamplingMethod() {
407+
if (between(0, DownsampleConfig.SamplingMethod.values().length) == 0) {
408+
return null;
409+
} else {
410+
return randomFrom(DownsampleConfig.SamplingMethod.values());
411+
}
412+
}
413+
404414
public static class SystemDataStreamWithDownsamplingConfigurationPlugin extends Plugin implements SystemIndexPlugin {
405415

406416
public static final DataStreamLifecycle.Template LIFECYCLE = DataStreamLifecycle.dataLifecycleBuilder()
417+
.downsamplingMethod(randomSamplingMethod())
407418
.downsamplingRounds(
408419
List.of(
409420
new DataStreamLifecycle.DownsamplingRound(TimeValue.timeValueMillis(0), new DateHistogramInterval("5m")),

0 commit comments

Comments
 (0)