Repository: sentry Updated Branches: refs/heads/sentry-ha-redesign 74e3913d7 -> c2f6fa609
SENTRY-1869: Try to use pool with idle connections first (Alex Kolbasov, reviewed by Vamsee Yarlagadda) Project: http://git-wip-us.apache.org/repos/asf/sentry/repo Commit: http://git-wip-us.apache.org/repos/asf/sentry/commit/c2f6fa60 Tree: http://git-wip-us.apache.org/repos/asf/sentry/tree/c2f6fa60 Diff: http://git-wip-us.apache.org/repos/asf/sentry/diff/c2f6fa60 Branch: refs/heads/sentry-ha-redesign Commit: c2f6fa609cb73416722d96447d955f2e399e93f7 Parents: 74e3913 Author: Alexander Kolbasov <[email protected]> Authored: Thu Jul 27 19:55:30 2017 +0200 Committer: Alexander Kolbasov <[email protected]> Committed: Thu Jul 27 19:55:44 2017 +0200 ---------------------------------------------------------------------- .../common/transport/SentryTransportPool.java | 60 +++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sentry/blob/c2f6fa60/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/core/common/transport/SentryTransportPool.java ---------------------------------------------------------------------- diff --git a/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/core/common/transport/SentryTransportPool.java b/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/core/common/transport/SentryTransportPool.java index 04a515a..80a8165 100644 --- a/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/core/common/transport/SentryTransportPool.java +++ b/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/core/common/transport/SentryTransportPool.java @@ -145,6 +145,15 @@ public final class SentryTransportPool implements AutoCloseable { poolConfig); } + /** + * Get an open transport instance. + * The instance can be connected to any of the available servers. + * We are trying to randomly load-balance between servers (unless it is + * disabled in configuration). + * + * @return connected transport + * @throws Exception if connection tto both servers fails + */ public TTransportWrapper getTransport() throws Exception { List<HostAndPort> servers; // If we are doing load balancing and there is more then one server, @@ -156,29 +165,40 @@ public final class SentryTransportPool implements AutoCloseable { servers = endpoints; } - // Try to get a connection from one of the pools + // Try to get a connection from one of the pools. Exception failure = null; - for(HostAndPort addr: servers) { - try { - TTransportWrapper transport = - isPoolEnabled ? - pool.borrowObject(addr) : - transportFactory.getTransport(addr); - LOGGER.debug("[{}] obtained transport {}", id, transport); - if (LOGGER.isDebugEnabled() && isPoolEnabled) { - LOGGER.debug("Currently {} active connections, {} idle connections", - pool.getNumActive(), pool.getNumIdle()); + boolean ignoreEmptyPool = true; + for (int attempt = 0; attempt < 2; attempt++) { + // First only attempt to borrow from pools which have some idle connections + // If this fails, try with all pools + for (HostAndPort addr : servers) { + if (isPoolEnabled && ignoreEmptyPool && (pool.getNumIdle(addr) == 0)) { + LOGGER.debug("Ignoring empty pool {}", addr); + ignoreEmptyPool = false; + continue; + } + try { + TTransportWrapper transport = + isPoolEnabled ? + pool.borrowObject(addr) : + transportFactory.getTransport(addr); + LOGGER.debug("[{}] obtained transport {}", id, transport); + if (LOGGER.isDebugEnabled() && isPoolEnabled) { + LOGGER.debug("Currently {} active connections, {} idle connections", + pool.getNumActive(), pool.getNumIdle()); + } + return transport; + } catch (IllegalStateException e) { + // Should not happen + LOGGER.error("Unexpected error from pool {}", id, e); + failure = e; + } catch (Exception e) { + LOGGER.error("Failed to obtain transport for {}: {}", + addr, e.getMessage()); + failure = e; } - return transport; - } catch (IllegalStateException e) { - // Should not happen - LOGGER.error("Unexpected error from pool {}", id, e); - failure = e; - } catch (Exception e) { - LOGGER.error("Failed to obtain transport for {}: {}", - addr, e.getMessage()); - failure = e; } + ignoreEmptyPool = false; } // Failed to borrow connect to any endpoint assert failure != null;
