001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import org.apache.hadoop.hbase.HRegionLocation;
021import org.apache.hadoop.hbase.ServerName;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * A {@link Result} with some statistics about the server/region status
026 */
027@InterfaceAudience.Private
028public final class ResultStatsUtil {
029
030  private ResultStatsUtil() {
031    //private ctor for util class
032  }
033
034  /**
035   * Update the stats for the specified region if the result is an instance of {@link
036   * ResultStatsUtil}
037   *
038   * @param r object that contains the result and possibly the statistics about the region
039   * @param serverStats stats tracker to update from the result
040   * @param server server from which the result was obtained
041   * @param regionName full region name for the stats.
042   * @return the underlying {@link Result} if the passed result is an {@link
043   * ResultStatsUtil} or just returns the result;
044   */
045  public static <T> T updateStats(T r, ServerStatisticTracker serverStats,
046      ServerName server, byte[] regionName) {
047    if (!(r instanceof Result)) {
048      return r;
049    }
050    Result result = (Result) r;
051    // early exit if there are no stats to collect
052    RegionLoadStats stats = result.getStats();
053    if(stats == null){
054      return r;
055    }
056
057    updateStats(serverStats, server, regionName, stats);
058    return r;
059  }
060
061  public static void updateStats(StatisticTrackable tracker, ServerName server, byte[] regionName,
062    RegionLoadStats stats) {
063    if (regionName != null && stats != null && tracker != null) {
064      tracker.updateRegionStats(server, regionName, stats);
065    }
066  }
067
068  public static <T> T updateStats(T r, ServerStatisticTracker stats,
069      HRegionLocation regionLocation) {
070    byte[] regionName = null;
071    ServerName server = null;
072    if (regionLocation != null) {
073      server = regionLocation.getServerName();
074      regionName = regionLocation.getRegionInfo().getRegionName();
075    }
076
077    return updateStats(r, stats, server, regionName);
078  }
079}