Skip to content

Commit

Permalink
Provide native-image.properties containing `--report-unsupported-el…
Browse files Browse the repository at this point in the history
…ements-at-runtime` of buildArgs
  • Loading branch information
linghengqian committed Nov 22, 2023
1 parent 2715b71 commit 511ce5d
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 99 deletions.
7 changes: 0 additions & 7 deletions distribution/proxy-native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-bootstrap</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-cluster-mode-repository-etcd</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down Expand Up @@ -103,7 +97,6 @@
<configuration>
<mainClass>org.apache.shardingsphere.proxy.Bootstrap</mainClass>
<buildArgs>
<arg>--report-unsupported-elements-at-runtime</arg>
<arg>-J-Xmx7g</arg>
</buildArgs>
<metadataRepository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ ShardingSphere JDBC 已在 GraalVM Native Image 下完成可用性验证。

构建包含 `org.apache.shardingsphere:shardingsphere-jdbc-core:${shardingsphere.version}` 的 Maven 依赖的 GraalVM Native
Image,你需要借助于 GraalVM Native Build Tools。GraalVM Native Build Tools 提供了 Maven Plugin 和 Gradle Plugin 来简化 GraalVM
CE 的 `native-image` 工具的长篇大论的 shell 命令。
CE 的 `native-image` 命令行工具的长篇大论的 shell 命令。

ShardingSphere JDBC 要求在如下或更高版本的 `GraalVM CE` 完成构建 GraalVM Native Image。使用者可通过 SDKMAN! 快速切换 JDK。这同理
适用于 `Oracle GraalVM``Liberica Native Image Kit``Mandrel``GraalVM CE` 的下游发行版。

- GraalVM CE 23.0.2 For JDK 17.0.9,对应于 SDKMAN! 的 `17.0.9-graalce`
- GraalVM CE 23.0.2 For JDK 21.0.1,对应于 SDKMAN! 的 `21.0.1-graalce`
- GraalVM CE 23.1.1 For JDK 21.0.1,对应于 SDKMAN! 的 `21.0.1-graalce`

### Maven 生态

使用者需要配置额外的 BuildArgs ,以阻止 GroovyShell 的相关类在构建 GraalVM Native Image 时报错。并主动使用 GraalVM Reachability
Metadata 中央仓库。如下配置可供参考,以配置项目额外的 Maven Profile,以 GraalVM Native Build Tools 的文档为准。
使用者需要主动使用 GraalVM Reachability Metadata 中央仓库。
如下配置可供参考,以配置项目额外的 Maven Profiles,以 GraalVM Native Build Tools 的文档为准。

