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