diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaTableUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaTableUtil.java index 4f1491136b62..d697c230e4e6 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaTableUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/quotas/QuotaTableUtil.java @@ -628,6 +628,34 @@ static List createDeletesForExistingSnapshotsFromScan(Connection connect } } + /** + * Remove table usage snapshots (u:p columns) for the namespace passed + * @param connection connection to re-use + * @param namespace the namespace to fetch the list of table usage snapshots + */ + static void deleteTableUsageSnapshotsForNamespace(Connection connection, String namespace) + throws IOException { + Scan s = new Scan(); + //Get rows for all tables in namespace + s.setRowPrefixFilter(Bytes.toBytes("t." + namespace)); + //Scan for table usage column (u:p) in quota table + s.addColumn(QUOTA_FAMILY_USAGE,QUOTA_QUALIFIER_POLICY); + //Scan for table quota column (q:s) if table has a space quota defined + s.addColumn(QUOTA_FAMILY_INFO,QUOTA_QUALIFIER_SETTINGS); + try (Table quotaTable = connection.getTable(QUOTA_TABLE_NAME); + ResultScanner rs = quotaTable.getScanner(s)) { + for (Result r : rs) { + byte[] data = r.getValue(QUOTA_FAMILY_INFO, QUOTA_QUALIFIER_SETTINGS); + //if table does not have a table space quota defined, delete table usage column (u:p) + if (data == null) { + Delete delete = new Delete(r.getRow()); + delete.addColumns(QUOTA_FAMILY_USAGE,QUOTA_QUALIFIER_POLICY); + quotaTable.delete(delete); + } + } + } + } + /** * Fetches the computed size of all snapshots against tables in a namespace for space quotas. */ diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaUtil.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaUtil.java index d334dd8dd760..716b6fe92efe 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaUtil.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/quotas/QuotaUtil.java @@ -32,7 +32,6 @@ import org.apache.hadoop.hbase.TableNotDisabledException; import org.apache.hadoop.hbase.TableNotEnabledException; import org.apache.hadoop.hbase.TableNotFoundException; -import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.Delete; @@ -265,31 +264,13 @@ private static void deleteQuotas(final Connection connection, final byte[] rowKe final byte[] qualifier) throws IOException { Delete delete = new Delete(rowKey); if (qualifier != null) { - //Check if delete qualifier is for persisted space quota snapshot usage column family - if (Arrays.equals(qualifier,QUOTA_QUALIFIER_POLICY)) { - delete.addColumns(QUOTA_FAMILY_USAGE, qualifier); - } else - delete.addColumns(QUOTA_FAMILY_INFO, qualifier); + delete.addColumns(QUOTA_FAMILY_INFO, qualifier); } if (isNamespaceRowKey(rowKey)) { - //Check namespace is not deleted before you get info about quota and list of tables in namespace - NamespaceDescriptor[] descs = connection.getAdmin().listNamespaceDescriptors(); String ns = getNamespaceFromRowKey(rowKey); - int index = 0; - while (index < descs.length) { - if (ns.equals(descs[index].getName())) { - Quotas namespaceQuota = getNamespaceQuota(connection,ns); - if (namespaceQuota != null && namespaceQuota.hasSpace()) { - TableName[] tableArray = connection.getAdmin().listTableNamesByNamespace(ns); - for (TableName tableName : tableArray) { - deleteQuotas(connection, getTableRowKey(tableName), QUOTA_QUALIFIER_POLICY); - } - } - //Exit the while loop by moving to last index - index = descs.length; - } else { - index++; - } + Quotas namespaceQuota = getNamespaceQuota(connection,ns); + if (namespaceQuota != null && namespaceQuota.hasSpace()) { + deleteTableUsageSnapshotsForNamespace(connection, ns); } } doDelete(connection, delete);