View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
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     * Convert a QuotaSettings to a protocol buffer SetQuotaRequest. This is used internally by the
51     * Admin client to serialize the quota settings and send them to the master.
52     * @deprecated Removed in HBase 2.0+ as a part of removing protobuf from our API
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     * Called by toSetQuotaRequestProto() the subclass should implement this method to set the
72     * specific SetQuotaRequest properties.
73     * @deprecated Removed in HBase 2.0+ as a part of removing protobuf from our API
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 }