From 7c0c6058de51a30311723f1385f1d8812723ff4f Mon Sep 17 00:00:00 2001 From: yuanfuyuan <1406957364@qq.com> Date: Tue, 10 Oct 2023 22:48:13 +0800 Subject: [PATCH 01/28] fix_4186 --- .../src/main/scala/org/apache/spark/kyuubi/StageStatus.scala | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala index 29644f9f4c7..9e30c69e78f 100644 --- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala +++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala @@ -31,3 +31,8 @@ class SparkStageInfo(val stageId: Int, val numTasks: Int) { class SparkJobInfo(val numStages: Int, val stageIds: Set[Int]) { val numCompleteStages = new AtomicInteger(0) } + + +class SparkJobInfo(val numStages: Int, val stageIds: Seq[Int]) { + var numCompleteStages = new AtomicInteger(0) +} \ No newline at end of file From 1892f148d927197beba9442ffa73549f4d87e881 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Thu, 26 Oct 2023 10:46:07 +0800 Subject: [PATCH 02/28] add common method to get session level config --- .../src/main/scala/org/apache/spark/kyuubi/StageStatus.scala | 5 ----- 1 file changed, 5 deletions(-) diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala index 9e30c69e78f..29644f9f4c7 100644 --- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala +++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/spark/kyuubi/StageStatus.scala @@ -31,8 +31,3 @@ class SparkStageInfo(val stageId: Int, val numTasks: Int) { class SparkJobInfo(val numStages: Int, val stageIds: Set[Int]) { val numCompleteStages = new AtomicInteger(0) } - - -class SparkJobInfo(val numStages: Int, val stageIds: Seq[Int]) { - var numCompleteStages = new AtomicInteger(0) -} \ No newline at end of file From 6a0445357a1633bb325d0f63fb2cf009892a84e4 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Wed, 27 Mar 2024 17:05:27 +0800 Subject: [PATCH 03/28] Kyuubi Server HA&ZK get server from serverHosts support more strategy --- .../jdbc/hive/ZooKeeperHiveClientHelper.java | 12 ++++- .../hive/strategy/ChooseServerStrategy.java | 10 +++++ .../jdbc/hive/strategy/StrategyFactory.java | 30 +++++++++++++ .../strategy/zk/PollingChooseStrategy.java | 45 +++++++++++++++++++ .../strategy/zk/RandomChooseStrategy.java | 14 ++++++ 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java create mode 100644 kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java create mode 100644 kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java create mode 100644 kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 948fd333463..3844c20b0d9 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -22,9 +22,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.concurrent.ThreadLocalRandom; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.StrategyFactory; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.CuratorFrameworkFactory; import org.apache.kyuubi.shaded.curator.retry.ExponentialBackoffRetry; @@ -111,7 +112,7 @@ static void configureConnParams(JdbcConnectionParams connParams) try (CuratorFramework zooKeeperClient = getZkClient(connParams)) { List serverHosts = getServerHosts(connParams, zooKeeperClient); // Now pick a server node randomly - String serverNode = serverHosts.get(ThreadLocalRandom.current().nextInt(serverHosts.size())); + String serverNode = chooseServer(connParams,serverHosts,zooKeeperClient); updateParamsWithZKServerNode(connParams, zooKeeperClient, serverNode); } catch (Exception e) { throw new ZooKeeperHiveClientException( @@ -120,6 +121,13 @@ static void configureConnParams(JdbcConnectionParams connParams) // Close the client connection with ZooKeeper } + private static String chooseServer(JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) throws ZooKeeperHiveClientException { + String zooKeeperNamespace = getZooKeeperNamespace(connParams); + String zkStrategy = connParams.getSessionVars().getOrDefault(JdbcConnectionParams.ZOOKEEPER_STRATEGY, "random"); + ChooseServerStrategy chooseServerStrategy = StrategyFactory.createStrategy(zkStrategy); + return chooseServerStrategy.chooseServer(serverHosts,zkClient,zooKeeperNamespace); + } + static List getDirectParamsList(JdbcConnectionParams connParams) throws ZooKeeperHiveClientException { try (CuratorFramework zooKeeperClient = getZkClient(connParams)) { diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java new file mode 100644 index 00000000000..3b8451c3162 --- /dev/null +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java @@ -0,0 +1,10 @@ +package org.apache.kyuubi.jdbc.hive.strategy; + +import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; +import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; + +import java.util.List; + +public interface ChooseServerStrategy { + String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace)throws ZooKeeperHiveClientException; +} diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java new file mode 100644 index 00000000000..36c9a1a87fb --- /dev/null +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -0,0 +1,30 @@ +package org.apache.kyuubi.jdbc.hive.strategy; + +import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; +import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingChooseStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomChooseStrategy; + +import java.lang.reflect.Constructor; + +public class StrategyFactory { + public static ChooseServerStrategy createStrategy(String strategy) throws ZooKeeperHiveClientException { + try { + switch (strategy) { + case "poll": + return new PollingChooseStrategy(); + case "random": + return new RandomChooseStrategy(); + default: + Class clazz = Class.forName(strategy); + if (ChooseServerStrategy.class.isAssignableFrom(clazz)) { + Constructor constructor = clazz.asSubclass(ChooseServerStrategy.class).getConstructor(); + return constructor.newInstance(); + } else { + throw new ZooKeeperHiveClientException("The loaded class does not implement ChooseServerStrategy"); + } + } + }catch (Exception e){ + throw new ZooKeeperHiveClientException("Oops, load the chooseStrategy is wrong, please check your connection params"); + } + } +} diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java new file mode 100644 index 00000000000..fbda48dd9c1 --- /dev/null +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -0,0 +1,45 @@ +package org.apache.kyuubi.jdbc.hive.strategy.zk; + + +import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; +import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; +import org.apache.kyuubi.shaded.curator.framework.recipes.locks.InterProcessSemaphoreMutex; +import org.apache.kyuubi.shaded.zookeeper.CreateMode; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class PollingChooseStrategy implements ChooseServerStrategy { + private static final String COUNTER_PATH_PREFIX = "/"; + private static final String COUNTER_PATH_SUFFIX = "-counter"; + private static final int COUNTER_RESET_VALUE = 1000; + @Override + public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) throws ZooKeeperHiveClientException { + String counter_path = COUNTER_PATH_PREFIX+namespace+COUNTER_PATH_SUFFIX; + InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient,counter_path); + try { + if (zkClient.checkExists().forPath(counter_path) == null ){ + zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(counter_path, "1".getBytes(StandardCharsets.UTF_8)); + } + if (!lock.acquire(60,TimeUnit.SECONDS)) { + return null; + } + byte[] data = zkClient.getData().forPath(counter_path); + String dataStr = new String(data, StandardCharsets.UTF_8); + int counter = Integer.parseInt(dataStr); + String server = serverHosts.get(counter % serverHosts.size()); + counter = (counter + 1) % COUNTER_RESET_VALUE; // 避免计数器溢出 + zkClient.setData().forPath(counter_path, (counter + "").getBytes(StandardCharsets.UTF_8)); + return server; + } catch (Exception e) { + throw new ZooKeeperHiveClientException("Oops, PollingChooseStrategy get the server is wrong!",e); + } finally { + try { + lock.release(); + } catch (Exception e) { + throw new ZooKeeperHiveClientException("Oops,PollingChooseStrategy releasing lock is wrong!",e); + } + } + } +} diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java new file mode 100644 index 00000000000..2ac86acc628 --- /dev/null +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java @@ -0,0 +1,14 @@ +package org.apache.kyuubi.jdbc.hive.strategy.zk; + +import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; + +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +public class RandomChooseStrategy implements ChooseServerStrategy { + @Override + public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) { + return serverHosts.get(ThreadLocalRandom.current().nextInt(serverHosts.size())); + } +} From ee5a9ad6841c18b6d2085873b9c96f17d39135bc Mon Sep 17 00:00:00 2001 From: davidyuan Date: Wed, 27 Mar 2024 17:14:15 +0800 Subject: [PATCH 04/28] Kyuubi Server HA&ZK get server from serverHosts support more strategy --- .../java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java | 1 + 1 file changed, 1 insertion(+) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java index b3884c694fd..759e34468b2 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java @@ -79,6 +79,7 @@ public class JdbcConnectionParams { // Use ZooKeeper for indirection while using dynamic service discovery static final String SERVICE_DISCOVERY_MODE_ZOOKEEPER = "zooKeeper"; static final String ZOOKEEPER_NAMESPACE = "zooKeeperNamespace"; + static final String ZOOKEEPER_STRATEGY = "zooKeeperStrategy"; // Default namespace value on ZooKeeper. // This value is used if the param "zooKeeperNamespace" is not specified in the JDBC Uri. static final String ZOOKEEPER_DEFAULT_NAMESPACE = "hiveserver2"; From 97b959776d1552fbd7d713e796c37e52e30b4ebf Mon Sep 17 00:00:00 2001 From: davidyuan Date: Wed, 27 Mar 2024 17:32:38 +0800 Subject: [PATCH 05/28] Kyuubi Server HA&ZK get server from serverHosts support more strategy --- .../hive/strategy/ChooseServerStrategy.java | 17 +++++++++++++++++ .../jdbc/hive/strategy/StrategyFactory.java | 17 +++++++++++++++++ .../hive/strategy/zk/PollingChooseStrategy.java | 17 +++++++++++++++++ .../hive/strategy/zk/RandomChooseStrategy.java | 17 +++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java index 3b8451c3162..4c5bf712571 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java @@ -1,3 +1,20 @@ +/* + * 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.kyuubi.jdbc.hive.strategy; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java index 36c9a1a87fb..0f956cfbc52 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -1,3 +1,20 @@ +/* + * 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.kyuubi.jdbc.hive.strategy; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index fbda48dd9c1..c035332f212 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -1,3 +1,20 @@ +/* + * 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.kyuubi.jdbc.hive.strategy.zk; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java index 2ac86acc628..7f0e31198f0 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java @@ -1,3 +1,20 @@ +/* + * 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.kyuubi.jdbc.hive.strategy.zk; import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; From 73952f878b54a8065f9e947090dadca710fd4de1 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Wed, 27 Mar 2024 17:43:14 +0800 Subject: [PATCH 06/28] Kyuubi Server HA&ZK get server from serverHosts support more strategy --- .../jdbc/hive/ZooKeeperHiveClientHelper.java | 11 ++- .../hive/strategy/ChooseServerStrategy.java | 6 +- .../jdbc/hive/strategy/StrategyFactory.java | 45 +++++------ .../strategy/zk/PollingChooseStrategy.java | 75 ++++++++++--------- .../strategy/zk/RandomChooseStrategy.java | 14 ++-- 5 files changed, 82 insertions(+), 69 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 3844c20b0d9..5d0d9e7ab67 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -112,7 +112,7 @@ static void configureConnParams(JdbcConnectionParams connParams) try (CuratorFramework zooKeeperClient = getZkClient(connParams)) { List serverHosts = getServerHosts(connParams, zooKeeperClient); // Now pick a server node randomly - String serverNode = chooseServer(connParams,serverHosts,zooKeeperClient); + String serverNode = chooseServer(connParams, serverHosts, zooKeeperClient); updateParamsWithZKServerNode(connParams, zooKeeperClient, serverNode); } catch (Exception e) { throw new ZooKeeperHiveClientException( @@ -121,11 +121,14 @@ static void configureConnParams(JdbcConnectionParams connParams) // Close the client connection with ZooKeeper } - private static String chooseServer(JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) throws ZooKeeperHiveClientException { + private static String chooseServer( + JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) + throws ZooKeeperHiveClientException { String zooKeeperNamespace = getZooKeeperNamespace(connParams); - String zkStrategy = connParams.getSessionVars().getOrDefault(JdbcConnectionParams.ZOOKEEPER_STRATEGY, "random"); + String zkStrategy = + connParams.getSessionVars().getOrDefault(JdbcConnectionParams.ZOOKEEPER_STRATEGY, "random"); ChooseServerStrategy chooseServerStrategy = StrategyFactory.createStrategy(zkStrategy); - return chooseServerStrategy.chooseServer(serverHosts,zkClient,zooKeeperNamespace); + return chooseServerStrategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } static List getDirectParamsList(JdbcConnectionParams connParams) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java index 4c5bf712571..cfb2fa190e4 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java @@ -17,11 +17,11 @@ package org.apache.kyuubi.jdbc.hive.strategy; +import java.util.List; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; -import java.util.List; - public interface ChooseServerStrategy { - String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace)throws ZooKeeperHiveClientException; + String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) + throws ZooKeeperHiveClientException; } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java index 0f956cfbc52..df1a01b359e 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -17,31 +17,34 @@ package org.apache.kyuubi.jdbc.hive.strategy; +import java.lang.reflect.Constructor; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingChooseStrategy; import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomChooseStrategy; -import java.lang.reflect.Constructor; - public class StrategyFactory { - public static ChooseServerStrategy createStrategy(String strategy) throws ZooKeeperHiveClientException { - try { - switch (strategy) { - case "poll": - return new PollingChooseStrategy(); - case "random": - return new RandomChooseStrategy(); - default: - Class clazz = Class.forName(strategy); - if (ChooseServerStrategy.class.isAssignableFrom(clazz)) { - Constructor constructor = clazz.asSubclass(ChooseServerStrategy.class).getConstructor(); - return constructor.newInstance(); - } else { - throw new ZooKeeperHiveClientException("The loaded class does not implement ChooseServerStrategy"); - } - } - }catch (Exception e){ - throw new ZooKeeperHiveClientException("Oops, load the chooseStrategy is wrong, please check your connection params"); - } + public static ChooseServerStrategy createStrategy(String strategy) + throws ZooKeeperHiveClientException { + try { + switch (strategy) { + case "poll": + return new PollingChooseStrategy(); + case "random": + return new RandomChooseStrategy(); + default: + Class clazz = Class.forName(strategy); + if (ChooseServerStrategy.class.isAssignableFrom(clazz)) { + Constructor constructor = + clazz.asSubclass(ChooseServerStrategy.class).getConstructor(); + return constructor.newInstance(); + } else { + throw new ZooKeeperHiveClientException( + "The loaded class does not implement ChooseServerStrategy"); + } + } + } catch (Exception e) { + throw new ZooKeeperHiveClientException( + "Oops, load the chooseStrategy is wrong, please check your connection params"); } + } } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index c035332f212..8c99d230c73 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -17,46 +17,53 @@ package org.apache.kyuubi.jdbc.hive.strategy.zk; - +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.concurrent.TimeUnit; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.recipes.locks.InterProcessSemaphoreMutex; import org.apache.kyuubi.shaded.zookeeper.CreateMode; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.concurrent.TimeUnit; public class PollingChooseStrategy implements ChooseServerStrategy { - private static final String COUNTER_PATH_PREFIX = "/"; - private static final String COUNTER_PATH_SUFFIX = "-counter"; - private static final int COUNTER_RESET_VALUE = 1000; - @Override - public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) throws ZooKeeperHiveClientException { - String counter_path = COUNTER_PATH_PREFIX+namespace+COUNTER_PATH_SUFFIX; - InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient,counter_path); - try { - if (zkClient.checkExists().forPath(counter_path) == null ){ - zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(counter_path, "1".getBytes(StandardCharsets.UTF_8)); - } - if (!lock.acquire(60,TimeUnit.SECONDS)) { - return null; - } - byte[] data = zkClient.getData().forPath(counter_path); - String dataStr = new String(data, StandardCharsets.UTF_8); - int counter = Integer.parseInt(dataStr); - String server = serverHosts.get(counter % serverHosts.size()); - counter = (counter + 1) % COUNTER_RESET_VALUE; // 避免计数器溢出 - zkClient.setData().forPath(counter_path, (counter + "").getBytes(StandardCharsets.UTF_8)); - return server; - } catch (Exception e) { - throw new ZooKeeperHiveClientException("Oops, PollingChooseStrategy get the server is wrong!",e); - } finally { - try { - lock.release(); - } catch (Exception e) { - throw new ZooKeeperHiveClientException("Oops,PollingChooseStrategy releasing lock is wrong!",e); - } - } + private static final String COUNTER_PATH_PREFIX = "/"; + private static final String COUNTER_PATH_SUFFIX = "-counter"; + private static final int COUNTER_RESET_VALUE = 1000; + + @Override + public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) + throws ZooKeeperHiveClientException { + String counter_path = COUNTER_PATH_PREFIX + namespace + COUNTER_PATH_SUFFIX; + InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient, counter_path); + try { + if (zkClient.checkExists().forPath(counter_path) == null) { + zkClient + .create() + .creatingParentsIfNeeded() + .withMode(CreateMode.PERSISTENT) + .forPath(counter_path, "1".getBytes(StandardCharsets.UTF_8)); + } + if (!lock.acquire(60, TimeUnit.SECONDS)) { + return null; + } + byte[] data = zkClient.getData().forPath(counter_path); + String dataStr = new String(data, StandardCharsets.UTF_8); + int counter = Integer.parseInt(dataStr); + String server = serverHosts.get(counter % serverHosts.size()); + counter = (counter + 1) % COUNTER_RESET_VALUE; // 避免计数器溢出 + zkClient.setData().forPath(counter_path, (counter + "").getBytes(StandardCharsets.UTF_8)); + return server; + } catch (Exception e) { + throw new ZooKeeperHiveClientException( + "Oops, PollingChooseStrategy get the server is wrong!", e); + } finally { + try { + lock.release(); + } catch (Exception e) { + throw new ZooKeeperHiveClientException( + "Oops,PollingChooseStrategy releasing lock is wrong!", e); + } } + } } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java index 7f0e31198f0..f16977699af 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java @@ -17,15 +17,15 @@ package org.apache.kyuubi.jdbc.hive.strategy.zk; -import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; -import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; - import java.util.List; import java.util.concurrent.ThreadLocalRandom; +import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; public class RandomChooseStrategy implements ChooseServerStrategy { - @Override - public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) { - return serverHosts.get(ThreadLocalRandom.current().nextInt(serverHosts.size())); - } + @Override + public String chooseServer( + List serverHosts, CuratorFramework zkClient, String namespace) { + return serverHosts.get(ThreadLocalRandom.current().nextInt(serverHosts.size())); + } } From 8eddd76826459268d5b5f28da31d389d5e46095e Mon Sep 17 00:00:00 2001 From: davidyuan Date: Thu, 28 Mar 2024 11:22:00 +0800 Subject: [PATCH 07/28] Add Strategy Unit Test Case and fix the polling strategy counter begin with 0 --- kyuubi-ha/pom.xml | 7 ++++ .../ZookeeperDiscoveryClientSuite.scala | 36 ++++++++++++++++++- .../strategy/zk/PollingChooseStrategy.java | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/kyuubi-ha/pom.xml b/kyuubi-ha/pom.xml index e1b46c8589a..749ab063266 100644 --- a/kyuubi-ha/pom.xml +++ b/kyuubi-ha/pom.xml @@ -132,6 +132,13 @@ ${project.version} test + + + org.apache.kyuubi + kyuubi-hive-jdbc + ${project.version} + test + diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 34ed0559383..3a552a5d3a1 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -34,8 +34,9 @@ import org.apache.kyuubi.ha.HighAvailabilityConf._ import org.apache.kyuubi.ha.client._ import org.apache.kyuubi.ha.client.DiscoveryClientProvider.withDiscoveryClient import org.apache.kyuubi.ha.client.zookeeper.ZookeeperClientProvider._ +import org.apache.kyuubi.jdbc.hive.strategy.{ChooseServerStrategy, StrategyFactory} import org.apache.kyuubi.service._ -import org.apache.kyuubi.shaded.curator.framework.CuratorFrameworkFactory +import org.apache.kyuubi.shaded.curator.framework.{CuratorFramework, CuratorFrameworkFactory} import org.apache.kyuubi.shaded.curator.retry.ExponentialBackoffRetry import org.apache.kyuubi.shaded.zookeeper.ZooDefs import org.apache.kyuubi.shaded.zookeeper.data.ACL @@ -227,4 +228,37 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests discovery.stop() } } + + test("strategy for zookeeper") { + val zkClient = CuratorFrameworkFactory.builder() + .connectString(getConnectString) + .sessionTimeoutMs(5000) + .retryPolicy(new ExponentialBackoffRetry(1000, 3)) + .build + zkClient.start() + val namespace = "kyuubi-strategy-test" + val testServerHosts = Seq( + "testNode1", + "testNode2", + "testNode3").toList.asJava + // test poll strategy + val pollingChooseStrategy = StrategyFactory.createStrategy("poll") + assert(pollingChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(pollingChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode2") + assert(pollingChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode3") + // test only get first serverHost strategy + assert(TestStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(TestStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(TestStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + zkClient.close() + } + + object TestStrategy extends ChooseServerStrategy { + override def chooseServer( + serverHosts: util.List[String], + zkClient: CuratorFramework, + namespace: String): String = { + serverHosts.get(0) + } + } } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index 8c99d230c73..9af787e2fb5 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -42,7 +42,7 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, .create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) - .forPath(counter_path, "1".getBytes(StandardCharsets.UTF_8)); + .forPath(counter_path, "0".getBytes(StandardCharsets.UTF_8)); } if (!lock.acquire(60, TimeUnit.SECONDS)) { return null; From 9ed2cac8559c05ef4362a7f8b7ad1695e3e76b9e Mon Sep 17 00:00:00 2001 From: david yuan Date: Thu, 28 Mar 2024 12:31:18 +0800 Subject: [PATCH 08/28] remove test comment --- .../kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index 9af787e2fb5..fafc0ec1173 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -51,7 +51,7 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, String dataStr = new String(data, StandardCharsets.UTF_8); int counter = Integer.parseInt(dataStr); String server = serverHosts.get(counter % serverHosts.size()); - counter = (counter + 1) % COUNTER_RESET_VALUE; // 避免计数器溢出 + counter = (counter + 1) % COUNTER_RESET_VALUE; zkClient.setData().forPath(counter_path, (counter + "").getBytes(StandardCharsets.UTF_8)); return server; } catch (Exception e) { From c95382a23a78eec2c5dda711274c0e3f13883a74 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Sun, 14 Apr 2024 16:41:17 +0800 Subject: [PATCH 09/28] fix var not valid and counter getAndIncrement --- .../strategy/zk/PollingChooseStrategy.java | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index fafc0ec1173..933d381959a 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -23,7 +23,10 @@ import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; +import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.AtomicValue; +import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.DistributedAtomicInteger; import org.apache.kyuubi.shaded.curator.framework.recipes.locks.InterProcessSemaphoreMutex; +import org.apache.kyuubi.shaded.curator.retry.RetryForever; import org.apache.kyuubi.shaded.zookeeper.CreateMode; public class PollingChooseStrategy implements ChooseServerStrategy { @@ -34,26 +37,20 @@ public class PollingChooseStrategy implements ChooseServerStrategy { @Override public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) throws ZooKeeperHiveClientException { - String counter_path = COUNTER_PATH_PREFIX + namespace + COUNTER_PATH_SUFFIX; - InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient, counter_path); + String counterPath = COUNTER_PATH_PREFIX + namespace + COUNTER_PATH_SUFFIX; + InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient, counterPath); try { - if (zkClient.checkExists().forPath(counter_path) == null) { + if (zkClient.checkExists().forPath(counterPath) == null) { zkClient .create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) - .forPath(counter_path, "0".getBytes(StandardCharsets.UTF_8)); + .forPath(counterPath, "0".getBytes(StandardCharsets.UTF_8)); } if (!lock.acquire(60, TimeUnit.SECONDS)) { return null; } - byte[] data = zkClient.getData().forPath(counter_path); - String dataStr = new String(data, StandardCharsets.UTF_8); - int counter = Integer.parseInt(dataStr); - String server = serverHosts.get(counter % serverHosts.size()); - counter = (counter + 1) % COUNTER_RESET_VALUE; - zkClient.setData().forPath(counter_path, (counter + "").getBytes(StandardCharsets.UTF_8)); - return server; + return serverHosts.get(getAndIncrement(zkClient, counterPath) % serverHosts.size()); } catch (Exception e) { throw new ZooKeeperHiveClientException( "Oops, PollingChooseStrategy get the server is wrong!", e); @@ -66,4 +63,14 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, } } } + + private int getAndIncrement(CuratorFramework zkClient, String path) throws Exception { + DistributedAtomicInteger dai = new DistributedAtomicInteger(zkClient, path, new RetryForever(1000)); + AtomicValue atomicVal; + do { + atomicVal = dai.add(1); + }while (atomicVal == null || !atomicVal.succeeded()); + dai.trySet(atomicVal.postValue() % COUNTER_RESET_VALUE); + return atomicVal.preValue(); + } } From 7b0c1b8117f83dd1e9d65fa6a0718a965cf1e2e1 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Sun, 14 Apr 2024 16:58:38 +0800 Subject: [PATCH 10/28] fix var not valid and counter getAndIncrement --- .../kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index 933d381959a..b5c017fbadf 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -65,11 +65,12 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, } private int getAndIncrement(CuratorFramework zkClient, String path) throws Exception { - DistributedAtomicInteger dai = new DistributedAtomicInteger(zkClient, path, new RetryForever(1000)); + DistributedAtomicInteger dai = + new DistributedAtomicInteger(zkClient, path, new RetryForever(1000)); AtomicValue atomicVal; do { atomicVal = dai.add(1); - }while (atomicVal == null || !atomicVal.succeeded()); + } while (atomicVal == null || !atomicVal.succeeded()); dai.trySet(atomicVal.postValue() % COUNTER_RESET_VALUE); return atomicVal.preValue(); } From 93f4a2699ac9a0c5379d9d9587e448bd6de910f7 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Sun, 14 Apr 2024 19:34:40 +0800 Subject: [PATCH 11/28] remove reset --- .../hive/strategy/zk/PollingChooseStrategy.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index b5c017fbadf..209e085e4c6 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -17,7 +17,6 @@ package org.apache.kyuubi.jdbc.hive.strategy.zk; -import java.nio.charset.StandardCharsets; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; @@ -27,12 +26,10 @@ import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.DistributedAtomicInteger; import org.apache.kyuubi.shaded.curator.framework.recipes.locks.InterProcessSemaphoreMutex; import org.apache.kyuubi.shaded.curator.retry.RetryForever; -import org.apache.kyuubi.shaded.zookeeper.CreateMode; public class PollingChooseStrategy implements ChooseServerStrategy { private static final String COUNTER_PATH_PREFIX = "/"; private static final String COUNTER_PATH_SUFFIX = "-counter"; - private static final int COUNTER_RESET_VALUE = 1000; @Override public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) @@ -40,17 +37,11 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, String counterPath = COUNTER_PATH_PREFIX + namespace + COUNTER_PATH_SUFFIX; InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient, counterPath); try { - if (zkClient.checkExists().forPath(counterPath) == null) { - zkClient - .create() - .creatingParentsIfNeeded() - .withMode(CreateMode.PERSISTENT) - .forPath(counterPath, "0".getBytes(StandardCharsets.UTF_8)); - } + int counter = getAndIncrement(zkClient, counterPath); if (!lock.acquire(60, TimeUnit.SECONDS)) { return null; } - return serverHosts.get(getAndIncrement(zkClient, counterPath) % serverHosts.size()); + return serverHosts.get(counter % serverHosts.size()); } catch (Exception e) { throw new ZooKeeperHiveClientException( "Oops, PollingChooseStrategy get the server is wrong!", e); @@ -71,7 +62,6 @@ private int getAndIncrement(CuratorFramework zkClient, String path) throws Excep do { atomicVal = dai.add(1); } while (atomicVal == null || !atomicVal.succeeded()); - dai.trySet(atomicVal.postValue() % COUNTER_RESET_VALUE); return atomicVal.preValue(); } } From 09a84f1f9153417ad5e58fdacbb9b0075d1d4d03 Mon Sep 17 00:00:00 2001 From: david yuan Date: Mon, 15 Apr 2024 16:18:53 +0800 Subject: [PATCH 12/28] remove the distirbuted lock --- .../jdbc/hive/strategy/zk/PollingChooseStrategy.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index 209e085e4c6..15c393e209f 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -35,13 +35,8 @@ public class PollingChooseStrategy implements ChooseServerStrategy { public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) throws ZooKeeperHiveClientException { String counterPath = COUNTER_PATH_PREFIX + namespace + COUNTER_PATH_SUFFIX; - InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(zkClient, counterPath); try { - int counter = getAndIncrement(zkClient, counterPath); - if (!lock.acquire(60, TimeUnit.SECONDS)) { - return null; - } - return serverHosts.get(counter % serverHosts.size()); + return serverHosts.get(getAndIncrement(zkClient, counterPath) % serverHosts.size()); } catch (Exception e) { throw new ZooKeeperHiveClientException( "Oops, PollingChooseStrategy get the server is wrong!", e); From 4655480059ad6a51b172e222e33fa3dfa89438f5 Mon Sep 17 00:00:00 2001 From: davidyuan Date: Mon, 15 Apr 2024 16:43:32 +0800 Subject: [PATCH 13/28] update --- .../jdbc/hive/strategy/zk/PollingChooseStrategy.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java index 15c393e209f..4d2a739d35e 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java @@ -18,13 +18,11 @@ package org.apache.kyuubi.jdbc.hive.strategy.zk; import java.util.List; -import java.util.concurrent.TimeUnit; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.AtomicValue; import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.DistributedAtomicInteger; -import org.apache.kyuubi.shaded.curator.framework.recipes.locks.InterProcessSemaphoreMutex; import org.apache.kyuubi.shaded.curator.retry.RetryForever; public class PollingChooseStrategy implements ChooseServerStrategy { @@ -40,13 +38,6 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, } catch (Exception e) { throw new ZooKeeperHiveClientException( "Oops, PollingChooseStrategy get the server is wrong!", e); - } finally { - try { - lock.release(); - } catch (Exception e) { - throw new ZooKeeperHiveClientException( - "Oops,PollingChooseStrategy releasing lock is wrong!", e); - } } } From b4aeb3dbd2b783cbcd239cc891ca430e34e4cddf Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 17 Oct 2024 09:22:55 +0800 Subject: [PATCH 14/28] repeat testing on pollingChooseStrategy --- .../ZookeeperDiscoveryClientSuite.scala | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 3a552a5d3a1..f5b5c96080e 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -236,29 +236,33 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build zkClient.start() + val namespace = "kyuubi-strategy-test" val testServerHosts = Seq( "testNode1", "testNode2", - "testNode3").toList.asJava + "testNode3").asJava // test poll strategy - val pollingChooseStrategy = StrategyFactory.createStrategy("poll") - assert(pollingChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(pollingChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode2") - assert(pollingChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode3") + val pollingStrategy = StrategyFactory.createStrategy("poll") + 1 to testServerHosts.size() foreach { _ => + assertResult(f"testNode1")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) + assertResult(f"testNode2")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) + assertResult(f"testNode3")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) + } + // test only get first serverHost strategy - assert(TestStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(TestStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(TestStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(CustomChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(CustomChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(CustomChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") zkClient.close() } +} - object TestStrategy extends ChooseServerStrategy { - override def chooseServer( - serverHosts: util.List[String], - zkClient: CuratorFramework, - namespace: String): String = { - serverHosts.get(0) - } +object CustomChooseStrategy extends ChooseServerStrategy { + override def chooseServer( + serverHosts: util.List[String], + zkClient: CuratorFramework, + namespace: String): String = { + serverHosts.get(0) } } From 125c82358265fda86c7b9abc413658a9b67d5ab1 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 11:09:57 +0800 Subject: [PATCH 15/28] rename ChooseServerStrategy to ServerSelectStrategy --- .../ZookeeperDiscoveryClientSuite.scala | 10 +++++----- .../jdbc/hive/ZooKeeperHiveClientHelper.java | 6 +++--- ...erStrategy.java => ServerSelectStrategy.java} | 2 +- .../jdbc/hive/strategy/StrategyFactory.java | 16 ++++++++-------- ...eStrategy.java => PollingSelectStrategy.java} | 4 ++-- ...seStrategy.java => RandomSelectStrategy.java} | 4 ++-- 6 files changed, 21 insertions(+), 21 deletions(-) rename kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/{ChooseServerStrategy.java => ServerSelectStrategy.java} (96%) rename kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/{PollingChooseStrategy.java => PollingSelectStrategy.java} (94%) rename kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/{RandomChooseStrategy.java => RandomSelectStrategy.java} (90%) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index f5b5c96080e..23fc5e9cbdf 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -34,7 +34,7 @@ import org.apache.kyuubi.ha.HighAvailabilityConf._ import org.apache.kyuubi.ha.client._ import org.apache.kyuubi.ha.client.DiscoveryClientProvider.withDiscoveryClient import org.apache.kyuubi.ha.client.zookeeper.ZookeeperClientProvider._ -import org.apache.kyuubi.jdbc.hive.strategy.{ChooseServerStrategy, StrategyFactory} +import org.apache.kyuubi.jdbc.hive.strategy.{ServerSelectStrategy, StrategyFactory} import org.apache.kyuubi.service._ import org.apache.kyuubi.shaded.curator.framework.{CuratorFramework, CuratorFrameworkFactory} import org.apache.kyuubi.shaded.curator.retry.ExponentialBackoffRetry @@ -251,14 +251,14 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests } // test only get first serverHost strategy - assert(CustomChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(CustomChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(CustomChooseStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") zkClient.close() } } -object CustomChooseStrategy extends ChooseServerStrategy { +object CustomSelectStrategy extends ServerSelectStrategy { override def chooseServer( serverHosts: util.List[String], zkClient: CuratorFramework, diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 5d0d9e7ab67..a52c73a5f38 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -24,7 +24,7 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; import org.apache.kyuubi.jdbc.hive.strategy.StrategyFactory; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.CuratorFrameworkFactory; @@ -127,8 +127,8 @@ private static String chooseServer( String zooKeeperNamespace = getZooKeeperNamespace(connParams); String zkStrategy = connParams.getSessionVars().getOrDefault(JdbcConnectionParams.ZOOKEEPER_STRATEGY, "random"); - ChooseServerStrategy chooseServerStrategy = StrategyFactory.createStrategy(zkStrategy); - return chooseServerStrategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); + ServerSelectStrategy strategy = StrategyFactory.createStrategy(zkStrategy); + return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } static List getDirectParamsList(JdbcConnectionParams connParams) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java similarity index 96% rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java index cfb2fa190e4..16c75dd8048 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ChooseServerStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java @@ -21,7 +21,7 @@ import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; -public interface ChooseServerStrategy { +public interface ServerSelectStrategy { String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) throws ZooKeeperHiveClientException; } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java index df1a01b359e..10af729f118 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -19,23 +19,23 @@ import java.lang.reflect.Constructor; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; -import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingChooseStrategy; -import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomChooseStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingSelectStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; public class StrategyFactory { - public static ChooseServerStrategy createStrategy(String strategy) + public static ServerSelectStrategy createStrategy(String strategy) throws ZooKeeperHiveClientException { try { switch (strategy) { case "poll": - return new PollingChooseStrategy(); + return new PollingSelectStrategy(); case "random": - return new RandomChooseStrategy(); + return new RandomSelectStrategy(); default: Class clazz = Class.forName(strategy); - if (ChooseServerStrategy.class.isAssignableFrom(clazz)) { - Constructor constructor = - clazz.asSubclass(ChooseServerStrategy.class).getConstructor(); + if (ServerSelectStrategy.class.isAssignableFrom(clazz)) { + Constructor constructor = + clazz.asSubclass(ServerSelectStrategy.class).getConstructor(); return constructor.newInstance(); } else { throw new ZooKeeperHiveClientException( diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java similarity index 94% rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java index 4d2a739d35e..8f3224b3093 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java @@ -19,13 +19,13 @@ import java.util.List; import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; -import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.AtomicValue; import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.DistributedAtomicInteger; import org.apache.kyuubi.shaded.curator.retry.RetryForever; -public class PollingChooseStrategy implements ChooseServerStrategy { +public class PollingSelectStrategy implements ServerSelectStrategy { private static final String COUNTER_PATH_PREFIX = "/"; private static final String COUNTER_PATH_SUFFIX = "-counter"; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java similarity index 90% rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java index f16977699af..80752974de4 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomChooseStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java @@ -19,10 +19,10 @@ import java.util.List; import java.util.concurrent.ThreadLocalRandom; -import org.apache.kyuubi.jdbc.hive.strategy.ChooseServerStrategy; +import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; -public class RandomChooseStrategy implements ChooseServerStrategy { +public class RandomSelectStrategy implements ServerSelectStrategy { @Override public String chooseServer( List serverHosts, CuratorFramework zkClient, String namespace) { From 228bf10915e7f3fe2fb02f7014386b2de33aabda Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 11:12:19 +0800 Subject: [PATCH 16/28] rename parameter zooKeeperStrategy to serverSelectStrategy --- .../org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java | 2 +- .../apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java index 759e34468b2..0db99da7100 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/JdbcConnectionParams.java @@ -79,7 +79,7 @@ public class JdbcConnectionParams { // Use ZooKeeper for indirection while using dynamic service discovery static final String SERVICE_DISCOVERY_MODE_ZOOKEEPER = "zooKeeper"; static final String ZOOKEEPER_NAMESPACE = "zooKeeperNamespace"; - static final String ZOOKEEPER_STRATEGY = "zooKeeperStrategy"; + static final String SERVER_SELECT_STRATEGY = "serverSelectStrategy"; // Default namespace value on ZooKeeper. // This value is used if the param "zooKeeperNamespace" is not specified in the JDBC Uri. static final String ZOOKEEPER_DEFAULT_NAMESPACE = "hiveserver2"; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index a52c73a5f38..80fe3a4c0b8 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -126,7 +126,9 @@ private static String chooseServer( throws ZooKeeperHiveClientException { String zooKeeperNamespace = getZooKeeperNamespace(connParams); String zkStrategy = - connParams.getSessionVars().getOrDefault(JdbcConnectionParams.ZOOKEEPER_STRATEGY, "random"); + connParams + .getSessionVars() + .getOrDefault(JdbcConnectionParams.SERVER_SELECT_STRATEGY, "random"); ServerSelectStrategy strategy = StrategyFactory.createStrategy(zkStrategy); return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } From 8f8ca28f206a20e297d673a8adcc4a5041480118 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 11:12:57 +0800 Subject: [PATCH 17/28] nit --- .../apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 80fe3a4c0b8..0145d0172d5 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -125,11 +125,11 @@ private static String chooseServer( JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) throws ZooKeeperHiveClientException { String zooKeeperNamespace = getZooKeeperNamespace(connParams); - String zkStrategy = + String serverSelectStrategyName = connParams .getSessionVars() .getOrDefault(JdbcConnectionParams.SERVER_SELECT_STRATEGY, "random"); - ServerSelectStrategy strategy = StrategyFactory.createStrategy(zkStrategy); + ServerSelectStrategy strategy = StrategyFactory.createStrategy(serverSelectStrategyName); return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } From 1ab79b4941163a53eb068dcda1752cd3ac0226e6 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 11:22:56 +0800 Subject: [PATCH 18/28] strategyName --- .../kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 7 ++++--- .../kyuubi/jdbc/hive/strategy/StrategyFactory.java | 12 ++++++------ .../jdbc/hive/strategy/zk/PollingSelectStrategy.java | 2 ++ .../jdbc/hive/strategy/zk/RandomSelectStrategy.java | 2 ++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 0145d0172d5..fce179c4a28 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -26,6 +26,7 @@ import java.util.regex.Pattern; import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; import org.apache.kyuubi.jdbc.hive.strategy.StrategyFactory; +import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.CuratorFrameworkFactory; import org.apache.kyuubi.shaded.curator.retry.ExponentialBackoffRetry; @@ -125,11 +126,11 @@ private static String chooseServer( JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) throws ZooKeeperHiveClientException { String zooKeeperNamespace = getZooKeeperNamespace(connParams); - String serverSelectStrategyName = + String strategyName = connParams .getSessionVars() - .getOrDefault(JdbcConnectionParams.SERVER_SELECT_STRATEGY, "random"); - ServerSelectStrategy strategy = StrategyFactory.createStrategy(serverSelectStrategyName); + .getOrDefault(JdbcConnectionParams.SERVER_SELECT_STRATEGY, RandomSelectStrategy.strategyName); + ServerSelectStrategy strategy = StrategyFactory.createStrategy(strategyName); return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java index 10af729f118..1c29adf7907 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -23,16 +23,16 @@ import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; public class StrategyFactory { - public static ServerSelectStrategy createStrategy(String strategy) + public static ServerSelectStrategy createStrategy(String strategyName) throws ZooKeeperHiveClientException { try { - switch (strategy) { - case "poll": + switch (strategyName) { + case PollingSelectStrategy.strategyName: return new PollingSelectStrategy(); - case "random": + case RandomSelectStrategy.strategyName: return new RandomSelectStrategy(); default: - Class clazz = Class.forName(strategy); + Class clazz = Class.forName(strategyName); if (ServerSelectStrategy.class.isAssignableFrom(clazz)) { Constructor constructor = clazz.asSubclass(ServerSelectStrategy.class).getConstructor(); @@ -44,7 +44,7 @@ public static ServerSelectStrategy createStrategy(String strategy) } } catch (Exception e) { throw new ZooKeeperHiveClientException( - "Oops, load the chooseStrategy is wrong, please check your connection params"); + "Oops, load the chooseStrategy is wrong, please check your connection params", e); } } } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java index 8f3224b3093..516aed6eefa 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java @@ -26,6 +26,8 @@ import org.apache.kyuubi.shaded.curator.retry.RetryForever; public class PollingSelectStrategy implements ServerSelectStrategy { + public static final String strategyName = "poll"; + private static final String COUNTER_PATH_PREFIX = "/"; private static final String COUNTER_PATH_SUFFIX = "-counter"; diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java index 80752974de4..f42fd529481 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/RandomSelectStrategy.java @@ -23,6 +23,8 @@ import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; public class RandomSelectStrategy implements ServerSelectStrategy { + public static final String strategyName = "random"; + @Override public String chooseServer( List serverHosts, CuratorFramework zkClient, String namespace) { From b39c567003f842da6d0d460979adf91c0166ee88 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 11:23:17 +0800 Subject: [PATCH 19/28] style --- .../org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 3 ++- .../org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index fce179c4a28..5b9a47b0537 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -129,7 +129,8 @@ private static String chooseServer( String strategyName = connParams .getSessionVars() - .getOrDefault(JdbcConnectionParams.SERVER_SELECT_STRATEGY, RandomSelectStrategy.strategyName); + .getOrDefault( + JdbcConnectionParams.SERVER_SELECT_STRATEGY, RandomSelectStrategy.strategyName); ServerSelectStrategy strategy = StrategyFactory.createStrategy(strategyName); return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java index 1c29adf7907..001bbb69033 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -44,7 +44,7 @@ public static ServerSelectStrategy createStrategy(String strategyName) } } catch (Exception e) { throw new ZooKeeperHiveClientException( - "Oops, load the chooseStrategy is wrong, please check your connection params", e); + "Oops, load the chooseStrategy is wrong, please check your connection params", e); } } } From 265965e5d72f2a41b27d9ab5222c208f3ba41074 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 11:38:15 +0800 Subject: [PATCH 20/28] polling --- .../ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala | 2 +- .../kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 23fc5e9cbdf..b32336233cd 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -243,7 +243,7 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests "testNode2", "testNode3").asJava // test poll strategy - val pollingStrategy = StrategyFactory.createStrategy("poll") + val pollingStrategy = StrategyFactory.createStrategy("polling") 1 to testServerHosts.size() foreach { _ => assertResult(f"testNode1")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) assertResult(f"testNode2")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java index 516aed6eefa..23c0a549b5c 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java @@ -26,7 +26,7 @@ import org.apache.kyuubi.shaded.curator.retry.RetryForever; public class PollingSelectStrategy implements ServerSelectStrategy { - public static final String strategyName = "poll"; + public static final String strategyName = "polling"; private static final String COUNTER_PATH_PREFIX = "/"; private static final String COUNTER_PATH_SUFFIX = "-counter"; @@ -39,7 +39,7 @@ public String chooseServer(List serverHosts, CuratorFramework zkClient, return serverHosts.get(getAndIncrement(zkClient, counterPath) % serverHosts.size()); } catch (Exception e) { throw new ZooKeeperHiveClientException( - "Oops, PollingChooseStrategy get the server is wrong!", e); + "Oops, PollingSelectStrategy get the server is wrong!", e); } } From e194ea62f421d3a55137066c733ac5f386df61e3 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 13:17:26 +0800 Subject: [PATCH 21/28] remove ZooKeeperHiveClientException from method signature of chooseServer --- .../zookeeper/ZookeeperDiscoveryClientSuite.scala | 4 +--- .../kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 11 +++++++---- .../jdbc/hive/strategy/ServerSelectStrategy.java | 4 +--- .../kyuubi/jdbc/hive/strategy/StrategyFactory.java | 6 ++---- .../jdbc/hive/strategy/zk/PollingSelectStrategy.java | 10 ++++------ 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index b32336233cd..0d9db7d489e 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -262,7 +262,5 @@ object CustomSelectStrategy extends ServerSelectStrategy { override def chooseServer( serverHosts: util.List[String], zkClient: CuratorFramework, - namespace: String): String = { - serverHosts.get(0) - } + namespace: String): String = serverHosts.get(0) } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 5b9a47b0537..d2c637d8dd5 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -123,16 +123,19 @@ static void configureConnParams(JdbcConnectionParams connParams) } private static String chooseServer( - JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) - throws ZooKeeperHiveClientException { + JdbcConnectionParams connParams, List serverHosts, CuratorFramework zkClient) { String zooKeeperNamespace = getZooKeeperNamespace(connParams); String strategyName = connParams .getSessionVars() .getOrDefault( JdbcConnectionParams.SERVER_SELECT_STRATEGY, RandomSelectStrategy.strategyName); - ServerSelectStrategy strategy = StrategyFactory.createStrategy(strategyName); - return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); + try { + ServerSelectStrategy strategy = StrategyFactory.createStrategy(strategyName); + return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); + } catch (Exception e) { + throw new RuntimeException("Failed to choose server with strategy " + strategyName, e); + } } static List getDirectParamsList(JdbcConnectionParams connParams) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java index 16c75dd8048..740c3577637 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategy.java @@ -18,10 +18,8 @@ package org.apache.kyuubi.jdbc.hive.strategy; import java.util.List; -import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; public interface ServerSelectStrategy { - String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) - throws ZooKeeperHiveClientException; + String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace); } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java index 001bbb69033..2edcd28ec6d 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java @@ -23,8 +23,7 @@ import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; public class StrategyFactory { - public static ServerSelectStrategy createStrategy(String strategyName) - throws ZooKeeperHiveClientException { + public static ServerSelectStrategy createStrategy(String strategyName) { try { switch (strategyName) { case PollingSelectStrategy.strategyName: @@ -43,8 +42,7 @@ public static ServerSelectStrategy createStrategy(String strategyName) } } } catch (Exception e) { - throw new ZooKeeperHiveClientException( - "Oops, load the chooseStrategy is wrong, please check your connection params", e); + throw new RuntimeException("Failed to init server select strategy", e); } } } diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java index 23c0a549b5c..664c76defa5 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/zk/PollingSelectStrategy.java @@ -18,7 +18,6 @@ package org.apache.kyuubi.jdbc.hive.strategy.zk; import java.util.List; -import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.recipes.atomic.AtomicValue; @@ -32,20 +31,19 @@ public class PollingSelectStrategy implements ServerSelectStrategy { private static final String COUNTER_PATH_SUFFIX = "-counter"; @Override - public String chooseServer(List serverHosts, CuratorFramework zkClient, String namespace) - throws ZooKeeperHiveClientException { + public String chooseServer( + List serverHosts, CuratorFramework zkClient, String namespace) { String counterPath = COUNTER_PATH_PREFIX + namespace + COUNTER_PATH_SUFFIX; try { return serverHosts.get(getAndIncrement(zkClient, counterPath) % serverHosts.size()); } catch (Exception e) { - throw new ZooKeeperHiveClientException( - "Oops, PollingSelectStrategy get the server is wrong!", e); + throw new RuntimeException("Failed to choose server by polling select strategy", e); } } private int getAndIncrement(CuratorFramework zkClient, String path) throws Exception { DistributedAtomicInteger dai = - new DistributedAtomicInteger(zkClient, path, new RetryForever(1000)); + new DistributedAtomicInteger(zkClient, path, new RetryForever(3000)); AtomicValue atomicVal; do { atomicVal = dai.add(1); From 7668f99cc8d7967222bfe887683c81ce341dcc73 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 13:33:51 +0800 Subject: [PATCH 22/28] test name --- .../ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 0d9db7d489e..4ed8069fc93 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -229,7 +229,7 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests } } - test("strategy for zookeeper") { + test("server select strategy with zookeeper") { val zkClient = CuratorFrameworkFactory.builder() .connectString(getConnectString) .sessionTimeoutMs(5000) From 40f427ae58d2ba8b7a10697cd962e472d4d555c5 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 13:34:51 +0800 Subject: [PATCH 23/28] rename StrategyFactory to StrategyFactoryServerStrategyFactory --- .../ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala | 6 +++--- .../apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 4 ++-- .../{StrategyFactory.java => ServerStrategyFactory.java} | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) rename kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/{StrategyFactory.java => ServerStrategyFactory.java} (98%) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 4ed8069fc93..638df366a04 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -34,7 +34,7 @@ import org.apache.kyuubi.ha.HighAvailabilityConf._ import org.apache.kyuubi.ha.client._ import org.apache.kyuubi.ha.client.DiscoveryClientProvider.withDiscoveryClient import org.apache.kyuubi.ha.client.zookeeper.ZookeeperClientProvider._ -import org.apache.kyuubi.jdbc.hive.strategy.{ServerSelectStrategy, StrategyFactory} +import org.apache.kyuubi.jdbc.hive.strategy.{ServerSelectStrategy, ServerStrategyFactory} import org.apache.kyuubi.service._ import org.apache.kyuubi.shaded.curator.framework.{CuratorFramework, CuratorFrameworkFactory} import org.apache.kyuubi.shaded.curator.retry.ExponentialBackoffRetry @@ -242,8 +242,8 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests "testNode1", "testNode2", "testNode3").asJava - // test poll strategy - val pollingStrategy = StrategyFactory.createStrategy("polling") + // test polling strategy + val pollingStrategy = ServerStrategyFactory.createStrategy("polling") 1 to testServerHosts.size() foreach { _ => assertResult(f"testNode1")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) assertResult(f"testNode2")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index d2c637d8dd5..7a15ae237da 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -25,7 +25,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; -import org.apache.kyuubi.jdbc.hive.strategy.StrategyFactory; +import org.apache.kyuubi.jdbc.hive.strategy.ServerStrategyFactory; import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.CuratorFrameworkFactory; @@ -131,7 +131,7 @@ private static String chooseServer( .getOrDefault( JdbcConnectionParams.SERVER_SELECT_STRATEGY, RandomSelectStrategy.strategyName); try { - ServerSelectStrategy strategy = StrategyFactory.createStrategy(strategyName); + ServerSelectStrategy strategy = ServerStrategyFactory.createStrategy(strategyName); return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } catch (Exception e) { throw new RuntimeException("Failed to choose server with strategy " + strategyName, e); diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java similarity index 98% rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java index 2edcd28ec6d..05dba66de89 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/StrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java @@ -22,7 +22,7 @@ import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingSelectStrategy; import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; -public class StrategyFactory { +public class ServerStrategyFactory { public static ServerSelectStrategy createStrategy(String strategyName) { try { switch (strategyName) { From e94f9e90939ee614158b4a0d6ed0ea9276acc6ec Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 13:48:14 +0800 Subject: [PATCH 24/28] nit --- .../apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java index 05dba66de89..f90d05b883e 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java @@ -38,7 +38,7 @@ public static ServerSelectStrategy createStrategy(String strategyName) { return constructor.newInstance(); } else { throw new ZooKeeperHiveClientException( - "The loaded class does not implement ChooseServerStrategy"); + "The loaded class does not implement ServerSelectStrategy"); } } } catch (Exception e) { From 619339402d66df8d2c4abe08b738238e7e96b4c1 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 13:58:43 +0800 Subject: [PATCH 25/28] nit --- .../kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java index f90d05b883e..3d475d3ac07 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java @@ -18,7 +18,6 @@ package org.apache.kyuubi.jdbc.hive.strategy; import java.lang.reflect.Constructor; -import org.apache.kyuubi.jdbc.hive.ZooKeeperHiveClientException; import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingSelectStrategy; import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; @@ -37,8 +36,7 @@ public static ServerSelectStrategy createStrategy(String strategyName) { clazz.asSubclass(ServerSelectStrategy.class).getConstructor(); return constructor.newInstance(); } else { - throw new ZooKeeperHiveClientException( - "The loaded class does not implement ServerSelectStrategy"); + throw new ClassNotFoundException("The loaded class does not implement ServerSelectStrategy"); } } } catch (Exception e) { From 8822ad4714878204c233feb57f759d50dddee04e Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 14:04:59 +0800 Subject: [PATCH 26/28] repeat --- .../ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 638df366a04..968d6461365 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -244,7 +244,7 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests "testNode3").asJava // test polling strategy val pollingStrategy = ServerStrategyFactory.createStrategy("polling") - 1 to testServerHosts.size() foreach { _ => + 1 to testServerHosts.size() * 2 foreach { _ => assertResult(f"testNode1")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) assertResult(f"testNode2")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) assertResult(f"testNode3")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) @@ -254,6 +254,7 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + zkClient.close() } } From 353f94059af3b189b443d357a7f7bd284c2a1c7d Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 14:09:36 +0800 Subject: [PATCH 27/28] repeat --- .../ZookeeperDiscoveryClientSuite.scala | 21 ++++++++++--------- .../hive/strategy/ServerStrategyFactory.java | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 968d6461365..2513e7dcd8b 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -251,17 +251,18 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests } // test only get first serverHost strategy - assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") - assert(CustomSelectStrategy.chooseServer(testServerHosts, zkClient, namespace) === "testNode1") + val customStrategy = new ServerSelectStrategy { + override def chooseServer( + serverHosts: util.List[String], + zkClient: CuratorFramework, + namespace: String): String = serverHosts.get(0) + } + 1 to testServerHosts.size() * 2 foreach { _ => + assertResult("testNode1") { + customStrategy.chooseServer(testServerHosts, zkClient, namespace) + } + } zkClient.close() } } - -object CustomSelectStrategy extends ServerSelectStrategy { - override def chooseServer( - serverHosts: util.List[String], - zkClient: CuratorFramework, - namespace: String): String = serverHosts.get(0) -} diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java index 3d475d3ac07..958314e8ba1 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java @@ -36,7 +36,8 @@ public static ServerSelectStrategy createStrategy(String strategyName) { clazz.asSubclass(ServerSelectStrategy.class).getConstructor(); return constructor.newInstance(); } else { - throw new ClassNotFoundException("The loaded class does not implement ServerSelectStrategy"); + throw new ClassNotFoundException( + "The loaded class does not implement ServerSelectStrategy"); } } } catch (Exception e) { From 961d3e9890e6ff7f7ed0ba58167319602eb46e91 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Wed, 23 Oct 2024 16:31:50 +0800 Subject: [PATCH 28/28] rename ServerStrategyFactory to ServerSelectStrategyFactory --- .../ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala | 4 ++-- .../apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java | 4 ++-- ...rStrategyFactory.java => ServerSelectStrategyFactory.java} | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/{ServerStrategyFactory.java => ServerSelectStrategyFactory.java} (97%) diff --git a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala index 2513e7dcd8b..ba3d0650d19 100644 --- a/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala +++ b/kyuubi-ha/src/test/scala/org/apache/kyuubi/ha/client/zookeeper/ZookeeperDiscoveryClientSuite.scala @@ -34,7 +34,7 @@ import org.apache.kyuubi.ha.HighAvailabilityConf._ import org.apache.kyuubi.ha.client._ import org.apache.kyuubi.ha.client.DiscoveryClientProvider.withDiscoveryClient import org.apache.kyuubi.ha.client.zookeeper.ZookeeperClientProvider._ -import org.apache.kyuubi.jdbc.hive.strategy.{ServerSelectStrategy, ServerStrategyFactory} +import org.apache.kyuubi.jdbc.hive.strategy.{ServerSelectStrategy, ServerSelectStrategyFactory} import org.apache.kyuubi.service._ import org.apache.kyuubi.shaded.curator.framework.{CuratorFramework, CuratorFrameworkFactory} import org.apache.kyuubi.shaded.curator.retry.ExponentialBackoffRetry @@ -243,7 +243,7 @@ abstract class ZookeeperDiscoveryClientSuite extends DiscoveryClientTests "testNode2", "testNode3").asJava // test polling strategy - val pollingStrategy = ServerStrategyFactory.createStrategy("polling") + val pollingStrategy = ServerSelectStrategyFactory.createStrategy("polling") 1 to testServerHosts.size() * 2 foreach { _ => assertResult(f"testNode1")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) assertResult(f"testNode2")(pollingStrategy.chooseServer(testServerHosts, zkClient, namespace)) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java index 7a15ae237da..f94bdb431e6 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java @@ -25,7 +25,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategy; -import org.apache.kyuubi.jdbc.hive.strategy.ServerStrategyFactory; +import org.apache.kyuubi.jdbc.hive.strategy.ServerSelectStrategyFactory; import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; import org.apache.kyuubi.shaded.curator.framework.CuratorFramework; import org.apache.kyuubi.shaded.curator.framework.CuratorFrameworkFactory; @@ -131,7 +131,7 @@ private static String chooseServer( .getOrDefault( JdbcConnectionParams.SERVER_SELECT_STRATEGY, RandomSelectStrategy.strategyName); try { - ServerSelectStrategy strategy = ServerStrategyFactory.createStrategy(strategyName); + ServerSelectStrategy strategy = ServerSelectStrategyFactory.createStrategy(strategyName); return strategy.chooseServer(serverHosts, zkClient, zooKeeperNamespace); } catch (Exception e) { throw new RuntimeException("Failed to choose server with strategy " + strategyName, e); diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategyFactory.java similarity index 97% rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategyFactory.java index 958314e8ba1..9950097adec 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerStrategyFactory.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/strategy/ServerSelectStrategyFactory.java @@ -21,7 +21,7 @@ import org.apache.kyuubi.jdbc.hive.strategy.zk.PollingSelectStrategy; import org.apache.kyuubi.jdbc.hive.strategy.zk.RandomSelectStrategy; -public class ServerStrategyFactory { +public class ServerSelectStrategyFactory { public static ServerSelectStrategy createStrategy(String strategyName) { try { switch (strategyName) {