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
21 @InterfaceAudience.Public
22 @InterfaceStability.Evolving
23 public abstract class QuotaSettings {
24 private final String userName;
25 private final String namespace;
26 private final TableName tableName;
27
28 protected QuotaSettings(final String userName, final TableName tableName,
29 final String namespace) {
30 this.userName = userName;
31 this.namespace = namespace;
32 this.tableName = tableName;
33 }
34
35 public abstract QuotaType getQuotaType();
36
37 public String getUserName() {
38 return userName;
39 }
40
41 public TableName getTableName() {
42 return tableName;
43 }
44
45 public String getNamespace() {
46 return namespace;
47 }
48
49
50
51
52
53
54 @Deprecated
55 public static SetQuotaRequest buildSetQuotaRequestProto(final QuotaSettings settings) {
56 SetQuotaRequest.Builder builder = SetQuotaRequest.newBuilder();
57 if (settings.getUserName() != null) {
58 builder.setUserName(settings.getUserName());
59 }
60 if (settings.getTableName() != null) {
61 builder.setTableName(ProtobufUtil.toProtoTableName(settings.getTableName()));
62 }
63 if (settings.getNamespace() != null) {
64 builder.setNamespace(settings.getNamespace());
65 }
66 settings.setupSetQuotaRequest(builder);
67 return builder.build();
68 }
69
70
71
72
73
74
75 @Deprecated
76 protected abstract void setupSetQuotaRequest(SetQuotaRequest.Builder builder);
77
78 protected String ownerToString() {
79 StringBuilder builder = new StringBuilder();
80 if (userName != null) {
81 builder.append("USER => '");
82 builder.append(userName);
83 builder.append("', ");
84 }
85 if (tableName != null) {
86 builder.append("TABLE => '");
87 builder.append(tableName.toString());
88 builder.append("', ");
89 }
90 if (namespace != null) {
91 builder.append("NAMESPACE => '");
92 builder.append(namespace);
93 builder.append("', ");
94 }
95 return builder.toString();
96 }
97
98 protected static String sizeToString(final long size) {
99 if (size >= (1L << 50)) return String.format("%dP", size / (1L << 50));
100 if (size >= (1L << 40)) return String.format("%dT", size / (1L << 40));
101 if (size >= (1L << 30)) return String.format("%dG", size / (1L << 30));
102 if (size >= (1L << 20)) return String.format("%dM", size / (1L << 20));
103 if (size >= (1L << 10)) return String.format("%dK", size / (1L << 10));
104 return String.format("%dB", size);
105 }
106
107 protected static String timeToString(final TimeUnit timeUnit) {
108 switch (timeUnit) {
109 case NANOSECONDS:
110 return "nsec";
111 case MICROSECONDS:
112 return "usec";
113 case MILLISECONDS:
114 return "msec";
115 case SECONDS:
116 return "sec";
117 case MINUTES:
118 return "min";
119 case HOURS:
120 return "hour";
121 case DAYS:
122 return "day";
123 default:
124 throw new RuntimeException("Invalid TimeUnit " + timeUnit);
125 }
126 }
127 }