1
2
3
4
5
6
7
8
9
10
11 package org.apache.hadoop.hbase.quotas;
12
13 import java.util.concurrent.TimeUnit;
14
15 import org.apache.hadoop.hbase.TableName;
16 import org.apache.hadoop.hbase.classification.InterfaceAudience;
17 import org.apache.hadoop.hbase.classification.InterfaceStability;
18 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
19 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetQuotaRequest;
20 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos;
21
22 @InterfaceAudience.Private
23 @InterfaceStability.Evolving
24 class ThrottleSettings extends QuotaSettings {
25 private final QuotaProtos.ThrottleRequest proto;
26
27 ThrottleSettings(final String userName, final TableName tableName, final String namespace,
28 final QuotaProtos.ThrottleRequest proto) {
29 super(userName, tableName, namespace);
30 this.proto = proto;
31 }
32
33 public ThrottleType getThrottleType() {
34 return ProtobufUtil.toThrottleType(proto.getType());
35 }
36
37 public long getSoftLimit() {
38 return proto.hasTimedQuota() ? proto.getTimedQuota().getSoftLimit() : -1;
39 }
40
41 public TimeUnit getTimeUnit() {
42 return proto.hasTimedQuota() ? ProtobufUtil.toTimeUnit(proto.getTimedQuota().getTimeUnit())
43 : null;
44 }
45
46 @Override
47 public QuotaType getQuotaType() {
48 return QuotaType.THROTTLE;
49 }
50
51 @Override
52 protected void setupSetQuotaRequest(SetQuotaRequest.Builder builder) {
53 builder.setThrottle(proto);
54 }
55
56 @Override
57 public String toString() {
58 StringBuilder builder = new StringBuilder();
59 builder.append("TYPE => THROTTLE");
60 if (proto.hasType()) {
61 builder.append(", THROTTLE_TYPE => ");
62 builder.append(proto.getType().toString());
63 }
64 if (proto.hasTimedQuota()) {
65 QuotaProtos.TimedQuota timedQuota = proto.getTimedQuota();
66 builder.append(", LIMIT => ");
67 if (timedQuota.hasSoftLimit()) {
68 switch (getThrottleType()) {
69 case REQUEST_NUMBER:
70 builder.append(String.format("%dreq", timedQuota.getSoftLimit()));
71 break;
72 case REQUEST_SIZE:
73 builder.append(sizeToString(timedQuota.getSoftLimit()));
74 break;
75 default:
76 throw new RuntimeException("Invalid throttle type: " + getThrottleType());
77 }
78 } else if (timedQuota.hasShare()) {
79 builder.append(String.format("%.2f%%", timedQuota.getShare()));
80 }
81 builder.append('/');
82 builder.append(timeToString(ProtobufUtil.toTimeUnit(timedQuota.getTimeUnit())));
83 if (timedQuota.hasScope()) {
84 builder.append(", SCOPE => ");
85 builder.append(timedQuota.getScope().toString());
86 }
87 } else {
88 builder.append(", LIMIT => NONE");
89 }
90 return builder.toString();
91 }
92
93 static ThrottleSettings fromTimedQuota(final String userName, final TableName tableName,
94 final String namespace, ThrottleType type, QuotaProtos.TimedQuota timedQuota) {
95 QuotaProtos.ThrottleRequest.Builder builder = QuotaProtos.ThrottleRequest.newBuilder();
96 builder.setType(ProtobufUtil.toProtoThrottleType(type));
97 builder.setTimedQuota(timedQuota);
98 return new ThrottleSettings(userName, tableName, namespace, builder.build());
99 }
100 }