001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * Instead of calculate a whole time average, this class focus on the last N. The last N is stored
024 * in a circle array.
025 */
026@InterfaceAudience.Private
027public class WindowMovingAverage extends MovingAverage {
028  protected final static int DEFAULT_SIZE = 5;
029
030  // The last n statistics.
031  protected long[] lastN;
032  // The index of the most recent statistics.
033  protected int mostRecent;
034  // If it travels a round.
035  protected boolean oneRound;
036
037  public WindowMovingAverage(String label) {
038    this(label, DEFAULT_SIZE);
039  }
040
041  public WindowMovingAverage(String label, int size) {
042    super(label);
043    this.lastN = new long[size <= 0 ? DEFAULT_SIZE : size];
044    this.mostRecent = -1;
045    this.oneRound = false;
046  }
047
048  @Override
049  protected void updateMostRecentTime(long elapsed) {
050    int index = moveForwardMostRecentPosistion();
051    lastN[index] = elapsed;
052  }
053
054  @Override
055  public double getAverageTime() {
056    return enoughStatistics()
057      ? (double) sum(getNumberOfStatistics()) / getNumberOfStatistics()
058      : (double) sum(getMostRecentPosistion() + 1) / (getMostRecentPosistion() + 1);
059  }
060
061  /**
062   * Check if there are enough statistics.
063   * @return true if lastN is full
064   */
065  protected boolean enoughStatistics() {
066    return oneRound;
067  }
068
069  /**
070   * @return number of statistics
071   */
072  protected int getNumberOfStatistics() {
073    return lastN.length;
074  }
075
076  /**
077   * Get statistics at index.
078   * @param index index of bar n
079   */
080  protected long getStatisticsAtIndex(int index) {
081    if (index < 0 || index >= getNumberOfStatistics()) {
082      // This case should not happen, but a prudent check.
083      throw new IndexOutOfBoundsException();
084    }
085    return lastN[index];
086  }
087
088  /**
089   * @return index of most recent
090   */
091  protected int getMostRecentPosistion() {
092    return mostRecent;
093  }
094
095  /**
096   * Move forward the most recent index.
097   * @return the most recent index
098   */
099  protected int moveForwardMostRecentPosistion() {
100    int index = ++mostRecent;
101    if (!oneRound && index == getNumberOfStatistics()) {
102      // Back to the head of the lastN, from now on will
103      // start to evict oldest value.
104      oneRound = true;
105    }
106    mostRecent = index % getNumberOfStatistics();
107    return mostRecent;
108  }
109
110  private long sum(int bound) {
111    long sum = 0;
112    for (int i = 0; i < bound; i++) {
113      sum += getStatisticsAtIndex(i);
114    }
115    return sum;
116  }
117}