HBASE-26481 Consider rolling upgrading from old region replication framework (#3880)
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / quotas / SpaceQuotaSnapshot.java
bloba72c0c9ae1ea550f856ec44ff5540834d7e01a2e
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;
20 import java.util.Optional;
21 import org.apache.commons.lang3.builder.HashCodeBuilder;
22 import org.apache.yetus.audience.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 implements SpaceQuotaSnapshotView {
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 implements SpaceQuotaStatusView {
45 private static final SpaceQuotaStatus NOT_IN_VIOLATION = new SpaceQuotaStatus(null, false);
46 final Optional<SpaceViolationPolicy> policy;
47 final boolean inViolation;
49 /**
50 * Constructs a {@code SpaceQuotaSnapshot} which is in violation of the provided {@code policy}.
51 * <p/>
52 * Use {@link #notInViolation()} to obtain an instance of this class for the cases when the
53 * quota is not in violation.
54 * @param policy The non-null policy being violated.
56 public SpaceQuotaStatus(SpaceViolationPolicy policy) {
57 // If the caller is instantiating a status, the policy must be non-null
58 this(Objects.requireNonNull(policy), true);
61 private SpaceQuotaStatus(SpaceViolationPolicy policy, boolean inViolation) {
62 this.policy = Optional.ofNullable(policy);
63 this.inViolation = inViolation;
66 /**
67 * Returns the violation policy, which may be null. It is guaranteed to be non-null if
68 * {@link #isInViolation()} is {@code true}, but may be null otherwise.
70 @Override
71 public Optional<SpaceViolationPolicy> getPolicy() {
72 return policy;
75 /**
76 * @return {@code true} if the quota is being violated, {@code false} otherwise.
78 @Override
79 public boolean isInViolation() {
80 return inViolation;
83 /**
84 * Returns a singleton referring to a quota which is not in violation.
86 public static SpaceQuotaStatus notInViolation() {
87 return NOT_IN_VIOLATION;
90 @Override
91 public int hashCode() {
92 return new HashCodeBuilder().append(policy == null ? 0 : policy.hashCode())
93 .append(inViolation).toHashCode();
96 @Override
97 public boolean equals(Object o) {
98 if (o instanceof SpaceQuotaStatus) {
99 SpaceQuotaStatus other = (SpaceQuotaStatus) o;
100 return Objects.equals(policy, other.policy) && inViolation == other.inViolation;
102 return false;
105 @Override
106 public String toString() {
107 StringBuilder sb = new StringBuilder(getClass().getSimpleName());
108 sb.append("[policy=").append(policy);
109 sb.append(", inViolation=").append(inViolation).append("]");
110 return sb.toString();
113 public static QuotaProtos.SpaceQuotaStatus toProto(SpaceQuotaStatus status) {
114 QuotaProtos.SpaceQuotaStatus.Builder builder = QuotaProtos.SpaceQuotaStatus.newBuilder();
115 builder.setInViolation(status.inViolation);
116 if (status.isInViolation()) {
117 builder.setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(status.getPolicy().get()));
119 return builder.build();
122 public static SpaceQuotaStatus toStatus(QuotaProtos.SpaceQuotaStatus proto) {
123 if (proto.getInViolation()) {
124 return new SpaceQuotaStatus(ProtobufUtil.toViolationPolicy(proto.getViolationPolicy()));
125 } else {
126 return NOT_IN_VIOLATION;
131 public SpaceQuotaSnapshot(SpaceQuotaStatus quotaStatus, long usage, long limit) {
132 this.quotaStatus = Objects.requireNonNull(quotaStatus);
133 this.usage = usage;
134 this.limit = limit;
138 * Returns the status of the quota.
140 @Override
141 public SpaceQuotaStatus getQuotaStatus() {
142 return quotaStatus;
146 * Returns the current usage, in bytes, of the target (e.g. table, namespace).
148 @Override
149 public long getUsage() {
150 return usage;
154 * Returns the limit, in bytes, of the target (e.g. table, namespace).
156 @Override
157 public long getLimit() {
158 return limit;
161 @Override
162 public int hashCode() {
163 return new HashCodeBuilder()
164 .append(quotaStatus.hashCode())
165 .append(usage)
166 .append(limit)
167 .toHashCode();
170 @Override
171 public boolean equals(Object o) {
172 if (o instanceof SpaceQuotaSnapshot) {
173 SpaceQuotaSnapshot other = (SpaceQuotaSnapshot) o;
174 return quotaStatus.equals(other.quotaStatus) && usage == other.usage && limit == other.limit;
176 return false;
179 @Override
180 public String toString() {
181 StringBuilder sb = new StringBuilder(32);
182 sb.append("SpaceQuotaSnapshot[policy=").append(quotaStatus).append(", use=");
183 sb.append(StringUtils.byteDesc(usage)).append("/");
184 sb.append(StringUtils.byteDesc(limit)).append("]");
185 return sb.toString();
188 // ProtobufUtil is in hbase-client, and this doesn't need to be public.
189 public static SpaceQuotaSnapshot toSpaceQuotaSnapshot(QuotaProtos.SpaceQuotaSnapshot proto) {
190 return new SpaceQuotaSnapshot(SpaceQuotaStatus.toStatus(proto.getQuotaStatus()),
191 proto.getQuotaUsage(), proto.getQuotaLimit());
194 public static QuotaProtos.SpaceQuotaSnapshot toProtoSnapshot(SpaceQuotaSnapshot snapshot) {
195 return QuotaProtos.SpaceQuotaSnapshot.newBuilder()
196 .setQuotaStatus(SpaceQuotaStatus.toProto(snapshot.getQuotaStatus()))
197 .setQuotaUsage(snapshot.getUsage()).setQuotaLimit(snapshot.getLimit()).build();
201 * Returns a singleton that corresponds to no snapshot information.
203 public static SpaceQuotaSnapshot getNoSuchSnapshot() {
204 return NO_SUCH_SNAPSHOT;