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 org.apache.hadoop.hbase.TableName;
20  import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
21  import org.apache.hadoop.hbase.quotas.SpaceViolationPolicyEnforcement;
22  import org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement;
23  import org.apache.hadoop.hbase.quotas.policies.DisableTableViolationPolicyEnforcement;
24  import org.apache.hadoop.hbase.quotas.policies.MissingSnapshotViolationPolicyEnforcement;
25  import org.apache.hadoop.hbase.quotas.policies.NoInsertsViolationPolicyEnforcement;
26  import org.apache.hadoop.hbase.quotas.policies.NoWritesCompactionsViolationPolicyEnforcement;
27  import org.apache.hadoop.hbase.quotas.policies.NoWritesViolationPolicyEnforcement;
28  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
29  
30  /**
31   * A factory class for instantiating {@link SpaceViolationPolicyEnforcement} instances.
32   */
33  public class SpaceViolationPolicyEnforcementFactory {
34  
35    private static final SpaceViolationPolicyEnforcementFactory INSTANCE =
36        new SpaceViolationPolicyEnforcementFactory();
37  
38    private SpaceViolationPolicyEnforcementFactory() {}
39  
40    /**
41     * Returns an instance of this factory.
42     */
43    public static SpaceViolationPolicyEnforcementFactory getInstance() {
44      return INSTANCE;
45    }
46  
47    /**
48     * Constructs the appropriate {@link SpaceViolationPolicyEnforcement} for tables that are
49     * in violation of their space quota.
50     */
51    public SpaceViolationPolicyEnforcement create(
52        RegionServerServices rss, TableName tableName, SpaceQuotaSnapshot snapshot) {
53      SpaceViolationPolicyEnforcement enforcement;
54      SpaceQuotaStatus status = snapshot.getQuotaStatus();
55      if (!status.isInViolation()) {
56        throw new IllegalArgumentException(tableName + " is not in violation. Snapshot=" + snapshot);
57      }
58      switch (status.getPolicy()) {
59        case DISABLE:
60          enforcement = new DisableTableViolationPolicyEnforcement();
61          break;
62        case NO_WRITES_COMPACTIONS:
63          enforcement = new NoWritesCompactionsViolationPolicyEnforcement();
64          break;
65        case NO_WRITES:
66          enforcement = new NoWritesViolationPolicyEnforcement();
67          break;
68        case NO_INSERTS:
69          enforcement = new NoInsertsViolationPolicyEnforcement();
70          break;
71        default:
72          throw new IllegalArgumentException("Unhandled SpaceViolationPolicy: " + status.getPolicy());
73      }
74      enforcement.initialize(rss, tableName, snapshot);
75      return enforcement;
76    }
77  
78    /**
79     * Creates the "default" {@link SpaceViolationPolicyEnforcement} for a table that isn't in
80     * violation. This is used to have uniform policy checking for tables in and not quotas. This
81     * policy will still verify that new bulk loads do not exceed the configured quota limit.
82     *
83     * @param rss RegionServerServices instance the policy enforcement should use.
84     * @param tableName The target HBase table.
85     * @param snapshot The current quota snapshot for the {@code tableName}, can be null.
86     */
87    public SpaceViolationPolicyEnforcement createWithoutViolation(
88        RegionServerServices rss, TableName tableName, SpaceQuotaSnapshot snapshot) {
89      if (snapshot == null) {
90        // If we have no snapshot, this is equivalent to no quota for this table.
91        // We should do use the (singleton instance) of this policy to do nothing.
92        return MissingSnapshotViolationPolicyEnforcement.getInstance();
93      }
94      // We have a snapshot which means that there is a quota set on this table, but it's not in
95      // violation of that quota. We need to construct a policy for this table.
96      SpaceQuotaStatus status = snapshot.getQuotaStatus();
97      if (status.isInViolation()) {
98        throw new IllegalArgumentException(
99            tableName + " is in violation. Logic error. Snapshot=" + snapshot);
100     }
101     // We have a unique size snapshot to use. Create an instance for this tablename + snapshot.
102     DefaultViolationPolicyEnforcement enforcement = new DefaultViolationPolicyEnforcement();
103     enforcement.initialize(rss, tableName, snapshot);
104     return enforcement;
105   }
106 }