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.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Objects;
23
24 import org.apache.hadoop.hbase.TableName;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.classification.InterfaceStability;
27 import org.apache.hadoop.hbase.regionserver.Region;
28 import org.apache.hadoop.hbase.regionserver.RegionServerServices;
29
30 /**
31 * A class to ease dealing with tables that have and do not have violation policies
32 * being enforced. This class is immutable, expect for {@code locallyCachedPolicies}.
33 *
34 * The {@code locallyCachedPolicies} are mutable given the current {@code activePolicies}
35 * and {@code snapshots}. It is expected that when a new instance of this class is
36 * instantiated, we also want to invalidate those previously cached policies (as they
37 * may now be invalidate if we received new quota usage information).
38 */
39 @InterfaceAudience.Private
40 @InterfaceStability.Evolving
41 public class ActivePolicyEnforcement {
42 private final Map<TableName,SpaceViolationPolicyEnforcement> activePolicies;
43 private final Map<TableName,SpaceQuotaSnapshot> snapshots;
44 private final RegionServerServices rss;
45 private final SpaceViolationPolicyEnforcementFactory factory;
46 private final Map<TableName,SpaceViolationPolicyEnforcement> locallyCachedPolicies;
47
48 public ActivePolicyEnforcement(Map<TableName,SpaceViolationPolicyEnforcement> activePolicies,
49 Map<TableName,SpaceQuotaSnapshot> snapshots, RegionServerServices rss) {
50 this(activePolicies, snapshots, rss, SpaceViolationPolicyEnforcementFactory.getInstance());
51 }
52
53 public ActivePolicyEnforcement(Map<TableName,SpaceViolationPolicyEnforcement> activePolicies,
54 Map<TableName,SpaceQuotaSnapshot> snapshots, RegionServerServices rss,
55 SpaceViolationPolicyEnforcementFactory factory) {
56 this.activePolicies = activePolicies;
57 this.snapshots = snapshots;
58 this.rss = rss;
59 this.factory = factory;
60 // Mutable!
61 this.locallyCachedPolicies = new HashMap<>();
62 }
63
64 /**
65 * Returns the proper {@link SpaceViolationPolicyEnforcement} implementation for the given table.
66 * If the given table does not have a violation policy enforced, a "no-op" policy will
67 * be returned which always allows an action.
68 *
69 * @see #getPolicyEnforcement(TableName)
70 */
71 public SpaceViolationPolicyEnforcement getPolicyEnforcement(Region r) {
72 return getPolicyEnforcement(Objects.requireNonNull(r).getTableDesc().getTableName());
73 }
74
75 /**
76 * Returns the proper {@link SpaceViolationPolicyEnforcement} implementation for the given table.
77 * If the given table does not have a violation policy enforced, a "no-op" policy will
78 * be returned which always allows an action.
79 *
80 * @param tableName The table to fetch the policy for.
81 * @return A non-null {@link SpaceViolationPolicyEnforcement} instance.
82 */
83 public SpaceViolationPolicyEnforcement getPolicyEnforcement(TableName tableName) {
84 SpaceViolationPolicyEnforcement policy = activePolicies.get(Objects.requireNonNull(tableName));
85 if (policy == null) {
86 synchronized (locallyCachedPolicies) {
87 // When we don't have an policy enforcement for the table, there could be one of two cases:
88 // 1) The table has no quota defined
89 // 2) The table is not in violation of its quota
90 // In both of these cases, we want to make sure that access remains fast and we minimize
91 // object creation. We can accomplish this by locally caching policies instead of creating
92 // a new instance of the policy each time.
93 policy = locallyCachedPolicies.get(tableName);
94 // We have already created/cached the enforcement, use it again. `activePolicies` and
95 // `snapshots` are immutable, thus this policy is valid for the lifetime of `this`.
96 if (policy != null) {
97 return policy;
98 }
99 // Create a PolicyEnforcement for this table and snapshot. The snapshot may be null
100 // which is OK.
101 policy = factory.createWithoutViolation(rss, tableName, snapshots.get(tableName));
102 // Cache the policy we created
103 locallyCachedPolicies.put(tableName, policy);
104 }
105 }
106 return policy;
107 }
108
109 /**
110 * Returns an unmodifiable version of the active {@link SpaceViolationPolicyEnforcement}s.
111 */
112 public Map<TableName,SpaceViolationPolicyEnforcement> getPolicies() {
113 return Collections.unmodifiableMap(activePolicies);
114 }
115
116 /**
117 * Returns an unmodifiable version of the policy enforcements that were cached because they are
118 * not in violation of their quota.
119 */
120 Map<TableName,SpaceViolationPolicyEnforcement> getLocallyCachedPolicies() {
121 return Collections.unmodifiableMap(locallyCachedPolicies);
122 }
123
124 @Override
125 public String toString() {
126 return getClass().getSimpleName() + ": " + activePolicies;
127 }
128 }