HBASE-26286: Add support for specifying store file tracker when restoring or cloning...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / quotas / AverageIntervalRateLimiter.java
blobd10e6eacc7c93c014a7b4237e362a8065f1b4575
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.
11 package org.apache.hadoop.hbase.quotas;
13 import org.apache.yetus.audience.InterfaceAudience;
14 import org.apache.yetus.audience.InterfaceStability;
15 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
17 /**
18 * This limiter will refill resources at every TimeUnit/resources interval. For example: For a
19 * limiter configured with 10resources/second, then 1 resource will be refilled after every 100ms
20 * (1sec/10resources)
22 @InterfaceAudience.Private
23 @InterfaceStability.Evolving
24 public class AverageIntervalRateLimiter extends RateLimiter {
25 private long nextRefillTime = -1L;
27 @Override
28 public long refill(long limit) {
29 final long now = EnvironmentEdgeManager.currentTime();
30 if (nextRefillTime == -1) {
31 // Till now no resource has been consumed.
32 nextRefillTime = EnvironmentEdgeManager.currentTime();
33 return limit;
36 long timeInterval = now - nextRefillTime;
37 long delta = 0;
38 long timeUnitInMillis = super.getTimeUnitInMillis();
39 if (timeInterval >= timeUnitInMillis) {
40 delta = limit;
41 } else if (timeInterval > 0) {
42 double r = ((double)timeInterval / (double)timeUnitInMillis) * limit;
43 delta = (long)r;
46 if (delta > 0) {
47 this.nextRefillTime = now;
50 return delta;
53 @Override
54 public long getWaitInterval(long limit, long available, long amount) {
55 if (nextRefillTime == -1) {
56 return 0;
59 double r = ((double)(amount - available)) * super.getTimeUnitInMillis() / limit;
60 return (long)r;
63 // This method is for strictly testing purpose only
64 @Override
65 public void setNextRefillTime(long nextRefillTime) {
66 this.nextRefillTime = nextRefillTime;
69 @Override
70 public long getNextRefillTime() {
71 return this.nextRefillTime;