/** * Return the transport used by the client to the shared pool. * * @param iface * The Client being returned or null. */ public static void returnClient(TServiceClient iface) { // Eew... the typing here is horrible if (iface != null) { ThriftTransportPool.getInstance().returnTransport(iface.getInputProtocol().getTransport()); } }
private static synchronized void enable() { if (instance == null) { // this code intentionally does not start the thread that closes idle connections. That thread // is created the first time something attempts to use this service. instance = new ThriftTransportPool(); } }
/** * Create a Thrift client using the given factory with a pooled transport (if available) using the * address, client context and timeout * * @param factory * Thrift client factory * @param address * Server address for client to connect to * @param context * RPC options * @param timeout * Socket timeout which overrides the ClientContext timeout */ public static <T extends TServiceClient> T getClient(TServiceClientFactory<T> factory, HostAndPort address, ClientContext context, long timeout) throws TTransportException { TTransport transport = ThriftTransportPool.getInstance().getTransport(address, timeout, context); return createClient(factory, transport); }
Pair<String,TTransport> pair = ThriftTransportPool.getInstance().getAnyTransport(servers, preferCachedConnections); CT client = ThriftUtil.createClient(factory, pair.getSecond());
log.info("Instance {}", getInstanceID()); timeKeeper = new MasterTime(this); ThriftTransportPool.getInstance() .setIdleTime(aconf.getTimeInMillis(Property.GENERAL_RPC_TIMEOUT)); tserverSet = new LiveTServerSet(context, this); this.tabletBalancer = Property.createInstanceFromPropertyName(aconf,
private TTransport getTransport(ThriftTransportKey cacheKey) throws TTransportException { // compute hash code outside of lock, this lowers the time the lock is held cacheKey.precomputeHashCode(); synchronized (this) { // atomically reserve location if it exist in cache CachedConnections ccl = getCache().get(cacheKey); if (ccl == null) { ccl = new CachedConnections(); getCache().put(cacheKey, ccl); } CachedConnection cachedConnection = ccl.reserveAny(); if (cachedConnection != null) { log.trace("Using existing connection to {}", cacheKey.getServer()); return cachedConnection.transport; } } return createNewTransport(cacheKey); }
private void shutdown() { Thread ctl; synchronized (this) { if (cache == null) return; // close any connections in the pool... even ones that are in use for (CachedConnections cachedConn : getCache().values()) { for (CachedConnection cc : Iterables.concat(cachedConn.reserved.values(), cachedConn.unreserved)) { try { cc.transport.close(); } catch (Exception e) { log.debug("Error closing transport during shutdown", e); } } } // this will render the pool unusable and cause the background thread to exit this.cache = null; ctl = checkThread; } if (ctl != null) { try { ctl.interrupt(); ctl.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
private static synchronized void disable() { if (instance != null) { try { instance.shutdown(); } finally { instance = null; } } }
public static synchronized ThriftTransportPool getInstance() { Preconditions.checkState(instance != null, "The Accumulo singleton for connection pooling is disabled. This is likely caused by all " + "AccumuloClients being closed or garbage collected."); instance.startCheckerThread(); return instance; }
public TTransport getTransport(HostAndPort location, long milliseconds, ClientContext context) throws TTransportException { return getTransport(new ThriftTransportKey(location, milliseconds, context)); }
serversSet.retainAll(getCache().keySet()); CachedConnection cachedConnection = getCache().get(ttk).reserveAny(); if (cachedConnection != null) { final String serverAddr = ttk.getServer().toString(); CachedConnections cachedConns = getCache().get(ttk); if (cachedConns != null) { CachedConnection cachedConnection = cachedConns.reserveAny(); return new Pair<>(ttk.getServer().toString(), createNewTransport(ttk)); } catch (TTransportException tte) { log.debug("Failed to connect to {}", servers.get(index), tte);
private TTransport createNewTransport(ThriftTransportKey cacheKey) throws TTransportException { TTransport transport = ThriftUtil.createClientTransport(cacheKey.getServer(), (int) cacheKey.getTimeout(), cacheKey.getSslParams(), cacheKey.getSaslParams()); log.trace("Creating new connection to connection to {}", cacheKey.getServer()); CachedTTransport tsc = new CachedTTransport(transport, cacheKey); CachedConnection cc = new CachedConnection(tsc); cc.reserve(); try { synchronized (this) { CachedConnections cachedConns = getCache().get(cacheKey); if (cachedConns == null) { cachedConns = new CachedConnections(); getCache().put(cacheKey, cachedConns); } cachedConns.reserved.put(cc.transport, cc); } } catch (TransportPoolShutdownException e) { cc.transport.close(); throw e; } return cc.transport; }
public static void close(MasterClientService.Iface iface) { TServiceClient client = (TServiceClient) iface; if (client != null && client.getInputProtocol() != null && client.getInputProtocol().getTransport() != null) { ThriftTransportPool.getInstance().returnTransport(client.getInputProtocol().getTransport()); } else { log.debug("Attempt to close null connection to the master", new Exception()); } }
/** * Create a Thrift client using the given factory with a pooled transport (if available), the * address and client context. Client timeout is extracted from the ClientContext * * @param factory * Thrift client factory * @param address * Server address for client to connect to * @param context * RPC options */ public static <T extends TServiceClient> T getClient(TServiceClientFactory<T> factory, HostAndPort address, ClientContext context) throws TTransportException { TTransport transport = ThriftTransportPool.getInstance().getTransport(address, context.getClientTimeoutInMillis(), context); return createClient(factory, transport); }
CachedConnections cachedConns = getCache().get(ctsc.getCacheKey()); if (cachedConns != null) { CachedConnection cachedConnection = cachedConns.reserved.remove(ctsc);
private static void close(TServiceClient client) { if (client != null && client.getInputProtocol() != null && client.getInputProtocol().getTransport() != null) { ThriftTransportPool.getInstance().returnTransport(client.getInputProtocol().getTransport()); } else { log.debug("Attempt to close null connection to the remote system", new Exception()); } }
public static void close(TServiceClient client) { if (client != null && client.getInputProtocol() != null && client.getInputProtocol().getTransport() != null) { ThriftTransportPool.getInstance().returnTransport(client.getInputProtocol().getTransport()); } else { log.debug("Attempt to close null connection to a server", new Exception()); } } }
throw new IOException(e); } finally { ThriftTransportPool.getInstance().returnTransport(transport);