001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license 003 * agreements. See the NOTICE file distributed with this work for additional information regarding 004 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the 005 * "License"); you may not use this file except in compliance with the License. You may obtain a 006 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 007 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS" 008 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License 009 * for the specific language governing permissions and limitations under the License. 010 */ 011package org.apache.hadoop.hbase.quotas; 012 013import org.apache.yetus.audience.InterfaceAudience; 014import org.apache.yetus.audience.InterfaceStability; 015import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 016 017/** 018 * This limiter will refill resources at every TimeUnit/resources interval. For example: For a 019 * limiter configured with 10resources/second, then 1 resource will be refilled after every 100ms 020 * (1sec/10resources) 021 */ 022@InterfaceAudience.Private 023@InterfaceStability.Evolving 024public class AverageIntervalRateLimiter extends RateLimiter { 025 private long nextRefillTime = -1L; 026 027 @Override 028 public long refill(long limit) { 029 final long now = EnvironmentEdgeManager.currentTime(); 030 if (nextRefillTime == -1) { 031 // Till now no resource has been consumed. 032 nextRefillTime = EnvironmentEdgeManager.currentTime(); 033 return limit; 034 } 035 036 long timeInterval = now - nextRefillTime; 037 long delta = 0; 038 long timeUnitInMillis = super.getTimeUnitInMillis(); 039 if (timeInterval >= timeUnitInMillis) { 040 delta = limit; 041 } else if (timeInterval > 0) { 042 double r = ((double)timeInterval / (double)timeUnitInMillis) * limit; 043 delta = (long)r; 044 } 045 046 if (delta > 0) { 047 this.nextRefillTime = now; 048 } 049 050 return delta; 051 } 052 053 @Override 054 public long getWaitInterval(long limit, long available, long amount) { 055 if (nextRefillTime == -1) { 056 return 0; 057 } 058 059 double r = ((double)(amount - available)) * super.getTimeUnitInMillis() / limit; 060 return (long)r; 061 } 062 063 // This method is for strictly testing purpose only 064 @Override 065 public void setNextRefillTime(long nextRefillTime) { 066 this.nextRefillTime = nextRefillTime; 067 } 068 069 @Override 070 public long getNextRefillTime() { 071 return this.nextRefillTime; 072 } 073 074}