|
21 | 21 | import java.io.ByteArrayInputStream; |
22 | 22 | import java.io.ByteArrayOutputStream; |
23 | 23 | import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
24 | 25 | import java.util.Collection; |
25 | 26 | import java.util.HashMap; |
| 27 | +import java.util.HashSet; |
26 | 28 | import java.util.List; |
27 | 29 | import java.util.Map; |
28 | 30 | import java.util.Objects; |
| 31 | +import java.util.Set; |
29 | 32 | import java.util.regex.Pattern; |
| 33 | + |
30 | 34 | import org.apache.commons.lang3.StringUtils; |
31 | 35 | import org.apache.hadoop.hbase.Cell; |
32 | 36 | import org.apache.hadoop.hbase.CellScanner; |
33 | 37 | import org.apache.hadoop.hbase.CompareOperator; |
34 | 38 | import org.apache.hadoop.hbase.NamespaceDescriptor; |
35 | 39 | import org.apache.hadoop.hbase.TableName; |
36 | 40 | import org.apache.hadoop.hbase.client.Connection; |
| 41 | +import org.apache.hadoop.hbase.client.Delete; |
37 | 42 | import org.apache.hadoop.hbase.client.Get; |
38 | 43 | import org.apache.hadoop.hbase.client.Put; |
39 | 44 | import org.apache.hadoop.hbase.client.Result; |
|
53 | 58 | import org.slf4j.Logger; |
54 | 59 | import org.slf4j.LoggerFactory; |
55 | 60 |
|
| 61 | +import org.apache.hbase.thirdparty.com.google.common.collect.HashMultimap; |
| 62 | +import org.apache.hbase.thirdparty.com.google.common.collect.Multimap; |
56 | 63 | import org.apache.hbase.thirdparty.com.google.protobuf.ByteString; |
57 | 64 | import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException; |
58 | 65 | import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; |
@@ -540,6 +547,87 @@ static Put createPutForNamespaceSnapshotSize(String namespace, long size) { |
540 | 547 | return p; |
541 | 548 | } |
542 | 549 |
|
| 550 | + /** |
| 551 | + * Returns a list of {@code Delete} to remove given table snapshot |
| 552 | + * entries to remove from quota table |
| 553 | + * @param snapshotEntriesToRemove the entries to remove |
| 554 | + */ |
| 555 | + static List<Delete> createDeletesForExistingTableSnapshotSizes( |
| 556 | + Multimap<TableName, String> snapshotEntriesToRemove) { |
| 557 | + List<Delete> deletes = new ArrayList<>(); |
| 558 | + for (Map.Entry<TableName, Collection<String>> entry : snapshotEntriesToRemove.asMap() |
| 559 | + .entrySet()) { |
| 560 | + for (String snapshot : entry.getValue()) { |
| 561 | + Delete d = new Delete(getTableRowKey(entry.getKey())); |
| 562 | + d.addColumns(QUOTA_FAMILY_USAGE, |
| 563 | + Bytes.add(QUOTA_SNAPSHOT_SIZE_QUALIFIER, Bytes.toBytes(snapshot))); |
| 564 | + deletes.add(d); |
| 565 | + } |
| 566 | + } |
| 567 | + return deletes; |
| 568 | + } |
| 569 | + |
| 570 | + /** |
| 571 | + * Returns a list of {@code Delete} to remove all table snapshot entries from quota table. |
| 572 | + * @param connection connection to re-use |
| 573 | + */ |
| 574 | + static List<Delete> createDeletesForExistingTableSnapshotSizes(Connection connection) |
| 575 | + throws IOException { |
| 576 | + return createDeletesForExistingSnapshotsFromScan(connection, createScanForSpaceSnapshotSizes()); |
| 577 | + } |
| 578 | + |
| 579 | + /** |
| 580 | + * Returns a list of {@code Delete} to remove given namespace snapshot |
| 581 | + * entries to removefrom quota table |
| 582 | + * @param snapshotEntriesToRemove the entries to remove |
| 583 | + */ |
| 584 | + static List<Delete> createDeletesForExistingNamespaceSnapshotSizes( |
| 585 | + Set<String> snapshotEntriesToRemove) { |
| 586 | + List<Delete> deletes = new ArrayList<>(); |
| 587 | + for (String snapshot : snapshotEntriesToRemove) { |
| 588 | + Delete d = new Delete(getNamespaceRowKey(snapshot)); |
| 589 | + d.addColumns(QUOTA_FAMILY_USAGE, QUOTA_SNAPSHOT_SIZE_QUALIFIER); |
| 590 | + deletes.add(d); |
| 591 | + } |
| 592 | + return deletes; |
| 593 | + } |
| 594 | + |
| 595 | + /** |
| 596 | + * Returns a list of {@code Delete} to remove all namespace snapshot entries from quota table. |
| 597 | + * @param connection connection to re-use |
| 598 | + */ |
| 599 | + static List<Delete> createDeletesForExistingNamespaceSnapshotSizes(Connection connection) |
| 600 | + throws IOException { |
| 601 | + return createDeletesForExistingSnapshotsFromScan(connection, |
| 602 | + createScanForNamespaceSnapshotSizes()); |
| 603 | + } |
| 604 | + |
| 605 | + /** |
| 606 | + * Returns a list of {@code Delete} to remove all entries returned by the passed scanner. |
| 607 | + * @param connection connection to re-use |
| 608 | + * @param scan the scanner to use to generate the list of deletes |
| 609 | + */ |
| 610 | + static List<Delete> createDeletesForExistingSnapshotsFromScan(Connection connection, Scan scan) |
| 611 | + throws IOException { |
| 612 | + List<Delete> deletes = new ArrayList<>(); |
| 613 | + try (Table quotaTable = connection.getTable(QUOTA_TABLE_NAME); |
| 614 | + ResultScanner rs = quotaTable.getScanner(scan)) { |
| 615 | + for (Result r : rs) { |
| 616 | + CellScanner cs = r.cellScanner(); |
| 617 | + while (cs.advance()) { |
| 618 | + Cell c = cs.current(); |
| 619 | + byte[] family = Bytes.copy(c.getFamilyArray(), c.getFamilyOffset(), c.getFamilyLength()); |
| 620 | + byte[] qual = |
| 621 | + Bytes.copy(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength()); |
| 622 | + Delete d = new Delete(r.getRow()); |
| 623 | + d.addColumns(family, qual); |
| 624 | + deletes.add(d); |
| 625 | + } |
| 626 | + } |
| 627 | + return deletes; |
| 628 | + } |
| 629 | + } |
| 630 | + |
543 | 631 | /** |
544 | 632 | * Fetches the computed size of all snapshots against tables in a namespace for space quotas. |
545 | 633 | */ |
@@ -575,6 +663,34 @@ static long parseSnapshotSize(Cell c) throws InvalidProtocolBufferException { |
575 | 663 | return QuotaProtos.SpaceQuotaSnapshot.parseFrom(bs).getQuotaUsage(); |
576 | 664 | } |
577 | 665 |
|
| 666 | + /** |
| 667 | + * Returns a scanner for all existing namespace snapshot entries. |
| 668 | + */ |
| 669 | + static Scan createScanForNamespaceSnapshotSizes() { |
| 670 | + return createScanForNamespaceSnapshotSizes(null); |
| 671 | + } |
| 672 | + |
| 673 | + /** |
| 674 | + * Returns a scanner for all namespace snapshot entries of the given namespace |
| 675 | + * @param namespace name of the namespace whose snapshot entries are to be scanned |
| 676 | + */ |
| 677 | + static Scan createScanForNamespaceSnapshotSizes(String namespace) { |
| 678 | + Scan s = new Scan(); |
| 679 | + if (namespace == null || namespace.isEmpty()) { |
| 680 | + // Read all namespaces, just look at the row prefix |
| 681 | + s.setRowPrefixFilter(QUOTA_NAMESPACE_ROW_KEY_PREFIX); |
| 682 | + } else { |
| 683 | + // Fetch the exact row for the table |
| 684 | + byte[] rowkey = getNamespaceRowKey(namespace); |
| 685 | + // Fetch just this one row |
| 686 | + s.withStartRow(rowkey).withStopRow(rowkey, true); |
| 687 | + } |
| 688 | + |
| 689 | + // Just the usage family and only the snapshot size qualifiers |
| 690 | + return s.addFamily(QUOTA_FAMILY_USAGE) |
| 691 | + .setFilter(new ColumnPrefixFilter(QUOTA_SNAPSHOT_SIZE_QUALIFIER)); |
| 692 | + } |
| 693 | + |
578 | 694 | static Scan createScanForSpaceSnapshotSizes() { |
579 | 695 | return createScanForSpaceSnapshotSizes(null); |
580 | 696 | } |
@@ -621,6 +737,46 @@ public static Map<String,Long> getObservedSnapshotSizes(Connection conn) throws |
621 | 737 | } |
622 | 738 | } |
623 | 739 |
|
| 740 | + /** |
| 741 | + * Returns a multimap for all existing table snapshot entries. |
| 742 | + * @param conn connection to re-use |
| 743 | + */ |
| 744 | + public static Multimap<TableName, String> getTableSnapshots(Connection conn) throws IOException { |
| 745 | + try (Table quotaTable = conn.getTable(QUOTA_TABLE_NAME); |
| 746 | + ResultScanner rs = quotaTable.getScanner(createScanForSpaceSnapshotSizes())) { |
| 747 | + Multimap<TableName, String> snapshots = HashMultimap.create(); |
| 748 | + for (Result r : rs) { |
| 749 | + CellScanner cs = r.cellScanner(); |
| 750 | + while (cs.advance()) { |
| 751 | + Cell c = cs.current(); |
| 752 | + |
| 753 | + final String snapshot = extractSnapshotNameFromSizeCell(c); |
| 754 | + snapshots.put(getTableFromRowKey(r.getRow()), snapshot); |
| 755 | + } |
| 756 | + } |
| 757 | + return snapshots; |
| 758 | + } |
| 759 | + } |
| 760 | + |
| 761 | + /** |
| 762 | + * Returns a set of the names of all namespaces containing snapshot entries. |
| 763 | + * @param conn connection to re-use |
| 764 | + */ |
| 765 | + public static Set<String> getNamespaceSnapshots(Connection conn) throws IOException { |
| 766 | + try (Table quotaTable = conn.getTable(QUOTA_TABLE_NAME); |
| 767 | + ResultScanner rs = quotaTable.getScanner(createScanForNamespaceSnapshotSizes())) { |
| 768 | + Set<String> snapshots = new HashSet<>(); |
| 769 | + for (Result r : rs) { |
| 770 | + CellScanner cs = r.cellScanner(); |
| 771 | + while (cs.advance()) { |
| 772 | + cs.current(); |
| 773 | + snapshots.add(getNamespaceFromRowKey(r.getRow())); |
| 774 | + } |
| 775 | + } |
| 776 | + return snapshots; |
| 777 | + } |
| 778 | + } |
| 779 | + |
624 | 780 | /** |
625 | 781 | * Returns the current space quota snapshot of the given {@code tableName} from |
626 | 782 | * {@code QuotaTableUtil.QUOTA_TABLE_NAME} or null if the no quota information is available for |
|
0 commit comments