1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.quotas;
18
19 import java.util.Objects;
20
21 import org.apache.hadoop.hbase.TableName;
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.classification.InterfaceStability;
24 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
25 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos;
26 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetQuotaRequest.Builder;
27 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.SpaceLimitRequest;
28 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.SpaceQuota;
29
30
31
32
33 @InterfaceAudience.Private
34 @InterfaceStability.Evolving
35 class SpaceLimitSettings extends QuotaSettings {
36
37 private final SpaceLimitRequest proto;
38
39 SpaceLimitSettings(TableName tableName, long sizeLimit, SpaceViolationPolicy violationPolicy) {
40 super(null, Objects.requireNonNull(tableName), null);
41 if (sizeLimit < 0L) {
42 throw new IllegalArgumentException("Size limit must be a non-negative value.");
43 }
44 proto = buildProtoAddQuota(sizeLimit, Objects.requireNonNull(violationPolicy));
45 }
46
47
48
49
50 SpaceLimitSettings(TableName tableName) {
51 super(null, Objects.requireNonNull(tableName), null);
52 proto = buildProtoRemoveQuota();
53 }
54
55 SpaceLimitSettings(String namespace, long sizeLimit, SpaceViolationPolicy violationPolicy) {
56 super(null, null, Objects.requireNonNull(namespace));
57 if (sizeLimit < 0L) {
58 throw new IllegalArgumentException("Size limit must be a non-negative value.");
59 }
60 proto = buildProtoAddQuota(sizeLimit, Objects.requireNonNull(violationPolicy));
61 }
62
63
64
65
66 SpaceLimitSettings(String namespace) {
67 super(null, null, Objects.requireNonNull(namespace));
68 proto = buildProtoRemoveQuota();
69 }
70
71
72
73
74
75
76
77
78 private SpaceLimitRequest buildProtoAddQuota(
79 long sizeLimit, SpaceViolationPolicy violationPolicy) {
80 return SpaceLimitRequest.newBuilder().setQuota(
81 SpaceQuota.newBuilder()
82 .setSoftLimit(sizeLimit)
83 .setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(violationPolicy))
84 .build())
85 .build();
86 }
87
88
89
90
91
92
93 private SpaceLimitRequest buildProtoRemoveQuota() {
94 return SpaceLimitRequest.newBuilder().setQuota(
95 SpaceQuota.newBuilder()
96 .setRemove(true)
97 .build())
98 .build();
99 }
100
101
102
103
104 SpaceLimitRequest getProto() {
105 return proto.toBuilder().build();
106 }
107
108 @Override
109 public QuotaType getQuotaType() {
110 return QuotaType.SPACE;
111 }
112
113 @Override
114 protected void setupSetQuotaRequest(Builder builder) {
115
116 builder.setSpaceLimit(proto);
117 }
118
119
120
121
122
123
124
125
126 static SpaceLimitSettings fromSpaceQuota(
127 final TableName tableName, final QuotaProtos.SpaceQuota proto) {
128 validateProtoArguments(proto);
129 return new SpaceLimitSettings(tableName, proto.getSoftLimit(),
130 ProtobufUtil.toViolationPolicy(proto.getViolationPolicy()));
131 }
132
133
134
135
136
137
138
139
140 static SpaceLimitSettings fromSpaceQuota(
141 final String namespace, final QuotaProtos.SpaceQuota proto) {
142 validateProtoArguments(proto);
143 return new SpaceLimitSettings(namespace, proto.getSoftLimit(),
144 ProtobufUtil.toViolationPolicy(proto.getViolationPolicy()));
145 }
146
147
148
149
150
151
152
153 static void validateProtoArguments(final QuotaProtos.SpaceQuota proto) {
154 if (!Objects.requireNonNull(proto).hasSoftLimit()) {
155 throw new IllegalArgumentException("Cannot handle SpaceQuota without a soft limit");
156 }
157 if (!proto.hasViolationPolicy()) {
158 throw new IllegalArgumentException("Cannot handle SpaceQuota without a violation policy");
159 }
160 }
161
162 @Override
163 public int hashCode() {
164 return Objects.hash(getTableName(), getNamespace(), proto);
165 }
166
167 @Override
168 public boolean equals(Object o) {
169 if (o == this) {
170 return true;
171 }
172 if (!(o instanceof SpaceLimitSettings)) {
173 return false;
174 }
175
176 SpaceLimitSettings other = (SpaceLimitSettings) o;
177 return Objects.equals(getTableName(), other.getTableName()) &&
178 Objects.equals(getNamespace(), other.getNamespace()) &&
179 Objects.equals(proto, other.proto);
180 }
181
182 @Override
183 public String toString() {
184 StringBuilder sb = new StringBuilder();
185 sb.append("TYPE => SPACE");
186 if (getTableName() != null) {
187 sb.append(", TABLE => ").append(getTableName());
188 }
189 if (getNamespace() != null) {
190 sb.append(", NAMESPACE => ").append(getNamespace());
191 }
192 if (proto.getQuota().getRemove()) {
193 sb.append(", REMOVE => ").append(proto.getQuota().getRemove());
194 } else {
195 sb.append(", LIMIT => ").append(proto.getQuota().getSoftLimit());
196 sb.append(", VIOLATION_POLICY => ").append(proto.getQuota().getViolationPolicy());
197 }
198 return sb.toString();
199 }
200 }