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