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<T> extends MovingAverage<T> {
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 = moveForwardMostRecentPosition();
051    lastN[index] = elapsed;
052  }
053
054  @Override
055  public double getAverageTime() {
056    return enoughStatistics()
057      ? (double) sum(getNumberOfStatistics()) / getNumberOfStatistics()
058      : (double) sum(getMostRecentPosition() + 1) / (getMostRecentPosition() + 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  /** Returns number of statistics */
070  protected int getNumberOfStatistics() {
071    return lastN.length;
072  }
073
074  /**
075   * Get statistics at index.
076   * @param index index of bar
077   */
078  protected long getStatisticsAtIndex(int index) {
079    if (index < 0 || index >= getNumberOfStatistics()) {
080      // This case should not happen, but a prudent check.
081      throw new IndexOutOfBoundsException();
082    }
083    return lastN[index];
084  }
085
086  /** Returns index of most recent */
087  protected int getMostRecentPosition() {
088    return mostRecent;
089  }
090
091  /**
092   * Move forward the most recent index.
093   * @return the most recent index
094   */
095  protected int moveForwardMostRecentPosition() {
096    int index = ++mostRecent;
097    if (!oneRound && index == getNumberOfStatistics()) {
098      // Back to the head of the lastN, from now on will
099      // start to evict oldest value.
100      oneRound = true;
101    }
102    mostRecent = index % getNumberOfStatistics();
103    return mostRecent;
104  }
105
106  private long sum(int bound) {
107    long sum = 0;
108    for (int i = 0; i < bound; i++) {
109      sum += getStatisticsAtIndex(i);
110    }
111    return sum;
112  }
113}