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 case WRITE_NUMBER:
71 case READ_NUMBER:
72 builder.append(String.format("%dreq", timedQuota.getSoftLimit()));
73 break;
74 case REQUEST_SIZE:
75 case WRITE_SIZE:
76 case READ_SIZE:
77 builder.append(sizeToString(timedQuota.getSoftLimit()));
78 break;
79 default:
80 throw new RuntimeException("Invalid throttle type: " + getThrottleType());
81 }
82 } else if (timedQuota.hasShare()) {
83 builder.append(String.format("%.2f%%", timedQuota.getShare()));
84 }
85 builder.append('/');
86 builder.append(timeToString(ProtobufUtil.toTimeUnit(timedQuota.getTimeUnit())));
87 if (timedQuota.hasScope()) {
88 builder.append(", SCOPE => ");
89 builder.append(timedQuota.getScope().toString());
90 }
91 } else {
92 builder.append(", LIMIT => NONE");
93 }
94 return builder.toString();
95 }
96
97 static ThrottleSettings fromTimedQuota(final String userName, final TableName tableName,
98 final String namespace, ThrottleType type, QuotaProtos.TimedQuota timedQuota) {
99 QuotaProtos.ThrottleRequest.Builder builder = QuotaProtos.ThrottleRequest.newBuilder();
100 builder.setType(ProtobufUtil.toProtoThrottleType(type));
101 builder.setTimedQuota(timedQuota);
102 return new ThrottleSettings(userName, tableName, namespace, builder.build());
103 }
104 }