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  import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.ThrottleRequest;
22  
23  @InterfaceAudience.Private
24  @InterfaceStability.Evolving
25  class ThrottleSettings extends QuotaSettings {
26    private final QuotaProtos.ThrottleRequest proto;
27  
28    ThrottleSettings(final String userName, final TableName tableName, final String namespace,
29        final QuotaProtos.ThrottleRequest proto) {
30      super(userName, tableName, namespace);
31      this.proto = proto;
32    }
33  
34    public ThrottleType getThrottleType() {
35      return ProtobufUtil.toThrottleType(proto.getType());
36    }
37  
38    public long getSoftLimit() {
39      return proto.hasTimedQuota() ? proto.getTimedQuota().getSoftLimit() : -1;
40    }
41  
42    public TimeUnit getTimeUnit() {
43      return proto.hasTimedQuota() ? ProtobufUtil.toTimeUnit(proto.getTimedQuota().getTimeUnit())
44          : null;
45    }
46  
47    @Override
48    public QuotaType getQuotaType() {
49      return QuotaType.THROTTLE;
50    }
51  
52    @Override
53    protected void setupSetQuotaRequest(SetQuotaRequest.Builder builder) {
54      builder.setThrottle(proto);
55    }
56  
57    ThrottleRequest getProto() {
58      return proto;
59    }
60  
61    @Override
62    public String toString() {
63      StringBuilder builder = new StringBuilder();
64      builder.append("TYPE => THROTTLE");
65      if (proto.hasType()) {
66        builder.append(", THROTTLE_TYPE => ");
67        builder.append(proto.getType().toString());
68      }
69      if (proto.hasTimedQuota()) {
70        QuotaProtos.TimedQuota timedQuota = proto.getTimedQuota();
71        builder.append(", LIMIT => ");
72        if (timedQuota.hasSoftLimit()) {
73          switch (getThrottleType()) {
74            case REQUEST_NUMBER:
75            case WRITE_NUMBER:
76            case READ_NUMBER:
77              builder.append(String.format("%dreq", timedQuota.getSoftLimit()));
78              break;
79            case REQUEST_SIZE:
80            case WRITE_SIZE:
81            case READ_SIZE:
82              builder.append(sizeToString(timedQuota.getSoftLimit()));
83              break;
84            case REQUEST_CAPACITY_UNIT:
85            case READ_CAPACITY_UNIT:
86            case WRITE_CAPACITY_UNIT:
87              builder.append(String.format("%dCU", timedQuota.getSoftLimit()));
88              break;
89            default:
90          }
91        } else if (timedQuota.hasShare()) {
92          builder.append(String.format("%.2f%%", timedQuota.getShare()));
93        }
94        builder.append('/');
95        builder.append(timeToString(ProtobufUtil.toTimeUnit(timedQuota.getTimeUnit())));
96        if (timedQuota.hasScope()) {
97          builder.append(", SCOPE => ");
98          builder.append(timedQuota.getScope().toString());
99        }
100     } else {
101       builder.append(", LIMIT => NONE");
102     }
103     return builder.toString();
104   }
105 
106   static ThrottleSettings fromTimedQuota(final String userName, final TableName tableName,
107       final String namespace, ThrottleType type, QuotaProtos.TimedQuota timedQuota) {
108     QuotaProtos.ThrottleRequest.Builder builder = QuotaProtos.ThrottleRequest.newBuilder();
109     builder.setType(ProtobufUtil.toProtoThrottleType(type));
110     builder.setTimedQuota(timedQuota);
111     return new ThrottleSettings(userName, tableName, namespace, builder.build());
112   }
113 }