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  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 }