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 org.apache.hadoop.hbase.classification.InterfaceAudience;
14  import org.apache.hadoop.hbase.classification.InterfaceStability;
15  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
16  import com.google.common.annotations.VisibleForTesting;
17  
18  /**
19   * This limiter will refill resources at every TimeUnit/resources interval. For example: For a
20   * limiter configured with 10resources/second, then 1 resource will be refilled after every 100ms
21   * (1sec/10resources)
22   */
23  @InterfaceAudience.Private
24  @InterfaceStability.Evolving
25  public class AverageIntervalRateLimiter extends RateLimiter {
26    private long nextRefillTime = -1L;
27  
28    @Override
29    public long refill(long limit) {
30      final long now = EnvironmentEdgeManager.currentTime();
31      if (nextRefillTime == -1) {
32        // Till now no resource has been consumed.
33        nextRefillTime = EnvironmentEdgeManager.currentTime();
34        return limit;
35      }
36  
37      long delta = (limit * (now - nextRefillTime)) / super.getTimeUnitInMillis();
38      if (delta > 0) {
39        this.nextRefillTime = now;
40        return Math.min(limit, delta);
41      }
42      return 0;
43    }
44  
45    @Override
46    public long getWaitInterval(long limit, long available, long amount) {
47      if (nextRefillTime == -1) {
48        return 0;
49      }
50      long timeUnitInMillis = super.getTimeUnitInMillis();
51      return ((amount * timeUnitInMillis) / limit) - ((available * timeUnitInMillis) / limit);
52    }
53  
54    // This method is for strictly testing purpose only
55    @VisibleForTesting
56    @Override
57    public void setNextRefillTime(long nextRefillTime) {
58      this.nextRefillTime = nextRefillTime;
59    }
60  
61    @VisibleForTesting
62    @Override
63    public long getNextRefillTime() {
64      return this.nextRefillTime;
65    }
66  
67  }