public HBaseServiceHandler(final Configuration c, final UserProvider userProvider) throws IOException { this.conf = c; int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000); int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000); connectionCache = new ConnectionCache( conf, userProvider, cleanInterval, maxIdleTime); }
/** * Obtain HBaseAdmin. Creates the instance if it is not already created. */ protected Admin getAdmin() throws IOException { return connectionCache.getAdmin(); }
/** * Caller closes the table afterwards. */ Table getTable(String tableName) throws IOException { return connectionCache.getTable(tableName); }
/** * Caller doesn't close the admin afterwards. * We need to manage it and close it properly. */ public Admin getAdmin() throws IOException { ConnectionInfo connInfo = getCurrentConnection(); if (connInfo.admin == null) { Lock lock = locker.acquireLock(getEffectiveUser()); try { if (connInfo.admin == null) { connInfo.admin = connInfo.connection.getAdmin(); } } finally { lock.unlock(); } } return connInfo.admin; }
void setEffectiveUser(String effectiveUser) { connectionCache.setEffectiveUser(effectiveUser); }
/** * test for ConnectionCache cleaning expired Connection */ @Test public void testConnectionChore() throws Exception { UTIL.startMiniCluster(); //1s for clean interval & 5s for maxIdleTime ConnectionCache cache = new ConnectionCache(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()), 1000, 5000); ConnectionCache.ConnectionInfo info = cache.getCurrentConnection(); assertEquals(false, info.connection.isClosed()); Thread.sleep(7000); assertEquals(true, info.connection.isClosed()); UTIL.shutdownMiniCluster(); }
/** * Retrieve a regionLocator for the table. The user should close the RegionLocator. */ public RegionLocator getRegionLocator(byte[] tableName) throws IOException { return getCurrentConnection().connection.getRegionLocator(TableName.valueOf(tableName)); }
private RegionLocator getLocator(ByteBuffer tableName) { try { return connectionCache.getRegionLocator(byteBufferToByteArray(tableName)); } catch (IOException ie) { throw new RuntimeException(ie); } }
/** * Shutdown any services that need to stop */ void shutdown() { if (pauseMonitor != null) pauseMonitor.stop(); if (connectionCache != null) connectionCache.shutdown(); }
/** * Updates the access time for the current connection. Used to keep Connections alive for * long-lived scanners. * @return whether we successfully updated the last access time */ public boolean updateConnectionAccessTime() { String userName = getEffectiveUser(); ConnectionInfo connInfo = connections.get(userName); if (connInfo != null) { return connInfo.updateAccessTime(); } return false; }
@Override public List<TResult> getScannerRows(int scannerId, int numRows) throws TIOError, TIllegalArgument, TException { ResultScanner scanner = getScanner(scannerId); if (scanner == null) { TIllegalArgument ex = new TIllegalArgument(); ex.setMessage("Invalid scanner Id"); throw ex; } try { connectionCache.updateConnectionAccessTime(); return resultsFromHBase(scanner.next(numRows)); } catch (IOException e) { throw getTIOError(e); } }
public void setEffectiveUser(String effectiveUser) { connectionCache.setEffectiveUser(effectiveUser); }
/** * test for ConnectionCache cleaning expired Connection */ @Test public void testConnectionChore() throws Exception { UTIL.startMiniCluster(); //1s for clean interval & 5s for maxIdleTime ConnectionCache cache = new ConnectionCache(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()), 1000, 5000); ConnectionCache.ConnectionInfo info = cache.getCurrentConnection(); assertEquals(false, info.connection.isClosed()); Thread.sleep(7000); assertEquals(true, info.connection.isClosed()); UTIL.shutdownMiniCluster(); }
/** * Caller doesn't close the admin afterwards. * We need to manage it and close it properly. */ public Admin getAdmin() throws IOException { ConnectionInfo connInfo = getCurrentConnection(); if (connInfo.admin == null) { Lock lock = locker.acquireLock(getEffectiveUser()); try { if (connInfo.admin == null) { connInfo.admin = connInfo.connection.getAdmin(); } } finally { lock.unlock(); } } return connInfo.admin; }
/** * Caller closes the table afterwards. */ public Table getTable(String tableName) throws IOException { ConnectionInfo connInfo = getCurrentConnection(); return connInfo.connection.getTable(TableName.valueOf(tableName)); }
try (RegionLocator locator = connectionCache.getRegionLocator(getBytes(tableName))) { List<HRegionLocation> regionLocations = locator.getAllRegionLocations(); List<TRegionInfo> results = new ArrayList<>(regionLocations.size());
/** * Shutdown any services that need to stop */ void shutdown() { if (pauseMonitor != null) pauseMonitor.stop(); if (connectionCache != null) connectionCache.shutdown(); }
/** * Get the cached connection for the current user. * If none or timed out, create a new one. */ ConnectionInfo getCurrentConnection() throws IOException { String userName = getEffectiveUser(); ConnectionInfo connInfo = connections.get(userName); if (connInfo == null || !connInfo.updateAccessTime()) { Lock lock = locker.acquireLock(userName); try { connInfo = connections.get(userName); if (connInfo == null) { UserGroupInformation ugi = realUser; if (!userName.equals(realUserName)) { ugi = UserGroupInformation.createProxyUser(userName, realUser); } User user = userProvider.create(ugi); Connection conn = ConnectionFactory.createConnection(conf, user); connInfo = new ConnectionInfo(conn, userName); connections.put(userName, connInfo); } } finally { lock.unlock(); } } return connInfo; }
@Override public List<TResult> getScannerRows(int scannerId, int numRows) throws TIOError, TIllegalArgument, TException { ResultScanner scanner = getScanner(scannerId); if (scanner == null) { TIllegalArgument ex = new TIllegalArgument(); ex.setMessage("Invalid scanner Id"); throw ex; } try { connectionCache.updateConnectionAccessTime(); return resultsFromHBase(scanner.next(numRows)); } catch (IOException e) { throw getTIOError(e); } }
Admin getAdmin() throws IOException { return connectionCache.getAdmin(); }