Build GraalVM Native Image(Alpha)

    • ShardingSphere Proxy is not yet ready to integrate with GraalVM Native Image. Fixes documentation for building GraalVM Native Image It exists nightly builds at . Assuming there is a conf folder containing server.yaml as ./custom/conf, you can test it with the following docker-compose.yml file.
    • If you find that the build process has missing GraalVM Reachability Metadata, A new issue should be opened at https://github.com/oracle/graalvm-reachability-metadata, And submit a PR containing GraalVM Reachability Metadata missing from ShardingSphere itself or dependent third-party libraries.

    • The master branch of ShardingSphere is not yet ready to handle unit tests in Native Image, Need to wait for the integration of Junit 5 Platform, you always need to build GraalVM Native Image in the process, Plus -DskipNativeTests or -DskipTests parameter specific to GraalVM Native Build Tools to skip unit tests in Native Image.

    • 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 which is not closed yet.

    1. Install and configure GraalVM CE or GraalVM EE for JDK 17 according to . GraalVM CE for JDK 17 can also be installed via SDKMAN!.

    2. Install the native-image component via the GraalVM Updater tool.

    3. If you need to build a Docker Image, make sure docker-cli is in the system environment variables.

    1. Get Apache ShardingSphere Git Source
    1. Build the product on the command line, in two cases.
    • Scenario 1: No need to use JARs with SPI implementations or 3rd party dependencies

    1. ./mvnw -am -pl distribution/proxy-native -B -Pnative -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true -Dspotless.apply.skip=true -Drat.skip=true clean package
    • Scenario 2: It is necessary to use a JAR that has an SPI implementation or a third-party dependent JAR of a LICENSE such as GPL V2.

    • Add SPI implementation JARs or third-party dependent JARs to dependencies in distribution/proxy-native/pom.xml. Examples are as follows

    1. <dependencies>
    2. <dependency>
    3. <artifactId>mysql-connector-j</artifactId>
    4. <version>8.0.31</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.shardingsphere</groupId>
    8. <artifactId>shardingsphere-sql-translator-jooq-provider</artifactId>
    9. <version>5.2.0</version>
    10. </dependency>
    11. </dependencies>
    • Build GraalVM Native Image via command line.
    1. ./apache-shardingsphere-proxy-native 3307 ./custom/conf
    1. If you need to build a Docker Image, after adding the dependencies of the SPI implementation or third-party dependencies, execute the following commands on the command line.
    1. ./mvnw -am -pl distribution/proxy-native -B -Pnative,docker.native -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true -Dspotless.apply.skip=true -Drat .skip=true clean package
    • Assuming that there is a conf folder containing server.yaml as ./custom/conf, you can start the Docker Image corresponding to GraalVM Native Image through the following docker-compose.yml file.
    • If you don’t make any changes to the Git Source, the commands mentioned above will use oraclelinux:9-slim as the Base Docker Image. But if you want to use a smaller Docker Image like busybox:glic, gcr.io/distroless/base or scratch as the Base Docker Image, you need according to Add operations such as -H:+StaticExecutableWithDynamicLibC to as the native profile of pom.xml. Also note that some 3rd party dependencies will require more system libraries such as libdl to be installed in the Dockerfile. So make sure to tune distribution/proxy-native according to your usage pom.xml and Dockerfile below.

    Observability

    • You can observe GraalVM Native Image using a series of command line tools or visualization tools available at and use VSCode to debug it according to its requirements. If you are using IntelliJ IDEA and want to debug the generated GraalVM Native Image, You can follow https://blog.jetbrains.com/idea/2022/06/intellij-idea-2022-2-eap-5/#Experimental_GraalVM_Native_Debugger_for_Java and its successors.

    • In the case of using APM Java Agent such as ShardingSphere Agent, GraalVM’s native-image component is not yet fully supported when building Native Images javaagent, you need to follow which has not been closed.

    • The following sections use the Apache SkyWalking Java Agent as an example, which can be used to track corresponding issues from the GraalVM community.

    1. Download https://dlcdn.apache.org/skywalking/java-agent/8.12.0/apache-skywalking-java-agent-8.12.0.tgz and untar it to distribution/proxy-native in ShardingSphere Git Source.

    2. Modify the native profile of distribution/proxy-native/pom.xml, Add the following jvmArgs to the configuration of org.graalvm.buildtools:native-maven-plugin.

    1. <jvmArgs>
    2. <arg>-Dskywalking.agent.service_name="your service name"</arg>
    3. <arg>-Dskywalking.collector.backend_service="your skywalking oap ip and port"</arg>
    4. <arg>-javaagent:./skywalking-agent/skywalking-agent.jar</arg>
    5. </jvmArgs>