001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase; 019 020import edu.umd.cs.findbugs.annotations.NonNull; 021import edu.umd.cs.findbugs.annotations.Nullable; 022import java.io.Closeable; 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.LinkedHashMap; 027import java.util.List; 028import java.util.Map; 029import java.util.Objects; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.hbase.Cell.Type; 032import org.apache.hadoop.hbase.ClientMetaTableAccessor.QueryType; 033import org.apache.hadoop.hbase.client.Connection; 034import org.apache.hadoop.hbase.client.Consistency; 035import org.apache.hadoop.hbase.client.Delete; 036import org.apache.hadoop.hbase.client.Get; 037import org.apache.hadoop.hbase.client.Mutation; 038import org.apache.hadoop.hbase.client.Put; 039import org.apache.hadoop.hbase.client.RegionInfo; 040import org.apache.hadoop.hbase.client.RegionReplicaUtil; 041import org.apache.hadoop.hbase.client.Result; 042import org.apache.hadoop.hbase.client.ResultScanner; 043import org.apache.hadoop.hbase.client.Scan; 044import org.apache.hadoop.hbase.client.Table; 045import org.apache.hadoop.hbase.client.TableState; 046import org.apache.hadoop.hbase.filter.Filter; 047import org.apache.hadoop.hbase.filter.RowFilter; 048import org.apache.hadoop.hbase.filter.SubstringComparator; 049import org.apache.hadoop.hbase.master.RegionState; 050import org.apache.hadoop.hbase.util.Bytes; 051import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 052import org.apache.hadoop.hbase.util.ExceptionUtil; 053import org.apache.hadoop.hbase.util.Pair; 054import org.apache.hadoop.hbase.util.PairOfSameType; 055import org.apache.yetus.audience.InterfaceAudience; 056import org.slf4j.Logger; 057import org.slf4j.LoggerFactory; 058 059/** 060 * Read/write operations on <code>hbase:meta</code> region as well as assignment information stored 061 * to <code>hbase:meta</code>. 062 * <p/> 063 * Some of the methods of this class take ZooKeeperWatcher as a param. The only reason for this is 064 * when this class is used on client-side (e.g. HBaseAdmin), we want to use short-lived connection 065 * (opened before each operation, closed right after), while when used on HM or HRS (like in 066 * AssignmentManager) we want permanent connection. 067 * <p/> 068 * HBASE-10070 adds a replicaId to HRI, meaning more than one HRI can be defined for the same table 069 * range (table, startKey, endKey). For every range, there will be at least one HRI defined which is 070 * called default replica. 071 * <p/> 072 * <h2>Meta layout</h2> For each table there is single row named for the table with a 'table' column 073 * family. The column family currently has one column in it, the 'state' column: 074 * 075 * <pre> 076 * table:state => contains table state 077 * </pre> 078 * 079 * For the catalog family, see the comments of {@link CatalogFamilyFormat} for more details. 080 * <p/> 081 * TODO: Add rep_barrier for serial replication explanation. See SerialReplicationChecker. 082 * <p/> 083 * The actual layout of meta should be encapsulated inside MetaTableAccessor methods, and should not 084 * leak out of it (through Result objects, etc) 085 * @see CatalogFamilyFormat 086 * @see ClientMetaTableAccessor 087 */ 088@InterfaceAudience.Private 089public final class MetaTableAccessor { 090 091 private static final Logger LOG = LoggerFactory.getLogger(MetaTableAccessor.class); 092 private static final Logger METALOG = LoggerFactory.getLogger("org.apache.hadoop.hbase.META"); 093 094 private MetaTableAccessor() { 095 } 096 097 //////////////////////// 098 // Reading operations // 099 //////////////////////// 100 101 /** 102 * Performs a full scan of <code>hbase:meta</code> for regions. 103 * @param connection connection we're using 104 * @param visitor Visitor invoked against each row in regions family. 105 */ 106 public static void fullScanRegions(Connection connection, 107 final ClientMetaTableAccessor.Visitor visitor) throws IOException { 108 scanMeta(connection, null, null, QueryType.REGION, visitor); 109 } 110 111 /** 112 * Performs a full scan of <code>hbase:meta</code> for regions. 113 * @param connection connection we're using 114 */ 115 public static List<Result> fullScanRegions(Connection connection) throws IOException { 116 return fullScan(connection, QueryType.REGION); 117 } 118 119 /** 120 * Performs a full scan of <code>hbase:meta</code> for tables. 121 * @param connection connection we're using 122 * @param visitor Visitor invoked against each row in tables family. 123 */ 124 public static void fullScanTables(Connection connection, 125 final ClientMetaTableAccessor.Visitor visitor) throws IOException { 126 scanMeta(connection, null, null, QueryType.TABLE, visitor); 127 } 128 129 /** 130 * Performs a full scan of <code>hbase:meta</code>. 131 * @param connection connection we're using 132 * @param type scanned part of meta 133 * @return List of {@link Result} 134 */ 135 private static List<Result> fullScan(Connection connection, QueryType type) throws IOException { 136 ClientMetaTableAccessor.CollectAllVisitor v = new ClientMetaTableAccessor.CollectAllVisitor(); 137 scanMeta(connection, null, null, type, v); 138 return v.getResults(); 139 } 140 141 /** 142 * Callers should call close on the returned {@link Table} instance. 143 * @param connection connection we're using to access Meta 144 * @return An {@link Table} for <code>hbase:meta</code> 145 * @throws NullPointerException if {@code connection} is {@code null} 146 */ 147 public static Table getMetaHTable(final Connection connection) throws IOException { 148 // We used to pass whole CatalogTracker in here, now we just pass in Connection 149 Objects.requireNonNull(connection, "Connection cannot be null"); 150 if (connection.isClosed()) { 151 throw new IOException("connection is closed"); 152 } 153 return connection.getTable(TableName.META_TABLE_NAME); 154 } 155 156 /** 157 * Gets the region info and assignment for the specified region. 158 * @param connection connection we're using 159 * @param regionName Region to lookup. 160 * @return Location and RegionInfo for <code>regionName</code> 161 * @deprecated use {@link #getRegionLocation(Connection, byte[])} instead 162 */ 163 @Deprecated 164 public static Pair<RegionInfo, ServerName> getRegion(Connection connection, byte[] regionName) 165 throws IOException { 166 HRegionLocation location = getRegionLocation(connection, regionName); 167 return location == null ? null : new Pair<>(location.getRegion(), location.getServerName()); 168 } 169 170 /** 171 * Returns the HRegionLocation from meta for the given region 172 * @param connection connection we're using 173 * @param regionName region we're looking for 174 * @return HRegionLocation for the given region 175 */ 176 public static HRegionLocation getRegionLocation(Connection connection, byte[] regionName) 177 throws IOException { 178 byte[] row = regionName; 179 RegionInfo parsedInfo = null; 180 try { 181 parsedInfo = CatalogFamilyFormat.parseRegionInfoFromRegionName(regionName); 182 row = CatalogFamilyFormat.getMetaKeyForRegion(parsedInfo); 183 } catch (Exception parseEx) { 184 // Ignore. This is used with tableName passed as regionName. 185 } 186 Get get = new Get(row); 187 get.addFamily(HConstants.CATALOG_FAMILY); 188 Result r; 189 try (Table t = getMetaHTable(connection)) { 190 r = t.get(get); 191 } 192 RegionLocations locations = CatalogFamilyFormat.getRegionLocations(r); 193 return locations == null 194 ? null 195 : locations.getRegionLocation( 196 parsedInfo == null ? RegionInfo.DEFAULT_REPLICA_ID : parsedInfo.getReplicaId()); 197 } 198 199 /** 200 * Returns the HRegionLocation from meta for the given region 201 * @param connection connection we're using 202 * @param regionInfo region information 203 * @return HRegionLocation for the given region 204 */ 205 public static HRegionLocation getRegionLocation(Connection connection, RegionInfo regionInfo) 206 throws IOException { 207 return CatalogFamilyFormat.getRegionLocation(getCatalogFamilyRow(connection, regionInfo), 208 regionInfo, regionInfo.getReplicaId()); 209 } 210 211 /** Returns Return the {@link HConstants#CATALOG_FAMILY} row from hbase:meta. */ 212 public static Result getCatalogFamilyRow(Connection connection, RegionInfo ri) 213 throws IOException { 214 Get get = new Get(CatalogFamilyFormat.getMetaKeyForRegion(ri)); 215 get.addFamily(HConstants.CATALOG_FAMILY); 216 try (Table t = getMetaHTable(connection)) { 217 return t.get(get); 218 } 219 } 220 221 /** 222 * Gets the result in hbase:meta for the specified region. 223 * @param connection connection we're using 224 * @param regionInfo region we're looking for 225 * @return result of the specified region 226 */ 227 public static Result getRegionResult(Connection connection, RegionInfo regionInfo) 228 throws IOException { 229 Get get = new Get(CatalogFamilyFormat.getMetaKeyForRegion(regionInfo)); 230 get.addFamily(HConstants.CATALOG_FAMILY); 231 try (Table t = getMetaHTable(connection)) { 232 return t.get(get); 233 } 234 } 235 236 /** 237 * Scans META table for a row whose key contains the specified <B>regionEncodedName</B>, returning 238 * a single related <code>Result</code> instance if any row is found, null otherwise. 239 * @param connection the connection to query META table. 240 * @param regionEncodedName the region encoded name to look for at META. 241 * @return <code>Result</code> instance with the row related info in META, null otherwise. 242 * @throws IOException if any errors occur while querying META. 243 */ 244 public static Result scanByRegionEncodedName(Connection connection, String regionEncodedName) 245 throws IOException { 246 RowFilter rowFilter = 247 new RowFilter(CompareOperator.EQUAL, new SubstringComparator(regionEncodedName)); 248 Scan scan = getMetaScan(connection.getConfiguration(), 1); 249 scan.setFilter(rowFilter); 250 try (Table table = getMetaHTable(connection); 251 ResultScanner resultScanner = table.getScanner(scan)) { 252 return resultScanner.next(); 253 } 254 } 255 256 /** 257 * Lists all of the regions currently in META. 258 * @param connection to connect with 259 * @param excludeOfflinedSplitParents False if we are to include offlined/splitparents regions, 260 * true and we'll leave out offlined regions from returned list 261 * @return List of all user-space regions. 262 */ 263 public static List<RegionInfo> getAllRegions(Connection connection, 264 boolean excludeOfflinedSplitParents) throws IOException { 265 List<Pair<RegionInfo, ServerName>> result; 266 267 result = getTableRegionsAndLocations(connection, null, excludeOfflinedSplitParents); 268 269 return getListOfRegionInfos(result); 270 271 } 272 273 /** 274 * Gets all of the regions of the specified table. Do not use this method to get meta table 275 * regions, use methods in MetaTableLocator instead. 276 * @param connection connection we're using 277 * @param tableName table we're looking for 278 * @return Ordered list of {@link RegionInfo}. 279 */ 280 public static List<RegionInfo> getTableRegions(Connection connection, TableName tableName) 281 throws IOException { 282 return getTableRegions(connection, tableName, false); 283 } 284 285 /** 286 * Gets all of the regions of the specified table. Do not use this method to get meta table 287 * regions, use methods in MetaTableLocator instead. 288 * @param connection connection we're using 289 * @param tableName table we're looking for 290 * @param excludeOfflinedSplitParents If true, do not include offlined split parents in the 291 * return. 292 * @return Ordered list of {@link RegionInfo}. 293 */ 294 public static List<RegionInfo> getTableRegions(Connection connection, TableName tableName, 295 final boolean excludeOfflinedSplitParents) throws IOException { 296 List<Pair<RegionInfo, ServerName>> result = 297 getTableRegionsAndLocations(connection, tableName, excludeOfflinedSplitParents); 298 return getListOfRegionInfos(result); 299 } 300 301 private static List<RegionInfo> 302 getListOfRegionInfos(final List<Pair<RegionInfo, ServerName>> pairs) { 303 if (pairs == null || pairs.isEmpty()) { 304 return Collections.emptyList(); 305 } 306 List<RegionInfo> result = new ArrayList<>(pairs.size()); 307 for (Pair<RegionInfo, ServerName> pair : pairs) { 308 result.add(pair.getFirst()); 309 } 310 return result; 311 } 312 313 /** 314 * This method creates a Scan object that will only scan catalog rows that belong to the specified 315 * table. It doesn't specify any columns. This is a better alternative to just using a start row 316 * and scan until it hits a new table since that requires parsing the HRI to get the table name. 317 * @param tableName bytes of table's name 318 * @return configured Scan object 319 */ 320 public static Scan getScanForTableName(Configuration conf, TableName tableName) { 321 // Start key is just the table name with delimiters 322 byte[] startKey = ClientMetaTableAccessor.getTableStartRowForMeta(tableName, QueryType.REGION); 323 // Stop key appends the smallest possible char to the table name 324 byte[] stopKey = ClientMetaTableAccessor.getTableStopRowForMeta(tableName, QueryType.REGION); 325 326 Scan scan = getMetaScan(conf, -1); 327 scan.withStartRow(startKey); 328 scan.withStopRow(stopKey); 329 return scan; 330 } 331 332 private static Scan getMetaScan(Configuration conf, int rowUpperLimit) { 333 Scan scan = new Scan(); 334 int scannerCaching = conf.getInt(HConstants.HBASE_META_SCANNER_CACHING, 335 HConstants.DEFAULT_HBASE_META_SCANNER_CACHING); 336 if (conf.getBoolean(HConstants.USE_META_REPLICAS, HConstants.DEFAULT_USE_META_REPLICAS)) { 337 scan.setConsistency(Consistency.TIMELINE); 338 } 339 if (rowUpperLimit > 0) { 340 scan.setLimit(rowUpperLimit); 341 scan.setReadType(Scan.ReadType.PREAD); 342 } 343 scan.setCaching(scannerCaching); 344 return scan; 345 } 346 347 /** 348 * Do not use this method to get meta table regions, use methods in MetaTableLocator instead. 349 * @param connection connection we're using 350 * @param tableName table we're looking for 351 * @return Return list of regioninfos and server. 352 */ 353 public static List<Pair<RegionInfo, ServerName>> 354 getTableRegionsAndLocations(Connection connection, TableName tableName) throws IOException { 355 return getTableRegionsAndLocations(connection, tableName, true); 356 } 357 358 /** 359 * Do not use this method to get meta table regions, use methods in MetaTableLocator instead. 360 * @param connection connection we're using 361 * @param tableName table to work with, can be null for getting all regions 362 * @param excludeOfflinedSplitParents don't return split parents 363 * @return Return list of regioninfos and server addresses. 364 */ 365 // What happens here when 1M regions in hbase:meta? This won't scale? 366 public static List<Pair<RegionInfo, ServerName>> getTableRegionsAndLocations( 367 Connection connection, @Nullable final TableName tableName, 368 final boolean excludeOfflinedSplitParents) throws IOException { 369 if (tableName != null && tableName.equals(TableName.META_TABLE_NAME)) { 370 throw new IOException( 371 "This method can't be used to locate meta regions;" + " use MetaTableLocator instead"); 372 } 373 // Make a version of CollectingVisitor that collects RegionInfo and ServerAddress 374 ClientMetaTableAccessor.CollectRegionLocationsVisitor visitor = 375 new ClientMetaTableAccessor.CollectRegionLocationsVisitor(excludeOfflinedSplitParents); 376 scanMeta(connection, 377 ClientMetaTableAccessor.getTableStartRowForMeta(tableName, QueryType.REGION), 378 ClientMetaTableAccessor.getTableStopRowForMeta(tableName, QueryType.REGION), QueryType.REGION, 379 visitor); 380 return visitor.getResults(); 381 } 382 383 public static void fullScanMetaAndPrint(Connection connection) throws IOException { 384 ClientMetaTableAccessor.Visitor v = r -> { 385 if (r == null || r.isEmpty()) { 386 return true; 387 } 388 LOG.info("fullScanMetaAndPrint.Current Meta Row: " + r); 389 TableState state = CatalogFamilyFormat.getTableState(r); 390 if (state != null) { 391 LOG.info("fullScanMetaAndPrint.Table State={}" + state); 392 } else { 393 RegionLocations locations = CatalogFamilyFormat.getRegionLocations(r); 394 if (locations == null) { 395 return true; 396 } 397 for (HRegionLocation loc : locations.getRegionLocations()) { 398 if (loc != null) { 399 LOG.info("fullScanMetaAndPrint.HRI Print={}", loc.getRegion()); 400 } 401 } 402 } 403 return true; 404 }; 405 scanMeta(connection, null, null, QueryType.ALL, v); 406 } 407 408 public static void scanMetaForTableRegions(Connection connection, 409 ClientMetaTableAccessor.Visitor visitor, TableName tableName) throws IOException { 410 scanMeta(connection, tableName, QueryType.REGION, Integer.MAX_VALUE, visitor); 411 } 412 413 private static void scanMeta(Connection connection, TableName table, QueryType type, int maxRows, 414 final ClientMetaTableAccessor.Visitor visitor) throws IOException { 415 scanMeta(connection, ClientMetaTableAccessor.getTableStartRowForMeta(table, type), 416 ClientMetaTableAccessor.getTableStopRowForMeta(table, type), type, maxRows, visitor); 417 } 418 419 public static void scanMeta(Connection connection, @Nullable final byte[] startRow, 420 @Nullable final byte[] stopRow, QueryType type, final ClientMetaTableAccessor.Visitor visitor) 421 throws IOException { 422 scanMeta(connection, startRow, stopRow, type, Integer.MAX_VALUE, visitor); 423 } 424 425 /** 426 * Performs a scan of META table for given table starting from given row. 427 * @param connection connection we're using 428 * @param visitor visitor to call 429 * @param tableName table withing we scan 430 * @param row start scan from this row 431 * @param rowLimit max number of rows to return 432 */ 433 public static void scanMeta(Connection connection, final ClientMetaTableAccessor.Visitor visitor, 434 final TableName tableName, final byte[] row, final int rowLimit) throws IOException { 435 byte[] startRow = null; 436 byte[] stopRow = null; 437 if (tableName != null) { 438 startRow = ClientMetaTableAccessor.getTableStartRowForMeta(tableName, QueryType.REGION); 439 if (row != null) { 440 RegionInfo closestRi = getClosestRegionInfo(connection, tableName, row); 441 startRow = 442 RegionInfo.createRegionName(tableName, closestRi.getStartKey(), HConstants.ZEROES, false); 443 } 444 stopRow = ClientMetaTableAccessor.getTableStopRowForMeta(tableName, QueryType.REGION); 445 } 446 scanMeta(connection, startRow, stopRow, QueryType.REGION, rowLimit, visitor); 447 } 448 449 /** 450 * Performs a scan of META table. 451 * @param connection connection we're using 452 * @param startRow Where to start the scan. Pass null if want to begin scan at first row. 453 * @param stopRow Where to stop the scan. Pass null if want to scan all rows from the start one 454 * @param type scanned part of meta 455 * @param maxRows maximum rows to return 456 * @param visitor Visitor invoked against each row. 457 */ 458 public static void scanMeta(Connection connection, @Nullable final byte[] startRow, 459 @Nullable final byte[] stopRow, QueryType type, int maxRows, 460 final ClientMetaTableAccessor.Visitor visitor) throws IOException { 461 scanMeta(connection, startRow, stopRow, type, null, maxRows, visitor); 462 } 463 464 public static void scanMeta(Connection connection, @Nullable final byte[] startRow, 465 @Nullable final byte[] stopRow, QueryType type, @Nullable Filter filter, int maxRows, 466 final ClientMetaTableAccessor.Visitor visitor) throws IOException { 467 int rowUpperLimit = maxRows > 0 ? maxRows : Integer.MAX_VALUE; 468 Scan scan = getMetaScan(connection.getConfiguration(), rowUpperLimit); 469 470 for (byte[] family : type.getFamilies()) { 471 scan.addFamily(family); 472 } 473 if (startRow != null) { 474 scan.withStartRow(startRow); 475 } 476 if (stopRow != null) { 477 scan.withStopRow(stopRow); 478 } 479 if (filter != null) { 480 scan.setFilter(filter); 481 } 482 483 if (LOG.isTraceEnabled()) { 484 LOG.trace("Scanning META" + " starting at row=" + Bytes.toStringBinary(startRow) 485 + " stopping at row=" + Bytes.toStringBinary(stopRow) + " for max=" + rowUpperLimit 486 + " with caching=" + scan.getCaching()); 487 } 488 489 int currentRow = 0; 490 try (Table metaTable = getMetaHTable(connection)) { 491 try (ResultScanner scanner = metaTable.getScanner(scan)) { 492 Result data; 493 while ((data = scanner.next()) != null) { 494 if (data.isEmpty()) { 495 continue; 496 } 497 // Break if visit returns false. 498 if (!visitor.visit(data)) { 499 break; 500 } 501 if (++currentRow >= rowUpperLimit) { 502 break; 503 } 504 } 505 } 506 } 507 if (visitor instanceof Closeable) { 508 try { 509 ((Closeable) visitor).close(); 510 } catch (Throwable t) { 511 ExceptionUtil.rethrowIfInterrupt(t); 512 LOG.debug("Got exception in closing the meta scanner visitor", t); 513 } 514 } 515 } 516 517 /** Returns Get closest metatable region row to passed <code>row</code> */ 518 @NonNull 519 private static RegionInfo getClosestRegionInfo(Connection connection, 520 @NonNull final TableName tableName, @NonNull final byte[] row) throws IOException { 521 byte[] searchRow = RegionInfo.createRegionName(tableName, row, HConstants.NINES, false); 522 Scan scan = getMetaScan(connection.getConfiguration(), 1); 523 scan.setReversed(true); 524 scan.withStartRow(searchRow); 525 try (ResultScanner resultScanner = getMetaHTable(connection).getScanner(scan)) { 526 Result result = resultScanner.next(); 527 if (result == null) { 528 throw new TableNotFoundException("Cannot find row in META " + " for table: " + tableName 529 + ", row=" + Bytes.toStringBinary(row)); 530 } 531 RegionInfo regionInfo = CatalogFamilyFormat.getRegionInfo(result); 532 if (regionInfo == null) { 533 throw new IOException("RegionInfo was null or empty in Meta for " + tableName + ", row=" 534 + Bytes.toStringBinary(row)); 535 } 536 return regionInfo; 537 } 538 } 539 540 /** 541 * Returns the {@link ServerName} from catalog table {@link Result} where the region is 542 * transitioning on. It should be the same as 543 * {@link CatalogFamilyFormat#getServerName(Result,int)} if the server is at OPEN state. 544 * @param r Result to pull the transitioning server name from 545 * @return A ServerName instance or {@link CatalogFamilyFormat#getServerName(Result,int)} if 546 * necessary fields not found or empty. 547 */ 548 @Nullable 549 public static ServerName getTargetServerName(final Result r, final int replicaId) { 550 final Cell cell = r.getColumnLatestCell(HConstants.CATALOG_FAMILY, 551 CatalogFamilyFormat.getServerNameColumn(replicaId)); 552 if (cell == null || cell.getValueLength() == 0) { 553 RegionLocations locations = CatalogFamilyFormat.getRegionLocations(r); 554 if (locations != null) { 555 HRegionLocation location = locations.getRegionLocation(replicaId); 556 if (location != null) { 557 return location.getServerName(); 558 } 559 } 560 return null; 561 } 562 return ServerName.parseServerName( 563 Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); 564 } 565 566 /** 567 * Returns the daughter regions by reading the corresponding columns of the catalog table Result. 568 * @param data a Result object from the catalog table scan 569 * @return pair of RegionInfo or PairOfSameType(null, null) if region is not a split parent 570 */ 571 public static PairOfSameType<RegionInfo> getDaughterRegions(Result data) { 572 RegionInfo splitA = CatalogFamilyFormat.getRegionInfo(data, HConstants.SPLITA_QUALIFIER); 573 RegionInfo splitB = CatalogFamilyFormat.getRegionInfo(data, HConstants.SPLITB_QUALIFIER); 574 return new PairOfSameType<>(splitA, splitB); 575 } 576 577 /** 578 * Fetch table state for given table from META table 579 * @param conn connection to use 580 * @param tableName table to fetch state for 581 */ 582 @Nullable 583 public static TableState getTableState(Connection conn, TableName tableName) throws IOException { 584 if (tableName.equals(TableName.META_TABLE_NAME)) { 585 return new TableState(tableName, TableState.State.ENABLED); 586 } 587 Table metaHTable = getMetaHTable(conn); 588 Get get = new Get(tableName.getName()).addColumn(HConstants.TABLE_FAMILY, 589 HConstants.TABLE_STATE_QUALIFIER); 590 Result result = metaHTable.get(get); 591 return CatalogFamilyFormat.getTableState(result); 592 } 593 594 /** 595 * Fetch table states from META table 596 * @param conn connection to use 597 * @return map {tableName -> state} 598 */ 599 public static Map<TableName, TableState> getTableStates(Connection conn) throws IOException { 600 final Map<TableName, TableState> states = new LinkedHashMap<>(); 601 ClientMetaTableAccessor.Visitor collector = r -> { 602 TableState state = CatalogFamilyFormat.getTableState(r); 603 if (state != null) { 604 states.put(state.getTableName(), state); 605 } 606 return true; 607 }; 608 fullScanTables(conn, collector); 609 return states; 610 } 611 612 /** 613 * Updates state in META Do not use. For internal use only. 614 * @param conn connection to use 615 * @param tableName table to look for 616 */ 617 public static void updateTableState(Connection conn, TableName tableName, TableState.State actual) 618 throws IOException { 619 updateTableState(conn, new TableState(tableName, actual)); 620 } 621 622 //////////////////////// 623 // Editing operations // 624 //////////////////////// 625 626 /** 627 * Generates and returns a {@link Put} containing the {@link RegionInfo} for the catalog table. 628 * @throws IllegalArgumentException when the provided RegionInfo is not the default replica. 629 */ 630 public static Put makePutFromRegionInfo(RegionInfo regionInfo) throws IOException { 631 return makePutFromRegionInfo(regionInfo, EnvironmentEdgeManager.currentTime()); 632 } 633 634 /** 635 * Generates and returns a {@link Put} containing the {@link RegionInfo} for the catalog table. 636 * @throws IllegalArgumentException when the provided RegionInfo is not the default replica. 637 */ 638 public static Put makePutFromRegionInfo(RegionInfo regionInfo, long ts) throws IOException { 639 return addRegionInfo(new Put(CatalogFamilyFormat.getMetaKeyForRegion(regionInfo), ts), 640 regionInfo); 641 } 642 643 /** 644 * Generates and returns a Delete containing the region info for the catalog table 645 */ 646 public static Delete makeDeleteFromRegionInfo(RegionInfo regionInfo, long ts) { 647 if (regionInfo == null) { 648 throw new IllegalArgumentException("Can't make a delete for null region"); 649 } 650 if (regionInfo.getReplicaId() != RegionInfo.DEFAULT_REPLICA_ID) { 651 throw new IllegalArgumentException( 652 "Can't make delete for a replica region. Operate on the primary"); 653 } 654 Delete delete = new Delete(CatalogFamilyFormat.getMetaKeyForRegion(regionInfo)); 655 delete.addFamily(HConstants.CATALOG_FAMILY, ts); 656 return delete; 657 } 658 659 /** 660 * Adds split daughters to the Put 661 */ 662 public static Put addDaughtersToPut(Put put, RegionInfo splitA, RegionInfo splitB) 663 throws IOException { 664 if (splitA != null) { 665 put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow()) 666 .setFamily(HConstants.CATALOG_FAMILY).setQualifier(HConstants.SPLITA_QUALIFIER) 667 .setTimestamp(put.getTimestamp()).setType(Type.Put).setValue(RegionInfo.toByteArray(splitA)) 668 .build()); 669 } 670 if (splitB != null) { 671 put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow()) 672 .setFamily(HConstants.CATALOG_FAMILY).setQualifier(HConstants.SPLITB_QUALIFIER) 673 .setTimestamp(put.getTimestamp()).setType(Type.Put).setValue(RegionInfo.toByteArray(splitB)) 674 .build()); 675 } 676 return put; 677 } 678 679 /** 680 * Put the passed <code>p</code> to the <code>hbase:meta</code> table. 681 * @param connection connection we're using 682 * @param p Put to add to hbase:meta 683 */ 684 private static void putToMetaTable(Connection connection, Put p) throws IOException { 685 try (Table table = getMetaHTable(connection)) { 686 put(table, p); 687 } 688 } 689 690 /** 691 * @param t Table to use 692 * @param p put to make 693 */ 694 private static void put(Table t, Put p) throws IOException { 695 debugLogMutation(p); 696 t.put(p); 697 } 698 699 /** 700 * Put the passed <code>ps</code> to the <code>hbase:meta</code> table. 701 * @param connection connection we're using 702 * @param ps Put to add to hbase:meta 703 */ 704 public static void putsToMetaTable(final Connection connection, final List<Put> ps) 705 throws IOException { 706 if (ps.isEmpty()) { 707 return; 708 } 709 try (Table t = getMetaHTable(connection)) { 710 debugLogMutations(ps); 711 // the implementation for putting a single Put is much simpler so here we do a check first. 712 if (ps.size() == 1) { 713 t.put(ps.get(0)); 714 } else { 715 t.put(ps); 716 } 717 } 718 } 719 720 /** 721 * Delete the passed <code>d</code> from the <code>hbase:meta</code> table. 722 * @param connection connection we're using 723 * @param d Delete to add to hbase:meta 724 */ 725 private static void deleteFromMetaTable(final Connection connection, final Delete d) 726 throws IOException { 727 List<Delete> dels = new ArrayList<>(1); 728 dels.add(d); 729 deleteFromMetaTable(connection, dels); 730 } 731 732 /** 733 * Delete the passed <code>deletes</code> from the <code>hbase:meta</code> table. 734 * @param connection connection we're using 735 * @param deletes Deletes to add to hbase:meta This list should support #remove. 736 */ 737 private static void deleteFromMetaTable(final Connection connection, final List<Delete> deletes) 738 throws IOException { 739 try (Table t = getMetaHTable(connection)) { 740 debugLogMutations(deletes); 741 t.delete(deletes); 742 } 743 } 744 745 /** 746 * Set the column value corresponding to this {@code replicaId}'s {@link RegionState} to the 747 * provided {@code state}. Mutates the provided {@link Put}. 748 */ 749 public static Put addRegionStateToPut(Put put, int replicaId, RegionState.State state) 750 throws IOException { 751 put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow()) 752 .setFamily(HConstants.CATALOG_FAMILY) 753 .setQualifier(CatalogFamilyFormat.getRegionStateColumn(replicaId)) 754 .setTimestamp(put.getTimestamp()).setType(Cell.Type.Put).setValue(Bytes.toBytes(state.name())) 755 .build()); 756 return put; 757 } 758 759 /** 760 * Update state column in hbase:meta. 761 */ 762 public static void updateRegionState(Connection connection, RegionInfo ri, 763 RegionState.State state) throws IOException { 764 final Put put = makePutFromRegionInfo(ri); 765 addRegionStateToPut(put, ri.getReplicaId(), state); 766 putsToMetaTable(connection, Collections.singletonList(put)); 767 } 768 769 /** 770 * Adds daughter region infos to hbase:meta row for the specified region. 771 * <p/> 772 * Note that this does not add its daughter's as different rows, but adds information about the 773 * daughters in the same row as the parent. Now only used in snapshot. Use 774 * {@link org.apache.hadoop.hbase.master.assignment.RegionStateStore} if you want to split a 775 * region. 776 * @param connection connection we're using 777 * @param regionInfo RegionInfo of parent region 778 * @param splitA first split daughter of the parent regionInfo 779 * @param splitB second split daughter of the parent regionInfo 780 * @throws IOException if problem connecting or updating meta 781 */ 782 public static void addSplitsToParent(Connection connection, RegionInfo regionInfo, 783 RegionInfo splitA, RegionInfo splitB) throws IOException { 784 try (Table meta = getMetaHTable(connection)) { 785 Put put = makePutFromRegionInfo(regionInfo); 786 addDaughtersToPut(put, splitA, splitB); 787 meta.put(put); 788 debugLogMutation(put); 789 LOG.debug("Added region {}", regionInfo.getRegionNameAsString()); 790 } 791 } 792 793 /** 794 * Adds a hbase:meta row for each of the specified new regions. Initial state for new regions is 795 * CLOSED. 796 * @param connection connection we're using 797 * @param regionInfos region information list 798 * @throws IOException if problem connecting or updating meta 799 */ 800 public static void addRegionsToMeta(Connection connection, List<RegionInfo> regionInfos, 801 int regionReplication) throws IOException { 802 addRegionsToMeta(connection, regionInfos, regionReplication, 803 EnvironmentEdgeManager.currentTime()); 804 } 805 806 /** 807 * Adds a hbase:meta row for each of the specified new regions. Initial state for new regions is 808 * CLOSED. 809 * @param connection connection we're using 810 * @param regionInfos region information list 811 * @param ts desired timestamp 812 * @throws IOException if problem connecting or updating meta 813 */ 814 public static void addRegionsToMeta(Connection connection, List<RegionInfo> regionInfos, 815 int regionReplication, long ts) throws IOException { 816 List<Put> puts = new ArrayList<>(); 817 for (RegionInfo regionInfo : regionInfos) { 818 if (!RegionReplicaUtil.isDefaultReplica(regionInfo)) { 819 continue; 820 } 821 Put put = makePutFromRegionInfo(regionInfo, ts); 822 // New regions are added with initial state of CLOSED. 823 addRegionStateToPut(put, regionInfo.getReplicaId(), RegionState.State.CLOSED); 824 // Add empty locations for region replicas so that number of replicas can be cached 825 // whenever the primary region is looked up from meta 826 for (int i = 1; i < regionReplication; i++) { 827 addEmptyLocation(put, i); 828 } 829 puts.add(put); 830 } 831 putsToMetaTable(connection, puts); 832 LOG.info("Added {} regions to meta.", puts.size()); 833 } 834 835 /** 836 * Update state of the table in meta. 837 * @param connection what we use for update 838 * @param state new state 839 */ 840 private static void updateTableState(Connection connection, TableState state) throws IOException { 841 Put put = makePutFromTableState(state, EnvironmentEdgeManager.currentTime()); 842 putToMetaTable(connection, put); 843 LOG.info("Updated {} in hbase:meta", state); 844 } 845 846 /** 847 * Construct PUT for given state 848 * @param state new state 849 */ 850 public static Put makePutFromTableState(TableState state, long ts) { 851 Put put = new Put(state.getTableName().getName(), ts); 852 put.addColumn(HConstants.TABLE_FAMILY, HConstants.TABLE_STATE_QUALIFIER, 853 state.convert().toByteArray()); 854 return put; 855 } 856 857 /** 858 * Remove state for table from meta 859 * @param connection to use for deletion 860 * @param table to delete state for 861 */ 862 public static void deleteTableState(Connection connection, TableName table) throws IOException { 863 long time = EnvironmentEdgeManager.currentTime(); 864 Delete delete = new Delete(table.getName()); 865 delete.addColumns(HConstants.TABLE_FAMILY, HConstants.TABLE_STATE_QUALIFIER, time); 866 deleteFromMetaTable(connection, delete); 867 LOG.info("Deleted table " + table + " state from META"); 868 } 869 870 /** 871 * Updates the location of the specified region in hbase:meta to be the specified server hostname 872 * and startcode. 873 * <p> 874 * Uses passed catalog tracker to get a connection to the server hosting hbase:meta and makes 875 * edits to that region. 876 * @param connection connection we're using 877 * @param regionInfo region to update location of 878 * @param openSeqNum the latest sequence number obtained when the region was open 879 * @param sn Server name 880 * @param masterSystemTime wall clock time from master if passed in the open region RPC 881 */ 882 public static void updateRegionLocation(Connection connection, RegionInfo regionInfo, 883 ServerName sn, long openSeqNum, long masterSystemTime) throws IOException { 884 updateLocation(connection, regionInfo, sn, openSeqNum, masterSystemTime); 885 } 886 887 /** 888 * Updates the location of the specified region to be the specified server. 889 * <p> 890 * Connects to the specified server which should be hosting the specified catalog region name to 891 * perform the edit. 892 * @param connection connection we're using 893 * @param regionInfo region to update location of 894 * @param sn Server name 895 * @param openSeqNum the latest sequence number obtained when the region was open 896 * @param masterSystemTime wall clock time from master if passed in the open region RPC 897 * @throws IOException In particular could throw {@link java.net.ConnectException} if the server 898 * is down on other end. 899 */ 900 private static void updateLocation(Connection connection, RegionInfo regionInfo, ServerName sn, 901 long openSeqNum, long masterSystemTime) throws IOException { 902 // region replicas are kept in the primary region's row 903 Put put = new Put(CatalogFamilyFormat.getMetaKeyForRegion(regionInfo), masterSystemTime); 904 addRegionInfo(put, regionInfo); 905 addLocation(put, sn, openSeqNum, regionInfo.getReplicaId()); 906 putToMetaTable(connection, put); 907 LOG.info("Updated row {} with server=", regionInfo.getRegionNameAsString(), sn); 908 } 909 910 public static Put addRegionInfo(final Put p, final RegionInfo hri) throws IOException { 911 p.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(p.getRow()) 912 .setFamily(HConstants.CATALOG_FAMILY).setQualifier(HConstants.REGIONINFO_QUALIFIER) 913 .setTimestamp(p.getTimestamp()).setType(Type.Put) 914 // Serialize the Default Replica HRI otherwise scan of hbase:meta 915 // shows an info:regioninfo value with encoded name and region 916 // name that differs from that of the hbase;meta row. 917 .setValue(RegionInfo.toByteArray(RegionReplicaUtil.getRegionInfoForDefaultReplica(hri))) 918 .build()); 919 return p; 920 } 921 922 public static Put addLocation(Put p, ServerName sn, long openSeqNum, int replicaId) 923 throws IOException { 924 CellBuilder builder = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY); 925 return p 926 .add(builder.clear().setRow(p.getRow()).setFamily(HConstants.CATALOG_FAMILY) 927 .setQualifier(CatalogFamilyFormat.getServerColumn(replicaId)).setTimestamp(p.getTimestamp()) 928 .setType(Cell.Type.Put).setValue(Bytes.toBytes(sn.getAddress().toString())).build()) 929 .add(builder.clear().setRow(p.getRow()).setFamily(HConstants.CATALOG_FAMILY) 930 .setQualifier(CatalogFamilyFormat.getStartCodeColumn(replicaId)) 931 .setTimestamp(p.getTimestamp()).setType(Cell.Type.Put) 932 .setValue(Bytes.toBytes(sn.getStartcode())).build()) 933 .add(builder.clear().setRow(p.getRow()).setFamily(HConstants.CATALOG_FAMILY) 934 .setQualifier(CatalogFamilyFormat.getSeqNumColumn(replicaId)).setTimestamp(p.getTimestamp()) 935 .setType(Type.Put).setValue(Bytes.toBytes(openSeqNum)).build()); 936 } 937 938 public static Put addEmptyLocation(Put p, int replicaId) throws IOException { 939 CellBuilder builder = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY); 940 return p 941 .add(builder.clear().setRow(p.getRow()).setFamily(HConstants.CATALOG_FAMILY) 942 .setQualifier(CatalogFamilyFormat.getServerColumn(replicaId)).setTimestamp(p.getTimestamp()) 943 .setType(Type.Put).build()) 944 .add(builder.clear().setRow(p.getRow()).setFamily(HConstants.CATALOG_FAMILY) 945 .setQualifier(CatalogFamilyFormat.getStartCodeColumn(replicaId)) 946 .setTimestamp(p.getTimestamp()).setType(Cell.Type.Put).build()) 947 .add(builder.clear().setRow(p.getRow()).setFamily(HConstants.CATALOG_FAMILY) 948 .setQualifier(CatalogFamilyFormat.getSeqNumColumn(replicaId)).setTimestamp(p.getTimestamp()) 949 .setType(Cell.Type.Put).build()); 950 } 951 952 private static void debugLogMutations(List<? extends Mutation> mutations) throws IOException { 953 if (!METALOG.isDebugEnabled()) { 954 return; 955 } 956 // Logging each mutation in separate line makes it easier to see diff between them visually 957 // because of common starting indentation. 958 for (Mutation mutation : mutations) { 959 debugLogMutation(mutation); 960 } 961 } 962 963 private static void debugLogMutation(Mutation p) throws IOException { 964 METALOG.debug("{} {}", p.getClass().getSimpleName(), p.toJSON()); 965 } 966}