Skip to content

Commit

Permalink
Add GraalVM Reachability Metadata and corresponding nativeTest for Zo…
Browse files Browse the repository at this point in the history
…okeeper integration (#29425)
  • Loading branch information
linghengqian authored Dec 20, 2023
1 parent 41eaef7 commit 4fd6850
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 209 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"condition":{"typeReachable":"org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader"},
"pattern":".*sql/.+\\.xml$"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.driver.ShardingSphereDriver"},
"condition":{"typeReachable":"org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager"},
"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.NewClusterContextManagerBuilder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.authority.spi.AuthorityRegistryProvider\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader"},
Expand Down Expand Up @@ -64,7 +64,7 @@
"condition":{"typeReachable":"org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.NewClusterContextManagerBuilder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry"},
Expand Down Expand Up @@ -102,9 +102,21 @@
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.manager.ContextManagerBuilder\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcherFactory"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.NewGovernanceWatcher\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.NewClusterContextManagerBuilder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.mode.repository.cluster.lock.holder.DistributedLockHolder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.creator.DistributedLockCreator\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.mode.metadata.builder.RuleConfigurationEventBuilder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.mode.spi.RuleNodePathProvider\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.readwritesplitting.route.standard.StandardReadwriteSplittingDataSourceRouter"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.readwritesplitting.route.standard.filter.ReadDataSourcesFilter\\E"
Expand All @@ -127,7 +139,7 @@
"condition":{"typeReachable":"org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.sqltranslator.spi.SQLTranslator\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader"},
"condition":{"typeReachable":"org.apache.shardingsphere.mode.manager.cluster.NewClusterContextManagerBuilder"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.timeservice.spi.TimestampService\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader"},
Expand Down
6 changes: 3 additions & 3 deletions test/native/native-image-filter/extra-filter.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
{"excludeClasses": "com.github.dockerjava.api.**"},
{"excludeClasses": "com.google.common.util.concurrent.**"},
{"excludeClasses": "com.ibm.icu.text.**"},
{"excludeClasses": "com.microsoft.sqlserver.jdbc.**"},
{"excludeClasses": "com.mysql.cj.**"},
{"excludeClasses": "com.zaxxer.hikari.**"},
{"excludeClasses": "javax.xml.**"},
{"excludeClasses": "groovy.**"},
{"excludeClasses": "org.apache.calcite.**"},
{"excludeClasses": "org.apache.zookeeper.**"},
{"excludeClasses": "org.codehaus.groovy.**"},
{"excludeClasses": "org.h2.**"},
{"excludeClasses": "org.locationtech.jts.geom.**"},
{"excludeClasses": "org.opengauss.**"},
{"excludeClasses": "org.postgresql.**"},
{"excludeClasses": "org.slf4j.event.**"},
{"excludeClasses": "org.testcontainers.**"},

{"excludeClasses": "org.apache.shardingsphere.test.natived.**"}
{"excludeClasses": "org.testcontainers.**"}
],
"regexRules": [
]
Expand Down
3 changes: 2 additions & 1 deletion test/native/native-image-filter/user-code-filter.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"rules": [
{"excludeClasses": "**"},
{"includeClasses": "org.apache.shardingsphere.**"}
{"includeClasses": "org.apache.shardingsphere.**"},
{"excludeClasses": "org.apache.shardingsphere.test.natived.**"}
]
}
7 changes: 4 additions & 3 deletions test/native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-cluster-mode-repository-zookeeper</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,33 @@

package org.apache.shardingsphere.test.natived.jdbc.databases;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService;
import org.apache.shardingsphere.test.natived.jdbc.commons.FileTestUtils;
import org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import java.util.Properties;

