Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 46A26200CC5 for ; Mon, 26 Jun 2017 17:01:25 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 45417160BFC; Mon, 26 Jun 2017 15:01:25 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 4C4D3160BDE for ; Mon, 26 Jun 2017 17:01:23 +0200 (CEST) Received: (qmail 77866 invoked by uid 500); 26 Jun 2017 15:01:19 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 77146 invoked by uid 99); 26 Jun 2017 15:01:18 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 26 Jun 2017 15:01:18 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id EB4A7E96F5; Mon, 26 Jun 2017 15:01:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: git-site-role@apache.org To: commits@hbase.apache.org Date: Mon, 26 Jun 2017 15:01:38 -0000 Message-Id: In-Reply-To: <7f7d7944d7124677bf1e8939f6b5d72a@git.apache.org> References: <7f7d7944d7124677bf1e8939f6b5d72a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [22/51] [partial] hbase-site git commit: Published site at 82d554e3783372cc6b05489452c815b57c06f6cd. archived-at: Mon, 26 Jun 2017 15:01:25 -0000 http://git-wip-us.apache.org/repos/asf/hbase-site/blob/aecb1286/devapidocs/src-html/org/apache/hadoop/hbase/MetaTableAccessor.QueryType.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/MetaTableAccessor.QueryType.html b/devapidocs/src-html/org/apache/hadoop/hbase/MetaTableAccessor.QueryType.html index e65748d..91a0ffa 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/MetaTableAccessor.QueryType.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/MetaTableAccessor.QueryType.html @@ -372,1874 +372,1873 @@ 364 * is stored in the name, so the returned object should only be used for the fields 365 * in the regionName. 366 */ -367 protected static HRegionInfo parseRegionInfoFromRegionName(byte[] regionName) -368 throws IOException { -369 byte[][] fields = HRegionInfo.parseRegionName(regionName); -370 long regionId = Long.parseLong(Bytes.toString(fields[2])); -371 int replicaId = fields.length > 3 ? Integer.parseInt(Bytes.toString(fields[3]), 16) : 0; -372 return new HRegionInfo( -373 TableName.valueOf(fields[0]), fields[1], fields[1], false, regionId, replicaId); -374 } -375 -376 /** -377 * Gets the result in hbase:meta for the specified region. -378 * @param connection connection we're using -379 * @param regionName region we're looking for -380 * @return result of the specified region -381 * @throws IOException -382 */ -383 public static Result getRegionResult(Connection connection, -384 byte[] regionName) throws IOException { -385 Get get = new Get(regionName); -386 get.addFamily(HConstants.CATALOG_FAMILY); -387 return get(getMetaHTable(connection), get); -388 } -389 -390 /** -391 * Get regions from the merge qualifier of the specified merged region -392 * @return null if it doesn't contain merge qualifier, else two merge regions -393 * @throws IOException -394 */ -395 @Nullable -396 public static Pair<HRegionInfo, HRegionInfo> getRegionsFromMergeQualifier( -397 Connection connection, byte[] regionName) throws IOException { -398 Result result = getRegionResult(connection, regionName); -399 HRegionInfo mergeA = getHRegionInfo(result, HConstants.MERGEA_QUALIFIER); -400 HRegionInfo mergeB = getHRegionInfo(result, HConstants.MERGEB_QUALIFIER); -401 if (mergeA == null && mergeB == null) { -402 return null; -403 } -404 return new Pair<>(mergeA, mergeB); -405 } -406 -407 /** -408 * Checks if the specified table exists. Looks at the hbase:meta table hosted on -409 * the specified server. -410 * @param connection connection we're using -411 * @param tableName table to check -412 * @return true if the table exists in meta, false if not -413 * @throws IOException -414 */ -415 public static boolean tableExists(Connection connection, -416 final TableName tableName) -417 throws IOException { -418 // Catalog tables always exist. -419 return tableName.equals(TableName.META_TABLE_NAME) -420 || getTableState(connection, tableName) != null; -421 } -422 -423 /** -424 * Lists all of the regions currently in META. -425 * -426 * @param connection to connect with -427 * @param excludeOfflinedSplitParents False if we are to include offlined/splitparents regions, -428 * true and we'll leave out offlined regions from returned list -429 * @return List of all user-space regions. -430 * @throws IOException -431 */ -432 @VisibleForTesting -433 public static List<HRegionInfo> getAllRegions(Connection connection, -434 boolean excludeOfflinedSplitParents) -435 throws IOException { -436 List<Pair<HRegionInfo, ServerName>> result; -437 -438 result = getTableRegionsAndLocations(connection, null, -439 excludeOfflinedSplitParents); -440 -441 return getListOfHRegionInfos(result); -442 -443 } -444 -445 /** -446 * Gets all of the regions of the specified table. Do not use this method -447 * to get meta table regions, use methods in MetaTableLocator instead. -448 * @param connection connection we're using -449 * @param tableName table we're looking for -450 * @return Ordered list of {@link HRegionInfo}. -451 * @throws IOException -452 */ -453 public static List<HRegionInfo> getTableRegions(Connection connection, TableName tableName) -454 throws IOException { -455 return getTableRegions(connection, tableName, false); -456 } -457 -458 /** -459 * Gets all of the regions of the specified table. Do not use this method -460 * to get meta table regions, use methods in MetaTableLocator instead. -461 * @param connection connection we're using -462 * @param tableName table we're looking for -463 * @param excludeOfflinedSplitParents If true, do not include offlined split -464 * parents in the return. -465 * @return Ordered list of {@link HRegionInfo}. -466 * @throws IOException -467 */ -468 public static List<HRegionInfo> getTableRegions(Connection connection, -469 TableName tableName, final boolean excludeOfflinedSplitParents) -470 throws IOException { -471 List<Pair<HRegionInfo, ServerName>> result = -472 getTableRegionsAndLocations(connection, tableName, excludeOfflinedSplitParents); -473 return getListOfHRegionInfos(result); -474 } -475 -476 @Nullable -477 static List<HRegionInfo> getListOfHRegionInfos(final List<Pair<HRegionInfo, ServerName>> pairs) { -478 if (pairs == null || pairs.isEmpty()) return null; -479 List<HRegionInfo> result = new ArrayList<>(pairs.size()); -480 for (Pair<HRegionInfo, ServerName> pair: pairs) { -481 result.add(pair.getFirst()); -482 } -483 return result; -484 } -485 -486 /** -487 * @param current region of current table we're working with -488 * @param tableName table we're checking against -489 * @return True if <code>current</code> tablename is equal to -490 * <code>tableName</code> -491 */ -492 static boolean isInsideTable(final HRegionInfo current, final TableName tableName) { -493 return tableName.equals(current.getTable()); -494 } -495 -496 /** -497 * @param tableName table we're working with -498 * @return start row for scanning META according to query type -499 */ -500 public static byte[] getTableStartRowForMeta(TableName tableName, QueryType type) { -501 if (tableName == null) { -502 return null; -503 } -504 switch (type) { -505 case REGION: -506 byte[] startRow = new byte[tableName.getName().length + 2]; -507 System.arraycopy(tableName.getName(), 0, startRow, 0, tableName.getName().length); -508 startRow[startRow.length - 2] = HConstants.DELIMITER; -509 startRow[startRow.length - 1] = HConstants.DELIMITER; -510 return startRow; -511 case ALL: -512 case TABLE: -513 default: -514 return tableName.getName(); -515 } -516 } -517 -518 /** -519 * @param tableName table we're working with -520 * @return stop row for scanning META according to query type -521 */ -522 public static byte[] getTableStopRowForMeta(TableName tableName, QueryType type) { -523 if (tableName == null) { -524 return null; -525 } -526 final byte[] stopRow; -527 switch (type) { -528 case REGION: -529 stopRow = new byte[tableName.getName().length + 3]; -530 System.arraycopy(tableName.getName(), 0, stopRow, 0, tableName.getName().length); -531 stopRow[stopRow.length - 3] = ' '; -532 stopRow[stopRow.length - 2] = HConstants.DELIMITER; -533 stopRow[stopRow.length - 1] = HConstants.DELIMITER; -534 break; -535 case ALL: -536 case TABLE: -537 default: -538 stopRow = new byte[tableName.getName().length + 1]; -539 System.arraycopy(tableName.getName(), 0, stopRow, 0, tableName.getName().length); -540 stopRow[stopRow.length - 1] = ' '; -541 break; -542 } -543 return stopRow; -544 } -545 -546 /** -547 * This method creates a Scan object that will only scan catalog rows that -548 * belong to the specified table. It doesn't specify any columns. -549 * This is a better alternative to just using a start row and scan until -550 * it hits a new table since that requires parsing the HRI to get the table -551 * name. -552 * @param tableName bytes of table's name -553 * @return configured Scan object -554 */ -555 @Deprecated -556 public static Scan getScanForTableName(Connection connection, TableName tableName) { -557 // Start key is just the table name with delimiters -558 byte[] startKey = getTableStartRowForMeta(tableName, QueryType.REGION); -559 // Stop key appends the smallest possible char to the table name -560 byte[] stopKey = getTableStopRowForMeta(tableName, QueryType.REGION); -561 -562 Scan scan = getMetaScan(connection, -1); -563 scan.setStartRow(startKey); -564 scan.setStopRow(stopKey); -565 return scan; -566 } -567 -568 private static Scan getMetaScan(Connection connection, int rowUpperLimit) { -569 Scan scan = new Scan(); -570 int scannerCaching = connection.getConfiguration() -571 .getInt(HConstants.HBASE_META_SCANNER_CACHING, -572 HConstants.DEFAULT_HBASE_META_SCANNER_CACHING); -573 if (connection.getConfiguration().getBoolean(HConstants.USE_META_REPLICAS, -574 HConstants.DEFAULT_USE_META_REPLICAS)) { -575 scan.setConsistency(Consistency.TIMELINE); -576 } -577 if (rowUpperLimit > 0) { -578 scan.setLimit(rowUpperLimit); -579 scan.setReadType(Scan.ReadType.PREAD); -580 } -581 scan.setCaching(scannerCaching); -582 return scan; -583 } -584 /** -585 * Do not use this method to get meta table regions, use methods in MetaTableLocator instead. -586 * @param connection connection we're using -587 * @param tableName table we're looking for -588 * @return Return list of regioninfos and server. -589 * @throws IOException -590 */ -591 public static List<Pair<HRegionInfo, ServerName>> -592 getTableRegionsAndLocations(Connection connection, TableName tableName) -593 throws IOException { -594 return getTableRegionsAndLocations(connection, tableName, true); -595 } -596 -597 /** -598 * Do not use this method to get meta table regions, use methods in MetaTableLocator instead. -599 * @param connection connection we're using -600 * @param tableName table to work with, can be null for getting all regions -601 * @param excludeOfflinedSplitParents don't return split parents -602 * @return Return list of regioninfos and server addresses. -603 * @throws IOException -604 */ -605 public static List<Pair<HRegionInfo, ServerName>> getTableRegionsAndLocations( -606 Connection connection, @Nullable final TableName tableName, -607 final boolean excludeOfflinedSplitParents) throws IOException { -608 if (tableName != null && tableName.equals(TableName.META_TABLE_NAME)) { -609 throw new IOException("This method can't be used to locate meta regions;" -610 + " use MetaTableLocator instead"); -611 } -612 // Make a version of CollectingVisitor that collects HRegionInfo and ServerAddress -613 CollectingVisitor<Pair<HRegionInfo, ServerName>> visitor = -614 new CollectingVisitor<Pair<HRegionInfo, ServerName>>() { -615 private RegionLocations current = null; -616 -617 @Override -618 public boolean visit(Result r) throws IOException { -619 current = getRegionLocations(r); -620 if (current == null || current.getRegionLocation().getRegionInfo() == null) { -621 LOG.warn("No serialized HRegionInfo in " + r); -622 return true; -623 } -624 HRegionInfo hri = current.getRegionLocation().getRegionInfo(); -625 if (excludeOfflinedSplitParents && hri.isSplitParent()) return true; -626 // Else call super and add this Result to the collection. -627 return super.visit(r); -628 } -629 -630 @Override -631 void add(Result r) { -632 if (current == null) { -633 return; -634 } -635 for (HRegionLocation loc : current.getRegionLocations()) { -636 if (loc != null) { -637 this.results.add(new Pair<>(loc.getRegionInfo(), loc.getServerName())); -638 } -639 } -640 } -641 }; -642 scanMeta(connection, -643 getTableStartRowForMeta(tableName, QueryType.REGION), -644 getTableStopRowForMeta(tableName, QueryType.REGION), -645 QueryType.REGION, visitor); -646 return visitor.getResults(); -647 } -648 -649 /** -650 * @param connection connection we're using -651 * @param serverName server whose regions we're interested in -652 * @return List of user regions installed on this server (does not include -653 * catalog regions). -654 * @throws IOException -655 */ -656 public static NavigableMap<HRegionInfo, Result> -657 getServerUserRegions(Connection connection, final ServerName serverName) -658 throws IOException { -659 final NavigableMap<HRegionInfo, Result> hris = new TreeMap<>(); -660 // Fill the above hris map with entries from hbase:meta that have the passed -661 // servername. -662 CollectingVisitor<Result> v = new CollectingVisitor<Result>() { -663 @Override -664 void add(Result r) { -665 if (r == null || r.isEmpty()) return; -666 RegionLocations locations = getRegionLocations(r); -667 if (locations == null) return; -668 for (HRegionLocation loc : locations.getRegionLocations()) { -669 if (loc != null) { -670 if (loc.getServerName() != null && loc.getServerName().equals(serverName)) { -671 hris.put(loc.getRegionInfo(), r); -672 } -673 } -674 } -675 } -676 }; -677 scanMeta(connection, null, null, QueryType.REGION, v); -678 return hris; -679 } -680 -681 public static void fullScanMetaAndPrint(Connection connection) -682 throws IOException { -683 Visitor v = new Visitor() { -684 @Override -685 public boolean visit(Result r) throws IOException { -686 if (r == null || r.isEmpty()) return true; -687 LOG.info("fullScanMetaAndPrint.Current Meta Row: " + r); -688 TableState state = getTableState(r); -689 if (state != null) { -690 LOG.info("Table State: " + state); -691 } else { -692 RegionLocations locations = getRegionLocations(r); -693 if (locations == null) return true; -694 for (HRegionLocation loc : locations.getRegionLocations()) { -695 if (loc != null) { -696 LOG.info("fullScanMetaAndPrint.HRI Print= " + loc.getRegionInfo()); -697 } -698 } -699 } -700 return true; -701 } -702 }; -703 scanMeta(connection, null, null, QueryType.ALL, v); -704 } -705 -706 public static void scanMetaForTableRegions(Connection connection, -707 Visitor visitor, TableName tableName) throws IOException { -708 scanMeta(connection, tableName, QueryType.REGION, Integer.MAX_VALUE, visitor); -709 } -710 -711 public static void scanMeta(Connection connection, TableName table, -712 QueryType type, int maxRows, final Visitor visitor) throws IOException { -713 scanMeta(connection, getTableStartRowForMeta(table, type), getTableStopRowForMeta(table, type), -714 type, maxRows, visitor); -715 } -716 -717 public static void scanMeta(Connection connection, -718 @Nullable final byte[] startRow, @Nullable final byte[] stopRow, -719 QueryType type, final Visitor visitor) throws IOException { -720 scanMeta(connection, startRow, stopRow, type, Integer.MAX_VALUE, visitor); -721 } -722 -723 /** -724 * Performs a scan of META table for given table starting from -725 * given row. -726 * -727 * @param connection connection we're using -728 * @param visitor visitor to call -729 * @param tableName table withing we scan -730 * @param row start scan from this row -731 * @param rowLimit max number of rows to return -732 * @throws IOException -733 */ -734 public static void scanMeta(Connection connection, -735 final Visitor visitor, final TableName tableName, -736 final byte[] row, final int rowLimit) -737 throws IOException { -738 -739 byte[] startRow = null; -740 byte[] stopRow = null; -741 if (tableName != null) { -742 startRow = -743 getTableStartRowForMeta(tableName, QueryType.REGION); -744 if (row != null) { -745 HRegionInfo closestRi = -746 getClosestRegionInfo(connection, tableName, row); -747 startRow = HRegionInfo -748 .createRegionName(tableName, closestRi.getStartKey(), HConstants.ZEROES, false); -749 } -750 stopRow = -751 getTableStopRowForMeta(tableName, QueryType.REGION); -752 } -753 scanMeta(connection, startRow, stopRow, QueryType.REGION, rowLimit, visitor); -754 } +367 public static HRegionInfo parseRegionInfoFromRegionName(byte[] regionName) throws IOException { +368 byte[][] fields = HRegionInfo.parseRegionName(regionName); +369 long regionId = Long.parseLong(Bytes.toString(fields[2])); +370 int replicaId = fields.length > 3 ? Integer.parseInt(Bytes.toString(fields[3]), 16) : 0; +371 return new HRegionInfo(TableName.valueOf(fields[0]), fields[1], fields[1], false, regionId, +372 replicaId); +373 } +374 +375 /** +376 * Gets the result in hbase:meta for the specified region. +377 * @param connection connection we're using +378 * @param regionName region we're looking for +379 * @return result of the specified region +380 * @throws IOException +381 */ +382 public static Result getRegionResult(Connection connection, +383 byte[] regionName) throws IOException { +384 Get get = new Get(regionName); +385 get.addFamily(HConstants.CATALOG_FAMILY); +386 return get(getMetaHTable(connection), get); +387 } +388 +389 /** +390 * Get regions from the merge qualifier of the specified merged region +391 * @return null if it doesn't contain merge qualifier, else two merge regions +392 * @throws IOException +393 */ +394 @Nullable +395 public static Pair<HRegionInfo, HRegionInfo> getRegionsFromMergeQualifier( +396 Connection connection, byte[] regionName) throws IOException { +397 Result result = getRegionResult(connection, regionName); +398 HRegionInfo mergeA = getHRegionInfo(result, HConstants.MERGEA_QUALIFIER); +399 HRegionInfo mergeB = getHRegionInfo(result, HConstants.MERGEB_QUALIFIER); +400 if (mergeA == null && mergeB == null) { +401 return null; +402 } +403 return new Pair<>(mergeA, mergeB); +404 } +405 +406 /** +407 * Checks if the specified table exists. Looks at the hbase:meta table hosted on +408 * the specified server. +409 * @param connection connection we're using +410 * @param tableName table to check +411 * @return true if the table exists in meta, false if not +412 * @throws IOException +413 */ +414 public static boolean tableExists(Connection connection, +415 final TableName tableName) +416 throws IOException { +417 // Catalog tables always exist. +418 return tableName.equals(TableName.META_TABLE_NAME) +419 || getTableState(connection, tableName) != null; +420 } +421 +422 /** +423 * Lists all of the regions currently in META. +424 * +425 * @param connection to connect with +426 * @param excludeOfflinedSplitParents False if we are to include offlined/splitparents regions, +427 * true and we'll leave out offlined regions from returned list +428 * @return List of all user-space regions. +429 * @throws IOException +430 */ +431 @VisibleForTesting +432 public static List<HRegionInfo> getAllRegions(Connection connection, +433 boolean excludeOfflinedSplitParents) +434 throws IOException { +435 List<Pair<HRegionInfo, ServerName>> result; +436 +437 result = getTableRegionsAndLocations(connection, null, +438 excludeOfflinedSplitParents); +439 +440 return getListOfHRegionInfos(result); +441 +442 } +443 +444 /** +445 * Gets all of the regions of the specified table. Do not use this method +446 * to get meta table regions, use methods in MetaTableLocator instead. +447 * @param connection connection we're using +448 * @param tableName table we're looking for +449 * @return Ordered list of {@link HRegionInfo}. +450 * @throws IOException +451 */ +452 public static List<HRegionInfo> getTableRegions(Connection connection, TableName tableName) +453 throws IOException { +454 return getTableRegions(connection, tableName, false); +455 } +456 +457 /** +458 * Gets all of the regions of the specified table. Do not use this method +459 * to get meta table regions, use methods in MetaTableLocator instead. +460 * @param connection connection we're using +461 * @param tableName table we're looking for +462 * @param excludeOfflinedSplitParents If true, do not include offlined split +463 * parents in the return. +464 * @return Ordered list of {@link HRegionInfo}. +465 * @throws IOException +466 */ +467 public static List<HRegionInfo> getTableRegions(Connection connection, +468 TableName tableName, final boolean excludeOfflinedSplitParents) +469 throws IOException { +470 List<Pair<HRegionInfo, ServerName>> result = +471 getTableRegionsAndLocations(connection, tableName, excludeOfflinedSplitParents); +472 return getListOfHRegionInfos(result); +473 } +474 +475 @Nullable +476 static List<HRegionInfo> getListOfHRegionInfos(final List<Pair<HRegionInfo, ServerName>> pairs) { +477 if (pairs == null || pairs.isEmpty()) return null; +478 List<HRegionInfo> result = new ArrayList<>(pairs.size()); +479 for (Pair<HRegionInfo, ServerName> pair: pairs) { +480 result.add(pair.getFirst()); +481 } +482 return result; +483 } +484 +485 /** +486 * @param current region of current table we're working with +487 * @param tableName table we're checking against +488 * @return True if <code>current</code> tablename is equal to +489 * <code>tableName</code> +490 */ +491 static boolean isInsideTable(final HRegionInfo current, final TableName tableName) { +492 return tableName.equals(current.getTable()); +493 } +494 +495 /** +496 * @param tableName table we're working with +497 * @return start row for scanning META according to query type +498 */ +499 public static byte[] getTableStartRowForMeta(TableName tableName, QueryType type) { +500 if (tableName == null) { +501 return null; +502 } +503 switch (type) { +504 case REGION: +505 byte[] startRow = new byte[tableName.getName().length + 2]; +506 System.arraycopy(tableName.getName(), 0, startRow, 0, tableName.getName().length); +507 startRow[startRow.length - 2] = HConstants.DELIMITER; +508 startRow[startRow.length - 1] = HConstants.DELIMITER; +509 return startRow; +510 case ALL: +511 case TABLE: +512 default: +513 return tableName.getName(); +514 } +515 } +516 +517 /** +518 * @param tableName table we're working with +519 * @return stop row for scanning META according to query type +520 */ +521 public static byte[] getTableStopRowForMeta(TableName tableName, QueryType type) { +522 if (tableName == null) { +523 return null; +524 } +525 final byte[] stopRow; +526 switch (type) { +527 case REGION: +528 stopRow = new byte[tableName.getName().length + 3]; +529 System.arraycopy(tableName.getName(), 0, stopRow, 0, tableName.getName().length); +530 stopRow[stopRow.length - 3] = ' '; +531 stopRow[stopRow.length - 2] = HConstants.DELIMITER; +532 stopRow[stopRow.length - 1] = HConstants.DELIMITER; +533 break; +534 case ALL: +535 case TABLE: +536 default: +537 stopRow = new byte[tableName.getName().length + 1]; +538 System.arraycopy(tableName.getName(), 0, stopRow, 0, tableName.getName().length); +539 stopRow[stopRow.length - 1] = ' '; +540 break; +541 } +542 return stopRow; +543 } +544 +545 /** +546 * This method creates a Scan object that will only scan catalog rows that +547 * belong to the specified table. It doesn't specify any columns. +548 * This is a better alternative to just using a start row and scan until +549 * it hits a new table since that requires parsing the HRI to get the table +550 * name. +551 * @param tableName bytes of table's name +552 * @return configured Scan object +553 */ +554 @Deprecated +555 public static Scan getScanForTableName(Connection connection, TableName tableName) { +556 // Start key is just the table name with delimiters +557 byte[] startKey = getTableStartRowForMeta(tableName, QueryType.REGION); +558 // Stop key appends the smallest possible char to the table name +559 byte[] stopKey = getTableStopRowForMeta(tableName, QueryType.REGION); +560 +561 Scan scan = getMetaScan(connection, -1); +562 scan.setStartRow(startKey); +563 scan.setStopRow(stopKey); +564 return scan; +565 } +566 +567 private static Scan getMetaScan(Connection connection, int rowUpperLimit) { +568 Scan scan = new Scan(); +569 int scannerCaching = connection.getConfiguration() +570 .getInt(HConstants.HBASE_META_SCANNER_CACHING, +571 HConstants.DEFAULT_HBASE_META_SCANNER_CACHING); +572 if (connection.getConfiguration().getBoolean(HConstants.USE_META_REPLICAS, +573 HConstants.DEFAULT_USE_META_REPLICAS)) { +574 scan.setConsistency(Consistency.TIMELINE); +575 } +576 if (rowUpperLimit > 0) { +577 scan.setLimit(rowUpperLimit); +578 scan.setReadType(Scan.ReadType.PREAD); +579 } +580 scan.setCaching(scannerCaching); +581 return scan; +582 } +583 /** +584 * Do not use this method to get meta table regions, use methods in MetaTableLocator instead. +585 * @param connection connection we're using +586 * @param tableName table we're looking for +587 * @return Return list of regioninfos and server. +588 * @throws IOException +589 */ +590 public static List<Pair<HRegionInfo, ServerName>> +591 getTableRegionsAndLocations(Connection connection, TableName tableName) +592 throws IOException { +593 return getTableRegionsAndLocations(connection, tableName, true); +594 } +595 +596 /** +597 * Do not use this method to get meta table regions, use methods in MetaTableLocator instead. +598 * @param connection connection we're using +599 * @param tableName table to work with, can be null for getting all regions +600 * @param excludeOfflinedSplitParents don't return split parents +601 * @return Return list of regioninfos and server addresses. +602 * @throws IOException +603 */ +604 public static List<Pair<HRegionInfo, ServerName>> getTableRegionsAndLocations( +605 Connection connection, @Nullable final TableName tableName, +606 final boolean excludeOfflinedSplitParents) throws IOException { +607 if (tableName != null && tableName.equals(TableName.META_TABLE_NAME)) { +608 throw new IOException("This method can't be used to locate meta regions;" +609 + " use MetaTableLocator instead"); +610 } +611 // Make a version of CollectingVisitor that collects HRegionInfo and ServerAddress +612 CollectingVisitor<Pair<HRegionInfo, ServerName>> visitor = +613 new CollectingVisitor<Pair<HRegionInfo, ServerName>>() { +614 private RegionLocations current = null; +615 +616 @Override +617 public boolean visit(Result r) throws IOException { +618 current = getRegionLocations(r); +619 if (current == null || current.getRegionLocation().getRegionInfo() == null) { +620 LOG.warn("No serialized HRegionInfo in " + r); +621 return true; +622 } +623 HRegionInfo hri = current.getRegionLocation().getRegionInfo(); +624 if (excludeOfflinedSplitParents && hri.isSplitParent()) return true; +625 // Else call super and add this Result to the collection. +626 return super.visit(r); +627 } +628 +629 @Override +630 void add(Result r) { +631 if (current == null) { +632 return; +633 } +634 for (HRegionLocation loc : current.getRegionLocations()) { +635 if (loc != null) { +636 this.results.add(new Pair<>(loc.getRegionInfo(), loc.getServerName())); +637 } +638 } +639 } +640 }; +641 scanMeta(connection, +642 getTableStartRowForMeta(tableName, QueryType.REGION), +643 getTableStopRowForMeta(tableName, QueryType.REGION), +644 QueryType.REGION, visitor); +645 return visitor.getResults(); +646 } +647 +648 /** +649 * @param connection connection we're using +650 * @param serverName server whose regions we're interested in +651 * @return List of user regions installed on this server (does not include +652 * catalog regions). +653 * @throws IOException +654 */ +655 public static NavigableMap<HRegionInfo, Result> +656 getServerUserRegions(Connection connection, final ServerName serverName) +657 throws IOException { +658 final NavigableMap<HRegionInfo, Result> hris = new TreeMap<>(); +659 // Fill the above hris map with entries from hbase:meta that have the passed +660 // servername. +661 CollectingVisitor<Result> v = new CollectingVisitor<Result>() { +662 @Override +663 void add(Result r) { +664 if (r == null || r.isEmpty()) return; +665 RegionLocations locations = getRegionLocations(r); +666 if (locations == null) return; +667 for (HRegionLocation loc : locations.getRegionLocations()) { +668 if (loc != null) { +669 if (loc.getServerName() != null && loc.getServerName().equals(serverName)) { +670 hris.put(loc.getRegionInfo(), r); +671 } +672 } +673 } +674 } +675 }; +676 scanMeta(connection, null, null, QueryType.REGION, v); +677 return hris; +678 } +679 +680 public static void fullScanMetaAndPrint(Connection connection) +681 throws IOException { +682 Visitor v = new Visitor() { +683 @Override +684 public boolean visit(Result r) throws IOException { +685 if (r == null || r.isEmpty()) return true; +686 LOG.info("fullScanMetaAndPrint.Current Meta Row: " + r); +687 TableState state = getTableState(r); +688 if (state != null) { +689 LOG.info("Table State: " + state); +690 } else { +691 RegionLocations locations = getRegionLocations(r); +692 if (locations == null) return true; +693 for (HRegionLocation loc : locations.getRegionLocations()) { +694 if (loc != null) { +695 LOG.info("fullScanMetaAndPrint.HRI Print= " + loc.getRegionInfo()); +696 } +697 } +698 } +699 return true; +700 } +701 }; +702 scanMeta(connection, null, null, QueryType.ALL, v); +703 } +704 +705 public static void scanMetaForTableRegions(Connection connection, +706 Visitor visitor, TableName tableName) throws IOException { +707 scanMeta(connection, tableName, QueryType.REGION, Integer.MAX_VALUE, visitor); +708 } +709 +710 public static void scanMeta(Connection connection, TableName table, +711 QueryType type, int maxRows, final Visitor visitor) throws IOException { +712 scanMeta(connection, getTableStartRowForMeta(table, type), getTableStopRowForMeta(table, type), +713 type, maxRows, visitor); +714 } +715 +716 public static void scanMeta(Connection connection, +717 @Nullable final byte[] startRow, @Nullable final byte[] stopRow, +718 QueryType type, final Visitor visitor) throws IOException { +719 scanMeta(connection, startRow, stopRow, type, Integer.MAX_VALUE, visitor); +720 } +721 +722 /** +723 * Performs a scan of META table for given table starting from +724 * given row. +725 * +726 * @param connection connection we're using +727 * @param visitor visitor to call +728 * @param tableName table withing we scan +729 * @param row start scan from this row +730 * @param rowLimit max number of rows to return +731 * @throws IOException +732 */ +733 public static void scanMeta(Connection connection, +734 final Visitor visitor, final TableName tableName, +735 final byte[] row, final int rowLimit) +736 throws IOException { +737 +738 byte[] startRow = null; +739 byte[] stopRow = null; +740 if (tableName != null) { +741 startRow = +742 getTableStartRowForMeta(tableName, QueryType.REGION); +743 if (row != null) { +744 HRegionInfo closestRi = +745 getClosestRegionInfo(connection, tableName, row); +746 startRow = HRegionInfo +747 .createRegionName(tableName, closestRi.getStartKey(), HConstants.ZEROES, false); +748 } +749 stopRow = +750 getTableStopRowForMeta(tableName, QueryType.REGION); +751 } +752 scanMeta(connection, startRow, stopRow, QueryType.REGION, rowLimit, visitor); +753 } +754 755 -756 -757 /** -758 * Performs a scan of META table. -759 * @param connection connection we're using -760 * @param startRow Where to start the scan. Pass null if want to begin scan -761 * at first row. -762 * @param stopRow Where to stop the scan. Pass null if want to scan all rows -763 * from the start one -764 * @param type scanned part of meta -765 * @param maxRows maximum rows to return -766 * @param visitor Visitor invoked against each row. -767 * @throws IOException -768 */ -769 public static void scanMeta(Connection connection, -770 @Nullable final byte[] startRow, @Nullable final byte[] stopRow, -771 QueryType type, int maxRows, final Visitor visitor) -772 throws IOException { -773 int rowUpperLimit = maxRows > 0 ? maxRows : Integer.MAX_VALUE; -774 Scan scan = getMetaScan(connection, rowUpperLimit); -775 -776 for (byte[] family : type.getFamilies()) { -777 scan.addFamily(family); -778 } -779 if (startRow != null) scan.setStartRow(startRow); -780 if (stopRow != null) scan.setStopRow(stopRow); -781 -782 if (LOG.isTraceEnabled()) { -783 LOG.trace("Scanning META" -784 + " starting at row=" + Bytes.toStringBinary(startRow) -785 + " stopping at row=" + Bytes.toStringBinary(stopRow) -786 + " for max=" + rowUpperLimit -787 + " with caching=" + scan.getCaching()); -788 } -789 -790 int currentRow = 0; -791 try (Table metaTable = getMetaHTable(connection)) { -792 try (ResultScanner scanner = metaTable.getScanner(scan)) { -793 Result data; -794 while ((data = scanner.next()) != null) { -795 if (data.isEmpty()) continue; -796 // Break if visit returns false. -797 if (!visitor.visit(data)) break; -798 if (++currentRow >= rowUpperLimit) break; -799 } -800 } -801 } -802 if (visitor != null && visitor instanceof Closeable) { -803 try { -804 ((Closeable) visitor).close(); -805 } catch (Throwable t) { -806 ExceptionUtil.rethrowIfInterrupt(t); -807 LOG.debug("Got exception in closing the meta scanner visitor", t); -808 } -809 } -810 } -811 -812 /** -813 * @return Get closest metatable region row to passed <code>row</code> -814 * @throws java.io.IOException -815 */ -816 @NonNull -817 public static HRegionInfo getClosestRegionInfo(Connection connection, -818 @NonNull final TableName tableName, -819 @NonNull final byte[] row) -820 throws IOException { -821 byte[] searchRow = HRegionInfo.createRegionName(tableName, row, HConstants.NINES, false); -822 Scan scan = getMetaScan(connection, 1); -823 scan.setReversed(true); -824 scan.setStartRow(searchRow); -825 try (ResultScanner resultScanner = getMetaHTable(connection).getScanner(scan)) { -826 Result result = resultScanner.next(); -827 if (result == null) { -828 throw new TableNotFoundException("Cannot find row in META " + -829 " for table: " + tableName + ", row=" + Bytes.toStringBinary(row)); -830 } -831 HRegionInfo regionInfo = getHRegionInfo(result); -832 if (regionInfo == null) { -833 throw new IOException("HRegionInfo was null or empty in Meta for " + -834 tableName + ", row=" + Bytes.toStringBinary(row)); -835 } -836 return regionInfo; -837 } -838 } -839 -840 /** -841 * Returns the column family used for meta columns. -842 * @return HConstants.CATALOG_FAMILY. -843 */ -844 protected static byte[] getCatalogFamily() { -845 return HConstants.CATALOG_FAMILY; -846 } -847 -848 /** -849 * Returns the column family used for table columns. -850 * @return HConstants.TABLE_FAMILY. -851 */ -852 protected static byte[] getTableFamily() { -853 return HConstants.TABLE_FAMILY; -854 } -855 -856 /** -857 * Returns the column qualifier for serialized region info -858 * @return HConstants.REGIONINFO_QUALIFIER -859 */ -860 protected static byte[] getRegionInfoColumn() { -861 return HConstants.REGIONINFO_QUALIFIER; -862 } -863 -864 /** -865 * Returns the column qualifier for serialized table state -866 * -867 * @return HConstants.TABLE_STATE_QUALIFIER -868 */ -869 protected static byte[] getStateColumn() { -870 return HConstants.TABLE_STATE_QUALIFIER; -871 } -872 -873 /** -874 * Returns the column qualifier for server column for replicaId -875 * @para