Index: src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java (revision 1507512) +++ src/test/java/org/apache/hadoop/hbase/client/TestMetaScanner.java (working copy) @@ -192,7 +192,7 @@ while(!isStopped()) { try { NavigableMap regions = - MetaScanner.allTableRegions(TEST_UTIL.getConfiguration(), TABLENAME, false); + MetaScanner.allTableRegions(TEST_UTIL.getConfiguration(), null, TABLENAME, false); LOG.info("-------"); byte[] lastEndKey = HConstants.EMPTY_START_ROW; Index: src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java (revision 1507512) +++ src/test/java/org/apache/hadoop/hbase/regionserver/TestEndToEndSplitTransaction.java (working copy) @@ -214,7 +214,8 @@ try { Random random = new Random(); for (int i=0; i< 5; i++) { - NavigableMap regions = MetaScanner.allTableRegions(conf, tableName, false); + NavigableMap regions = MetaScanner.allTableRegions(conf, null, + tableName, false); if (regions.size() == 0) { continue; } @@ -286,7 +287,7 @@ void verifyRegionsUsingMetaScanner() throws Exception { //MetaScanner.allTableRegions() - NavigableMap regions = MetaScanner.allTableRegions(conf, tableName, + NavigableMap regions = MetaScanner.allTableRegions(conf, null, tableName, false); verifyTableRegions(regions.keySet()); Index: src/test/java/org/apache/hadoop/hbase/master/TestRestartCluster.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/master/TestRestartCluster.java (revision 1507512) +++ src/test/java/org/apache/hadoop/hbase/master/TestRestartCluster.java (working copy) @@ -102,7 +102,7 @@ } List allRegions = - MetaScanner.listAllRegions(UTIL.getConfiguration()); + MetaScanner.listAllRegions(UTIL.getConfiguration(), true); assertEquals(3, allRegions.size()); LOG.info("\n\nShutting down cluster"); @@ -118,7 +118,7 @@ // Otherwise we're reusing an HConnection that has gone stale because // the shutdown of the cluster also called shut of the connection. allRegions = MetaScanner. - listAllRegions(new Configuration(UTIL.getConfiguration())); + listAllRegions(new Configuration(UTIL.getConfiguration()), true); assertEquals(3, allRegions.size()); LOG.info("\n\nWaiting for tables to be available"); Index: src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java (revision 1507512) +++ src/main/java/org/apache/hadoop/hbase/rest/RegionsResource.java (working copy) @@ -76,7 +76,7 @@ String tableName = tableResource.getName(); TableInfoModel model = new TableInfoModel(tableName); Map regions = MetaScanner.allTableRegions( - servlet.getConfiguration(), Bytes.toBytes(tableName), false); + servlet.getConfiguration(), null, Bytes.toBytes(tableName), false); for (Map.Entry e: regions.entrySet()) { HRegionInfo hri = e.getKey(); ServerName addr = e.getValue(); Index: src/main/java/org/apache/hadoop/hbase/util/HBaseFsck.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/util/HBaseFsck.java (revision 1507512) +++ src/main/java/org/apache/hadoop/hbase/util/HBaseFsck.java (working copy) @@ -2593,7 +2593,7 @@ }; // Scan -ROOT- to pick up META regions - MetaScanner.metaScan(getConf(), visitor, null, null, + MetaScanner.metaScan(getConf(), null, visitor, null, null, Integer.MAX_VALUE, HConstants.ROOT_TABLE_NAME); if (!checkMetaOnly) { Index: src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java (revision 1507512) +++ src/main/java/org/apache/hadoop/hbase/client/MetaScanner.java (working copy) @@ -85,6 +85,22 @@ } /** + * Scans the meta table and calls a visitor on each RowResult and uses a empty + * start row value as table name. + * + * @param configuration conf + * @param connection connection to be used internally (null not allowed) + * @param visitor A custom visitor + * @throws IOException e + */ + public static void metaScan(Configuration configuration, HConnection connection, + MetaScannerVisitor visitor, byte [] userTableName) + throws IOException { + metaScan(configuration, connection, visitor, userTableName, null, Integer.MAX_VALUE, + HConstants.META_TABLE_NAME); + } + + /** * Scans the meta table and calls a visitor on each RowResult. Uses a table * name and a row name to locate meta regions. And it only scans at most * rowLimit of rows. @@ -103,7 +119,7 @@ MetaScannerVisitor visitor, byte [] userTableName, byte[] row, int rowLimit) throws IOException { - metaScan(configuration, visitor, userTableName, row, rowLimit, + metaScan(configuration, null, visitor, userTableName, row, rowLimit, HConstants.META_TABLE_NAME); } @@ -113,6 +129,7 @@ * rowLimit of rows. * * @param configuration HBase configuration. + * @param connection connection to be used internally (null not allowed) * @param visitor Visitor object. Closes the visitor before returning. * @param tableName User table name in meta table to start scan at. Pass * null if not interested in a particular table. @@ -123,42 +140,28 @@ * @param metaTableName Meta table to scan, root or meta. * @throws IOException e */ - public static void metaScan(Configuration configuration, - final MetaScannerVisitor visitor, final byte[] tableName, - final byte[] row, final int rowLimit, final byte[] metaTableName) - throws IOException { - try { - HConnectionManager.execute(new HConnectable(configuration) { - @Override - public Void connect(HConnection connection) throws IOException { - metaScan(conf, connection, visitor, tableName, row, rowLimit, - metaTableName); - return null; - } - }); - } finally { - visitor.close(); - } - } - - private static void metaScan(Configuration configuration, HConnection connection, + public static void metaScan(Configuration configuration, HConnection connection, MetaScannerVisitor visitor, byte [] tableName, byte[] row, int rowLimit, final byte [] metaTableName) throws IOException { - int rowUpperLimit = rowLimit > 0 ? rowLimit: Integer.MAX_VALUE; + HTable metaTable = null; + try { + if (connection == null) { + metaTable = new HTable(configuration, HConstants.META_TABLE_NAME, null); + } else { + metaTable = new HTable(HConstants.META_TABLE_NAME, connection, null); + } + int rowUpperLimit = rowLimit > 0 ? rowLimit: Integer.MAX_VALUE; - // if row is not null, we want to use the startKey of the row's region as - // the startRow for the meta scan. - byte[] startRow; - if (row != null) { - // Scan starting at a particular row in a particular table - assert tableName != null; - byte[] searchRow = - HRegionInfo.createRegionName(tableName, row, HConstants.NINES, - false); - HTable metaTable = null; - try { - metaTable = new HTable(configuration, HConstants.META_TABLE_NAME); + // if row is not null, we want to use the startKey of the row's region as + // the startRow for the meta scan. + byte[] startRow; + if (row != null) { + // Scan starting at a particular row in a particular table + assert tableName != null; + byte[] searchRow = + HRegionInfo.createRegionName(tableName, row, HConstants.NINES, + false); Result startRowResult = metaTable.getRowOrBefore(searchRow, HConstants.CATALOG_FAMILY); if (startRowResult == null) { @@ -176,82 +179,72 @@ byte[] rowBefore = regionInfo.getStartKey(); startRow = HRegionInfo.createRegionName(tableName, rowBefore, HConstants.ZEROES, false); - } finally { - if (metaTable != null) { - metaTable.close(); - } + } else if (tableName == null || tableName.length == 0) { + // Full META scan + startRow = HConstants.EMPTY_START_ROW; + } else { + // Scan META for an entire table + startRow = HRegionInfo.createRegionName( + tableName, HConstants.EMPTY_START_ROW, HConstants.ZEROES, false); } - } else if (tableName == null || tableName.length == 0) { - // Full META scan - startRow = HConstants.EMPTY_START_ROW; - } else { - // Scan META for an entire table - startRow = HRegionInfo.createRegionName( - tableName, HConstants.EMPTY_START_ROW, HConstants.ZEROES, false); - } - // Scan over each meta region - ScannerCallable callable; - int rows = Math.min(rowLimit, configuration.getInt( - HConstants.HBASE_META_SCANNER_CACHING, - HConstants.DEFAULT_HBASE_META_SCANNER_CACHING)); - do { - final Scan scan = new Scan(startRow).addFamily(HConstants.CATALOG_FAMILY); - if (LOG.isDebugEnabled()) { - LOG.debug("Scanning " + Bytes.toString(metaTableName) + - " starting at row=" + Bytes.toStringBinary(startRow) + " for max=" + - rowUpperLimit + " rows using " + connection.toString()); - } - callable = new ScannerCallable(connection, metaTableName, scan, null); - // Open scanner - callable.withRetries(); + // Scan over each meta region + ScannerCallable callable; + int rows = Math.min(rowLimit, configuration.getInt( + HConstants.HBASE_META_SCANNER_CACHING, + HConstants.DEFAULT_HBASE_META_SCANNER_CACHING)); + do { + final Scan scan = new Scan(startRow).addFamily(HConstants.CATALOG_FAMILY); + if (LOG.isDebugEnabled()) { + LOG.debug("Scanning " + Bytes.toString(metaTableName) + + " starting at row=" + Bytes.toStringBinary(startRow) + " for max=" + + rowUpperLimit + " rows using " + metaTable.getConnection().toString()); + } + callable = new ScannerCallable(metaTable.getConnection(), metaTableName, scan, null); + // Open scanner + callable.withRetries(); - int processedRows = 0; - try { - callable.setCaching(rows); - done: do { - if (processedRows >= rowUpperLimit) { - break; - } - //we have all the rows here - Result [] rrs = callable.withRetries(); - if (rrs == null || rrs.length == 0 || rrs[0].size() == 0) { - break; //exit completely - } - for (Result rr : rrs) { + int processedRows = 0; + try { + callable.setCaching(rows); + done: do { if (processedRows >= rowUpperLimit) { - break done; + break; } - if (!visitor.processRow(rr)) - break done; //exit completely - processedRows++; - } - //here, we didn't break anywhere. Check if we have more rows - } while(true); - // Advance the startRow to the end key of the current region - startRow = callable.getHRegionInfo().getEndKey(); - } finally { - // Close scanner - callable.setClose(); - callable.withRetries(); + //we have all the rows here + Result [] rrs = callable.withRetries(); + if (rrs == null || rrs.length == 0 || rrs[0].size() == 0) { + break; //exit completely + } + for (Result rr : rrs) { + if (processedRows >= rowUpperLimit) { + break done; + } + if (!visitor.processRow(rr)) + break done; //exit completely + processedRows++; + } + //here, we didn't break anywhere. Check if we have more rows + } while(true); + // Advance the startRow to the end key of the current region + startRow = callable.getHRegionInfo().getEndKey(); + } finally { + // Close scanner + callable.setClose(); + callable.withRetries(); + } + } while (Bytes.compareTo(startRow, HConstants.LAST_ROW) != 0); + } finally { + visitor.close(); + if (metaTable != null) { + metaTable.close(); } - } while (Bytes.compareTo(startRow, HConstants.LAST_ROW) != 0); + } } /** * Lists all of the regions currently in META. * @param conf - * @return List of all user-space regions. - * @throws IOException - */ - public static List listAllRegions(Configuration conf) - throws IOException { - return listAllRegions(conf, true); - } - - /** - * Lists all of the regions currently in META. - * @param conf * @param offlined True if we are to include offlined regions, false and we'll * leave out offlined regions from returned list. * @return List of all user-space regions. @@ -286,13 +279,14 @@ /** * Lists all of the table regions currently in META. * @param conf + * @param connection connection to be used internally (null to create a new connection) * @param offlined True if we are to include offlined regions, false and we'll * leave out offlined regions from returned list. * @return Map of all user-space regions to servers * @throws IOException */ public static NavigableMap allTableRegions(Configuration conf, - final byte [] tablename, final boolean offlined) throws IOException { + HConnection connection, final byte[] tablename, final boolean offlined) throws IOException { final NavigableMap regions = new TreeMap(); MetaScannerVisitor visitor = new TableMetaScannerVisitor(conf, tablename) { @@ -321,7 +315,7 @@ return true; } }; - metaScan(conf, visitor, tablename); + metaScan(conf, connection, visitor, tablename); return regions; } Index: src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java (revision 1507512) +++ src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java (working copy) @@ -802,7 +802,7 @@ return true; } }; - MetaScanner.metaScan(conf, visitor); + MetaScanner.metaScan(conf, this, visitor, null); return available.get() && (regionCount.get() > 0); } @@ -842,8 +842,8 @@ @Override public List locateRegions(final byte[] tableName, final boolean useCache, final boolean offlined) throws IOException { - NavigableMap regions = MetaScanner.allTableRegions(conf, tableName, - offlined); + NavigableMap regions = MetaScanner.allTableRegions(conf, this, + tableName, offlined); final List locations = new ArrayList(); for (HRegionInfo regionInfo : regions.keySet()) { locations.add(locateRegion(tableName, regionInfo.getStartKey(), useCache, true)); @@ -955,8 +955,8 @@ }; try { // pre-fetch certain number of regions info at region cache. - MetaScanner.metaScan(conf, visitor, tableName, row, - this.prefetchRegionLimit); + MetaScanner.metaScan(conf, this, visitor, tableName, row, + this.prefetchRegionLimit, HConstants.META_TABLE_NAME); } catch (IOException e) { LOG.warn("Encountered problems when prefetch META table: ", e); } Index: src/main/java/org/apache/hadoop/hbase/client/HTable.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/HTable.java (revision 1507512) +++ src/main/java/org/apache/hadoop/hbase/client/HTable.java (working copy) @@ -210,9 +210,6 @@ */ public HTable(final byte[] tableName, final HConnection connection, final ExecutorService pool) throws IOException { - if (pool == null || pool.isShutdown()) { - throw new IllegalArgumentException("Pool is null or shut down."); - } if (connection == null || connection.isClosed()) { throw new IllegalArgumentException("Connection is null or closed."); } @@ -481,7 +478,7 @@ * @throws IOException if a remote or network exception occurs */ public NavigableMap getRegionLocations() throws IOException { - return MetaScanner.allTableRegions(getConfiguration(), getTableName(), false); + return MetaScanner.allTableRegions(getConfiguration(), this.connection, getTableName(), false); } /**