2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.quotas
;
20 import java
.io
.IOException
;
21 import java
.util
.concurrent
.TimeUnit
;
22 import org
.apache
.hadoop
.hbase
.DoNotRetryIOException
;
23 import org
.apache
.hadoop
.hbase
.TableName
;
24 import org
.apache
.yetus
.audience
.InterfaceAudience
;
25 import org
.apache
.yetus
.audience
.InterfaceStability
;
27 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.ProtobufUtil
;
28 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.MasterProtos
.SetQuotaRequest
;
29 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.QuotaProtos
;
30 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.QuotaProtos
.TimedQuota
;
32 @InterfaceAudience.Private
33 @InterfaceStability.Evolving
34 public class ThrottleSettings
extends QuotaSettings
{
35 final QuotaProtos
.ThrottleRequest proto
;
37 ThrottleSettings(final String userName
, final TableName tableName
, final String namespace
,
38 final String regionServer
, final QuotaProtos
.ThrottleRequest proto
) {
39 super(userName
, tableName
, namespace
, regionServer
);
43 public ThrottleType
getThrottleType() {
44 return ProtobufUtil
.toThrottleType(proto
.getType());
47 public long getSoftLimit() {
48 return proto
.hasTimedQuota() ? proto
.getTimedQuota().getSoftLimit() : -1;
52 * Returns a copy of the internal state of <code>this</code>
54 QuotaProtos
.ThrottleRequest
getProto() {
55 return proto
.toBuilder().build();
58 public TimeUnit
getTimeUnit() {
59 return proto
.hasTimedQuota() ?
60 ProtobufUtil
.toTimeUnit(proto
.getTimedQuota().getTimeUnit()) : null;
63 public QuotaScope
getQuotaScope() {
64 return proto
.hasTimedQuota() ? ProtobufUtil
.toQuotaScope(proto
.getTimedQuota().getScope())
69 public QuotaType
getQuotaType() {
70 return QuotaType
.THROTTLE
;
74 protected void setupSetQuotaRequest(SetQuotaRequest
.Builder builder
) {
75 builder
.setThrottle(proto
);
79 public String
toString() {
80 StringBuilder builder
= new StringBuilder();
81 builder
.append("TYPE => THROTTLE");
82 if (proto
.hasType()) {
83 builder
.append(", THROTTLE_TYPE => ");
84 builder
.append(proto
.getType().toString());
86 if (proto
.hasTimedQuota()) {
87 QuotaProtos
.TimedQuota timedQuota
= proto
.getTimedQuota();
88 builder
.append(", LIMIT => ");
89 if (timedQuota
.hasSoftLimit()) {
90 switch (getThrottleType()) {
94 builder
.append(String
.format("%dreq", timedQuota
.getSoftLimit()));
99 builder
.append(sizeToString(timedQuota
.getSoftLimit()));
101 case REQUEST_CAPACITY_UNIT
:
102 case READ_CAPACITY_UNIT
:
103 case WRITE_CAPACITY_UNIT
:
104 builder
.append(String
.format("%dCU", timedQuota
.getSoftLimit()));
108 } else if (timedQuota
.hasShare()) {
109 builder
.append(String
.format("%.2f%%", timedQuota
.getShare()));
112 builder
.append(timeToString(ProtobufUtil
.toTimeUnit(timedQuota
.getTimeUnit())));
113 if (timedQuota
.hasScope()) {
114 builder
.append(", SCOPE => ");
115 builder
.append(timedQuota
.getScope().toString());
118 builder
.append(", LIMIT => NONE");
120 return builder
.toString();
124 protected ThrottleSettings
merge(QuotaSettings other
) throws IOException
{
125 if (other
instanceof ThrottleSettings
) {
126 ThrottleSettings otherThrottle
= (ThrottleSettings
) other
;
128 // Make sure this and the other target the same "subject"
129 validateQuotaTarget(other
);
131 QuotaProtos
.ThrottleRequest
.Builder builder
= proto
.toBuilder();
132 if (!otherThrottle
.proto
.hasType()) {
136 QuotaProtos
.ThrottleRequest otherProto
= otherThrottle
.proto
;
137 if (otherProto
.hasTimedQuota()) {
138 if (otherProto
.hasTimedQuota()) {
139 validateTimedQuota(otherProto
.getTimedQuota());
142 if (!proto
.getType().equals(otherProto
.getType())) {
143 throw new IllegalArgumentException(
144 "Cannot merge a ThrottleRequest for " + proto
.getType() + " with " +
145 otherProto
.getType());
147 QuotaProtos
.TimedQuota
.Builder timedQuotaBuilder
= proto
.getTimedQuota().toBuilder();
148 timedQuotaBuilder
.mergeFrom(otherProto
.getTimedQuota());
150 QuotaProtos
.ThrottleRequest mergedReq
= builder
.setTimedQuota(
151 timedQuotaBuilder
.build()).build();
152 return new ThrottleSettings(getUserName(), getTableName(), getNamespace(),
153 getRegionServer(), mergedReq
);
159 private void validateTimedQuota(final TimedQuota timedQuota
) throws IOException
{
160 if (timedQuota
.getSoftLimit() < 1) {
161 throw new DoNotRetryIOException(new UnsupportedOperationException(
162 "The throttle limit must be greater then 0, got " + timedQuota
.getSoftLimit()));
166 static ThrottleSettings
fromTimedQuota(final String userName
, final TableName tableName
,
167 final String namespace
, final String regionServer
, ThrottleType type
,
168 QuotaProtos
.TimedQuota timedQuota
) {
169 QuotaProtos
.ThrottleRequest
.Builder builder
= QuotaProtos
.ThrottleRequest
.newBuilder();
170 builder
.setType(ProtobufUtil
.toProtoThrottleType(type
));
171 builder
.setTimedQuota(timedQuota
);
172 return new ThrottleSettings(userName
, tableName
, namespace
, regionServer
, builder
.build());