This is an automated email from the ASF dual-hosted git repository.

dengzhhu653 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new b8dc73f5302 HIVE-29620: RetryingMetaStoreClient uses 
HiveMetaStoreClientBuilder t… (#6499)
b8dc73f5302 is described below

commit b8dc73f53020ef808cd480335e6705b3081b7f18
Author: dengzh <[email protected]>
AuthorDate: Fri Jun 5 08:27:35 2026 +0800

    HIVE-29620: RetryingMetaStoreClient uses HiveMetaStoreClientBuilder t… 
(#6499)
---
 .../iceberg/hive/client/HiveRESTCatalogClient.java |  4 --
 .../org/apache/hadoop/hive/ql/metadata/Hive.java   |  3 +-
 .../hadoop/hive/metastore/HiveMetaStoreClient.java |  5 +-
 .../hadoop/hive/metastore/IMetaStoreClient.java    |  5 +-
 .../hive/metastore/RetryingMetaStoreClient.java    | 44 ++++--------
 .../client/builder/HiveMetaStoreClientBuilder.java | 80 +++++++++++++++++-----
 6 files changed, 79 insertions(+), 62 deletions(-)

diff --git 
a/iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/client/HiveRESTCatalogClient.java
 
b/iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/client/HiveRESTCatalogClient.java
index 4390d5a0bca..a0ca7c937aa 100644
--- 
a/iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/client/HiveRESTCatalogClient.java
+++ 
b/iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/client/HiveRESTCatalogClient.java
@@ -68,10 +68,6 @@ public class HiveRESTCatalogClient extends 
BaseMetaStoreClient {
 
   private RESTCatalog restCatalog;
 
-  public HiveRESTCatalogClient(Configuration conf, boolean allowEmbedded) {
-    this(conf);
-  }
-
   public HiveRESTCatalogClient(Configuration conf) {
     super(conf);
     reconnect();
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java 
b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
index cd33896807b..31f695ed558 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
@@ -6094,8 +6094,7 @@ public HiveMetaHook getHook(
       }
     };
 
-    HiveMetaStoreClientBuilder msClientBuilder = new 
HiveMetaStoreClientBuilder(conf)
-        .newClient(allowEmbedded)
+    HiveMetaStoreClientBuilder msClientBuilder = new 
HiveMetaStoreClientBuilder(conf, allowEmbedded)
         .enhanceWith(client ->
             HiveMetaStoreClientWithLocalCache.newClient(conf, client))
         .enhanceWith(client ->
diff --git 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java
 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java
index 122c0c3c491..75defe50ebb 100644
--- 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java
+++ 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStoreClient.java
@@ -61,7 +61,7 @@ public HiveMetaStoreClient(Configuration conf, 
HiveMetaHookLoader hookLoader) th
 
   public HiveMetaStoreClient(Configuration conf, HiveMetaHookLoader 
hookLoader, Boolean allowEmbedded)
     throws MetaException {
-    this(conf, hookLoader, new 
HiveMetaStoreClientBuilder(conf).newClient(allowEmbedded).build());
+    this(conf, hookLoader, new HiveMetaStoreClientBuilder(conf, 
allowEmbedded).build());
   }
 
   private HiveMetaStoreClient(Configuration conf, HiveMetaHookLoader 
hookLoader,
@@ -75,8 +75,7 @@ private HiveMetaStoreClient(Configuration conf, 
HiveMetaHookLoader hookLoader,
 
   private static IMetaStoreClient createUnderlyingClient(Configuration conf, 
HiveMetaHookLoader hookLoader,
       IMetaStoreClient baseMetaStoreClient) {
-    return new HiveMetaStoreClientBuilder(conf)
-       .client(baseMetaStoreClient)
+    return new HiveMetaStoreClientBuilder(conf, baseMetaStoreClient)
        .withHooks(hookLoader)
        .threadSafe()
        .build();
diff --git 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java
 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java
index 7b03f7f096e..5b7b174c26e 100644
--- 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java
+++ 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/IMetaStoreClient.java
@@ -65,11 +65,12 @@ default void setHiveAddedJars(String addedJars) {
 
   /**
    * Returns true if the current client is using an in process metastore 
(local metastore).
+   * Default false, as in real production the client should always connect to 
a remote meta service
    *
    * @return
    */
-  default boolean isLocalMetaStore(){
-    throw new UnsupportedOperationException("MetaStore client does not support 
checking if metastore is local");
+  default boolean isLocalMetaStore() {
+    return false;
   }
 
   /**
diff --git 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/RetryingMetaStoreClient.java
 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/RetryingMetaStoreClient.java
index 0cf9901fd2a..14f6ba3c342 100644
--- 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/RetryingMetaStoreClient.java
+++ 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/RetryingMetaStoreClient.java
@@ -33,9 +33,11 @@
 import java.util.concurrent.TimeUnit;
 import java.util.function.Supplier;
 
+import org.apache.commons.lang3.ClassUtils;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.common.classification.RetrySemantics;
+import 
org.apache.hadoop.hive.metastore.client.builder.HiveMetaStoreClientBuilder;
 import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
 import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
 import org.apache.hadoop.hive.metastore.utils.JavaUtils;
@@ -69,15 +71,7 @@ public class RetryingMetaStoreClient implements 
InvocationHandler {
   private final Map<String, Long> metaCallTimeMap;
   private final long connectionLifeTimeInMillis;
   private long lastConnectionTime;
-  private boolean localMetaStore;
-
-
-  protected RetryingMetaStoreClient(Configuration conf, Class<?>[] 
constructorArgTypes,
-      Object[] constructorArgs, Map<String, Long> metaCallTimeMap,
-      Class<? extends IMetaStoreClient> msClientClass) throws MetaException {
-    this(conf, metaCallTimeMap, () ->
-        JavaUtils.newInstance(msClientClass, constructorArgTypes, 
constructorArgs));
-  }
+  private final boolean localMetaStore;
 
   protected RetryingMetaStoreClient(Configuration conf, Map<String, Long> 
metaCallTimeMap,
         Supplier<? extends IMetaStoreClient> msClient) throws MetaException {
@@ -95,12 +89,11 @@ protected RetryingMetaStoreClient(Configuration conf, 
Map<String, Long> metaCall
     this.connectionLifeTimeInMillis = MetastoreConf.getTimeVar(conf,
         ConfVars.CLIENT_SOCKET_LIFETIME, TimeUnit.MILLISECONDS);
     this.lastConnectionTime = System.currentTimeMillis();
-    String msUri = MetastoreConf.getVar(conf, ConfVars.THRIFT_URIS);
-    localMetaStore = (msUri == null) || msUri.trim().isEmpty();
 
     SecurityUtils.reloginExpiringKeytabUser();
 
     this.base = msClient.get();
+    this.localMetaStore = base.isLocalMetaStore();
 
     LOG.info("RetryingMetaStoreClient proxy=" + base.getClass() + " ugi=" + 
this.ugi
         + " retries=" + this.retryLimit + " delay=" + this.retryDelaySeconds
@@ -109,9 +102,7 @@ protected RetryingMetaStoreClient(Configuration conf, 
Map<String, Long> metaCall
 
   public static IMetaStoreClient getProxy(
       Configuration hiveConf, boolean allowEmbedded) throws MetaException {
-    return getProxy(hiveConf, new Class[]{Configuration.class, 
HiveMetaHookLoader.class, Boolean.class},
-        new Object[]{hiveConf, null, allowEmbedded}, null, 
HiveMetaStoreClient.class.getName()
-    );
+    return new HiveMetaStoreClientBuilder(hiveConf, 
allowEmbedded).withRetry(null).build();
   }
 
   @VisibleForTesting
@@ -123,13 +114,10 @@ public static IMetaStoreClient getProxy(Configuration 
hiveConf, HiveMetaHookLoad
   public static IMetaStoreClient getProxy(Configuration hiveConf, 
HiveMetaHookLoader hookLoader,
       Map<String, Long> metaCallTimeMap, String mscClassName, boolean 
allowEmbedded)
           throws MetaException {
-
-    return getProxy(hiveConf,
-        new Class[] {Configuration.class, HiveMetaHookLoader.class, 
Boolean.class},
-        new Object[] {hiveConf, hookLoader, allowEmbedded},
-        metaCallTimeMap,
-        mscClassName
-    );
+    return
+        new HiveMetaStoreClientBuilder(hiveConf, mscClassName, allowEmbedded)
+        .withHooks(hookLoader)
+        .withRetry(metaCallTimeMap).build();
   }
 
   /**
@@ -148,26 +136,18 @@ public static IMetaStoreClient getProxy(Configuration 
hiveConf, Class<?>[] const
   public static IMetaStoreClient getProxy(Configuration hiveConf, Class<?>[] 
constructorArgTypes,
       Object[] constructorArgs, Map<String, Long> metaCallTimeMap,
       String mscClassName) throws MetaException {
-
     @SuppressWarnings("unchecked")
     Class<? extends IMetaStoreClient> baseClass =
         JavaUtils.getClass(mscClassName, IMetaStoreClient.class);
-
-    RetryingMetaStoreClient handler =
-        new RetryingMetaStoreClient(hiveConf, constructorArgTypes, 
constructorArgs,
-            metaCallTimeMap, baseClass);
-    return getProxy(baseClass.getInterfaces(), handler);
+    IMetaStoreClient baseClient = JavaUtils.newInstance(baseClass, 
constructorArgTypes, constructorArgs);
+    return new HiveMetaStoreClientBuilder(hiveConf, 
baseClient).withRetry(metaCallTimeMap).build();
   }
 
   public static IMetaStoreClient getProxy(Configuration hiveConf, Map<String, 
Long> metaCallTimeMap,
       IMetaStoreClient msClient) throws MetaException {
     RetryingMetaStoreClient handler =
         new RetryingMetaStoreClient(hiveConf, metaCallTimeMap, () -> msClient);
-    return getProxy(msClient.getClass().getInterfaces(), handler);
-  }
-
-  private static IMetaStoreClient getProxy(Class<?>[] interfaces,
-      RetryingMetaStoreClient handler) {
+    Class<?>[] interfaces = 
ClassUtils.getAllInterfaces(msClient.getClass()).toArray(new Class[0]);
     return (IMetaStoreClient) Proxy.newProxyInstance(
             RetryingMetaStoreClient.class.getClassLoader(), interfaces, 
handler);
   }
diff --git 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/builder/HiveMetaStoreClientBuilder.java
 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/builder/HiveMetaStoreClientBuilder.java
index 903a3543ee2..3f858d4f09b 100644
--- 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/builder/HiveMetaStoreClientBuilder.java
+++ 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/builder/HiveMetaStoreClientBuilder.java
@@ -19,6 +19,8 @@
 package org.apache.hadoop.hive.metastore.client.builder;
 
 import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.commons.lang3.reflect.ConstructorUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.metastore.HiveMetaHookLoader;
 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
@@ -32,28 +34,43 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.lang.reflect.Constructor;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Function;
 
 public class HiveMetaStoreClientBuilder {
   private static final Logger LOG = 
LoggerFactory.getLogger(HiveMetaStoreClientBuilder.class);
+  private static final Map<Class<? extends IMetaStoreClient>, 
MetaStoreClientFactory>
+      CLIENT_FACTORIES = new ConcurrentHashMap<>();
 
   private final Configuration conf;
   private IMetaStoreClient client;
 
-  public HiveMetaStoreClientBuilder(Configuration conf) {
-    this.conf = Objects.requireNonNull(conf);
+  public HiveMetaStoreClientBuilder(Configuration configuration) throws 
MetaException {
+    this(configuration, true);
   }
 
-  public HiveMetaStoreClientBuilder newClient(boolean allowEmbedded) throws 
MetaException {
-    this.client = createClient(conf, allowEmbedded);
-    return this;
+  public HiveMetaStoreClientBuilder(Configuration configuration, boolean 
allowEmbedded) throws MetaException {
+    this.conf = Objects.requireNonNull(configuration);
+    Class<? extends IMetaStoreClient> mscClass = MetastoreConf.getClass(
+        conf, MetastoreConf.ConfVars.METASTORE_CLIENT_IMPL,
+        ThriftHiveMetaStoreClient.class, IMetaStoreClient.class);
+    this.client = createClient(conf, mscClass, allowEmbedded);
   }
 
-  public HiveMetaStoreClientBuilder client(IMetaStoreClient client) {
-    this.client = client;
-    return this;
+  public HiveMetaStoreClientBuilder(Configuration configuration, String 
clientImpl,
+      boolean allowEmbedded) throws MetaException {
+    this.conf =  Objects.requireNonNull(configuration);
+    Class<? extends IMetaStoreClient> baseClass =
+        JavaUtils.getClass(clientImpl, IMetaStoreClient.class);
+    this.client = createClient(configuration, baseClass, allowEmbedded);
+  }
+
+  public HiveMetaStoreClientBuilder(Configuration configuration, 
IMetaStoreClient client) {
+    this.conf =  Objects.requireNonNull(configuration);
+    this.client = Objects.requireNonNull(client);
   }
 
   public HiveMetaStoreClientBuilder enhanceWith(Function<IMetaStoreClient, 
IMetaStoreClient> wrapperFunction) {
@@ -80,17 +97,15 @@ public IMetaStoreClient build() {
     return Objects.requireNonNull(client);
   }
 
-  private static IMetaStoreClient createClient(Configuration conf, boolean 
allowEmbedded) throws MetaException {
-    Class<? extends IMetaStoreClient> mscClass = MetastoreConf.getClass(
-        conf, MetastoreConf.ConfVars.METASTORE_CLIENT_IMPL,
-        ThriftHiveMetaStoreClient.class, IMetaStoreClient.class);
-    LOG.info("Using {} as a base MetaStoreClient", mscClass.getName());
-
-    IMetaStoreClient baseMetaStoreClient = null;
+  private static IMetaStoreClient createClient(Configuration conf,
+      Class<? extends IMetaStoreClient> mscClass, boolean allowEmbedded) 
throws MetaException {
     try {
-      baseMetaStoreClient = JavaUtils.newInstance(mscClass,
-          new Class[]{Configuration.class, boolean.class},
-          new Object[]{conf, allowEmbedded});
+      LOG.info("Using {} as a base MetaStoreClient", mscClass.getName());
+      MetaStoreClientFactory factory = CLIENT_FACTORIES.get(mscClass);
+      if (factory == null) {
+        CLIENT_FACTORIES.put(mscClass, factory = new 
MetaStoreClientFactory(mscClass));
+      }
+      return factory.createClient(conf, allowEmbedded);
     } catch (Throwable t) {
       // Reflection by JavaUtils will throw RuntimeException, try to get real 
MetaException here.
       Throwable rootCause = ExceptionUtils.getRootCause(t);
@@ -100,7 +115,34 @@ private static IMetaStoreClient createClient(Configuration 
conf, boolean allowEm
         throw new MetaException(rootCause.getMessage());
       }
     }
+  }
 
-    return baseMetaStoreClient;
+  private static class MetaStoreClientFactory {
+    private Constructor<? extends IMetaStoreClient> bestMatchingCtr;
+    private Function<Pair<Configuration, Boolean>, Object[]> argsTransformer;
+
+    MetaStoreClientFactory(Class<? extends IMetaStoreClient> mscClass) {
+      Constructor<? extends IMetaStoreClient> candidate =
+          ConstructorUtils.getMatchingAccessibleConstructor(mscClass, 
Configuration.class, boolean.class);
+      if (candidate != null) {
+        this.bestMatchingCtr = candidate;
+        this.argsTransformer = args -> new Object[] {args.getLeft(), (boolean) 
args.getRight()};
+      } else if ((candidate = 
ConstructorUtils.getMatchingAccessibleConstructor(mscClass, Configuration.class,
+          HiveMetaHookLoader.class, Boolean.class)) != null) {
+        this.bestMatchingCtr = candidate;
+        this.argsTransformer = args ->
+            new Object[] {args.getLeft(), null, 
Boolean.valueOf(args.getRight())};
+      } else if ((candidate = 
ConstructorUtils.getMatchingAccessibleConstructor(mscClass, 
Configuration.class)) != null) {
+        this.bestMatchingCtr = candidate;
+        this.argsTransformer = args -> new Object[] {args.getLeft()};
+      }
+      if (bestMatchingCtr == null) {
+        throw new RuntimeException("No matching constructor found for this 
IMetaStoreClient " + mscClass);
+      }
+    }
+
+    IMetaStoreClient createClient(Configuration conf, boolean allowEmbedded) 
throws Exception {
+      return bestMatchingCtr.newInstance(argsTransformer.apply(Pair.of(conf, 
allowEmbedded)));
+    }
   }
 }

Reply via email to