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;
19  
20  import org.apache.hadoop.hbase.HRegionLocation;
21  import org.apache.hadoop.hbase.ServerName;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
24  
25  /**
26   * A {@link Result} with some statistics about the server/region status
27   */
28  @InterfaceAudience.Private
29  public final class ResultStatsUtil {
30  
31    private ResultStatsUtil() {
32      //private ctor for util class
33    }
34  
35    /**
36     * Update the stats for the specified region if the result is an instance of {@link
37     * ResultStatsUtil}
38     *
39     * @param r object that contains the result and possibly the statistics about the region
40     * @param serverStats stats tracker to update from the result
41     * @param server server from which the result was obtained
42     * @param regionName full region name for the stats.
43     * @return the underlying {@link Result} if the passed result is an {@link
44     * ResultStatsUtil} or just returns the result;
45     */
46    public static <T> T updateStats(T r, ServerStatisticTracker serverStats,
47        ServerName server, byte[] regionName) {
48      if (!(r instanceof Result)) {
49        return r;
50      }
51      Result result = (Result) r;
52      // early exit if there are no stats to collect
53      ClientProtos.RegionLoadStats stats = result.getStats();
54      if(stats == null){
55        return r;
56      }
57  
58      updateStats(serverStats, server, regionName, stats);
59      return r;
60    }
61  
62    public static void updateStats(StatisticTrackable tracker, ServerName server, byte[] regionName,
63      ClientProtos.RegionLoadStats stats) {
64      if (regionName != null && stats != null && tracker != null) {
65        tracker.updateRegionStats(server, regionName, stats);
66      }
67    }
68  
69    public static <T> T updateStats(T r, ServerStatisticTracker stats,
70        HRegionLocation regionLocation) {
71      byte[] regionName = null;
72      ServerName server = null;
73      if (regionLocation != null) {
74        server = regionLocation.getServerName();
75        regionName = regionLocation.getRegionInfo().getRegionName();
76      }
77  
78      return updateStats(r, stats, server, regionName);
79    }
80  }