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 static org.apache.hadoop.hbase.util.CollectionUtils.computeIfAbsent;
021
022import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
023
024import java.util.concurrent.ConcurrentHashMap;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.apache.hadoop.hbase.client.backoff.ServerStatistics;
031
032/**
033 * Tracks the statistics for multiple regions
034 */
035@InterfaceAudience.Private
036public class ServerStatisticTracker implements StatisticTrackable {
037
038  private final ConcurrentHashMap<ServerName, ServerStatistics> stats = new ConcurrentHashMap<>();
039
040  @Override
041  public void updateRegionStats(ServerName server, byte[] region, RegionLoadStats currentStats) {
042    computeIfAbsent(stats, server, ServerStatistics::new).update(region, currentStats);
043  }
044
045  public ServerStatistics getStats(ServerName server) {
046    return this.stats.get(server);
047  }
048
049  public static ServerStatisticTracker create(Configuration conf) {
050    if (!conf.getBoolean(HConstants.ENABLE_CLIENT_BACKPRESSURE,
051        HConstants.DEFAULT_ENABLE_CLIENT_BACKPRESSURE)) {
052      return null;
053    }
054    return new ServerStatisticTracker();
055  }
056
057  @VisibleForTesting
058  ServerStatistics getServerStatsForTesting(ServerName server) {
059    return stats.get(server);
060  }
061}