-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes the issue that ShardingSphere cannot connect to HiveServer2 usi…
…ng remote Hive Metastore Server
- Loading branch information
1 parent
4e5b0ce
commit 22ae9c1
Showing
7 changed files
with
298 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...a/org/apache/shardingsphere/test/natived/jdbc/databases/hive/StandaloneMetastoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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.hive; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.apache.shardingsphere.test.natived.commons.TestShardingService; | ||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledInNativeImage; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import javax.sql.DataSource; | ||
import java.nio.file.Paths; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.time.Duration; | ||
import java.util.Properties; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection", "resource"}) | ||
@EnabledInNativeImage | ||
@Testcontainers | ||
class StandaloneMetastoreTest { | ||
|
||
private static final Network NETWORK = Network.newNetwork(); | ||
|
||
@Container | ||
public static final GenericContainer<?> HMS_CONTAINER = new GenericContainer<>("apache/hive:4.0.1") | ||
.withEnv("SERVICE_NAME", "metastore") | ||
.withNetwork(NETWORK) | ||
.withNetworkAliases("metastore"); | ||
|
||
@Container | ||
public static final GenericContainer<?> HS2_CONTAINER = new GenericContainer<>("apache/hive:4.0.1") | ||
.withEnv("SERVICE_NAME", "hiveserver2") | ||
.withEnv("SERVICE_OPTS", "-Dhive.metastore.uris=thrift://metastore:9083") | ||
.withNetwork(NETWORK) | ||
.withExposedPorts(10000) | ||
.dependsOn(HMS_CONTAINER); | ||
|
||
private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.database.hive.hms."; | ||
|
||
// Due to https://issues.apache.org/jira/browse/HIVE-28317 , the `initFile` parameter of HiveServer2 JDBC Driver must be an absolute path. | ||
private static final String ABSOLUTE_PATH = Paths.get("src/test/resources/test-native/sql/test-native-databases-hive-iceberg.sql").toAbsolutePath().toString(); | ||
|
||
private String jdbcUrlPrefix; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"), is(nullValue())); | ||
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"), is(nullValue())); | ||
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"), is(nullValue())); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
NETWORK.close(); | ||
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"); | ||
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"); | ||
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"); | ||
} | ||
|
||
@Test | ||
void assertShardingInLocalTransactions() throws SQLException { | ||
jdbcUrlPrefix = "jdbc:hive2://localhost:" + HS2_CONTAINER.getMappedPort(10000) + "/"; | ||
DataSource dataSource = createDataSource(); | ||
TestShardingService testShardingService = new TestShardingService(dataSource); | ||
testShardingService.processSuccessInHive(); | ||
} | ||
|
||
private Connection openConnection() throws SQLException { | ||
Properties props = new Properties(); | ||
return DriverManager.getConnection(jdbcUrlPrefix, props); | ||
} | ||
|
||
private DataSource createDataSource() throws SQLException { | ||
Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptions().until(() -> { | ||
openConnection().close(); | ||
return true; | ||
}); | ||
try ( | ||
Connection connection = openConnection(); | ||
Statement statement = connection.createStatement()) { | ||
statement.executeUpdate("CREATE DATABASE demo_ds_0"); | ||
statement.executeUpdate("CREATE DATABASE demo_ds_1"); | ||
statement.executeUpdate("CREATE DATABASE demo_ds_2"); | ||
} | ||
HikariConfig config = new HikariConfig(); | ||
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver"); | ||
config.setJdbcUrl("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/hive/standalone-hms.yaml?placeholder-type=system_props"); | ||
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0" + ";initFile=" + ABSOLUTE_PATH); | ||
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1" + ";initFile=" + ABSOLUTE_PATH); | ||
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2" + ";initFile=" + ABSOLUTE_PATH); | ||
return new HikariDataSource(config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
test/native/src/test/resources/test-native/yaml/jdbc/databases/hive/standalone-hms.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
dataSources: | ||
ds_0: | ||
dataSourceClassName: com.zaxxer.hikari.HikariDataSource | ||
driverClassName: org.apache.hive.jdbc.HiveDriver | ||
jdbcUrl: $${fixture.test-native.yaml.database.hive.hms.ds0.jdbc-url::} | ||
ds_1: | ||
dataSourceClassName: com.zaxxer.hikari.HikariDataSource | ||
driverClassName: org.apache.hive.jdbc.HiveDriver | ||
jdbcUrl: $${fixture.test-native.yaml.database.hive.hms.ds1.jdbc-url::} | ||
ds_2: | ||
dataSourceClassName: com.zaxxer.hikari.HikariDataSource | ||
driverClassName: org.apache.hive.jdbc.HiveDriver | ||
jdbcUrl: $${fixture.test-native.yaml.database.hive.hms.ds2.jdbc-url::} | ||
|
||
rules: | ||
- !SHARDING | ||
tables: | ||
t_order: | ||
actualDataNodes: <LITERAL>ds_0.t_order, ds_1.t_order, ds_2.t_order | ||
keyGenerateStrategy: | ||
column: order_id | ||
keyGeneratorName: snowflake | ||
t_order_item: | ||
actualDataNodes: <LITERAL>ds_0.t_order_item, ds_1.t_order_item, ds_2.t_order_item | ||
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.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture | ||
keyGenerators: | ||
snowflake: | ||
type: SNOWFLAKE | ||
auditors: | ||
sharding_key_required_auditor: | ||
type: DML_SHARDING_CONDITIONS | ||
|
||
- !BROADCAST | ||
tables: | ||
- t_address |