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.client.backoff;
019
020import org.apache.yetus.audience.InterfaceAudience;
021import org.apache.hadoop.hbase.client.RegionLoadStats;
022import org.apache.hadoop.hbase.util.Bytes;
023
024import java.util.Map;
025import java.util.TreeMap;
026
027/**
028 * Track the statistics for a single region
029 */
030@InterfaceAudience.Private
031public class ServerStatistics {
032
033  private Map<byte[], RegionStatistics> stats = new TreeMap<>(Bytes.BYTES_COMPARATOR);
034
035  /**
036   * Good enough attempt. Last writer wins. It doesn't really matter which one gets to update,
037   * as something gets set
038   * @param region
039   * @param currentStats
040   */
041  public void update(byte[] region, RegionLoadStats currentStats) {
042    RegionStatistics regionStat = this.stats.get(region);
043    if(regionStat == null){
044      regionStat = new RegionStatistics();
045      this.stats.put(region, regionStat);
046    }
047
048    regionStat.update(currentStats);
049  }
050
051  @InterfaceAudience.Private
052  public RegionStatistics getStatsForRegion(byte[] regionName){
053    return stats.get(regionName);
054  }
055
056  public static class RegionStatistics {
057    private int memstoreLoad = 0;
058    private int heapOccupancy = 0;
059    private int compactionPressure = 0;
060
061    public void update(RegionLoadStats currentStats) {
062      this.memstoreLoad = currentStats.getMemStoreLoad();
063      this.heapOccupancy = currentStats.getHeapOccupancy();
064      this.compactionPressure = currentStats.getCompactionPressure();
065    }
066
067    public int getMemStoreLoadPercent(){
068      return this.memstoreLoad;
069    }
070
071    public int getHeapOccupancyPercent(){
072      return this.heapOccupancy;
073    }
074
075    public int getCompactionPressure() {
076      return compactionPressure;
077    }
078
079  }
080}