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      serverStats.updateRegionStats(server, regionName, stats);
62      return r;
63    }
64  
65    public static <T> T updateStats(T r, ServerStatisticTracker stats,
66        HRegionLocation regionLocation) {
67      // Writes submitted using multi() will receive MultiResponses
68      if (r instanceof MultiResponse) {
69        MultiResponse mr = (MultiResponse) r;
70        for (Map.Entry<byte[], List<Pair<Integer, Object>>> e: mr.getResults().entrySet()) {
71          byte[] regionName = e.getKey();
72          for (Pair<Integer, Object> regionResult : e.getValue()) {
73            Object o = regionResult.getSecond();
74            if (o instanceof Result) {
75              Result result = (Result) o;
76              ClientProtos.RegionLoadStats loadStats = result.getStats();
77              if (loadStats != null) {
78                stats.updateRegionStats(regionLocation.getServerName(), regionName, loadStats);
79                // Once we have stats for one region we can move on to the next
80                break;
81              }
82            }
83          }
84        }
85      }
86      return r;
87    }
88  }