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