From 6fa3b703b1061db802f51c86ba383f7e6c9d2f89 Mon Sep 17 00:00:00 2001 From: Ling Hengqian Date: Fri, 15 Dec 2023 00:21:27 +0800 Subject: [PATCH] Add GraalVM Reachability Metadata and corresponding nativeTest for MS SQL Server integration (#29404) --- distribution/proxy-native/pom.xml | 1 + .../graalvm-native-image/_index.cn.md | 37 ++++++++-- .../graalvm-native-image/_index.en.md | 40 +++++++++-- .../SQLServerConnectionPropertiesParser.java | 2 +- ...LServerConnectionPropertiesParserTest.java | 3 +- .../reflect-config.json | 61 ++++++++++++---- pom.xml | 3 + .../native-image-filter/extra-filter.json | 2 +- test/native/pom.xml | 10 +++ .../commons/repository/AddressRepository.java | 18 +++++ .../repository/OrderItemRepository.java | 21 ++++++ .../commons/repository/OrderRepository.java | 21 ++++++ .../jdbc/databases/MSSQLServerTest.java | 52 ++++++++++++++ .../resource-config.json | 6 ++ .../container-license-acceptance.txt | 1 + .../yaml/databases/mssqlserver.yaml | 72 +++++++++++++++++++ 16 files changed, 321 insertions(+), 29 deletions(-) create mode 100644 test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MSSQLServerTest.java create mode 100644 test/native/src/test/resources/container-license-acceptance.txt create mode 100644 test/native/src/test/resources/test-native/yaml/databases/mssqlserver.yaml diff --git a/distribution/proxy-native/pom.xml b/distribution/proxy-native/pom.xml index 1530b227d3763..cfbbb5fcf60c7 100644 --- a/distribution/proxy-native/pom.xml +++ b/distribution/proxy-native/pom.xml @@ -86,6 +86,7 @@ org.apache.shardingsphere.proxy.Bootstrap -J-Xmx7g + -H:+AddAllCharsets true diff --git a/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.cn.md b/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.cn.md index 9c81c2c6d11d2..09a969e7ca38e 100644 --- a/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.cn.md +++ b/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.cn.md @@ -41,6 +41,9 @@ ShardingSphere JDBC 要求在如下或更高版本的 `GraalVM CE` 完成构建 0.9.28 true + + -H:+AddAllCharsets + true @@ -74,17 +77,25 @@ ShardingSphere JDBC 要求在如下或更高版本的 `GraalVM CE` 完成构建 ```groovy plugins { - id 'org.graalvm.buildtools.native' version '0.9.28' + id 'org.graalvm.buildtools.native' version '0.9.28' } dependencies { - implementation 'org.apache.shardingsphere:shardingsphere-jdbc-core:${shardingsphere.version}' + implementation 'org.apache.shardingsphere:shardingsphere-jdbc-core:${shardingsphere.version}' } graalvmNative { - metadataRepository { - enabled = true - } + binaries { + main { + buildArgs.add('-H:+AddAllCharsets') + } + test { + buildArgs.add('-H:+AddAllCharsets') + } + } + metadataRepository { + enabled = true + } } ``` @@ -193,7 +204,21 @@ rules: 文件的 GraalVM Reachability Metadata。使用者可通过 GraalVM Native Build Tools 的 GraalVM Tracing Agent 来快速采集 GraalVM Reachability Metadata。 -4. 尚未验证 DistSQL 的可用性。使用者需自行添加额外的 GraalVM Reachability Metadata。 +4. 以 MS SQL Server 的 JDBC Driver 为代表的 `com.microsoft.sqlserver:mssql-jdbc` 等 Maven 模块会根据数据库中使用的编码动态加载不同的字符集,这是不可预测的行为。 +当遇到如下 Error,使用者需要添加 `-H:+AddAllCharsets` 的 `buildArg` 到 GraalVM Native Build Tools 的配置中。 + +```shell +Caused by: java.io.UnsupportedEncodingException: SQL Server collation SQL_Latin1_General_CP1_CI_AS is not supported by this driver. + com.microsoft.sqlserver.jdbc.SQLCollation.encodingFromSortId(SQLCollation.java:506) + com.microsoft.sqlserver.jdbc.SQLCollation.(SQLCollation.java:63) + com.microsoft.sqlserver.jdbc.SQLServerConnection.processEnvChange(SQLServerConnection.java:3174) + [...] +Caused by: java.io.UnsupportedEncodingException: Codepage Cp1252 is not supported by the Java environment. + com.microsoft.sqlserver.jdbc.Encoding.checkSupported(SQLCollation.java:572) + com.microsoft.sqlserver.jdbc.SQLCollation$SortOrder.getEncoding(SQLCollation.java:473) + com.microsoft.sqlserver.jdbc.SQLCollation.encodingFromSortId(SQLCollation.java:501) + [...] +``` ## 贡献 GraalVM Reachability Metadata diff --git a/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.en.md b/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.en.md index 23d81f9daaee9..9f337c4006538 100644 --- a/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.en.md +++ b/docs/document/content/user-manual/shardingsphere-jdbc/graalvm-native-image/_index.en.md @@ -43,9 +43,12 @@ and the documentation of GraalVM Native Build Tools shall prevail. 0.9.28 true - - true - + + -H:+AddAllCharsets + + + true + @@ -85,9 +88,17 @@ dependencies { } graalvmNative { - metadataRepository { - enabled = true - } + binaries { + main { + buildArgs.add('-H:+AddAllCharsets') + } + test { + buildArgs.add('-H:+AddAllCharsets') + } + } + metadataRepository { + enabled = true + } } ``` @@ -201,7 +212,22 @@ rules: folder or `src/test/resources/META-INF/native-image` folder. Users can quickly collect GraalVM Reachability Metadata through the GraalVM Tracing Agent of GraalVM Native Build Tools. -4. DistSQL availability has not been verified. Users need to add additional GraalVM Reachability Metadata by themselves. +4. Maven modules such as `com.microsoft.sqlserver:mssql-jdbc`, represented by the JDBC Driver of MS SQL Server, +will dynamically load different character sets based on the encoding used in the database, which is unpredictable behavior. +When encountering the following Error, users need to add the `buildArg` of `-H:+AddAllCharsets` to the configuration of GraalVM Native Build Tools. + +```shell +Caused by: java.io.UnsupportedEncodingException: SQL Server collation SQL_Latin1_General_CP1_CI_AS is not supported by this driver. + com.microsoft.sqlserver.jdbc.SQLCollation.encodingFromSortId(SQLCollation.java:506) + com.microsoft.sqlserver.jdbc.SQLCollation.(SQLCollation.java:63) + com.microsoft.sqlserver.jdbc.SQLServerConnection.processEnvChange(SQLServerConnection.java:3174) + [...] +Caused by: java.io.UnsupportedEncodingException: Codepage Cp1252 is not supported by the Java environment. + com.microsoft.sqlserver.jdbc.Encoding.checkSupported(SQLCollation.java:572) + com.microsoft.sqlserver.jdbc.SQLCollation$SortOrder.getEncoding(SQLCollation.java:473) + com.microsoft.sqlserver.jdbc.SQLCollation.encodingFromSortId(SQLCollation.java:501) + [...] +``` ## Contribute GraalVM Reachability Metadata diff --git a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParser.java b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParser.java index 2c37c29ca2b5e..8b61d3f39d0a1 100644 --- a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParser.java +++ b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParser.java @@ -34,7 +34,7 @@ public final class SQLServerConnectionPropertiesParser implements ConnectionProp private static final int DEFAULT_PORT = 1433; - private static final Pattern URL_PATTERN = Pattern.compile("jdbc:(microsoft:)?sqlserver://([\\w\\-\\.]+):?(\\d*);\\S*(DatabaseName|database)=([\\w\\-\\.]+);?", Pattern.CASE_INSENSITIVE); + private static final Pattern URL_PATTERN = Pattern.compile("jdbc:(microsoft:|tc:)?sqlserver:.*//([\\w\\-\\.]+):?(\\d*);\\S*(databaseName|database)=([\\w\\-\\.]+);?", Pattern.CASE_INSENSITIVE); @Override public ConnectionProperties parse(final String url, final String username, final String catalog) { diff --git a/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParserTest.java b/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParserTest.java index 535f3acdbf7d1..bedc120df4347 100644 --- a/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParserTest.java +++ b/infra/database/type/sqlserver/src/test/java/org/apache/shardingsphere/infra/database/sqlserver/connector/SQLServerConnectionPropertiesParserTest.java @@ -67,7 +67,8 @@ public Stream provideArguments(final ExtensionContext exten Arguments.of("defaultPortAndMicrosoft", "jdbc:microsoft:sqlserver://127.0.0.1;DatabaseName=foo_ds", "127.0.0.1", 1433, "foo_ds", null), Arguments.of("defaultPortWithoutMicrosoft", "jdbc:sqlserver://127.0.0.1;DatabaseName=foo_ds", "127.0.0.1", 1433, "foo_ds", null), Arguments.of("databaseNameContainDotAndMicrosoft", "jdbc:microsoft:sqlserver://127.0.0.1:9999;DatabaseName=foo_0.0.0", "127.0.0.1", 9999, "foo_0.0.0", null), - Arguments.of("databaseNameContainDotAndWithoutMicrosoft", "jdbc:sqlserver://127.0.0.1:9999;DatabaseName=foo_0.0.0", "127.0.0.1", 9999, "foo_0.0.0", null)); + Arguments.of("databaseNameContainDotAndWithoutMicrosoft", "jdbc:sqlserver://127.0.0.1:9999;DatabaseName=foo_0.0.0", "127.0.0.1", 9999, "foo_0.0.0", null), + Arguments.of("testcontainers", "jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-hostname;databaseName=test_database", "test-hostname", 1433, "test_database", null)); } } } diff --git a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json index c68ecdf7860d7..851418370ec22 100644 --- a/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json +++ b/infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/shardingsphere-infra-reachability-metadata/reflect-config.json @@ -19,7 +19,7 @@ "name":"org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration"}, + "condition":{"typeReachable":"org.apache.shardingsphere.broadcast.yaml.swapper.NewYamlBroadcastRuleConfigurationSwapper"}, "name":"org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -34,7 +34,7 @@ "name":"org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfigurationCustomizer" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.database.DatabaseTypeEngine"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager"}, "name":"org.apache.shardingsphere.driver.ShardingSphereDriver" }, { @@ -67,7 +67,7 @@ "name":"org.apache.shardingsphere.encrypt.yaml.config.YamlCompatibleEncryptRuleConfiguration" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.shortcut.YamlRuleConfigurationShortcuts"}, "name":"org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -206,7 +206,7 @@ "methods":[{"name":"getRows","parameterTypes":[] }, {"name":"getUniqueKey","parameterTypes":[] }] }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.metadata.database.schema.builder.SystemSchemaBuilder"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase"}, "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereColumn", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -221,7 +221,7 @@ "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereColumnCustomizer" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereIndex"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.metadata.database.schema.builder.SystemSchemaBuilder"}, "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereIndex", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -236,7 +236,7 @@ "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereIndexCustomizer" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable"}, + "condition":{"typeReachable":"org.apache.shardingsphere.metadata.persist.service.schema.NewTableMetaDataPersistService"}, "name":"org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -406,7 +406,7 @@ "name":"org.apache.shardingsphere.parser.yaml.config.YamlSQLParserCacheOptionRuleConfigurationCustomizer" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfiguration"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.shortcut.YamlRuleConfigurationShortcuts"}, "name":"org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfiguration", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -613,7 +613,7 @@ "methods":[{"name":"","parameterTypes":[] }] }, { - "condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration"}, + "condition":{"typeReachable":"org.apache.shardingsphere.infra.yaml.config.shortcut.YamlRuleConfigurationShortcuts"}, "name":"org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -628,7 +628,7 @@ "name":"org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfigurationCustomizer" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration"}, + "condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.swapper.NewYamlShardingRuleConfigurationSwapper"}, "name":"org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -658,7 +658,7 @@ "name":"org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfigurationCustomizer" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration"}, + "condition":{"typeReachable":"org.apache.shardingsphere.sharding.yaml.swapper.NewYamlShardingRuleConfigurationSwapper"}, "name":"org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration", "allDeclaredFields":true, "queryAllPublicMethods":true, @@ -755,6 +755,11 @@ "name":"org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type.OpenGaussDDLStatementVisitor", "methods":[{"name":"","parameterTypes":[] }] }, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type.OpenGaussDMLStatementVisitor"}, + "name":"org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type.OpenGaussDMLStatementVisitor", + "methods":[{"name":"","parameterTypes":[] }] +}, { "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"}, "name":"org.apache.shardingsphere.sql.parser.oracle.visitor.statement.OracleStatementVisitorFacade", @@ -830,18 +835,48 @@ "name":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLSelectStatement", "methods":[{"name":"","parameterTypes":[] }] }, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerDeleteStatement"}, + "name":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerDeleteStatement", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement"}, + "name":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerInsertStatement", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement"}, + "name":"org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement", + "methods":[{"name":"","parameterTypes":[] }] +}, { "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"}, "name":"org.apache.shardingsphere.sql.parser.sql92.visitor.statement.SQL92StatementVisitorFacade", "methods":[{"name":"","parameterTypes":[] }] }, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sqlserver.parser.SQLServerLexer"}, + "name":"org.apache.shardingsphere.sql.parser.sqlserver.parser.SQLServerLexer", + "methods":[{"name":"","parameterTypes":["org.antlr.v4.runtime.CharStream"] }] +}, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sqlserver.parser.SQLServerParser"}, + "name":"org.apache.shardingsphere.sql.parser.sqlserver.parser.SQLServerParser", + "methods":[{"name":"","parameterTypes":["org.antlr.v4.runtime.TokenStream"] }] +}, { "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.core.database.visitor.SQLStatementVisitorFactory"}, "name":"org.apache.shardingsphere.sql.parser.sqlserver.visitor.statement.SQLServerStatementVisitorFacade", "methods":[{"name":"","parameterTypes":[] }] }, { - "condition":{"typeReachable":"org.apache.shardingsphere.sqlfederation.optimizer.planner.util.SQLFederationFunctionUtils"}, + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sqlserver.visitor.statement.type.SQLServerDDLStatementVisitor"}, + "name":"org.apache.shardingsphere.sql.parser.sqlserver.visitor.statement.type.SQLServerDDLStatementVisitor", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule"}, "name":"org.apache.shardingsphere.sqlfederation.optimizer.planner.util.SQLFederationFunctionUtils", "queryAllPublicMethods":true }, @@ -867,8 +902,8 @@ "name":"org.apache.shardingsphere.transaction.yaml.config.YamlTransactionRuleConfiguration" }, { - "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type.OpenGaussDMLStatementVisitor"}, - "name":"org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type.OpenGaussDMLStatementVisitor", + "condition":{"typeReachable":"org.apache.shardingsphere.sql.parser.sqlserver.visitor.statement.type.SQLServerDMLStatementVisitor"}, + "name":"org.apache.shardingsphere.sql.parser.sqlserver.visitor.statement.type.SQLServerDMLStatementVisitor", "methods":[{"name":"","parameterTypes":[] }] } ] diff --git a/pom.xml b/pom.xml index b84dd0f003ff3..fe6de490281d6 100644 --- a/pom.xml +++ b/pom.xml @@ -1078,6 +1078,9 @@ ${native-maven-plugin.version} true + + -H:+AddAllCharsets + ${project.build.outputDirectory} true diff --git a/test/native/native-image-filter/extra-filter.json b/test/native/native-image-filter/extra-filter.json index 491d699692951..327dcc4e9e386 100644 --- a/test/native/native-image-filter/extra-filter.json +++ b/test/native/native-image-filter/extra-filter.json @@ -20,7 +20,7 @@ {"excludeClasses": "com.ibm.icu.text.**"}, {"excludeClasses": "com.mysql.cj.**"}, {"excludeClasses": "com.zaxxer.hikari.**"}, - {"excludeClasses": "javax.xml.bind.annotation.**"}, + {"excludeClasses": "javax.xml.**"}, {"excludeClasses": "groovy.**"}, {"excludeClasses": "org.apache.calcite.**"}, {"excludeClasses": "org.codehaus.groovy.**"}, diff --git a/test/native/pom.xml b/test/native/pom.xml index 482868a8107f6..bafa5da53a9c8 100644 --- a/test/native/pom.xml +++ b/test/native/pom.xml @@ -65,11 +65,21 @@ opengauss-jdbc test + + com.microsoft.sqlserver + mssql-jdbc + test + org.testcontainers postgresql test + + org.testcontainers + mssqlserver + test + diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java index d3505fc67af45..4dcf0c7e2aa88 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/AddressRepository.java @@ -50,6 +50,24 @@ public void createTableIfNotExists() throws SQLException { } } + /** + * create table t_address in MS SQL Server. + * This also ignored the default schema of the `dbo`. + * @throws SQLException SQL exception + */ + public void createTableInSQLServer() throws SQLException { + String sql = "CREATE TABLE [t_address] (\n" + + " address_id bigint NOT NULL,\n" + + " address_name varchar(100) NOT NULL,\n" + + " PRIMARY KEY (address_id)\n" + + ");"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } + } + /** * drop table t_address. * @throws SQLException SQL exception diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java index 3bfb69558ea31..28933d400dc41 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderItemRepository.java @@ -70,6 +70,27 @@ public void createTableIfNotExistsInPostgres() throws SQLException { } } + /** + * create table in MS SQL Server. `order_item_id` is not set to `IDENTITY(1,1)` to simplify the unit test. + * This also ignored the default schema of the `dbo`. + * @throws SQLException SQL exception + */ + public void createTableInSQLServer() throws SQLException { + String sql = "CREATE TABLE [t_order_item] (\n" + + " order_item_id bigint NOT NULL,\n" + + " order_id bigint NOT NULL,\n" + + " user_id int NOT NULL,\n" + + " phone varchar(50),\n" + + " status varchar(50),\n" + + " PRIMARY KEY (order_item_id)\n" + + ");"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } + } + /** * drop table. * @throws SQLException SQL exception diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java index 41a800949ec8a..099ff83753780 100644 --- a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/commons/repository/OrderRepository.java @@ -70,6 +70,27 @@ public void createTableIfNotExistsInPostgres() throws SQLException { } } + /** + * create table in MS SQL Server. `order_item_id` is not set to `IDENTITY(1,1)` to simplify the unit test. + * This also ignored the default schema of the `dbo`. + * @throws SQLException SQL exception + */ + public void createTableInSQLServer() throws SQLException { + String sql = "CREATE TABLE [t_order] (\n" + + " order_id bigint NOT NULL,\n" + + " order_type int NOT NULL,\n" + + " user_id int NOT NULL,\n" + + " address_id bigint NOT NULL,\n" + + " status varchar(50),\n" + + " PRIMARY KEY (order_id)\n" + + ");"; + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } + } + /** * drop table. * TODO There is a bug in this function in shadow's unit test and requires additional fixes. diff --git a/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MSSQLServerTest.java b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MSSQLServerTest.java new file mode 100644 index 0000000000000..f9d874e33c065 --- /dev/null +++ b/test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MSSQLServerTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.natived.jdbc.databases; + +import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory; +import org.apache.shardingsphere.test.natived.jdbc.commons.AbstractShardingCommonTest; +import org.apache.shardingsphere.test.natived.jdbc.commons.FileTestUtils; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledInNativeImage; + +import javax.sql.DataSource; +import java.io.IOException; +import java.sql.SQLException; + +public class MSSQLServerTest { + + private AbstractShardingCommonTest abstractShardingCommonTest; + + @Test + @EnabledInNativeImage + void assertShardingInLocalTransactions() throws SQLException, IOException { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("test-native/yaml/databases/mssqlserver.yaml")); + abstractShardingCommonTest = new AbstractShardingCommonTest(dataSource); + this.initEnvironment(); + abstractShardingCommonTest.processSuccess(); + abstractShardingCommonTest.cleanEnvironment(); + } + + private void initEnvironment() throws SQLException { + abstractShardingCommonTest.getOrderRepository().createTableInSQLServer(); + abstractShardingCommonTest.getOrderItemRepository().createTableInSQLServer(); + abstractShardingCommonTest.getAddressRepository().createTableInSQLServer(); + abstractShardingCommonTest.getOrderRepository().truncateTable(); + abstractShardingCommonTest.getOrderItemRepository().truncateTable(); + abstractShardingCommonTest.getAddressRepository().truncateTable(); + } +} diff --git a/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json b/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json index 00d30ce0bf539..f299bfa645756 100644 --- a/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json +++ b/test/native/src/test/resources/META-INF/native-image/shardingsphere-test-native-test-metadata/resource-config.json @@ -1,6 +1,12 @@ { "resources":{ "includes":[{ + "condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.databases.MSSQLServerTest"}, + "pattern":"\\Qcontainer-license-acceptance.txt\\E" + }, { + "condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.databases.MSSQLServerTest"}, + "pattern":"\\Qtest-native/yaml/databases/mssqlserver.yaml\\E" + }, { "condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.databases.MySQLTest"}, "pattern":"\\Qtest-native/yaml/databases/mysql.yaml\\E" }, { diff --git a/test/native/src/test/resources/container-license-acceptance.txt b/test/native/src/test/resources/container-license-acceptance.txt new file mode 100644 index 0000000000000..f65e2cb5adf03 --- /dev/null +++ b/test/native/src/test/resources/container-license-acceptance.txt @@ -0,0 +1 @@ +mcr.microsoft.com/mssql/server:2022-CU10-ubuntu-22.04 diff --git a/test/native/src/test/resources/test-native/yaml/databases/mssqlserver.yaml b/test/native/src/test/resources/test-native/yaml/databases/mssqlserver.yaml new file mode 100644 index 0000000000000..df16a8fa3245c --- /dev/null +++ b/test/native/src/test/resources/test-native/yaml/databases/mssqlserver.yaml @@ -0,0 +1,72 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +mode: + type: Standalone + repository: + type: JDBC + +dataSources: + ds_0: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_0;TC_DAEMON=true; + ds_1: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_1;TC_DAEMON=true; + ds_2: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + jdbcUrl: jdbc:tc:sqlserver:2022-CU10-ubuntu-22.04://test-native-databases-mssqlserver;databaseName=demo_ds_2;TC_DAEMON=true; + +rules: +- !SHARDING + tables: + t_order: + actualDataNodes: + keyGenerateStrategy: + column: order_id + keyGeneratorName: snowflake + t_order_item: + actualDataNodes: + keyGenerateStrategy: + column: order_item_id + keyGeneratorName: snowflake + defaultDatabaseStrategy: + standard: + shardingColumn: user_id + shardingAlgorithmName: inline + shardingAlgorithms: + inline: + type: CLASS_BASED + props: + strategy: STANDARD + algorithmClassName: org.apache.shardingsphere.test.natived.jdbc.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture + keyGenerators: + snowflake: + type: SNOWFLAKE + auditors: + sharding_key_required_auditor: + type: DML_SHARDING_CONDITIONS + +- !BROADCAST + tables: + - t_address + +props: + sql-show: false