View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client.backoff;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
22  import org.apache.hadoop.hbase.util.Bytes;
23  
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  /**
28   * Track the statistics for a single region
29   */
30  @InterfaceAudience.Private
31  public class ServerStatistics {
32  
33    private Map<byte[], RegionStatistics>
34        stats = new TreeMap<byte[], RegionStatistics>(Bytes.BYTES_COMPARATOR);
35  
36    /**
37     * Good enough attempt. Last writer wins. It doesn't really matter which one gets to update,
38     * as something gets set
39     * @param region
40     * @param currentStats
41     */
42    public void update(byte[] region, ClientProtos.RegionLoadStats currentStats) {
43      RegionStatistics regionStat = this.stats.get(region);
44      if(regionStat == null){
45        regionStat = new RegionStatistics();
46        this.stats.put(region, regionStat);
47      }
48  
49      regionStat.update(currentStats);
50    }
51  
52    @InterfaceAudience.Private
53    public RegionStatistics getStatsForRegion(byte[] regionName){
54      return stats.get(regionName);
55    }
56  
57    public static class RegionStatistics {
58      private int memstoreLoad = 0;
59      private int heapOccupancy = 0;
60  
61      public void update(ClientProtos.RegionLoadStats currentStats) {
62        this.memstoreLoad = currentStats.getMemstoreLoad();
63        this.heapOccupancy = currentStats.getHeapOccupancy();
64      }
65  
66      public int getMemstoreLoadPercent(){
67        return this.memstoreLoad;
68      }
69  
70      public int getHeapOccupancyPercent(){
71        return this.heapOccupancy;
72      }
73    }
74  }