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