2424import org .apache .hadoop .hbase .TableName ;
2525import org .apache .hadoop .hbase .client .Admin ;
2626import org .apache .hadoop .hbase .client .Connection ;
27+ import org .apache .hadoop .hbase .coprocessor .CoprocessorException ;
28+ import org .apache .hadoop .hbase .coprocessor .CoreCoprocessor ;
29+ import org .apache .hadoop .hbase .coprocessor .HasMasterServices ;
2730import org .apache .hadoop .hbase .coprocessor .MasterCoprocessor ;
2831import org .apache .hadoop .hbase .coprocessor .MasterCoprocessorEnvironment ;
2932import org .apache .hadoop .hbase .coprocessor .MasterObserver ;
3033import org .apache .hadoop .hbase .coprocessor .ObserverContext ;
34+ import org .apache .hadoop .hbase .master .MasterServices ;
3135import org .apache .yetus .audience .InterfaceAudience ;
3236import org .apache .hadoop .hbase .shaded .protobuf .generated .QuotaProtos .Quotas ;
3337
3438/**
3539 * An observer to automatically delete quotas when a table/namespace
3640 * is deleted.
3741 */
42+ @ CoreCoprocessor
3843@ InterfaceAudience .Private
3944public class MasterQuotasObserver implements MasterCoprocessor , MasterObserver {
4045 public static final String REMOVE_QUOTA_ON_TABLE_DELETE = "hbase.quota.remove.on.table.delete" ;
@@ -43,6 +48,7 @@ public class MasterQuotasObserver implements MasterCoprocessor, MasterObserver {
4348 private CoprocessorEnvironment cpEnv ;
4449 private Configuration conf ;
4550 private boolean quotasEnabled = false ;
51+ private MasterServices masterServices ;
4652
4753 @ Override
4854 public Optional <MasterObserver > getMasterObserver () {
@@ -51,9 +57,19 @@ public Optional<MasterObserver> getMasterObserver() {
5157
5258 @ Override
5359 public void start (CoprocessorEnvironment ctx ) throws IOException {
54- this .cpEnv = ctx ;
55- this .conf = cpEnv .getConfiguration ();
60+ this .conf = ctx .getConfiguration ();
5661 this .quotasEnabled = QuotaUtil .isQuotaEnabled (conf );
62+
63+ if (!(ctx instanceof MasterCoprocessorEnvironment )) {
64+ throw new CoprocessorException ("Must be loaded on master." );
65+ }
66+ // if running on master
67+ MasterCoprocessorEnvironment mEnv = (MasterCoprocessorEnvironment ) ctx ;
68+ if (mEnv instanceof HasMasterServices ) {
69+ this .masterServices = ((HasMasterServices ) mEnv ).getMasterServices ();
70+ } else {
71+ throw new CoprocessorException ("Must be loaded on a master having master services." );
72+ }
5773 }
5874
5975 @ Override
@@ -64,18 +80,23 @@ public void postDeleteTable(
6480 return ;
6581 }
6682 final Connection conn = ctx .getEnvironment ().getConnection ();
67- Quotas quotas = QuotaUtil .getTableQuota (conn , tableName );
68- if (quotas != null ){
69- if (quotas .hasSpace ()){
70- QuotaSettings settings = QuotaSettingsFactory .removeTableSpaceLimit (tableName );
71- try (Admin admin = conn .getAdmin ()) {
72- admin .setQuota (settings );
83+ Quotas tableQuotas = QuotaUtil .getTableQuota (conn , tableName );
84+ Quotas namespaceQuotas = QuotaUtil .getNamespaceQuota (conn , tableName .getNamespaceAsString ());
85+ if (tableQuotas != null || namespaceQuotas != null ) {
86+ // Remove regions of table from space quota map.
87+ this .masterServices .getMasterQuotaManager ().removeRegionSizesForTable (tableName );
88+ if (tableQuotas != null ) {
89+ if (tableQuotas .hasSpace ()) {
90+ QuotaSettings settings = QuotaSettingsFactory .removeTableSpaceLimit (tableName );
91+ try (Admin admin = conn .getAdmin ()) {
92+ admin .setQuota (settings );
93+ }
7394 }
74- }
75- if ( quotas . hasThrottle ()){
76- QuotaSettings settings = QuotaSettingsFactory . unthrottleTable ( tableName );
77- try ( Admin admin = conn . getAdmin ()) {
78- admin . setQuota ( settings );
95+ if ( tableQuotas . hasThrottle ()) {
96+ QuotaSettings settings = QuotaSettingsFactory . unthrottleTable ( tableName );
97+ try ( Admin admin = conn . getAdmin ()) {
98+ admin . setQuota ( settings );
99+ }
79100 }
80101 }
81102 }
0 commit comments