wecharyu commented on code in PR #6269:
URL: https://github.com/apache/hive/pull/6269#discussion_r2702549769


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/DropTableHandler.java:
##########
@@ -206,6 +210,10 @@ protected void afterExecute(DropTableResult result) throws 
MetaException, IOExce
       }
     } finally {
       super.afterExecute(result);
+      if (async) {
+        ms.shutdown();

Review Comment:
   ditto.



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/AbstractRequestHandler.java:
##########
@@ -107,45 +108,49 @@ public abstract class AbstractOperationHandler<T extends 
TBase, A extends Abstra
   protected final String id;
   private long timeout;
 
-  private AbstractOperationHandler(String id) {
+  private AbstractRequestHandler(String id) {
     this.id = id;
   }
 
-  AbstractOperationHandler(IHMSHandler handler, boolean async, T request) {
-    this.id = UUID.randomUUID().toString();
+  AbstractRequestHandler(IHMSHandler handler, boolean async, T request) {
+    this.id = UUID.randomUUID() + "-" + ID_GEN.incrementAndGet();
     this.handler = handler;
     this.request = request;
     this.async = async;
     this.timeout = MetastoreConf.getBoolVar(handler.getConf(), HIVE_IN_TEST) ? 
10 : 5000;
-    final Timer.Context timerContext;
-    if (getHandlerAlias() != null) {
-      Timer timer = Metrics.getOrCreateTimer(MetricsConstants.API_PREFIX + 
getHandlerAlias());
-      timerContext = timer != null ? timer.time() : null;
-    } else {
-      timerContext = null;
-    }
 
     if (async) {
-      OPID_TO_HANDLER.put(id, this);
+      ID_TO_HANDLER.put(id, this);
       this.executor = Executors.newFixedThreadPool(1, r -> {
         Thread thread = new Thread(r);
         thread.setDaemon(true);
-        thread.setName("OperationHandler[" + id + "]");
+        thread.setName("RequestHandler[" + id + "]");
         return thread;
       });
     } else {
       this.executor = MoreExecutors.newDirectExecutorService();
     }
+    this.future = executeRequest();
+  }
+
+  private Future<Result> executeRequest() {
+    Timer.Context timerContext;
+    if (StringUtils.isNotEmpty(getMetricAlias())) {
+      Timer timer = Metrics.getOrCreateTimer(MetricsConstants.API_PREFIX + 
getMetricAlias());

Review Comment:
   will multi reflection calls affect the performance?



##########
standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/JavaUtils.java:
##########
@@ -128,6 +128,18 @@ public static <T> void setField(T req, String methodName, 
Class[] argsCls, Objec
     }
   }
 
+  public static <T> T getField(Object payload, String methodName) {

Review Comment:
   Can we use a common method for both `setField` and `getField` like:
   ```java
   public static <T> T invokeMethod(Object payload, String methodName, 
Class<?>[] paramTypes, Object... args);
   ```



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/DropDatabaseHandler.java:
##########
@@ -457,12 +457,16 @@ protected void afterExecute(DropDatabaseResult result) 
throws MetaException, IOE
         }
       }
     } finally {
+      super.afterExecute(result);
+      if (async) {
+        rs.shutdown();

Review Comment:
   why cleanup rs here? it will be cleaned if connection is closed in 
`HMSHandler.cleanupHandlerContext()`.



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/AbstractRequestHandler.java:
##########
@@ -35,65 +37,64 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.hadoop.hive.metastore.IHMSHandler;
-import org.apache.hadoop.hive.metastore.api.AddPartitionsRequest;
 import org.apache.hadoop.hive.metastore.api.AsyncOperationResp;
-import org.apache.hadoop.hive.metastore.api.DropDatabaseRequest;
-import org.apache.hadoop.hive.metastore.api.DropPartitionsRequest;
-import org.apache.hadoop.hive.metastore.api.DropTableRequest;
 import org.apache.hadoop.hive.metastore.api.MetaException;
 import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
 import org.apache.hadoop.hive.metastore.metrics.Metrics;
 import org.apache.hadoop.hive.metastore.metrics.MetricsConstants;
 import org.apache.thrift.TBase;
 import org.apache.thrift.TException;
+import org.reflections.Reflections;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static 
org.apache.hadoop.hive.metastore.ExceptionHandler.handleException;
 import static 
org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars.HIVE_IN_TEST;
-
-public abstract class AbstractOperationHandler<T extends TBase, A extends 
AbstractOperationHandler.Result> {
-
-  private static final Logger LOG = 
LoggerFactory.getLogger(AbstractOperationHandler.class);
-  private static final Map<String, AbstractOperationHandler> OPID_TO_HANDLER = 
new ConcurrentHashMap<>();
-  private static final ScheduledExecutorService OPID_CLEANER = 
Executors.newScheduledThreadPool(1, r -> {
+import static org.apache.hadoop.hive.metastore.utils.JavaUtils.getField;
+import static org.apache.hadoop.hive.metastore.utils.JavaUtils.newInstance;
+
+public abstract class AbstractRequestHandler<T extends TBase, A extends 
AbstractRequestHandler.Result> {
+  private static final Logger LOG = 
LoggerFactory.getLogger(AbstractRequestHandler.class);
+  private static final Map<String, AbstractRequestHandler> ID_TO_HANDLER = new 
ConcurrentHashMap<>();
+  private static final AtomicLong ID_GEN = new AtomicLong(0);
+  private static final ScheduledExecutorService REQUEST_CLEANER = 
Executors.newScheduledThreadPool(1, r -> {
     Thread thread = new Thread(r);
     thread.setDaemon(true);
-    thread.setName("OperationHandler-Cleaner");
+    thread.setName("RequestHandler-Cleaner");
     return thread;
   });
 
   private static final Map<Class<? extends TBase>, HandlerFactory> 
REQ_FACTORIES = new ConcurrentHashMap<>();
   static {
-    REQ_FACTORIES.put(DropTableRequest.class, (base, request) -> {
-      DropTableRequest req = (DropTableRequest) request;
-      AbstractOperationHandler opHandler = ofCache(req.getId(), 
req.isCancel());
-      if (opHandler == null) {
-        opHandler = new DropTableHandler(base, req);
+    Set<Class<? extends AbstractRequestHandler>> handlerClasses =
+        new 
Reflections("org.apache.hadoop.hive.metastore.handler").getSubTypesOf(AbstractRequestHandler.class);
+    for (Class<? extends AbstractRequestHandler> clz : handlerClasses) {
+      if (Modifier.isAbstract(clz.getModifiers())) {
+        continue;
       }
-      return opHandler;
-    });
-
-    REQ_FACTORIES.put(DropDatabaseRequest.class, (base, request) -> {
-      DropDatabaseRequest req = (DropDatabaseRequest) request;
-      AbstractOperationHandler opHandler = ofCache(req.getId(), 
req.isCancel());
-      if (opHandler == null) {
-        opHandler = new DropDatabaseHandler(base, req);
+      RequestHandler handler = clz.getAnnotation(RequestHandler.class);
+      Class<? extends TBase> requestBody;
+      if (handler == null || (requestBody = handler.requestBody()) == null) {
+        continue;

Review Comment:
   Can we check this in `validateHandler()`? return request body if exists 
otherwise throw exception.



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/RequestHandler.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.hadoop.hive.metastore.handler;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.thrift.TBase;
+
[email protected]
+@Retention(RetentionPolicy.RUNTIME)
+public @interface RequestHandler {
+  Class<? extends TBase> requestBody();
+  boolean supportAsync() default false;
+  String id() default "getId";
+  String cancel() default "isCancel";

Review Comment:
   `id` and `cancel` is from request, it seems a little strange to set get such 
annotations for handler.



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

To unsubscribe, e-mail: [email protected]

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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to