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


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HMSHandler.java:
##########
@@ -4528,23 +3571,30 @@ public AddPartitionsResult 
add_partitions_req(AddPartitionsRequest request)
       // Make sure all the partitions have the catalog set as well
       request.getParts().forEach(p -> p.setCatName(catName));
       request.setCatName(catName);
-      List<Partition> parts = add_partitions_core(getMS(), request);
-      if (request.isNeedResult()) {
+      AddPartitionsHandler addPartsOp = AbstractOperationHandler.offer(this, 
request);
+      if (addPartsOp.success() && request.isNeedResult()) {
+        AddPartitionsHandler.AddPartitionsResult addPartsResult = 
addPartsOp.getResult();

Review Comment:
   Can we store the partition list in the `AddPartitionsResult` and return it 
directly here?



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HMSHandler.java:
##########
@@ -4528,23 +3571,30 @@ public AddPartitionsResult 
add_partitions_req(AddPartitionsRequest request)
       // Make sure all the partitions have the catalog set as well
       request.getParts().forEach(p -> p.setCatName(catName));
       request.setCatName(catName);

Review Comment:
   These checks can be moved to `beforeExecute()`.



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/handler/AbstractOperationHandler.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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 com.codahale.metrics.Timer;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.util.concurrent.MoreExecutors;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+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.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 -> {
+    Thread thread = new Thread(r);
+    thread.setDaemon(true);
+    thread.setName("OperationHandler-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);
+      }
+      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);
+      }
+      return opHandler;
+    });
+
+    REQ_FACTORIES.put(DropPartitionsRequest.class, (base, request) -> {
+      DropPartitionsRequest req = (DropPartitionsRequest) request;
+      return new DropPartitionsHandler(base, req);
+    });
+
+    REQ_FACTORIES.put(AddPartitionsRequest.class, (base, request) -> {
+      AddPartitionsRequest req = (AddPartitionsRequest) request;
+      return new AddPartitionsHandler(base, req);
+    });
+  }
+
+  private Result result;
+  private Future<Result> future;
+  private ExecutorService executor;
+  private final AtomicBoolean aborted = new AtomicBoolean();
+
+  protected T request;
+  protected boolean async;
+  protected IHMSHandler handler;
+  protected final String id;
+  private long timeout;
+
+  private AbstractOperationHandler(String id) {
+    this.id = id;
+  }
+
+  AbstractOperationHandler(IHMSHandler handler, boolean async, T request) {
+    this.id = UUID.randomUUID().toString();
+    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);
+      this.executor = Executors.newFixedThreadPool(1, r -> {
+        Thread thread = new Thread(r);
+        thread.setDaemon(true);
+        thread.setName("OperationHandler[" + id + "]");
+        return thread;
+      });
+    } else {
+      this.executor = MoreExecutors.newDirectExecutorService();
+    }
+
+    this.future =  executor.submit(() -> {
+      A resultV = null;
+      beforeExecute();
+      try {
+        resultV = execute();
+      } finally {
+        try {
+          if (async) {
+            OPID_CLEANER.schedule(() -> OPID_TO_HANDLER.remove(id), 1, 
TimeUnit.HOURS);
+          }
+          afterExecute(resultV);

Review Comment:
   If `afterExecute()` is needed only when the `execute()` is success, we can 
check the result here
   ```suggestion
             if (resultV != null && resultV.success()) {
               afterExecute(resultV);
             }```



##########
standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/security/HadoopThriftAuthBridge.java:
##########
@@ -646,11 +648,16 @@ public void process(final TProtocol inProt, final 
TProtocol outProt) throws TExc
          }
        }
 
-       UserGroupInformation clientUgi = null;
        try {
          if (useProxy) {
-           clientUgi = UserGroupInformation.createProxyUser(
-               endUser, UserGroupInformation.getLoginUser());
+           UserGroupInformation clientUgi =
+               UserGroupInformation.createProxyUser(endUser, 
UserGroupInformation.getLoginUser());
+           if (inProt.getTransport() instanceof TUGIContainingTransport 
ugiTransport) {
+             if (ugiTransport.getClientUGI() == null) {
+               ugiTransport.setClientUGI(clientUgi);
+             }
+             clientUgi = ugiTransport.getClientUGI();

Review Comment:
   Is this line unnecessary? `clientUgi` is already initialized.



-- 
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