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.policies;
18  
19  import java.io.IOException;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.TableNotDisabledException;
24  import org.apache.hadoop.hbase.TableNotEnabledException;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.client.Mutation;
27  import org.apache.hadoop.hbase.quotas.SpaceLimitingException;
28  import org.apache.hadoop.hbase.quotas.SpaceViolationPolicy;
29  import org.apache.hadoop.hbase.quotas.SpaceViolationPolicyEnforcement;
30  
31  /**
32   * A {@link SpaceViolationPolicyEnforcement} which disables the table. The enforcement
33   * countepart to {@link SpaceViolationPolicy#DISABLE}.
34   */
35  @InterfaceAudience.Private
36  public class DisableTableViolationPolicyEnforcement extends DefaultViolationPolicyEnforcement {
37    private static final Log LOG = LogFactory.getLog(DisableTableViolationPolicyEnforcement.class);
38  
39    @Override
40    public void enable() throws IOException {
41      try {
42        if (LOG.isTraceEnabled()) {
43          LOG.trace("Starting disable of " + getTableName());
44        }
45        getRegionServerServices().getConnection().getAdmin().disableTable(getTableName());
46        if (LOG.isTraceEnabled()) {
47          LOG.trace("Disable is complete for " + getTableName());
48        }
49      } catch (TableNotEnabledException tnee) {
50        // The state we wanted it to be in.
51      }
52    }
53  
54    @Override
55    public void disable() throws IOException {
56      try {
57        if (LOG.isTraceEnabled()) {
58          LOG.trace("Starting enable of " + getTableName());
59        }
60        getRegionServerServices().getConnection().getAdmin().enableTable(getTableName());
61        if (LOG.isTraceEnabled()) {
62          LOG.trace("Enable is complete for " + getTableName());
63        }
64      } catch (TableNotDisabledException tnde) {
65        // The state we wanted it to be in
66      }
67    }
68  
69    @Override
70    public void check(Mutation m) throws SpaceLimitingException {
71      // If this policy is enacted, then the table is (or should be) disabled.
72      throw new SpaceLimitingException(
73          getPolicyName(), "This table is disabled due to violating a space quota.");
74    }
75  
76    @Override
77    public String getPolicyName() {
78      return SpaceViolationPolicy.DISABLE.name();
79    }
80  }