You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/05/26 03:33:35 UTC

[GitHub] [iceberg] StefanXiepj commented on a change in pull request #2325: Hive: Use HiveClientPool cache instead of HiveCatalog global cache.

StefanXiepj commented on a change in pull request #2325:
URL: https://github.com/apache/iceberg/pull/2325#discussion_r639376971



##########
File path: hive-metastore/src/main/java/org/apache/iceberg/hive/ClientPoolImpl.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.iceberg.hive;
+
+import java.io.Closeable;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class ClientPoolImpl<C, E extends Exception> implements Closeable, ClientPool<C, E> {
+  private static final Logger LOG = LoggerFactory.getLogger(ClientPoolImpl.class);
+
+  private final int poolSize;
+  private final Deque<C> clients;
+  private final Class<? extends E> reconnectExc;
+  private final Object signal = new Object();
+  private volatile int currentSize;
+  private boolean closed;
+
+  ClientPoolImpl(int poolSize, Class<? extends E> reconnectExc) {
+    this.poolSize = poolSize;
+    this.reconnectExc = reconnectExc;
+    this.clients = new ArrayDeque<>(poolSize);
+    this.currentSize = 0;
+    this.closed = false;
+  }
+
+  @Override
+  public <R> R run(Action<R, C, E> action) throws E, InterruptedException {
+    C client = get();
+    try {
+      return action.run(client);
+
+    } catch (Exception exc) {
+      if (isConnectionException(exc)) {
+        try {
+          client = reconnect(client);
+        } catch (Exception ignored) {
+          // if reconnection throws any exception, rethrow the original failure
+          throw reconnectExc.cast(exc);
+        }
+
+        return action.run(client);
+      }
+
+      throw exc;
+
+    } finally {
+      release(client);
+    }
+  }
+
+  protected abstract C newClient();
+
+  protected abstract C reconnect(C client);
+
+  protected boolean isConnectionException(Exception exc) {
+    return reconnectExc.isInstance(exc);
+  }
+
+  protected abstract void close(C client);
+
+  @Override
+  public void close() {
+    this.closed = true;
+    try {
+      while (currentSize > 0) {
+        if (!clients.isEmpty()) {
+          synchronized (this) {
+            if (!clients.isEmpty()) {
+              C client = clients.removeFirst();
+              close(client);
+              currentSize -= 1;
+            }
+          }
+        }
+        if (clients.isEmpty() && currentSize > 0) {
+          // wake every second in case this missed the signal
+          synchronized (signal) {
+            signal.wait(1000);
+          }
+        }
+      }
+
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+      LOG.warn("Interrupted while shutting down pool. Some clients may not be closed.", e);

Review comment:
       hi,why not catching InterruptedException in the while-loop and continue to close others client? or thrown exception directly?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org