linghengqian commented on code in PR #33768:
URL: https://github.com/apache/shardingsphere/pull/33768#discussion_r1855150431


##########
test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/hive/HiveZookeeperServiceDiscoveryTest.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.curator.test.InstanceSpec;
+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.FixedHostPortGenericContainer;
+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.List;
+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", "deprecation"})
+@EnabledInNativeImage
+@Testcontainers
+class HiveZookeeperServiceDiscoveryTest {
+    
+    private static final int RANDOM_PORT_FIRST = InstanceSpec.getRandomPort();
+    
+    private static final Network NETWORK = Network.newNetwork();
+    
+    @Container
+    private static final GenericContainer<?> ZOOKEEPER_CONTAINER = new 
GenericContainer<>("zookeeper:3.9.3-jre-17")
+            .withNetwork(NETWORK)
+            .withNetworkAliases("foo")
+            .withExposedPorts(2181);
+    
+    /**
+     * TODO Maybe we should be able to find a better solution than {@link 
InstanceSpec#getRandomPort()} to use a random available port on the host.
+     *  It is not a good practice to use {@link FixedHostPortGenericContainer}.
+     */
+    @SuppressWarnings("unused")
+    @Container
+    private static final GenericContainer<?> HIVE_SERVER2_1_CONTAINER = new 
FixedHostPortGenericContainer<>("apache/hive:4.0.1")
+            .withNetwork(NETWORK)
+            .withEnv("SERVICE_NAME", "hiveserver2")
+            .withEnv("SERVICE_OPTS", 
"-Dhive.server2.support.dynamic.service.discovery=true" + " "
+                    + "-Dhive.zookeeper.quorum=" + 
ZOOKEEPER_CONTAINER.getNetworkAliases().get(0) + ":2181" + " "
+                    + "-Dhive.server2.thrift.bind.host=0.0.0.0" + " "
+                    + "-Dhive.server2.thrift.port=" + RANDOM_PORT_FIRST)
+            .withFixedExposedPort(RANDOM_PORT_FIRST, RANDOM_PORT_FIRST)
+            .dependsOn(ZOOKEEPER_CONTAINER);
+    
+    private static final String SYSTEM_PROP_KEY_PREFIX = 
"fixture.test-native.yaml.database.hive.zookeeper.sde.";
+    
+    // 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.sql").toAbsolutePath().normalize().toString();
+    
+    private final String jdbcUrlSuffix = 
";serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2";
+    
+    private String jdbcUrlPrefix;
+    
+    private TestShardingService testShardingService;
+    
+    @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");
+    }
+    
+    /**
+     * TODO Need to fix `shardingsphere-parser-sql-hive` module to use {@link 
TestShardingService#cleanEnvironment()}
+     *      after {@link TestShardingService#processSuccessInHive()}.
+     * TODO Same problem {@link InstanceSpec#getRandomPort()} as {@code 
HIVE_SERVER2_1_CONTAINER}.
+     *
+     * @throws SQLException An exception that provides information on a 
database access error or other errors.
+     */
+    @Test
+    void assertShardingInLocalTransactions() throws SQLException {
+        jdbcUrlPrefix = "jdbc:hive2://" + ZOOKEEPER_CONTAINER.getHost() + ":" 
+ ZOOKEEPER_CONTAINER.getMappedPort(2181) + "/";
+        DataSource dataSource = createDataSource();
+        testShardingService = new TestShardingService(dataSource);
+        testShardingService.processSuccessInHive();
+        HIVE_SERVER2_1_CONTAINER.stop();
+        int randomPortSecond = InstanceSpec.getRandomPort();
+        try (
+                GenericContainer<?> hiveServer2SecondContainer = new 
FixedHostPortGenericContainer<>("apache/hive:4.0.1")
+                        .withNetwork(NETWORK)
+                        .withEnv("SERVICE_NAME", "hiveserver2")
+                        .withEnv("SERVICE_OPTS", 
"-Dhive.server2.support.dynamic.service.discovery=true" + " "
+                                + "-Dhive.zookeeper.quorum=" + 
ZOOKEEPER_CONTAINER.getNetworkAliases().get(0) + ":2181" + " "
+                                + "-Dhive.server2.thrift.bind.host=0.0.0.0" + 
" "
+                                + "-Dhive.server2.thrift.port=" + 
randomPortSecond)
+                        .withFixedExposedPort(randomPortSecond, 
randomPortSecond)
+                        .dependsOn(ZOOKEEPER_CONTAINER)) {
+            hiveServer2SecondContainer.start();
+            
extracted(hiveServer2SecondContainer.getMappedPort(randomPortSecond));
+            testShardingService.processSuccessInHive();
+        }

Review Comment:
   - Let me investigate a bit on the testcontainers side first. See 
https://github.com/testcontainers/testcontainers-java/issues/9552 .



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to