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 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.ThrottleRequest;
22
23 @InterfaceAudience.Private
24 @InterfaceStability.Evolving
25 class ThrottleSettings extends QuotaSettings {
26 private final QuotaProtos.ThrottleRequest proto;
27
28 ThrottleSettings(final String userName, final TableName tableName, final String namespace,
29 final QuotaProtos.ThrottleRequest proto) {
30 super(userName, tableName, namespace);
31 this.proto = proto;
32 }
33
34 public ThrottleType getThrottleType() {
35 return ProtobufUtil.toThrottleType(proto.getType());
36 }
37
38 public long getSoftLimit() {
39 return proto.hasTimedQuota() ? proto.getTimedQuota().getSoftLimit() : -1;
40 }
41
42 public TimeUnit getTimeUnit() {
43 return proto.hasTimedQuota() ? ProtobufUtil.toTimeUnit(proto.getTimedQuota().getTimeUnit())
44 : null;
45 }
46
47 @Override
48 public QuotaType getQuotaType() {
49 return QuotaType.THROTTLE;
50 }
51
52 @Override
53 protected void setupSetQuotaRequest(SetQuotaRequest.Builder builder) {
54 builder.setThrottle(proto);
55 }
56
57 ThrottleRequest getProto() {
58 return proto;
59 }
60
61 @Override
62 public String toString() {
63 StringBuilder builder = new StringBuilder();
64 builder.append("TYPE => THROTTLE");
65 if (proto.hasType()) {
66 builder.append(", THROTTLE_TYPE => ");
67 builder.append(proto.getType().toString());
68 }
69 if (proto.hasTimedQuota()) {
70 QuotaProtos.TimedQuota timedQuota = proto.getTimedQuota();
71 builder.append(", LIMIT => ");
72 if (timedQuota.hasSoftLimit()) {
73 switch (getThrottleType()) {
74 case REQUEST_NUMBER:
75 case WRITE_NUMBER:
76 case READ_NUMBER:
77 builder.append(String.format("%dreq", timedQuota.getSoftLimit()));
78 break;
79 case REQUEST_SIZE:
80 case WRITE_SIZE:
81 case READ_SIZE:
82 builder.append(sizeToString(timedQuota.getSoftLimit()));
83 break;
84 case REQUEST_CAPACITY_UNIT:
85 case READ_CAPACITY_UNIT:
86 case WRITE_CAPACITY_UNIT:
87 builder.append(String.format("%dCU", timedQuota.getSoftLimit()));
88 break;
89 default:
90 }
91 } else if (timedQuota.hasShare()) {
92 builder.append(String.format("%.2f%%", timedQuota.getShare()));
93 }
94 builder.append('/');
95 builder.append(timeToString(ProtobufUtil.toTimeUnit(timedQuota.getTimeUnit())));
96 if (timedQuota.hasScope()) {
97 builder.append(", SCOPE => ");
98 builder.append(timedQuota.getScope().toString());
99 }
100 } else {
101 builder.append(", LIMIT => NONE");
102 }
103 return builder.toString();
104 }
105
106 static ThrottleSettings fromTimedQuota(final String userName, final TableName tableName,
107 final String namespace, ThrottleType type, QuotaProtos.TimedQuota timedQuota) {
108 QuotaProtos.ThrottleRequest.Builder builder = QuotaProtos.ThrottleRequest.newBuilder();
109 builder.setType(ProtobufUtil.toProtoThrottleType(type));
110 builder.setTimedQuota(timedQuota);
111 return new ThrottleSettings(userName, tableName, namespace, builder.build());
112 }
113 }