class MySQLTest {
/**
* Unable to use `org.testcontainers:mysql:1.19.3` under GraalVM Native Image.
* Background comes from <a href="https://github.com/testcontainers/testcontainers-java/issues/7954">testcontainers/testcontainers-java#7954</a>.
*/
public class MySQLTest {

private static final String USERNAME = "root";

Expand All @@ -44,20 +53,25 @@ class MySQLTest {

private static final String JDBC_URL = "jdbc:mysql://localhost:65107/" + DATABASE;

private static Process process;

private TestShardingService testShardingService;

@SuppressWarnings("resource")
@Test
@EnabledInNativeImage
void assertShardingInLocalTransactions() throws SQLException, IOException {
beforeAll();
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("test-native/yaml/databases/mysql.yaml"));
testShardingService = new TestShardingService(dataSource);
this.initEnvironment();
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
tearDown();
try (
GenericContainer<?> mySQLContainer = new GenericContainer<>(DockerImageName.parse("mysql:8.2.0-oracle"))
.withEnv("MYSQL_DATABASE", DATABASE)
.withEnv("MYSQL_ROOT_PASSWORD", PASSWORD)
.withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(65107), new ExposedPort(3306)))))) {
mySQLContainer.start();
beforeAll();
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("test-native/yaml/databases/mysql.yaml"));
testShardingService = new TestShardingService(dataSource);
this.initEnvironment();
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand All @@ -77,14 +91,7 @@ private static Connection openConnection() throws SQLException {
}

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
private static void beforeAll() throws IOException {
System.out.println("Starting MySQL ...");
process = new ProcessBuilder(
"docker", "run", "--rm", "-p", "65107:3306", "-e", "MYSQL_DATABASE=" + DATABASE,
"-e", "MYSQL_ROOT_PASSWORD=" + PASSWORD, "mysql:8.2.0-oracle")
.redirectOutput(new File("target/mysql-stdout.txt"))
.redirectError(new File("target/mysql-stderr.txt"))
.start();
private void beforeAll() {
Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException)
.until(() -> {
openConnection().close();
Expand All @@ -97,13 +104,5 @@ private static void beforeAll() throws IOException {
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println("MySQL started");
}

private static void tearDown() {
if (null != process && process.isAlive()) {
System.out.println("Shutting down MySQL");
process.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@

package org.apache.shardingsphere.test.natived.jdbc.databases;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService;
import org.apache.shardingsphere.test.natived.jdbc.commons.FileTestUtils;
import org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
Expand All @@ -43,20 +48,24 @@ class OpenGaussTest {

private static final String JDBC_URL = "jdbc:opengauss://localhost:62390/" + DATABASE;

private static Process process;

private TestShardingService testShardingService;

@SuppressWarnings("resource")
@Test
@EnabledInNativeImage
void assertShardingInLocalTransactions() throws SQLException, IOException {
beforeAll();
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("test-native/yaml/databases/opengauss.yaml"));
testShardingService = new TestShardingService(dataSource);
this.initEnvironment();
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
tearDown();
try (
GenericContainer<?> openGaussContainer = new GenericContainer<>(DockerImageName.parse("opengauss/opengauss:5.0.0"))
.withEnv("GS_PASSWORD", PASSWORD)
.withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(62390), new ExposedPort(5432)))))) {
openGaussContainer.start();
beforeAll();
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("test-native/yaml/databases/opengauss.yaml"));
testShardingService = new TestShardingService(dataSource);
this.initEnvironment();
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand All @@ -76,14 +85,7 @@ private static Connection openConnection() throws SQLException {
}

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
private static void beforeAll() throws IOException {
System.out.println("Starting OpenGauss ...");
process = new ProcessBuilder(
"docker", "run", "--rm", "-p", "62390:5432", "-e", "GS_PASSWORD=" + PASSWORD,
"opengauss/opengauss:5.0.0")
.redirectOutput(new File("target/opengauss-stdout.txt"))
.redirectError(new File("target/opengauss-stderr.txt"))
.start();
private void beforeAll() {
Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptions().until(() -> {
openConnection().close();
return true;
Expand All @@ -95,13 +97,5 @@ private static void beforeAll() throws IOException {
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println("OpenGauss started");
}

private static void tearDown() {
if (null != process && process.isAlive()) {
System.out.println("Shutting down OpenGauss");
process.destroy();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.mode.cluster;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.test.natived.jdbc.commons.FileTestUtils;
import org.apache.shardingsphere.test.natived.jdbc.commons.TestShardingService;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class ZookeeperTest {

private TestShardingService testShardingService;

@SuppressWarnings("resource")
@Test
@EnabledInNativeImage
void assertShardingInLocalTransactions() throws SQLException, IOException {
try (
GenericContainer<?> zookeeperContainer = new GenericContainer<>(DockerImageName.parse("zookeeper:3.9.1-jre-17"))
.withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(62372), new ExposedPort(2181)))))) {
zookeeperContainer.start();
this.beforeAll();
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("test-native/yaml/mode/cluster/zookeeper.yaml"));
testShardingService = new TestShardingService(dataSource);
this.initEnvironment();
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
testShardingService.getOrderRepository().createTableIfNotExistsInMySQL();
testShardingService.getOrderItemRepository().createTableIfNotExistsInMySQL();
testShardingService.getAddressRepository().createTableIfNotExists();
testShardingService.getOrderRepository().truncateTable();
testShardingService.getOrderItemRepository().truncateTable();
testShardingService.getAddressRepository().truncateTable();
}

private void beforeAll() {
Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptions().until(() -> {
CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:" + 62372, new ExponentialBackoffRetry(1000, 3));
client.start();
boolean connected = client.blockUntilConnected(5, TimeUnit.SECONDS);
client.close();
return connected;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.features.ShardingTest"},
"pattern":"\\Qtest-native/yaml/features/sharding.yaml\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.test.natived.jdbc.mode.cluster.ZookeeperTest"},
"pattern":"\\Qtest-native/yaml/mode/cluster/zookeeper.yaml\\E"
}]},
"bundles":[]
}
Loading

0 comments on commit 4fd6850

Please sign in to comment.