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; 016import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 017 018/** 019 * This limiter will refill resources at every TimeUnit/resources interval. For example: For a 020 * limiter configured with 10resources/second, then 1 resource will be refilled after every 100ms 021 * (1sec/10resources) 022 */ 023@InterfaceAudience.Private 024@InterfaceStability.Evolving 025public class AverageIntervalRateLimiter extends RateLimiter { 026 private long nextRefillTime = -1L; 027 028 @Override 029 public long refill(long limit) { 030 final long now = EnvironmentEdgeManager.currentTime(); 031 if (nextRefillTime == -1) { 032 // Till now no resource has been consumed. 033 nextRefillTime = EnvironmentEdgeManager.currentTime(); 034 return limit; 035 } 036 037 long timeInterval = now - nextRefillTime; 038 long delta = 0; 039 long timeUnitInMillis = super.getTimeUnitInMillis(); 040 if (timeInterval >= timeUnitInMillis) { 041 delta = limit; 042 } else if (timeInterval > 0) { 043 double r = ((double)timeInterval / (double)timeUnitInMillis) * limit; 044 delta = (long)r; 045 } 046 047 if (delta > 0) { 048 this.nextRefillTime = now; 049 } 050 051 return delta; 052 } 053 054 @Override 055 public long getWaitInterval(long limit, long available, long amount) { 056 if (nextRefillTime == -1) { 057 return 0; 058 } 059 060 double r = ((double)(amount - available)) * super.getTimeUnitInMillis() / limit; 061 return (long)r; 062 } 063 064 // This method is for strictly testing purpose only 065 @VisibleForTesting 066 @Override 067 public void setNextRefillTime(long nextRefillTime) { 068 this.nextRefillTime = nextRefillTime; 069 } 070 071 @VisibleForTesting 072 @Override 073 public long getNextRefillTime() { 074 return this.nextRefillTime; 075 } 076 077}