View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.quotas;
18  
19  import java.io.IOException;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.CoprocessorEnvironment;
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.client.Admin;
26  import org.apache.hadoop.hbase.client.Connection;
27  import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
28  import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
29  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
30  import org.apache.hadoop.hbase.master.MasterServices;
31  import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.Quotas;
32  
33  /**
34   * An observer to automatically delete space quotas when a table/namespace
35   * are deleted.
36   */
37  @InterfaceAudience.Private
38  public class MasterSpaceQuotaObserver extends BaseMasterObserver {
39    public static final String REMOVE_QUOTA_ON_TABLE_DELETE = "hbase.quota.remove.on.table.delete";
40    public static final boolean REMOVE_QUOTA_ON_TABLE_DELETE_DEFAULT = true;
41  
42    private CoprocessorEnvironment cpEnv;
43    private Configuration conf;
44    private boolean quotasEnabled = false;
45  
46    @Override
47    public void start(CoprocessorEnvironment ctx) throws IOException {
48      this.cpEnv = ctx;
49      this.conf = cpEnv.getConfiguration();
50      this.quotasEnabled = QuotaUtil.isQuotaEnabled(conf);
51    }
52  
53    @Override
54    public void postDeleteTable(
55        ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName) throws IOException {
56      // Do nothing if quotas aren't enabled
57      if (!quotasEnabled) {
58        return;
59      }
60      final MasterServices master = ctx.getEnvironment().getMasterServices();
61      final Connection conn = master.getConnection();
62      Quotas quotas = QuotaUtil.getTableQuota(master.getConnection(), tableName);
63      if (quotas != null && quotas.hasSpace()) {
64        QuotaSettings settings = QuotaSettingsFactory.removeTableSpaceLimit(tableName);
65        try (Admin admin = conn.getAdmin()) {
66          admin.setQuota(settings);
67        }
68      }
69    }
70  
71    @Override
72    public void postDeleteNamespace(
73        ObserverContext<MasterCoprocessorEnvironment> ctx, String namespace) throws IOException {
74      // Do nothing if quotas aren't enabled
75      if (!quotasEnabled) {
76        return;
77      }
78      final MasterServices master = ctx.getEnvironment().getMasterServices();
79      final Connection conn = master.getConnection();
80      Quotas quotas = QuotaUtil.getNamespaceQuota(master.getConnection(), namespace);
81      if (quotas != null && quotas.hasSpace()) {
82        QuotaSettings settings = QuotaSettingsFactory.removeNamespaceSpaceLimit(namespace);
83        try (Admin admin = conn.getAdmin()) {
84          admin.setQuota(settings);
85        }
86      }
87    }
88  }