HBASE-17748 Include HBase snapshots in space quotas
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / quotas / SpaceQuotaSnapshot.java
blob18fe76a659fffefef9ea8afdb8a7b91ee9574de2
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.hadoop.hbase.quotas;
19 import java.util.Objects;
21 import org.apache.commons.lang.builder.HashCodeBuilder;
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
24 import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos;
25 import org.apache.hadoop.util.StringUtils;
27 /**
28 * A point-in-time view of a space quota on a table.
30 @InterfaceAudience.Private
31 public class SpaceQuotaSnapshot {
32 private static final SpaceQuotaSnapshot NO_SUCH_SNAPSHOT = new SpaceQuotaSnapshot(
33 SpaceQuotaStatus.notInViolation(), 0, Long.MAX_VALUE);
34 private final SpaceQuotaStatus quotaStatus;
35 private final long usage;
36 private final long limit;
38 /**
39 * Encapsulates the state of a quota on a table. The quota may or may not be in violation.
40 * If the quota is not in violation, the violation may be null. If the quota is in violation,
41 * there is guaranteed to be a non-null violation policy.
43 @InterfaceAudience.Private
44 public static class SpaceQuotaStatus {
45 private static final SpaceQuotaStatus NOT_IN_VIOLATION = new SpaceQuotaStatus(null, false);
46 final SpaceViolationPolicy policy;
47 final boolean inViolation;
49 /**
50 * Constructs a {@code SpaceQuotaSnapshot} which is in violation of the provided {@code policy}.
52 * Use {@link #notInViolation()} to obtain an instance of this class for the cases when the
53 * quota is not in violation.
55 * @param policy The non-null policy being violated.
57 public SpaceQuotaStatus(SpaceViolationPolicy policy) {
58 // If the caller is instantiating a status, the policy must be non-null
59 this (Objects.requireNonNull(policy), true);
62 private SpaceQuotaStatus(SpaceViolationPolicy policy, boolean inViolation) {
63 this.policy = policy;
64 this.inViolation = inViolation;
67 /**
68 * Returns the violation policy, which may be null. It is guaranteed to be non-null if
69 * {@link #isInViolation()} is {@code true}, but may be null otherwise.
71 public SpaceViolationPolicy getPolicy() {
72 return policy;
75 /**
76 * @return {@code true} if the quota is being violated, {@code false} otherwise.
78 public boolean isInViolation() {
79 return inViolation;
82 /**
83 * Returns a singleton referring to a quota which is not in violation.
85 public static SpaceQuotaStatus notInViolation() {
86 return NOT_IN_VIOLATION;
89 @Override
90 public int hashCode() {
91 return new HashCodeBuilder().append(policy == null ? 0 : policy.hashCode())
92 .append(inViolation).toHashCode();
95 @Override
96 public boolean equals(Object o) {
97 if (o instanceof SpaceQuotaStatus) {
98 SpaceQuotaStatus other = (SpaceQuotaStatus) o;
99 return Objects.equals(policy, other.policy) && inViolation == other.inViolation;
101 return false;
104 @Override
105 public String toString() {
106 StringBuilder sb = new StringBuilder(getClass().getSimpleName());
107 sb.append("[policy=").append(policy);
108 sb.append(", inViolation=").append(inViolation).append("]");
109 return sb.toString();
112 public static QuotaProtos.SpaceQuotaStatus toProto(SpaceQuotaStatus status) {
113 QuotaProtos.SpaceQuotaStatus.Builder builder = QuotaProtos.SpaceQuotaStatus.newBuilder();
114 builder.setInViolation(status.inViolation);
115 if (status.isInViolation()) {
116 builder.setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(status.getPolicy()));
118 return builder.build();
121 public static SpaceQuotaStatus toStatus(QuotaProtos.SpaceQuotaStatus proto) {
122 if (proto.getInViolation()) {
123 return new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(proto.getViolationPolicy()));
124 } else {
125 return NOT_IN_VIOLATION;
130 public SpaceQuotaSnapshot(SpaceQuotaStatus quotaStatus, long usage, long limit) {
131 this.quotaStatus = Objects.requireNonNull(quotaStatus);
132 this.usage = usage;
133 this.limit = limit;
137 * Returns the status of the quota.
139 public SpaceQuotaStatus getQuotaStatus() {
140 return quotaStatus;
144 * Returns the current usage, in bytes, of the target (e.g. table, namespace).
146 public long getUsage() {
147 return usage;
151 * Returns the limit, in bytes, of the target (e.g. table, namespace).
153 public long getLimit() {
154 return limit;
157 @Override
158 public int hashCode() {
159 return new HashCodeBuilder()
160 .append(quotaStatus.hashCode())
161 .append(usage)
162 .append(limit)
163 .toHashCode();
166 @Override
167 public boolean equals(Object o) {
168 if (o instanceof SpaceQuotaSnapshot) {
169 SpaceQuotaSnapshot other = (SpaceQuotaSnapshot) o;
170 return quotaStatus.equals(other.quotaStatus) && usage == other.usage && limit == other.limit;
172 return false;
175 @Override
176 public String toString() {
177 StringBuilder sb = new StringBuilder(32);
178 sb.append("SpaceQuotaSnapshot[policy=").append(quotaStatus).append(", use=");
179 sb.append(StringUtils.byteDesc(usage)).append("/");
180 sb.append(StringUtils.byteDesc(limit)).append("]");
181 return sb.toString();
184 // ProtobufUtil is in hbase-client, and this doesn't need to be public.
185 public static SpaceQuotaSnapshot toSpaceQuotaSnapshot(QuotaProtos.SpaceQuotaSnapshot proto) {
186 return new SpaceQuotaSnapshot(SpaceQuotaStatus.toStatus(proto.getQuotaStatus()),
187 proto.getQuotaUsage(), proto.getQuotaLimit());
190 public static QuotaProtos.SpaceQuotaSnapshot toProtoSnapshot(SpaceQuotaSnapshot snapshot) {
191 return QuotaProtos.SpaceQuotaSnapshot.newBuilder()
192 .setQuotaStatus(SpaceQuotaStatus.toProto(snapshot.getQuotaStatus()))
193 .setQuotaUsage(snapshot.getUsage()).setQuotaLimit(snapshot.getLimit()).build();
197 * Returns a singleton that corresponds to no snapshot information.
199 public static SpaceQuotaSnapshot getNoSuchSnapshot() {
200 return NO_SUCH_SNAPSHOT;