diff --git a/build.gradle b/build.gradle index 5db5aba7e3..fe5856f2e5 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,6 @@ buildscript { ext { opensearch_version = System.getProperty("opensearch.version", "2.7.0-SNAPSHOT") - spring_version = "5.3.22" isSnapshot = "true" == System.getProperty("build.snapshot", "true") buildVersionQualifier = System.getProperty("build.version_qualifier", "") version_tokens = opensearch_version.tokenize('-') diff --git a/core/build.gradle b/core/build.gradle index 0b8ffc422c..791d64df65 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -35,8 +35,6 @@ repositories { dependencies { api group: 'com.google.guava', name: 'guava', version: '31.0.1-jre' - api group: 'org.springframework', name: 'spring-context', version: "${spring_version}" - api group: 'org.springframework', name: 'spring-beans', version: "${spring_version}" api group: 'org.apache.commons', name: 'commons-lang3', version: '3.10' api group: 'com.facebook.presto', name: 'presto-matching', version: '0.240' api group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1' @@ -47,7 +45,6 @@ dependencies { testImplementation('org.junit.jupiter:junit-jupiter:5.6.2') testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: '2.1' - testImplementation group: 'org.springframework', name: 'spring-test', version: "${spring_version}" testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.12.4' testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.12.4' } diff --git a/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java index 044949ea35..1db29a6a42 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java @@ -65,7 +65,6 @@ import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.ast.dsl.AstDSL; import org.opensearch.sql.ast.expression.Argument; import org.opensearch.sql.ast.expression.DataType; @@ -89,15 +88,7 @@ import org.opensearch.sql.planner.logical.LogicalProject; import org.opensearch.sql.planner.logical.LogicalRelation; import org.opensearch.sql.planner.physical.datasource.DataSourceTable; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTest.class}) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) + class AnalyzerTest extends AnalyzerTestBase { @Test diff --git a/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTestBase.java b/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTestBase.java index c68ba2653e..51c1f06433 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTestBase.java +++ b/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTestBase.java @@ -13,6 +13,7 @@ import com.google.common.collect.ImmutableSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.apache.commons.lang3.tuple.Pair; import org.opensearch.sql.analysis.symbol.Namespace; @@ -39,8 +40,6 @@ import org.opensearch.sql.planner.physical.PhysicalPlan; import org.opensearch.sql.storage.StorageEngine; import org.opensearch.sql.storage.Table; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; public class AnalyzerTestBase { @@ -49,14 +48,12 @@ protected Map typeMapping() { return TestConfig.typeMapping; } - @Bean protected StorageEngine storageEngine() { return (dataSourceSchemaName, tableName) -> table; } - @Bean protected Table table() { - return new Table() { + return Optional.ofNullable(table).orElseGet(() -> new Table() { @Override public boolean exists() { return true; @@ -76,31 +73,13 @@ public Map getFieldTypes() { public PhysicalPlan implement(LogicalPlan plan) { throw new UnsupportedOperationException(); } - }; - } - - @Bean - protected Table dataSourceTable() { - return new Table() { - @Override - public Map getFieldTypes() { - return typeMapping(); - } - - @Override - public PhysicalPlan implement(LogicalPlan plan) { - throw new UnsupportedOperationException(); - } - }; + }); } - @Bean protected DataSourceService dataSourceService() { - return new DefaultDataSourceService(); + return Optional.ofNullable(dataSourceService).orElseGet(DefaultDataSourceService::new); } - - @Bean protected SymbolTable symbolTable() { SymbolTable symbolTable = new SymbolTable(); typeMapping().entrySet() @@ -110,7 +89,6 @@ protected SymbolTable symbolTable() { return symbolTable; } - @Bean protected Environment typeEnv() { return var -> { if (var instanceof ReferenceExpression) { @@ -123,25 +101,16 @@ protected Environment typeEnv() { }; } - @Autowired - protected AnalysisContext analysisContext; - - @Autowired - protected ExpressionAnalyzer expressionAnalyzer; + protected AnalysisContext analysisContext = analysisContext(typeEnvironment(symbolTable())); - @Autowired - protected Analyzer analyzer; + protected ExpressionAnalyzer expressionAnalyzer = expressionAnalyzer(); - @Autowired - protected Table table; + protected Table table = table(); - @Autowired - protected DataSourceService dataSourceService; + protected DataSourceService dataSourceService = dataSourceService(); - @Autowired - protected Environment typeEnv; + protected Analyzer analyzer = analyzer(expressionAnalyzer(), dataSourceService, table); - @Bean protected Analyzer analyzer(ExpressionAnalyzer expressionAnalyzer, DataSourceService dataSourceService, Table table) { @@ -167,17 +136,14 @@ public FunctionName getFunctionName() { return new Analyzer(expressionAnalyzer, dataSourceService, functionRepository); } - @Bean protected TypeEnvironment typeEnvironment(SymbolTable symbolTable) { return new TypeEnvironment(null, symbolTable); } - @Bean protected AnalysisContext analysisContext(TypeEnvironment typeEnvironment) { return new AnalysisContext(typeEnvironment); } - @Bean protected ExpressionAnalyzer expressionAnalyzer() { return new ExpressionAnalyzer(BuiltinFunctionRepository.getInstance()); } diff --git a/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java index 5a8ae6b8cd..c7a11658e3 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java @@ -31,7 +31,6 @@ import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.analysis.symbol.Namespace; import org.opensearch.sql.analysis.symbol.Symbol; import org.opensearch.sql.ast.dsl.AstDSL; @@ -48,15 +47,8 @@ import org.opensearch.sql.expression.DSL; import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.FunctionExpression; -import org.opensearch.sql.expression.function.FunctionPropertiesTestConfig; import org.opensearch.sql.expression.window.aggregation.AggregateWindowFunction; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {FunctionPropertiesTestConfig.class, AnalyzerTestBase.class}) class ExpressionAnalyzerTest extends AnalyzerTestBase { @Test diff --git a/core/src/test/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizerTest.java b/core/src/test/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizerTest.java index 287cff0e3a..89d5f699e3 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/ExpressionReferenceOptimizerTest.java @@ -14,20 +14,13 @@ import com.google.common.collect.ImmutableList; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.expression.DSL; import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.function.BuiltinFunctionRepository; import org.opensearch.sql.expression.window.WindowDefinition; import org.opensearch.sql.planner.logical.LogicalPlan; import org.opensearch.sql.planner.logical.LogicalPlanDSL; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTest.class}) class ExpressionReferenceOptimizerTest extends AnalyzerTestBase { @Test diff --git a/core/src/test/java/org/opensearch/sql/analysis/NamedExpressionAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/NamedExpressionAnalyzerTest.java index 913593add3..e9c891905c 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/NamedExpressionAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/NamedExpressionAnalyzerTest.java @@ -11,19 +11,12 @@ import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.ast.dsl.AstDSL; import org.opensearch.sql.ast.expression.Alias; import org.opensearch.sql.ast.expression.HighlightFunction; import org.opensearch.sql.ast.expression.Literal; import org.opensearch.sql.expression.NamedExpression; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTestBase.class}) class NamedExpressionAnalyzerTest extends AnalyzerTestBase { @Test void visit_named_select_item() { diff --git a/core/src/test/java/org/opensearch/sql/analysis/QualifierAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/QualifierAnalyzerTest.java index 8c87b24d3e..5833ef6ae4 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/QualifierAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/QualifierAnalyzerTest.java @@ -13,18 +13,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.analysis.symbol.Namespace; import org.opensearch.sql.analysis.symbol.Symbol; import org.opensearch.sql.common.antlr.SyntaxCheckException; import org.opensearch.sql.data.type.ExprType; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTestBase.class}) class QualifierAnalyzerTest extends AnalyzerTestBase { private QualifierAnalyzer qualifierAnalyzer; diff --git a/core/src/test/java/org/opensearch/sql/analysis/SelectAnalyzeTest.java b/core/src/test/java/org/opensearch/sql/analysis/SelectAnalyzeTest.java index 82f3a87e30..3bd90f0081 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/SelectAnalyzeTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/SelectAnalyzeTest.java @@ -17,20 +17,13 @@ import com.google.common.collect.ImmutableMap; import java.util.Map; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.ast.dsl.AstDSL; import org.opensearch.sql.ast.expression.AllFields; import org.opensearch.sql.data.type.ExprCoreType; import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.DSL; import org.opensearch.sql.planner.logical.LogicalPlanDSL; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {SelectAnalyzeTest.class}) public class SelectAnalyzeTest extends AnalyzerTestBase { @Override diff --git a/core/src/test/java/org/opensearch/sql/analysis/SelectExpressionAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/SelectExpressionAnalyzerTest.java index a9fb03d2d5..b2fe29b509 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/SelectExpressionAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/SelectExpressionAnalyzerTest.java @@ -24,14 +24,8 @@ import org.opensearch.sql.ast.expression.UnresolvedExpression; import org.opensearch.sql.expression.DSL; import org.opensearch.sql.expression.NamedExpression; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) @ExtendWith(MockitoExtension.class) -@ContextConfiguration(classes = {SelectExpressionAnalyzerTest.class}) public class SelectExpressionAnalyzerTest extends AnalyzerTestBase { @Mock diff --git a/core/src/test/java/org/opensearch/sql/analysis/WindowExpressionAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/WindowExpressionAnalyzerTest.java index cf0a90a436..dd4361ad6a 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/WindowExpressionAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/WindowExpressionAnalyzerTest.java @@ -21,10 +21,7 @@ import java.util.Collections; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayNameGeneration; -import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.ast.dsl.AstDSL; import org.opensearch.sql.ast.expression.Alias; import org.opensearch.sql.ast.tree.Sort.SortOption; @@ -34,14 +31,7 @@ import org.opensearch.sql.planner.logical.LogicalPlanDSL; import org.opensearch.sql.planner.logical.LogicalRelation; import org.opensearch.sql.planner.logical.LogicalSort; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {SelectExpressionAnalyzerTest.class}) -@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class WindowExpressionAnalyzerTest extends AnalyzerTestBase { private LogicalPlan child; diff --git a/core/src/test/java/org/opensearch/sql/config/TestConfig.java b/core/src/test/java/org/opensearch/sql/config/TestConfig.java index 4159ae12ff..74dde6c2e9 100644 --- a/core/src/test/java/org/opensearch/sql/config/TestConfig.java +++ b/core/src/test/java/org/opensearch/sql/config/TestConfig.java @@ -22,13 +22,10 @@ import org.opensearch.sql.planner.physical.PhysicalPlan; import org.opensearch.sql.storage.StorageEngine; import org.opensearch.sql.storage.Table; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; /** * Configuration will be used for UT. */ -@Configuration public class TestConfig { public static final String INT_TYPE_NULL_VALUE_FIELD = "int_null_value"; public static final String INT_TYPE_MISSING_VALUE_FIELD = "int_missing_value"; @@ -61,7 +58,6 @@ public class TestConfig { .put("field_value2", ExprCoreType.STRING) .build(); - @Bean protected StorageEngine storageEngine() { return new StorageEngine() { @Override @@ -91,8 +87,6 @@ public PhysicalPlan implement(LogicalPlan plan) { }; } - - @Bean protected SymbolTable symbolTable() { SymbolTable symbolTable = new SymbolTable(); typeMapping.entrySet() @@ -102,7 +96,6 @@ protected SymbolTable symbolTable() { return symbolTable; } - @Bean protected Environment typeEnv() { return var -> { if (var instanceof ReferenceExpression) { diff --git a/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java b/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java index 431efd9fb4..4df38027f4 100644 --- a/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java +++ b/core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java @@ -32,11 +32,9 @@ import org.opensearch.sql.planner.logical.LogicalPlan; import org.opensearch.sql.planner.physical.PhysicalPlan; import org.opensearch.sql.storage.split.Split; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; @ExtendWith(MockitoExtension.class) class QueryServiceTest { - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private QueryService queryService; diff --git a/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java b/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java index c73bd8ac18..3d735d6762 100644 --- a/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java +++ b/core/src/test/java/org/opensearch/sql/expression/ExpressionTestBase.java @@ -29,33 +29,18 @@ import com.google.common.collect.ImmutableMap; import java.util.List; import java.util.function.Function; -import org.junit.jupiter.api.extension.ExtendWith; -import org.opensearch.sql.config.TestConfig; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.expression.env.Environment; import org.opensearch.sql.expression.function.BuiltinFunctionName; import org.opensearch.sql.expression.function.FunctionProperties; -import org.opensearch.sql.expression.function.FunctionPropertiesTestConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {FunctionPropertiesTestConfig.class, ExpressionTestBase.class, - TestConfig.class}) public class ExpressionTestBase { - @Autowired - protected FunctionProperties functionProperties; + protected FunctionProperties functionProperties = new FunctionProperties(); - @Autowired protected Environment typeEnv; - @Bean protected static Environment valueEnv() { return var -> { if (var instanceof ReferenceExpression) { @@ -95,7 +80,6 @@ protected static Environment valueEnv() { }; } - @Bean protected Environment typeEnv() { return typeEnv; } diff --git a/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTestConfig.java b/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTestConfig.java deleted file mode 100644 index dfc9b543ae..0000000000 --- a/core/src/test/java/org/opensearch/sql/expression/function/FunctionPropertiesTestConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.sql.expression.function; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class FunctionPropertiesTestConfig { - @Bean - FunctionProperties functionProperties() { - return new FunctionProperties(); - } -} diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java index 84cef2cbf3..fe751506ce 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalDedupeTest.java @@ -18,16 +18,9 @@ import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.analysis.AnalyzerTestBase; import org.opensearch.sql.expression.DSL; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTestBase.class}) class LogicalDedupeTest extends AnalyzerTestBase { @Test public void analyze_dedup_with_two_field_with_default_option() { diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java index 63429453f9..55bc793045 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalEvalTest.java @@ -16,13 +16,7 @@ import org.opensearch.sql.analysis.AnalyzerTestBase; import org.opensearch.sql.ast.dsl.AstDSL; import org.opensearch.sql.expression.DSL; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTestBase.class}) @ExtendWith(MockitoExtension.class) public class LogicalEvalTest extends AnalyzerTestBase { diff --git a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java index deeda2b82c..1a33795009 100644 --- a/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java +++ b/core/src/test/java/org/opensearch/sql/planner/logical/LogicalSortTest.java @@ -19,17 +19,10 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.opensearch.sql.analysis.AnalyzerTestBase; import org.opensearch.sql.ast.tree.Sort.SortOption; import org.opensearch.sql.expression.DSL; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -@Configuration -@ExtendWith(SpringExtension.class) -@ContextConfiguration(classes = {AnalyzerTestBase.class}) class LogicalSortTest extends AnalyzerTestBase { @Test public void analyze_sort_with_two_field_with_default_option() { diff --git a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java index 97bf8ebb79..a5bb61f2f7 100644 --- a/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java +++ b/core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTestBase.java @@ -22,7 +22,6 @@ import org.opensearch.sql.expression.Expression; import org.opensearch.sql.expression.ReferenceExpression; import org.opensearch.sql.expression.env.Environment; -import org.springframework.context.annotation.Bean; public class PhysicalPlanTestBase { @@ -183,7 +182,6 @@ public class PhysicalPlanTestBase { "errors", 8))) .build(); - @Bean protected Environment typeEnv() { return var -> { if (var instanceof ReferenceExpression) { diff --git a/integ-test/build.gradle b/integ-test/build.gradle index b792ecb18f..511005397b 100644 --- a/integ-test/build.gradle +++ b/integ-test/build.gradle @@ -60,7 +60,7 @@ loggerUsageCheck.enabled = false configurations.all { resolutionStrategy.force 'junit:junit:4.13.2' - exclude group: "commons-logging", module: "commons-logging" + resolutionStrategy.force "commons-logging:commons-logging:1.2" // enforce 1.1.3, https://www.whitesourcesoftware.com/vulnerability-database/WS-2019-0379 resolutionStrategy.force 'commons-codec:commons-codec:1.13' resolutionStrategy.force 'com.google.guava:guava:31.0.1-jre' diff --git a/integ-test/src/test/java/org/opensearch/sql/ppl/StandaloneIT.java b/integ-test/src/test/java/org/opensearch/sql/ppl/StandaloneIT.java index 33050e7200..71988a8e31 100644 --- a/integ-test/src/test/java/org/opensearch/sql/ppl/StandaloneIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/ppl/StandaloneIT.java @@ -16,10 +16,16 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import lombok.RequiredArgsConstructor; import org.junit.jupiter.api.Test; import org.opensearch.client.Request; import org.opensearch.client.RestClient; import org.opensearch.client.RestHighLevelClient; +import org.opensearch.common.inject.AbstractModule; +import org.opensearch.common.inject.Injector; +import org.opensearch.common.inject.ModulesBuilder; +import org.opensearch.common.inject.Provides; +import org.opensearch.common.inject.Singleton; import org.opensearch.sql.analysis.Analyzer; import org.opensearch.sql.analysis.ExpressionAnalyzer; import org.opensearch.sql.common.response.ResponseListener; @@ -32,27 +38,27 @@ import org.opensearch.sql.executor.QueryService; import org.opensearch.sql.executor.execution.QueryPlanFactory; import org.opensearch.sql.expression.function.BuiltinFunctionRepository; -import org.opensearch.sql.expression.function.FunctionProperties; import org.opensearch.sql.monitor.AlwaysHealthyMonitor; +import org.opensearch.sql.monitor.ResourceMonitor; import org.opensearch.sql.opensearch.client.OpenSearchClient; import org.opensearch.sql.opensearch.client.OpenSearchRestClient; import org.opensearch.sql.opensearch.executor.OpenSearchExecutionEngine; +import org.opensearch.sql.opensearch.executor.protector.ExecutionProtector; import org.opensearch.sql.opensearch.executor.protector.OpenSearchExecutionProtector; +import org.opensearch.sql.opensearch.security.SecurityAccess; import org.opensearch.sql.opensearch.storage.OpenSearchDataSourceFactory; +import org.opensearch.sql.opensearch.storage.OpenSearchStorageEngine; import org.opensearch.sql.planner.Planner; import org.opensearch.sql.planner.optimizer.LogicalPlanOptimizer; -import org.opensearch.sql.ppl.config.PPLServiceConfig; +import org.opensearch.sql.ppl.antlr.PPLSyntaxParser; import org.opensearch.sql.ppl.domain.PPLQueryRequest; import org.opensearch.sql.protocol.response.QueryResult; import org.opensearch.sql.protocol.response.format.SimpleJsonResponseFormatter; +import org.opensearch.sql.sql.SQLService; +import org.opensearch.sql.sql.antlr.SQLSyntaxParser; import org.opensearch.sql.storage.DataSourceFactory; +import org.opensearch.sql.storage.StorageEngine; import org.opensearch.sql.util.ExecuteOnCallerThreadQueryManager; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; /** * Run PPL with query engine outside OpenSearch cluster. This IT doesn't require our plugin @@ -67,27 +73,19 @@ public class StandaloneIT extends PPLIntegTestCase { @Override public void init() { - // Using client() defined in ODFERestTestCase. restClient = new InternalRestHighLevelClient(client()); - OpenSearchClient client = new OpenSearchRestClient(restClient); - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - context.registerBean(ExecutionEngine.class, () -> new OpenSearchExecutionEngine(client, - new OpenSearchExecutionProtector(new AlwaysHealthyMonitor()))); - context.registerBean(OpenSearchClient.class, () -> client); - context.registerBean(Settings.class, () -> defaultSettings()); - context.registerBean(FunctionProperties.class, FunctionProperties::new); DataSourceService dataSourceService = new DataSourceServiceImpl( new ImmutableSet.Builder() .add(new OpenSearchDataSourceFactory(client, defaultSettings())) .build()); dataSourceService.addDataSource(defaultOpenSearchDataSourceMetadata()); - context.registerBean(DataSourceService.class, () -> dataSourceService); - context.register(StandaloneConfig.class); - context.register(PPLServiceConfig.class); - context.refresh(); - pplService = context.getBean(PPLService.class); + ModulesBuilder modules = new ModulesBuilder(); + modules.add(new StandaloneModule(new InternalRestHighLevelClient(client()), defaultSettings(), dataSourceService)); + Injector injector = modules.createInjector(); + pplService = + SecurityAccess.doPrivileged(() -> injector.getInstance(PPLService.class)); } @Test @@ -170,27 +168,68 @@ public InternalRestHighLevelClient(RestClient restClient) { } } - @Configuration - static class StandaloneConfig { - @Autowired - private DataSourceService dataSourceService; + @RequiredArgsConstructor + public class StandaloneModule extends AbstractModule { + + private final RestHighLevelClient client; + + private final Settings settings; + + private final DataSourceService dataSourceService; + + private final BuiltinFunctionRepository functionRepository = + BuiltinFunctionRepository.getInstance(); - @Autowired - private ExecutionEngine executionEngine; + @Override + protected void configure() {} - @Bean - QueryManager queryManager() { + @Provides + public OpenSearchClient openSearchClient() { + return new OpenSearchRestClient(client); + } + + @Provides + public StorageEngine storageEngine(OpenSearchClient client) { + return new OpenSearchStorageEngine(client, settings); + } + + @Provides + public ExecutionEngine executionEngine(OpenSearchClient client, ExecutionProtector protector) { + return new OpenSearchExecutionEngine(client, protector); + } + + @Provides + public ResourceMonitor resourceMonitor() { + return new AlwaysHealthyMonitor(); + } + + @Provides + public ExecutionProtector protector(ResourceMonitor resourceMonitor) { + return new OpenSearchExecutionProtector(resourceMonitor); + } + + @Provides + @Singleton + public QueryManager queryManager() { return new ExecuteOnCallerThreadQueryManager(); } - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - QueryPlanFactory queryExecutionFactory() { - BuiltinFunctionRepository functionRepository = BuiltinFunctionRepository.getInstance(); - Analyzer analyzer = new Analyzer(new ExpressionAnalyzer(functionRepository), - dataSourceService, functionRepository); - Planner planner = - new Planner(LogicalPlanOptimizer.create()); + @Provides + public PPLService pplService(QueryManager queryManager, QueryPlanFactory queryPlanFactory) { + return new PPLService(new PPLSyntaxParser(), queryManager, queryPlanFactory); + } + + @Provides + public SQLService sqlService(QueryManager queryManager, QueryPlanFactory queryPlanFactory) { + return new SQLService(new SQLSyntaxParser(), queryManager, queryPlanFactory); + } + + @Provides + public QueryPlanFactory queryPlanFactory(ExecutionEngine executionEngine) { + Analyzer analyzer = + new Analyzer( + new ExpressionAnalyzer(functionRepository), dataSourceService, functionRepository); + Planner planner = new Planner(LogicalPlanOptimizer.create()); return new QueryPlanFactory(new QueryService(analyzer, executionEngine, planner)); } } diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java index a5a3ac5a4f..bc97f71b47 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java @@ -15,6 +15,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.node.NodeClient; +import org.opensearch.common.inject.Injector; import org.opensearch.rest.BaseRestHandler; import org.opensearch.rest.BytesRestResponse; import org.opensearch.rest.RestChannel; @@ -36,7 +37,6 @@ import org.opensearch.sql.protocol.response.format.ResponseFormatter; import org.opensearch.sql.sql.SQLService; import org.opensearch.sql.sql.domain.SQLQueryRequest; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * New SQL REST action handler. This will not be registered to OpenSearch unless: @@ -49,14 +49,14 @@ public class RestSQLQueryAction extends BaseRestHandler { public static final RestChannelConsumer NOT_SUPPORTED_YET = null; - private final AnnotationConfigApplicationContext applicationContext; + private final Injector injector; /** * Constructor of RestSQLQueryAction. */ - public RestSQLQueryAction(AnnotationConfigApplicationContext applicationContext) { + public RestSQLQueryAction(Injector injector) { super(); - this.applicationContext = applicationContext; + this.injector = injector; } @Override @@ -91,7 +91,7 @@ public RestChannelConsumer prepareRequest( } SQLService sqlService = - SecurityAccess.doPrivileged(() -> applicationContext.getBean(SQLService.class)); + SecurityAccess.doPrivileged(() -> injector.getInstance(SQLService.class)); if (request.isExplainRequest()) { return channel -> diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java index de09bcee1a..88ed42010b 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java @@ -26,6 +26,7 @@ import org.apache.logging.log4j.Logger; import org.opensearch.client.Client; import org.opensearch.client.node.NodeClient; +import org.opensearch.common.inject.Injector; import org.opensearch.common.settings.Settings; import org.opensearch.index.IndexNotFoundException; import org.opensearch.rest.BaseRestHandler; @@ -62,7 +63,6 @@ import org.opensearch.sql.legacy.utils.JsonPrettyFormatter; import org.opensearch.sql.legacy.utils.QueryDataAnonymizer; import org.opensearch.sql.sql.domain.SQLQueryRequest; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class RestSqlAction extends BaseRestHandler { @@ -87,16 +87,10 @@ public class RestSqlAction extends BaseRestHandler { */ private final RestSQLQueryAction newSqlQueryHandler; - /** - * Application context used to create SQLService for each request. - */ - private final AnnotationConfigApplicationContext applicationContext; - - public RestSqlAction(Settings settings, AnnotationConfigApplicationContext applicationContext) { + public RestSqlAction(Settings settings, Injector injector) { super(); this.allowExplicitIndex = MULTI_ALLOW_EXPLICIT_INDEX.get(settings); - this.newSqlQueryHandler = new RestSQLQueryAction(applicationContext); - this.applicationContext = applicationContext; + this.newSqlQueryHandler = new RestSQLQueryAction(injector); } @Override diff --git a/legacy/src/test/java/org/opensearch/sql/legacy/plugin/RestSQLQueryActionTest.java b/legacy/src/test/java/org/opensearch/sql/legacy/plugin/RestSQLQueryActionTest.java index 3eafbd32d9..1bc34edf50 100644 --- a/legacy/src/test/java/org/opensearch/sql/legacy/plugin/RestSQLQueryActionTest.java +++ b/legacy/src/test/java/org/opensearch/sql/legacy/plugin/RestSQLQueryActionTest.java @@ -23,20 +23,19 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.opensearch.client.node.NodeClient; +import org.opensearch.common.inject.Injector; +import org.opensearch.common.inject.ModulesBuilder; import org.opensearch.common.util.concurrent.ThreadContext; import org.opensearch.rest.BaseRestHandler; import org.opensearch.rest.RestChannel; import org.opensearch.rest.RestRequest; -import org.opensearch.sql.datasource.DataSourceService; import org.opensearch.sql.common.antlr.SyntaxCheckException; -import org.opensearch.sql.executor.ExecutionEngine; import org.opensearch.sql.executor.QueryManager; import org.opensearch.sql.executor.execution.QueryPlanFactory; -import org.opensearch.sql.sql.config.SQLServiceConfig; +import org.opensearch.sql.sql.SQLService; +import org.opensearch.sql.sql.antlr.SQLSyntaxParser; import org.opensearch.sql.sql.domain.SQLQueryRequest; -import org.opensearch.sql.storage.StorageEngine; import org.opensearch.threadpool.ThreadPool; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; @RunWith(MockitoJUnitRunner.class) public class RestSQLQueryActionTest extends BaseRestHandler { @@ -52,25 +51,19 @@ public class RestSQLQueryActionTest extends BaseRestHandler { @Mock private QueryPlanFactory factory; - @Mock - private ExecutionEngine.Schema schema; - @Mock private RestChannel restChannel; - private AnnotationConfigApplicationContext context; + private Injector injector; @Before public void setup() { nodeClient = new NodeClient(org.opensearch.common.settings.Settings.EMPTY, threadPool); - context = new AnnotationConfigApplicationContext(); - context.registerBean(StorageEngine.class, () -> Mockito.mock(StorageEngine.class)); - context.registerBean(ExecutionEngine.class, () -> Mockito.mock(ExecutionEngine.class)); - context.registerBean(DataSourceService.class, () -> Mockito.mock(DataSourceService.class)); - context.registerBean(QueryManager.class, () -> queryManager); - context.registerBean(QueryPlanFactory.class, () -> factory); - context.register(SQLServiceConfig.class); - context.refresh(); + ModulesBuilder modules = new ModulesBuilder(); + modules.add(b -> { + b.bind(SQLService.class).toInstance(new SQLService(new SQLSyntaxParser(), queryManager, factory)); + }); + injector = modules.createInjector(); Mockito.lenient().when(threadPool.getThreadContext()) .thenReturn(new ThreadContext(org.opensearch.common.settings.Settings.EMPTY)); } @@ -83,7 +76,7 @@ public void handleQueryThatCanSupport() throws Exception { QUERY_API_ENDPOINT, ""); - RestSQLQueryAction queryAction = new RestSQLQueryAction(context); + RestSQLQueryAction queryAction = new RestSQLQueryAction(injector); queryAction.prepareRequest(request, (channel, exception) -> { fail(); }, (channel, exception) -> { @@ -99,7 +92,7 @@ public void handleExplainThatCanSupport() throws Exception { EXPLAIN_API_ENDPOINT, ""); - RestSQLQueryAction queryAction = new RestSQLQueryAction(context); + RestSQLQueryAction queryAction = new RestSQLQueryAction(injector); queryAction.prepareRequest(request, (channel, exception) -> { fail(); }, (channel, exception) -> { @@ -117,7 +110,7 @@ public void queryThatNotSupportIsHandledByFallbackHandler() throws Exception { ""); AtomicBoolean fallback = new AtomicBoolean(false); - RestSQLQueryAction queryAction = new RestSQLQueryAction(context); + RestSQLQueryAction queryAction = new RestSQLQueryAction(injector); queryAction.prepareRequest(request, (channel, exception) -> { fallback.set(true); assertTrue(exception instanceof SyntaxCheckException); @@ -142,7 +135,7 @@ public void queryExecutionFailedIsHandledByExecutionErrorHandler() throws Except .submit(any()); AtomicBoolean executionErrorHandler = new AtomicBoolean(false); - RestSQLQueryAction queryAction = new RestSQLQueryAction(context); + RestSQLQueryAction queryAction = new RestSQLQueryAction(injector); queryAction.prepareRequest(request, (channel, exception) -> { assertTrue(exception instanceof SyntaxCheckException); }, (channel, exception) -> { diff --git a/plugin/build.gradle b/plugin/build.gradle index da0aa1ee3e..ed13bff8b8 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -83,7 +83,7 @@ thirdPartyAudit.enabled = false configurations.all { resolutionStrategy.force 'junit:junit:4.13.2' // conflict with spring-jcl - exclude group: "commons-logging", module: "commons-logging" + resolutionStrategy.force "commons-logging:commons-logging:1.2" // enforce 2.12.6, https://github.com/opensearch-project/sql/issues/424 resolutionStrategy.force "com.fasterxml.jackson.core:jackson-core:${versions.jackson}" // enforce 1.1.3, https://www.whitesourcesoftware.com/vulnerability-database/WS-2019-0379 @@ -110,7 +110,6 @@ compileTestJava { } dependencies { - api group: 'org.springframework', name: 'spring-beans', version: "${spring_version}" api "com.fasterxml.jackson.core:jackson-core:${versions.jackson}" api "com.fasterxml.jackson.core:jackson-databind:${versions.jackson_databind}" api "com.fasterxml.jackson.core:jackson-annotations:${versions.jackson}" diff --git a/plugin/src/main/java/org/opensearch/sql/plugin/SQLPlugin.java b/plugin/src/main/java/org/opensearch/sql/plugin/SQLPlugin.java index fab14966d8..98826c6f62 100644 --- a/plugin/src/main/java/org/opensearch/sql/plugin/SQLPlugin.java +++ b/plugin/src/main/java/org/opensearch/sql/plugin/SQLPlugin.java @@ -31,6 +31,8 @@ import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.service.ClusterService; +import org.opensearch.common.inject.Injector; +import org.opensearch.common.inject.ModulesBuilder; import org.opensearch.common.io.stream.NamedWriteableRegistry; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.IndexScopedSettings; @@ -67,7 +69,7 @@ import org.opensearch.sql.opensearch.storage.OpenSearchDataSourceFactory; import org.opensearch.sql.opensearch.storage.script.ExpressionScriptEngine; import org.opensearch.sql.opensearch.storage.serialization.DefaultExpressionSerializer; -import org.opensearch.sql.plugin.config.OpenSearchPluginConfig; +import org.opensearch.sql.plugin.config.OpenSearchPluginModule; import org.opensearch.sql.plugin.datasource.DataSourceSettings; import org.opensearch.sql.plugin.rest.RestPPLQueryAction; import org.opensearch.sql.plugin.rest.RestPPLStatsAction; @@ -75,15 +77,12 @@ import org.opensearch.sql.plugin.transport.PPLQueryAction; import org.opensearch.sql.plugin.transport.TransportPPLQueryAction; import org.opensearch.sql.plugin.transport.TransportPPLQueryResponse; -import org.opensearch.sql.ppl.config.PPLServiceConfig; import org.opensearch.sql.prometheus.storage.PrometheusStorageFactory; -import org.opensearch.sql.sql.config.SQLServiceConfig; import org.opensearch.sql.storage.DataSourceFactory; import org.opensearch.threadpool.ExecutorBuilder; import org.opensearch.threadpool.FixedExecutorBuilder; import org.opensearch.threadpool.ThreadPool; import org.opensearch.watcher.ResourceWatcherService; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SQLPlugin extends Plugin implements ActionPlugin, ScriptPlugin, ReloadablePlugin { @@ -98,10 +97,10 @@ public class SQLPlugin extends Plugin implements ActionPlugin, ScriptPlugin, Rel private NodeClient client; - private AnnotationConfigApplicationContext applicationContext; - private DataSourceService dataSourceService; + private Injector injector; + public String name() { return "sql"; } @@ -127,7 +126,7 @@ public List getRestHandlers( return Arrays.asList( new RestPPLQueryAction(pluginSettings, settings), - new RestSqlAction(settings, applicationContext), + new RestSqlAction(settings, injector), new RestSqlStatsAction(settings, restController), new RestPPLStatsAction(settings, restController), new RestQuerySettingsAction(settings, restController)); @@ -172,25 +171,16 @@ public Collection createComponents( LocalClusterState.state().setClusterService(clusterService); LocalClusterState.state().setPluginSettings((OpenSearchSettings) pluginSettings); - this.applicationContext = new AnnotationConfigApplicationContext(); - SecurityAccess.doPrivileged( - () -> { - applicationContext.registerBean(ClusterService.class, () -> clusterService); - applicationContext.registerBean(NodeClient.class, () -> (NodeClient) client); - applicationContext.registerBean( - org.opensearch.sql.common.setting.Settings.class, () -> pluginSettings); - applicationContext.registerBean( - DataSourceService.class, () -> dataSourceService); - applicationContext.register(OpenSearchPluginConfig.class); - applicationContext.register(PPLServiceConfig.class); - applicationContext.register(SQLServiceConfig.class); - applicationContext.refresh(); - return null; - }); + ModulesBuilder modules = new ModulesBuilder(); + modules.add(new OpenSearchPluginModule()); + modules.add(b -> { + b.bind(NodeClient.class).toInstance((NodeClient) client); + b.bind(org.opensearch.sql.common.setting.Settings.class).toInstance(pluginSettings); + b.bind(DataSourceService.class).toInstance(dataSourceService); + }); - // return objects used by Guice to inject dependencies for e.g., - // transport action handler constructors - return ImmutableList.of(applicationContext); + injector = modules.createInjector(); + return ImmutableList.of(dataSourceService); } @Override diff --git a/plugin/src/main/java/org/opensearch/sql/plugin/config/OpenSearchPluginConfig.java b/plugin/src/main/java/org/opensearch/sql/plugin/config/OpenSearchPluginConfig.java deleted file mode 100644 index 1a98510aad..0000000000 --- a/plugin/src/main/java/org/opensearch/sql/plugin/config/OpenSearchPluginConfig.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - - -package org.opensearch.sql.plugin.config; - -import org.opensearch.client.node.NodeClient; -import org.opensearch.sql.analysis.Analyzer; -import org.opensearch.sql.analysis.ExpressionAnalyzer; -import org.opensearch.sql.common.setting.Settings; -import org.opensearch.sql.datasource.DataSourceService; -import org.opensearch.sql.executor.ExecutionEngine; -import org.opensearch.sql.executor.QueryManager; -import org.opensearch.sql.executor.QueryService; -import org.opensearch.sql.executor.execution.QueryPlanFactory; -import org.opensearch.sql.expression.function.BuiltinFunctionRepository; -import org.opensearch.sql.monitor.ResourceMonitor; -import org.opensearch.sql.opensearch.client.OpenSearchClient; -import org.opensearch.sql.opensearch.client.OpenSearchNodeClient; -import org.opensearch.sql.opensearch.executor.OpenSearchExecutionEngine; -import org.opensearch.sql.opensearch.executor.OpenSearchQueryManager; -import org.opensearch.sql.opensearch.executor.protector.ExecutionProtector; -import org.opensearch.sql.opensearch.executor.protector.OpenSearchExecutionProtector; -import org.opensearch.sql.opensearch.monitor.OpenSearchMemoryHealthy; -import org.opensearch.sql.opensearch.monitor.OpenSearchResourceMonitor; -import org.opensearch.sql.opensearch.storage.OpenSearchStorageEngine; -import org.opensearch.sql.planner.Planner; -import org.opensearch.sql.planner.optimizer.LogicalPlanOptimizer; -import org.opensearch.sql.storage.StorageEngine; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; - -/** - * OpenSearch plugin config that injects cluster service and node client from plugin - * and initialize OpenSearch storage and execution engine. - */ -@Configuration -public class OpenSearchPluginConfig { - - @Autowired - private NodeClient nodeClient; - - @Autowired - private Settings settings; - - @Autowired - private DataSourceService dataSourceService; - - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public OpenSearchClient client() { - return new OpenSearchNodeClient(nodeClient); - } - - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public StorageEngine storageEngine() { - return new OpenSearchStorageEngine(client(), settings); - } - - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public ExecutionEngine executionEngine() { - return new OpenSearchExecutionEngine(client(), protector()); - } - - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public ResourceMonitor resourceMonitor() { - return new OpenSearchResourceMonitor(settings, new OpenSearchMemoryHealthy()); - } - - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public ExecutionProtector protector() { - return new OpenSearchExecutionProtector(resourceMonitor()); - } - - /** - * Per node singleton object. - */ - @Bean - public QueryManager queryManager() { - return new OpenSearchQueryManager(nodeClient); - } - - /** - * QueryPlanFactory. - */ - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public QueryPlanFactory queryExecutionFactory() { - BuiltinFunctionRepository functionRepository = BuiltinFunctionRepository.getInstance(); - Analyzer analyzer = new Analyzer(new ExpressionAnalyzer(functionRepository), - dataSourceService, functionRepository); - Planner planner = - new Planner(LogicalPlanOptimizer.create()); - return new QueryPlanFactory(new QueryService(analyzer, executionEngine(), planner)); - } -} diff --git a/plugin/src/main/java/org/opensearch/sql/plugin/config/OpenSearchPluginModule.java b/plugin/src/main/java/org/opensearch/sql/plugin/config/OpenSearchPluginModule.java new file mode 100644 index 0000000000..5ab4bbaecd --- /dev/null +++ b/plugin/src/main/java/org/opensearch/sql/plugin/config/OpenSearchPluginModule.java @@ -0,0 +1,103 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.sql.plugin.config; + +import lombok.RequiredArgsConstructor; +import org.opensearch.client.node.NodeClient; +import org.opensearch.common.inject.AbstractModule; +import org.opensearch.common.inject.Provides; +import org.opensearch.common.inject.Singleton; +import org.opensearch.sql.analysis.Analyzer; +import org.opensearch.sql.analysis.ExpressionAnalyzer; +import org.opensearch.sql.common.setting.Settings; +import org.opensearch.sql.datasource.DataSourceService; +import org.opensearch.sql.executor.ExecutionEngine; +import org.opensearch.sql.executor.QueryManager; +import org.opensearch.sql.executor.QueryService; +import org.opensearch.sql.executor.execution.QueryPlanFactory; +import org.opensearch.sql.expression.function.BuiltinFunctionRepository; +import org.opensearch.sql.monitor.ResourceMonitor; +import org.opensearch.sql.opensearch.client.OpenSearchClient; +import org.opensearch.sql.opensearch.client.OpenSearchNodeClient; +import org.opensearch.sql.opensearch.executor.OpenSearchExecutionEngine; +import org.opensearch.sql.opensearch.executor.OpenSearchQueryManager; +import org.opensearch.sql.opensearch.executor.protector.ExecutionProtector; +import org.opensearch.sql.opensearch.executor.protector.OpenSearchExecutionProtector; +import org.opensearch.sql.opensearch.monitor.OpenSearchMemoryHealthy; +import org.opensearch.sql.opensearch.monitor.OpenSearchResourceMonitor; +import org.opensearch.sql.opensearch.storage.OpenSearchStorageEngine; +import org.opensearch.sql.planner.Planner; +import org.opensearch.sql.planner.optimizer.LogicalPlanOptimizer; +import org.opensearch.sql.ppl.PPLService; +import org.opensearch.sql.ppl.antlr.PPLSyntaxParser; +import org.opensearch.sql.sql.SQLService; +import org.opensearch.sql.sql.antlr.SQLSyntaxParser; +import org.opensearch.sql.storage.StorageEngine; + +@RequiredArgsConstructor +public class OpenSearchPluginModule extends AbstractModule { + + private final BuiltinFunctionRepository functionRepository = + BuiltinFunctionRepository.getInstance(); + + @Override + protected void configure() { + } + + @Provides + public OpenSearchClient openSearchClient(NodeClient nodeClient) { + return new OpenSearchNodeClient(nodeClient); + } + + @Provides + public StorageEngine storageEngine(OpenSearchClient client, Settings settings) { + return new OpenSearchStorageEngine(client, settings); + } + + @Provides + public ExecutionEngine executionEngine(OpenSearchClient client, ExecutionProtector protector) { + return new OpenSearchExecutionEngine(client, protector); + } + + @Provides + public ResourceMonitor resourceMonitor(Settings settings) { + return new OpenSearchResourceMonitor(settings, new OpenSearchMemoryHealthy()); + } + + @Provides + public ExecutionProtector protector(ResourceMonitor resourceMonitor) { + return new OpenSearchExecutionProtector(resourceMonitor); + } + + @Provides + @Singleton + public QueryManager queryManager(NodeClient nodeClient) { + return new OpenSearchQueryManager(nodeClient); + } + + @Provides + public PPLService pplService(QueryManager queryManager, QueryPlanFactory queryPlanFactory) { + return new PPLService(new PPLSyntaxParser(), queryManager, queryPlanFactory); + } + + @Provides + public SQLService sqlService(QueryManager queryManager, QueryPlanFactory queryPlanFactory) { + return new SQLService(new SQLSyntaxParser(), queryManager, queryPlanFactory); + } + + /** + * {@link QueryPlanFactory}. + */ + @Provides + public QueryPlanFactory queryPlanFactory( + DataSourceService dataSourceService, ExecutionEngine executionEngine) { + Analyzer analyzer = + new Analyzer( + new ExpressionAnalyzer(functionRepository), dataSourceService, functionRepository); + Planner planner = new Planner(LogicalPlanOptimizer.create()); + return new QueryPlanFactory(new QueryService(analyzer, executionEngine, planner)); + } +} diff --git a/plugin/src/main/java/org/opensearch/sql/plugin/transport/TransportPPLQueryAction.java b/plugin/src/main/java/org/opensearch/sql/plugin/transport/TransportPPLQueryAction.java index af57c91e5c..6825b2ac92 100644 --- a/plugin/src/main/java/org/opensearch/sql/plugin/transport/TransportPPLQueryAction.java +++ b/plugin/src/main/java/org/opensearch/sql/plugin/transport/TransportPPLQueryAction.java @@ -16,14 +16,18 @@ import org.opensearch.client.node.NodeClient; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; +import org.opensearch.common.inject.Injector; +import org.opensearch.common.inject.ModulesBuilder; import org.opensearch.sql.common.response.ResponseListener; -import org.opensearch.sql.common.setting.Settings; import org.opensearch.sql.common.utils.QueryContext; +import org.opensearch.sql.datasource.DataSourceService; +import org.opensearch.sql.datasource.DataSourceServiceImpl; import org.opensearch.sql.executor.ExecutionEngine; import org.opensearch.sql.legacy.metrics.MetricName; import org.opensearch.sql.legacy.metrics.Metrics; import org.opensearch.sql.opensearch.security.SecurityAccess; import org.opensearch.sql.opensearch.setting.OpenSearchSettings; +import org.opensearch.sql.plugin.config.OpenSearchPluginModule; import org.opensearch.sql.ppl.PPLService; import org.opensearch.sql.ppl.domain.PPLQueryRequest; import org.opensearch.sql.protocol.response.QueryResult; @@ -36,21 +40,12 @@ import org.opensearch.sql.protocol.response.format.VisualizationResponseFormatter; import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** Send PPL query transport action. */ public class TransportPPLQueryAction extends HandledTransportAction { - private final NodeClient client; - - /** Cluster service required by bean initialization. */ - private final ClusterService clusterService; - - /** Settings required by been initialization. */ - private final Settings pluginSettings; - - private final AnnotationConfigApplicationContext applicationContext; + private final Injector injector; /** Constructor of TransportPPLQueryAction. */ @Inject @@ -59,12 +54,19 @@ public TransportPPLQueryAction( ActionFilters actionFilters, NodeClient client, ClusterService clusterService, - AnnotationConfigApplicationContext applicationContext) { + DataSourceServiceImpl dataSourceService) { super(PPLQueryAction.NAME, transportService, actionFilters, TransportPPLQueryRequest::new); - this.client = client; - this.clusterService = clusterService; - this.pluginSettings = new OpenSearchSettings(clusterService.getClusterSettings()); - this.applicationContext = applicationContext; + + ModulesBuilder modules = new ModulesBuilder(); + modules.add(new OpenSearchPluginModule()); + modules.add( + b -> { + b.bind(NodeClient.class).toInstance(client); + b.bind(org.opensearch.sql.common.setting.Settings.class) + .toInstance(new OpenSearchSettings(clusterService.getClusterSettings())); + b.bind(DataSourceService.class).toInstance(dataSourceService); + }); + this.injector = modules.createInjector(); } /** @@ -80,7 +82,7 @@ protected void doExecute( QueryContext.addRequestId(); PPLService pplService = - SecurityAccess.doPrivileged(() -> applicationContext.getBean(PPLService.class)); + SecurityAccess.doPrivileged(() -> injector.getInstance(PPLService.class)); TransportPPLQueryRequest transportRequest = TransportPPLQueryRequest.fromActionRequest(request); // in order to use PPL service, we need to convert TransportPPLQueryRequest to PPLQueryRequest PPLQueryRequest transformedRequest = transportRequest.toPPLQueryRequest(); diff --git a/ppl/build.gradle b/ppl/build.gradle index 1c605f4372..9e2ce321a4 100644 --- a/ppl/build.gradle +++ b/ppl/build.gradle @@ -47,8 +47,6 @@ dependencies { implementation "org.antlr:antlr4-runtime:4.7.1" implementation group: 'com.google.guava', name: 'guava', version: '31.0.1-jre' api group: 'org.json', name: 'json', version: '20180813' - implementation group: 'org.springframework', name: 'spring-context', version: "${spring_version}" - implementation group: 'org.springframework', name: 'spring-beans', version: "${spring_version}" implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.17.1' api project(':common') api project(':core') diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/config/PPLServiceConfig.java b/ppl/src/main/java/org/opensearch/sql/ppl/config/PPLServiceConfig.java deleted file mode 100644 index 1067bbaa6b..0000000000 --- a/ppl/src/main/java/org/opensearch/sql/ppl/config/PPLServiceConfig.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - - -package org.opensearch.sql.ppl.config; - -import org.opensearch.sql.executor.QueryManager; -import org.opensearch.sql.executor.execution.QueryPlanFactory; -import org.opensearch.sql.ppl.PPLService; -import org.opensearch.sql.ppl.antlr.PPLSyntaxParser; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; - -@Configuration -public class PPLServiceConfig { - - @Autowired - private QueryManager queryManager; - - @Autowired - private QueryPlanFactory queryPlanFactory; - - /** - * The registration of OpenSearch storage engine happens here because - * OpenSearchStorageEngine is dependent on NodeClient. - * - * @return PPLService. - */ - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public PPLService pplService() { - return new PPLService(new PPLSyntaxParser(), queryManager, queryPlanFactory); - } - -} diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java index a1b1ccaf14..178335a126 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/PPLServiceTest.java @@ -19,20 +19,15 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.opensearch.sql.common.response.ResponseListener; -import org.opensearch.sql.datasource.DataSourceService; import org.opensearch.sql.executor.DefaultQueryManager; import org.opensearch.sql.executor.ExecutionEngine; import org.opensearch.sql.executor.ExecutionEngine.ExplainResponse; import org.opensearch.sql.executor.ExecutionEngine.ExplainResponseNode; import org.opensearch.sql.executor.ExecutionEngine.QueryResponse; -import org.opensearch.sql.executor.QueryManager; import org.opensearch.sql.executor.QueryService; import org.opensearch.sql.executor.execution.QueryPlanFactory; -import org.opensearch.sql.expression.function.FunctionProperties; -import org.opensearch.sql.ppl.config.PPLServiceConfig; +import org.opensearch.sql.ppl.antlr.PPLSyntaxParser; import org.opensearch.sql.ppl.domain.PPLQueryRequest; -import org.opensearch.sql.storage.StorageEngine; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; @RunWith(MockitoJUnitRunner.class) public class PPLServiceTest { @@ -41,42 +36,24 @@ public class PPLServiceTest { private static String EXPLAIN = "/_plugins/_ppl/_explain"; - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - private PPLService pplService; - @Mock - private QueryService queryService; - - @Mock - private StorageEngine storageEngine; - - @Mock - private ExecutionEngine executionEngine; + private DefaultQueryManager queryManager; @Mock - private DataSourceService dataSourceService; + private QueryService queryService; @Mock private ExecutionEngine.Schema schema; - private DefaultQueryManager queryManager; - /** * Setup the test context. */ @Before public void setUp() { queryManager = DefaultQueryManager.defaultQueryManager(); - context.registerBean(QueryManager.class, () -> queryManager); - context.registerBean(QueryPlanFactory.class, () -> new QueryPlanFactory(queryService)); - context.registerBean(StorageEngine.class, () -> storageEngine); - context.registerBean(ExecutionEngine.class, () -> executionEngine); - context.registerBean(DataSourceService.class, () -> dataSourceService); - context.registerBean(FunctionProperties.class, FunctionProperties::new); - context.register(PPLServiceConfig.class); - context.refresh(); - pplService = context.getBean(PPLService.class); + pplService = new PPLService(new PPLSyntaxParser(), queryManager, + new QueryPlanFactory(queryService)); } @After diff --git a/sql/build.gradle b/sql/build.gradle index 13a7ceba53..5c85231b77 100644 --- a/sql/build.gradle +++ b/sql/build.gradle @@ -47,8 +47,6 @@ dependencies { implementation "org.antlr:antlr4-runtime:4.7.1" implementation group: 'com.google.guava', name: 'guava', version: '31.0.1-jre' implementation group: 'org.json', name: 'json', version:'20180813' - implementation group: 'org.springframework', name: 'spring-context', version: "${spring_version}" - implementation group: 'org.springframework', name: 'spring-beans', version: "${spring_version}" implementation project(':common') implementation project(':core') api project(':protocol') diff --git a/sql/src/main/java/org/opensearch/sql/sql/config/SQLServiceConfig.java b/sql/src/main/java/org/opensearch/sql/sql/config/SQLServiceConfig.java deleted file mode 100644 index 4287883c34..0000000000 --- a/sql/src/main/java/org/opensearch/sql/sql/config/SQLServiceConfig.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - - -package org.opensearch.sql.sql.config; - -import org.opensearch.sql.executor.QueryManager; -import org.opensearch.sql.executor.execution.QueryPlanFactory; -import org.opensearch.sql.sql.SQLService; -import org.opensearch.sql.sql.antlr.SQLSyntaxParser; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; - -/** - * SQL service configuration for Spring container initialization. - */ -@Configuration -public class SQLServiceConfig { - - @Autowired - private QueryManager queryManager; - - @Autowired - private QueryPlanFactory queryExecutionFactory; - - /** - * The registration of OpenSearch storage engine happens here because - * OpenSearchStorageEngine is dependent on NodeClient. - * - * @return SQLService. - */ - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) - public SQLService sqlService() { - return new SQLService( - new SQLSyntaxParser(), - queryManager, - queryExecutionFactory); - } - -} - diff --git a/sql/src/test/java/org/opensearch/sql/sql/SQLServiceTest.java b/sql/src/test/java/org/opensearch/sql/sql/SQLServiceTest.java index 9abe37cd06..a351c30609 100644 --- a/sql/src/test/java/org/opensearch/sql/sql/SQLServiceTest.java +++ b/sql/src/test/java/org/opensearch/sql/sql/SQLServiceTest.java @@ -26,12 +26,10 @@ import org.opensearch.sql.executor.ExecutionEngine; import org.opensearch.sql.executor.ExecutionEngine.ExplainResponse; import org.opensearch.sql.executor.ExecutionEngine.ExplainResponseNode; -import org.opensearch.sql.executor.QueryManager; import org.opensearch.sql.executor.QueryService; import org.opensearch.sql.executor.execution.QueryPlanFactory; -import org.opensearch.sql.sql.config.SQLServiceConfig; +import org.opensearch.sql.sql.antlr.SQLSyntaxParser; import org.opensearch.sql.sql.domain.SQLQueryRequest; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; @ExtendWith(MockitoExtension.class) class SQLServiceTest { @@ -40,8 +38,6 @@ class SQLServiceTest { private static String EXPLAIN = "/_plugins/_sql/_explain"; - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - private SQLService sqlService; private DefaultQueryManager queryManager; @@ -55,11 +51,8 @@ class SQLServiceTest { @BeforeEach public void setUp() { queryManager = DefaultQueryManager.defaultQueryManager(); - context.registerBean(QueryManager.class, () -> queryManager); - context.registerBean(QueryPlanFactory.class, () -> new QueryPlanFactory(queryService)); - context.register(SQLServiceConfig.class); - context.refresh(); - sqlService = context.getBean(SQLService.class); + sqlService = new SQLService(new SQLSyntaxParser(), queryManager, + new QueryPlanFactory(queryService)); } @AfterEach