001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.quotas.policies;
018
019import java.io.IOException;
020
021import org.apache.hadoop.hbase.TableNotDisabledException;
022import org.apache.hadoop.hbase.TableNotEnabledException;
023import org.apache.hadoop.hbase.TableNotFoundException;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.apache.hadoop.hbase.client.Mutation;
028import org.apache.hadoop.hbase.quotas.SpaceLimitingException;
029import org.apache.hadoop.hbase.quotas.SpaceViolationPolicy;
030import org.apache.hadoop.hbase.quotas.SpaceViolationPolicyEnforcement;
031
032/**
033 * A {@link SpaceViolationPolicyEnforcement} which disables the table. The enforcement
034 * counterpart to {@link SpaceViolationPolicy#DISABLE}.
035 */
036@InterfaceAudience.Private
037public class DisableTableViolationPolicyEnforcement extends DefaultViolationPolicyEnforcement {
038  private static final Logger LOG =
039      LoggerFactory.getLogger(DisableTableViolationPolicyEnforcement.class);
040
041  @Override
042  public void enable() throws IOException {
043    try {
044      if (LOG.isTraceEnabled()) {
045        LOG.trace("Starting disable of " + getTableName());
046      }
047      getRegionServerServices().getClusterConnection().getAdmin().disableTable(getTableName());
048      if (LOG.isTraceEnabled()) {
049        LOG.trace("Disable is complete for " + getTableName());
050      }
051    } catch (TableNotEnabledException tnee) {
052      // The state we wanted it to be in.
053    }
054  }
055
056  @Override
057  public void disable() throws IOException {
058    try {
059      if (LOG.isTraceEnabled()) {
060        LOG.trace("Starting enable of " + getTableName());
061      }
062      getRegionServerServices().getClusterConnection().getAdmin().enableTable(getTableName());
063      if (LOG.isTraceEnabled()) {
064        LOG.trace("Enable is complete for " + getTableName());
065      }
066    } catch (TableNotDisabledException | TableNotFoundException e) {
067      // The state we wanted it to be in
068      // Or, in case table is not found, nothing to do
069    }
070  }
071
072  @Override
073  public void check(Mutation m) throws SpaceLimitingException {
074    // If this policy is enacted, then the table is (or should be) disabled.
075    throw new SpaceLimitingException(
076        getPolicyName(), "This table is disabled due to violating a space quota.");
077  }
078
079  @Override
080  public String getPolicyName() {
081    return SpaceViolationPolicy.DISABLE.name();
082  }
083}