Skip to content

Commit

Permalink
Add GraalVM Reachability Metadata and corresponding nativeTest for Hive
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Jun 5, 2024
1 parent feb2de8 commit a350788
Show file tree
Hide file tree
Showing 23 changed files with 484 additions and 112 deletions.
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
<opengauss.version>3.1.0-og</opengauss.version>
<mariadb-java-client.version>2.4.2</mariadb-java-client.version>
<clickhouse-jdbc.version>0.6.0-patch5</clickhouse-jdbc.version>
<hive.version>4.0.0</hive.version>
<hadoop.version>3.3.6</hadoop.version>

<hikari-cp.version>4.0.3</hikari-cp.version>

Expand Down Expand Up @@ -479,6 +481,24 @@
<classifier>http</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>${hive.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client-runtime</artifactId>
<version>${hadoop.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
Expand Down Expand Up @@ -970,7 +990,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED</argLine>
<!-- For `org.apache.hive:hive-common:4.0.0`-->
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED</argLine>
</configuration>
</plugin>
</plugins>
Expand Down
31 changes: 31 additions & 0 deletions test/native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ using Seata Client under GraalVM Native Image requires using the version release
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-parser-sql-hive</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
Expand Down Expand Up @@ -155,6 +161,31 @@ using Seata Client under GraalVM Native Image requires using the version release
<classifier>http</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client-runtime</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.sql.DataSource;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -38,6 +39,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

@Getter
public final class TestShardingService {
Expand All @@ -60,7 +62,7 @@ public TestShardingService(final DataSource dataSource) {
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public void processSuccess() throws SQLException {
final Collection<Long> orderIds = insertData();
final Collection<Long> orderIds = insertData(Statement.RETURN_GENERATED_KEYS);
Collection<Order> orders = orderRepository.selectAll();
assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()),
equalTo(Arrays.asList(1, 1, 1, 1, 1, 0, 0, 0, 0, 0)));
Expand Down Expand Up @@ -89,12 +91,12 @@ public void processSuccess() throws SQLException {
/**
* Process success in ClickHouse.
* ClickHouse has not fully supported transactions. Refer to <a href="https://github.com/ClickHouse/clickhouse-docs/issues/2300">ClickHouse/clickhouse-docs#2300</a>.
* So ShardingSphere should not use {@code orderItemRepository.assertRollbackWithTransactions()} in the method here.
* So ShardingSphere should not use {@link OrderItemRepository#assertRollbackWithTransactions()} in the method here.
*
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public void processSuccessInClickHouse() throws SQLException {
Collection<Long> orderIds = insertDataInClickHouse();
Collection<Long> orderIds = insertData(Statement.NO_GENERATED_KEYS);
assertThat(orderIds, notNullValue());
Collection<Order> orders = orderRepository.selectAll();
assertThat(orders.stream().map(Order::getOrderType).collect(Collectors.toList()),
Expand All @@ -121,61 +123,71 @@ public void processSuccessInClickHouse() throws SQLException {
}

/**
* Insert data.
* Process success in Hive.
* Hive has not fully supported BEGIN, COMMIT, and ROLLBACK. Refer to <a href="https://cwiki.apache.org/confluence/display/Hive/Hive+Transactions">Hive Transactions</a>.
* So ShardingSphere should not use {@link OrderItemRepository#assertRollbackWithTransactions()}
* TODO It seems that there is no way to force all HiveServer2 insert statements to complete. This results in the following assertion always failing, which needs to be investigated on the apache/hive side.
* <pre class="code">
* insertDataInHive();
* assertThat(addressRepository.selectAll(),
* equalTo(LongStream.range(1L, 11L).mapToObj(each -> new Address(each, "address_test_" + each)).collect(Collectors.toList())));
* </pre>
*
* @return orderId of the insert statement.
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public Collection<Long> insertData() throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
order.setOrderType(i % 2);
order.setAddressId(i);
order.setStatus("INSERT_TEST");
orderRepository.insert(order);
OrderItem orderItem = new OrderItem();
orderItem.setOrderId(order.getOrderId());
orderItem.setUserId(i);
orderItem.setPhone("13800000001");
orderItem.setStatus("INSERT_TEST");
orderItemRepository.insert(orderItem);
Address address = new Address((long) i, "address_test_" + i);
addressRepository.insert(address);
result.add(order.getOrderId());
}
return result;
public void processSuccessInHive() throws SQLException {
insertDataInHive();
assertThat(addressRepository.selectAll().size(), greaterThanOrEqualTo(1));
deleteDataInHive();
assertThat(addressRepository.selectAll(), equalTo(Collections.emptyList()));
}

/**
* Insert data in ClickHouse.
* Insert data.
*
* @param autoGeneratedKeys a flag indicating whether auto-generated keys
* should be returned; one of
* {@code Statement.RETURN_GENERATED_KEYS} or
* {@code Statement.NO_GENERATED_KEYS}
* @return orderId of the insert statement.
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public Collection<Long> insertDataInClickHouse() throws SQLException {
public Collection<Long> insertData(final int autoGeneratedKeys) throws SQLException {
Collection<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
order.setUserId(i);
order.setOrderType(i % 2);
order.setAddressId(i);
order.setStatus("INSERT_TEST");
orderRepository.insertWithoutAutoGeneratedKey(order);
orderRepository.insert(order, autoGeneratedKeys);
OrderItem orderItem = new OrderItem();
orderItem.setOrderId(order.getOrderId());
orderItem.setUserId(i);
orderItem.setPhone("13800000001");
orderItem.setStatus("INSERT_TEST");
orderItemRepository.insertWithoutAutoGeneratedKey(orderItem);
orderItemRepository.insert(orderItem, autoGeneratedKeys);
Address address = new Address((long) i, "address_test_" + i);
addressRepository.insert(address);
result.add(order.getOrderId());
}
return result;
}

/**
* Insert data in Hive.
*/
public void insertDataInHive() {
LongStream.range(1L, 11L).forEach(action -> {
Address address = new Address(action, "address_test_" + action);
try {
addressRepository.insert(address);
} catch (final SQLException ex) {
throw new RuntimeException(ex);
}
});
}

/**
* Delete data.
*
Expand Down Expand Up @@ -206,6 +218,18 @@ public void deleteDataInClickHouse(final Collection<Long> orderIds) throws SQLEx
}
}

/**
* Delete data in Hive.
*
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public void deleteDataInHive() throws SQLException {
long count = 1L;
for (int i = 1; i <= 10; i++) {
addressRepository.deleteInHive(count++);
}
}

/**
* Clean environment.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public AddressRepository(final DataSource dataSource) {
}

/**
* create table t_address if not exists.
* create table t_address if not exists in MySQL.
*
* @throws SQLException SQL exception
*/
public void createTableIfNotExists() throws SQLException {
public void createTableIfNotExistsInMySQL() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS t_address (address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT NULL, PRIMARY KEY (address_id))";
try (
Connection connection = dataSource.getConnection();
Expand All @@ -62,7 +62,26 @@ public void createTableInSQLServer() throws SQLException {
+ " 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);
}
}

/**
* create table t_address if not exists in Hive.
*
* @throws SQLException SQL exception
*/
public void createTableIfNotExistsInHive() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS t_address\n"
+ "(\n"
+ " address_id BIGINT NOT NULL,\n"
+ " address_name VARCHAR(100) NOT NULL,\n"
+ " PRIMARY KEY (address_id) disable novalidate\n"
+ ") CLUSTERED BY (address_id) INTO 2 BUCKETS STORED AS ORC TBLPROPERTIES ('transactional'='true')";
try (
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
Expand Down Expand Up @@ -133,6 +152,22 @@ public void delete(final Long id) throws SQLException {
}
}

/**
* delete by id.
*
* @param id id
* @throws SQLException SQL exception
*/
public void deleteInHive(final Long id) throws SQLException {
String sql = "DELETE FROM t_address WHERE address_id=?";
try (
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, id);
preparedStatement.executeUpdate();
}
}

/**
* delete by id in ClickHouse.
*
Expand Down
Loading

0 comments on commit a350788

Please sign in to comment.