```xml
<project>
Expand All @@ -41,9 +41,6 @@ Metadata 中央仓库。如下配置可供参考,以配置项目额外的 Mave
<version>0.9.28</version>
<extensions>true</extensions>
<configuration>
<buildArgs>
<arg>--report-unsupported-elements-at-runtime</arg>
</buildArgs>
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
Expand Down Expand Up @@ -72,8 +69,8 @@ Metadata 中央仓库。如下配置可供参考,以配置项目额外的 Mave

### Gradle 生态

使用者需要配置额外的 BuildArgs ,以阻止 GroovyShell 的相关类在构建 GraalVM Native Image 时报错。并主动使用 GraalVM Reachability
Metadata 中央仓库。如下配置可供参考,以配置项目额外的 Gradle Task,以 GraalVM Native Build Tools 的文档为准。
使用者需要主动使用 GraalVM Reachability Metadata 中央仓库。
如下配置可供参考,以配置项目额外的 Gradle Tasks,以 GraalVM Native Build Tools 的文档为准。

```groovy
plugins {
Expand All @@ -85,26 +82,17 @@ dependencies {
}
graalvmNative {
binaries {
main {
buildArgs.add('--report-unsupported-elements-at-runtime')
}
test {
buildArgs.add('--report-unsupported-elements-at-runtime')
}
}
metadataRepository {
enabled = true
}
}
```

### 对于 SBT 等不被 GraalVM Native Build Tools 支持的构建工具
### 对于 sbt 等不被 GraalVM Native Build Tools 支持的构建工具

此类需求需要在 https://github.com/graalvm/native-build-tools 打开额外的 issue 并提供对应构建工具的 Plugin 实现。


### 使用限制
## 使用限制

1. 如下的算法类由于涉及到 https://github.com/oracle/graal/issues/5522 , 暂未可在 GraalVM Native Image 下使用。
- `org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ ShardingSphere JDBC has been validated for availability under GraalVM Native Ima

Build GraalVM Native containing Maven dependencies of `org.apache.shardingsphere:shardingsphere-jdbc-core:${shardingsphere.version}`
Image, you need to resort to GraalVM Native Build Tools. GraalVM Native Build Tools provides Maven Plugin and Gradle Plugin
to simplify long list of shell commands for GraalVM CE's `native-image` tool.
to simplify long list of shell commands for GraalVM CE's `native-image` command line tool.

ShardingSphere JDBC requires GraalVM Native Image to be built with GraalVM CE as follows or higher. Users can quickly switch
JDK through `SDKMAN!`. Same reason applicable to downstream distributions of `GraalVM CE` such as `Oracle GraalVM`, `Liberica Native Image Kit`
and `Mandrel`.

- GraalVM CE 23.0.2 For JDK 17.0.9, corresponding to `17.0.9-graalce` of SDKMAN!
- GraalVM CE 23.0.2 For JDK 21.0.1, corresponding to `21.0.1-graalce` of SDKMAN!
- GraalVM CE 23.1.1 For JDK 21.0.1, corresponding to `21.0.1-graalce` of SDKMAN!

### Maven Ecology

Users need to configure additional `BuildArgs` to prevent GroovyShell related classes from reporting errors when building
GraalVM Native Image. and proactively use GraalVM Reachability Metadata central repository. The following configuration
is for reference to configure additional Maven Profiles for the project, and the documentation of GraalVM Native Build
Tools shall prevail.
Users need to actively use the GraalVM Reachability Metadata central repository.
The following configuration is for reference to configure additional Maven Profiles for the project,
and the documentation of GraalVM Native Build Tools shall prevail.

```xml
<project>
Expand All @@ -44,9 +43,6 @@ Tools shall prevail.
<version>0.9.28</version>
<extensions>true</extensions>
<configuration>
<buildArgs>
<arg>--report-unsupported-elements-at-runtime</arg>
</buildArgs>
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
Expand Down Expand Up @@ -75,10 +71,9 @@ Tools shall prevail.

### Gradle Ecosystem

Users need to configure additional `BuildArgs` to prevent GroovyShell related classes from reporting errors when building
GraalVM Native Image. and proactively use GraalVM Reachability Metadata central repository. The following configuration
is for reference to configure additional Gradle Task for the project, and the documentation of GraalVM Native Build
Tools shall prevail.
Users need to actively use the GraalVM Reachability Metadata central repository.
The following configuration is for reference to configure additional Gradle Tasks for the project,
and the documentation of GraalVM Native Build Tools shall prevail.

```groovy
plugins {
Expand All @@ -90,27 +85,18 @@ dependencies {
}
graalvmNative {
binaries {
main {
buildArgs.add('--report-unsupported-elements-at-runtime')
}
test {
buildArgs.add('--report-unsupported-elements-at-runtime')
}
}
metadataRepository {
enabled = true
}
}
```

### For build tools such as SBT that are not supported by GraalVM Native Build Tools

Such requirements require opening additional issues at https://github.com/graalvm/native-build-tools and providing the Plugin
implementation of the corresponding build tool.
### For build tools such as sbt that are not supported by GraalVM Native Build Tools

Such requirements require opening additional issues at https://github.com/graalvm/native-build-tools
and providing the Plugin implementation of the corresponding build tool.

### Usage restrictions
## Usage restrictions

1. The following algorithm classes are not available under GraalVM Native Image due to the involvement of https://github.com/oracle/graal/issues/5522.
- `org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,9 @@ services:
- "3307:3307"
```
- 当前阶段,GraalVM Native Image 形态的 ShardingSphere Proxy 不支持使用 `InlineExpressionParser` SPI 的默认实现的 `行表达式`,
这首先导致 `数据分片` 功能的`actualDataNodes` 属性只能使用其他 `InlineExpressionParser` SPI 的实现来配置, 例如使用
`InlineExpressionParser` SPI 实现为 `LITERAL` 的 `行表达式`, 即 `<LITERAL>ds_0.t_order_0, ds_0.t_order_1`
或 `<LITERAL>ds_0.t_user_0, ds_15.t_user_1023`。

- 本节假定处于 Linux(amd64,aarch64), MacOS(amd64)或 Windows(amd64)环境。
如果你位于 MacOS(aarch64/M1) 环境,你需要关注尚未关闭的 https://github.com/oracle/graal/issues/2666 。
- `org.apache.shardingsphere:shardingsphere-cluster-mode-repository-etcd`
https://github.com/micronaut-projects/micronaut-gcp/issues/532 影响,不可使用。

## 前提条件
1. 根据 https://www.graalvm.org/downloads/ 要求安装和配置 JDK 17 对应的 `GraalVM Community Edition`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,10 @@ services:
- "3307:3307"
````

- At this stage, ShardingSphere Proxy in the form of GraalVM Native Image does not support `row expressions` using the
default implementation of the `InlineExpressionParser` SPI.
This first results in the `actualDataNodes` property of the `data sharding` feature that can only be configured using
other implementations of the `InlineExpressionParser` SPI, for example using `LITERAL` implemented
`InlineExpressionParser` SPI for `row expression`, i.e. `<LITERAL>ds_0.t_order_0, ds_0.t_order_1`
or `<LITERAL>ds_0.t_user_0, ds_15.t_user_1023`.

- This section assumes a Linux (amd64, aarch64), MacOS (amd64) or Windows (amd64) environment.
If you are on MacOS (aarch64/M1) environment, you need to follow https://github.com/oracle/graal/issues/2666 which is
not closed yet.

- `org.apache.shardingsphere:shardingsphere-cluster-mode-repository-etcd` is affected by
https://github.com/micronaut-projects/micronaut-gcp/issues/532 and cannot be used.

## Premise

1. Install and configure `GraalVM Community Edition` or a downstream distribution of `GraalVM Community Edition` for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public final class InlineExpressionParserFactory {

private static final String TYPE_NAME_END_SYMBOL = ">";

/**
* ShardingSphere has never directly supported the `GROOVY` implementation of Row Value Expression SPI under GraalVM Native Image.
* Therefore, ShardingSphere JDBC Core will directly avoid compiling `Closure` related classes under GraalVM Native Image until a new solution emerges.
*
* @see groovy.lang.Closure
*/
private static final String DEFAULT_TYPE_NAME = null == System.getProperty("org.graalvm.nativeimage.imagecode") ? "GROOVY" : "LITERAL";

/**
* Create new instance of inline expression parser by inlineExpression.
* And for compatibility reasons, inlineExpression allows to be null.
Expand All @@ -44,11 +52,11 @@ public final class InlineExpressionParserFactory {
public static InlineExpressionParser newInstance(final String inlineExpression) {
Properties props = new Properties();
if (null == inlineExpression) {
return TypedSPILoader.getService(InlineExpressionParser.class, "GROOVY", props);
return TypedSPILoader.getService(InlineExpressionParser.class, DEFAULT_TYPE_NAME, props);
}
if (!inlineExpression.startsWith(TYPE_NAME_BEGIN_SYMBOL)) {
props.setProperty(InlineExpressionParser.INLINE_EXPRESSION_KEY, inlineExpression);
return TypedSPILoader.getService(InlineExpressionParser.class, "GROOVY", props);
return TypedSPILoader.getService(InlineExpressionParser.class, DEFAULT_TYPE_NAME, props);
}
Integer typeBeginIndex = inlineExpression.indexOf(TYPE_NAME_BEGIN_SYMBOL);
Integer typeEndIndex = inlineExpression.indexOf(TYPE_NAME_END_SYMBOL);
Expand All @@ -57,7 +65,7 @@ public static InlineExpressionParser newInstance(final String inlineExpression)
}

private static String getTypeName(final String inlineExpression, final Integer beginIndex, final Integer endIndex) {
return beginIndex.equals(-1) || endIndex.equals(-1) ? "GROOVY" : inlineExpression.substring(beginIndex + 1, endIndex);
return beginIndex.equals(-1) || endIndex.equals(-1) ? DEFAULT_TYPE_NAME : inlineExpression.substring(beginIndex + 1, endIndex);
}

private static String getExprWithoutTypeName(final String inlineExpression, final Integer beginIndex, final Integer endIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
"condition":{"typeReachable":"org.codehaus.groovy.reflection.GeneratedMetaMethod$DgmMethodRecord"},
"name":"groovy.util.BufferedIterator"
},
{
"condition":{"typeReachable":"org.codehaus.groovy.reflection.GeneratedMetaMethod$DgmMethodRecord"},
"name":"java.lang.Boolean"
},
{
"condition":{"typeReachable":"org.codehaus.groovy.reflection.GeneratedMetaMethod$DgmMethodRecord"},
"name":"java.lang.Thread"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"resources":{
"includes":[{
"condition":{"typeReachable":"org.codehaus.groovy.reflection.GeneratedMetaMethod$DgmMethodRecord"},
"pattern":"\\QMETA-INF/dgminfo\\E"
}]},
"bundles":[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

Args = --report-unsupported-elements-at-runtime
11 changes: 2 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1011,10 +1011,6 @@
<id>generateMetadata</id>
<properties>
<java.version>17</java.version>
<jacoco.skip>true</jacoco.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
<checkstyle.skip>true</checkstyle.skip>
<rat.skip>true</rat.skip>
<spotless.apply.skip>true</spotless.apply.skip>
</properties>
<build>
Expand All @@ -1041,8 +1037,8 @@
<defaultMode>Conditional</defaultMode>
<modes>
<conditional>
<userCodeFilterPath>${user.dir}/test/natived/native-image-filter/user-code-filter.json</userCodeFilterPath>
<extraFilterPath>${user.dir}/test/natived/native-image-filter/extra-filter.json</extraFilterPath>
<userCodeFilterPath>${user.dir}/test/native/native-image-filter/user-code-filter.json</userCodeFilterPath>
<extraFilterPath>${user.dir}/test/native/native-image-filter/extra-filter.json</extraFilterPath>
<parallel>true</parallel>
</conditional>
</modes>
Expand Down Expand Up @@ -1098,9 +1094,6 @@
<version>${native-maven-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<buildArgs>
<arg>--report-unsupported-elements-at-runtime</arg>
</buildArgs>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<metadataRepository>
<enabled>true</enabled>
Expand Down
14 changes: 0 additions & 14 deletions test/native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
<artifactId>shardingsphere-test-native</artifactId>
<name>${project.artifactId}</name>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand All @@ -43,16 +39,6 @@
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dataSources:
rules:
- !READWRITE_SPLITTING
# We need to prevent the logical database name, writeDataSourceName and readDataSourceNames from being parsed by
# GroovyShell under Native Image for the read-write separation function.
# `groovy.lang.Closure` under Native Image for the read-write separation function.
# That is, switch to another Row Value Expression SPI implementation.
dataSources:
<LITERAL>readwrite_ds:
Expand Down
2 changes: 1 addition & 1 deletion test/native/src/test/resources/yaml/sharding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ rules:
shardingColumn: user_id
shardingAlgorithmName: inline
shardingAlgorithms:
# GroovyShell related classes are not available on GraalVM CE 23.0.2 For JDK 17.0.9,
# `groovy.lang.Closure` related classes are not available on GraalVM CE 23.0.2 For JDK 17.0.9,
# This CLASS_BASE algorithm class is designed to emulate INLINE's `ds_${user_id % 2}`.
# See https://github.com/oracle/graal/issues/5522 .
inline:
Expand Down

0 comments on commit 511ce5d

Please sign in to comment.