From cae385eee76a7410293bd0ff6f8bfd56a67591f6 Mon Sep 17 00:00:00 2001 From: "ievgen.degtiarenko" Date: Thu, 2 Apr 2026 11:36:47 +0200 Subject: [PATCH 1/2] Speedup CsvTests --- .../elasticsearch/xpack/esql/CsvTests.java | 35 ++++++++----------- .../TestPhysicalOperationProviders.java | 9 +++-- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java index 1f6cc3340242a..3aa5b3987e58f 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java @@ -42,8 +42,6 @@ import org.elasticsearch.core.Releasables; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; -import org.elasticsearch.env.Environment; -import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.IndexMode; import org.elasticsearch.logging.LogManager; import org.elasticsearch.logging.Logger; @@ -148,6 +146,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.net.URI; import java.net.URL; import java.nio.file.Files; @@ -967,13 +966,6 @@ void executeSubPlan( ExchangeSourceHandler exchangeSource = new ExchangeSourceHandler(between(1, 64), executor); ExchangeSinkHandler exchangeSink = new ExchangeSinkHandler(blockFactory, between(1, 64), threadPool::relativeTimeInMillis); - UserAgentParserRegistry userAgentRegistry; - try { - userAgentRegistry = createUserAgentRegistry(); - } catch (IOException e) { - throw new IllegalStateException("Failed to create UserAgentParserRegistry", e); - } - LocalExecutionPlanner executionPlanner = new LocalExecutionPlanner( getTestName(), "", @@ -987,7 +979,7 @@ void executeSubPlan( mock(EnrichLookupService.class), mock(LookupFromIndexService.class), mock(InferenceService.class), - userAgentRegistry, + createUserAgentRegistry(), physicalOperationProviders, operatorFactoryRegistry ); @@ -1105,17 +1097,18 @@ private static List coordinatorSplits(OperatorFactoryRegistry ope * Creates a {@link UserAgentParserRegistry} with a config directory * containing the custom-regexes.yml test resource, so csv-spec tests can exercise the {@code regex_file} option. */ - private static UserAgentParserRegistry createUserAgentRegistry() throws IOException { - Path homeDir = createTempDir(); - Path userAgentConfigDir = homeDir.resolve("config").resolve("user-agent"); - Files.createDirectories(userAgentConfigDir); - try (InputStream is = CsvTests.class.getResourceAsStream("/custom-regexes.yml")) { - assert is != null : "custom-regexes.yml not found on classpath"; - Files.copy(is, userAgentConfigDir.resolve("custom-regexes.yml")); + private static UserAgentParserRegistry createUserAgentRegistry() { + Path userAgentConfigDir = TestPhysicalOperationProviders.TEST_ENV.configDir().resolve("user-agent"); + Path customRegexes = userAgentConfigDir.resolve("custom-regexes.yml"); + if (Files.exists(customRegexes) == false) { + try (InputStream is = CsvTests.class.getResourceAsStream("/custom-regexes.yml")) { + assert is != null : "custom-regexes.yml not found on classpath"; + Files.createDirectories(userAgentConfigDir); + Files.copy(is, customRegexes); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } - return UserAgentPlugin.createRegistry( - TestEnvironment.newEnvironment(Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), homeDir).build()), - Settings.EMPTY - ); + return UserAgentPlugin.createRegistry(TestPhysicalOperationProviders.TEST_ENV, Settings.EMPTY); } } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java index 4cce7aa85e9b7..1e743f7fa479f 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java @@ -81,6 +81,11 @@ import static org.elasticsearch.index.mapper.MappedFieldType.FieldExtractPreference.EXTRACT_SPATIAL_CENTROID; public class TestPhysicalOperationProviders extends AbstractPhysicalOperationProviders { + + public static final Environment TEST_ENV = TestEnvironment.newEnvironment( + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); + private final List indexPages; private final UnmappedResolution unmappedResolution; @@ -112,9 +117,7 @@ Optional columnIndex(String columnName) { private static AnalysisRegistry createAnalysisRegistry() throws IOException { return new AnalysisModule( - TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() - ), + TEST_ENV, List.of(new MachineLearning(Settings.EMPTY), new CommonAnalysisPlugin()), new StablePluginsRegistry() ).getAnalysisRegistry(); From 02c3c5db3b0864d0fe093a96b93e2cbc7b3104aa Mon Sep 17 00:00:00 2001 From: "ievgen.degtiarenko" Date: Thu, 2 Apr 2026 13:04:22 +0200 Subject: [PATCH 2/2] lazy init env --- .../test/java/org/elasticsearch/xpack/esql/CsvTests.java | 6 ++++-- .../esql/planner/TestPhysicalOperationProviders.java | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java index 3aa5b3987e58f..99da60482f70a 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/CsvTests.java @@ -42,6 +42,7 @@ import org.elasticsearch.core.Releasables; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; +import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexMode; import org.elasticsearch.logging.LogManager; import org.elasticsearch.logging.Logger; @@ -1098,7 +1099,8 @@ private static List coordinatorSplits(OperatorFactoryRegistry ope * containing the custom-regexes.yml test resource, so csv-spec tests can exercise the {@code regex_file} option. */ private static UserAgentParserRegistry createUserAgentRegistry() { - Path userAgentConfigDir = TestPhysicalOperationProviders.TEST_ENV.configDir().resolve("user-agent"); + Environment env = TestPhysicalOperationProviders.TEST_ENV.getOrCompute(); + Path userAgentConfigDir = env.configDir().resolve("user-agent"); Path customRegexes = userAgentConfigDir.resolve("custom-regexes.yml"); if (Files.exists(customRegexes) == false) { try (InputStream is = CsvTests.class.getResourceAsStream("/custom-regexes.yml")) { @@ -1109,6 +1111,6 @@ private static UserAgentParserRegistry createUserAgentRegistry() { throw new UncheckedIOException(e); } } - return UserAgentPlugin.createRegistry(TestPhysicalOperationProviders.TEST_ENV, Settings.EMPTY); + return UserAgentPlugin.createRegistry(env, Settings.EMPTY); } } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java index 1e743f7fa479f..154f7cbefc365 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/planner/TestPhysicalOperationProviders.java @@ -10,6 +10,7 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.analysis.common.CommonAnalysisPlugin; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.LazyInitializable; import org.elasticsearch.compute.aggregation.AggregatorMode; import org.elasticsearch.compute.aggregation.GroupingAggregator; import org.elasticsearch.compute.aggregation.blockhash.BlockHash; @@ -82,8 +83,10 @@ public class TestPhysicalOperationProviders extends AbstractPhysicalOperationProviders { - public static final Environment TEST_ENV = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + public static final LazyInitializable TEST_ENV = new LazyInitializable<>( + () -> TestEnvironment.newEnvironment( + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ) ); private final List indexPages; @@ -117,7 +120,7 @@ Optional columnIndex(String columnName) { private static AnalysisRegistry createAnalysisRegistry() throws IOException { return new AnalysisModule( - TEST_ENV, + TEST_ENV.getOrCompute(), List.of(new MachineLearning(Settings.EMPTY), new CommonAnalysisPlugin()), new StablePluginsRegistry() ).getAnalysisRegistry();