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      if (regionName != null) {
59        serverStats.updateRegionStats(server, regionName, stats);
60      }
61  
62      return r;
63    }
64  
65    public static <T> T updateStats(T r, ServerStatisticTracker stats,
66        HRegionLocation regionLocation) {
67      byte[] regionName = null;
68      ServerName server = null;
69      if (regionLocation != null) {
70        server = regionLocation.getServerName();
71        regionName = regionLocation.getRegionInfo().getRegionName();
72      }
73  
74      return updateStats(r, stats, server, regionName);
75    }
76  }