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 * With this limiter resources will be refilled only after a fixed interval of time.
019 */
020@InterfaceAudience.Private
021@InterfaceStability.Evolving
022public class FixedIntervalRateLimiter extends RateLimiter {
023  private long nextRefillTime = -1L;
024
025  @Override
026  public long refill(long limit) {
027    final long now = EnvironmentEdgeManager.currentTime();
028    if (now < nextRefillTime) {
029      return 0;
030    }
031    nextRefillTime = now + super.getTimeUnitInMillis();
032    return limit;
033  }
034
035  @Override
036  public long getWaitInterval(long limit, long available, long amount) {
037    if (nextRefillTime == -1) {
038      return 0;
039    }
040    final long now = EnvironmentEdgeManager.currentTime();
041    final long refillTime = nextRefillTime;
042    return refillTime - now;
043  }
044
045  // This method is for strictly testing purpose only
046  @Override
047  public void setNextRefillTime(long nextRefillTime) {
048    this.nextRefillTime = nextRefillTime;
049  }
050
051  @Override
052  public long getNextRefillTime() {
053    return this.nextRefillTime;
054  }
055}