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     */
53    public static SetQuotaRequest buildSetQuotaRequestProto(final QuotaSettings settings) {
54      SetQuotaRequest.Builder builder = SetQuotaRequest.newBuilder();
55      if (settings.getUserName() != null) {
56        builder.setUserName(settings.getUserName());
57      }
58      if (settings.getTableName() != null) {
59        builder.setTableName(ProtobufUtil.toProtoTableName(settings.getTableName()));
60      }
61      if (settings.getNamespace() != null) {
62        builder.setNamespace(settings.getNamespace());
63      }
64      settings.setupSetQuotaRequest(builder);
65      return builder.build();
66    }
67  
68    /**
69     * Called by toSetQuotaRequestProto() the subclass should implement this method to set the
70     * specific SetQuotaRequest properties.
71     */
72    protected abstract void setupSetQuotaRequest(SetQuotaRequest.Builder builder);
73  
74    protected String ownerToString() {
75      StringBuilder builder = new StringBuilder();
76      if (userName != null) {
77        builder.append("USER => '");
78        builder.append(userName);
79        builder.append("', ");
80      }
81      if (tableName != null) {
82        builder.append("TABLE => '");
83        builder.append(tableName.toString());
84        builder.append("', ");
85      }
86      if (namespace != null) {
87        builder.append("NAMESPACE => '");
88        builder.append(namespace);
89        builder.append("', ");
90      }
91      return builder.toString();
92    }
93  
94    protected static String sizeToString(final long size) {
95      if (size >= (1L << 50)) return String.format("%dP", size / (1L << 50));
96      if (size >= (1L << 40)) return String.format("%dT", size / (1L << 40));
97      if (size >= (1L << 30)) return String.format("%dG", size / (1L << 30));
98      if (size >= (1L << 20)) return String.format("%dM", size / (1L << 20));
99      if (size >= (1L << 10)) return String.format("%dK", size / (1L << 10));
100     return String.format("%dB", size);
101   }
102 
103   protected static String timeToString(final TimeUnit timeUnit) {
104     switch (timeUnit) {
105     case NANOSECONDS:
106       return "nsec";
107     case MICROSECONDS:
108       return "usec";
109     case MILLISECONDS:
110       return "msec";
111     case SECONDS:
112       return "sec";
113     case MINUTES:
114       return "min";
115     case HOURS:
116       return "hour";
117     case DAYS:
118       return "day";
119     default:
120       throw new RuntimeException("Invalid TimeUnit " + timeUnit);
121     }
122   }
123 }