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 com.google.common.annotations.VisibleForTesting;
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.ServerName;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.client.backoff.ServerStatistics;
26  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
27  
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  /**
32   * Tracks the statistics for multiple regions
33   */
34  @InterfaceAudience.Private
35  public class ServerStatisticTracker {
36  
37    private final ConcurrentHashMap<ServerName, ServerStatistics> stats =
38        new ConcurrentHashMap<ServerName, ServerStatistics>();
39  
40    public void updateRegionStats(ServerName server, byte[] region, ClientProtos.RegionLoadStats
41        currentStats) {
42      ServerStatistics stat = stats.get(server);
43  
44      if (stat == null) {
45        stat = stats.get(server);
46        // We don't have stats for that server yet, so we need to make an entry.
47        // If we race with another thread it's a harmless unnecessary allocation.
48        if (stat == null) {
49          stat = new ServerStatistics();
50          ServerStatistics old = stats.putIfAbsent(server, stat);
51          if (old != null) {
52            stat = old;
53  	}
54        }
55      }
56      stat.update(region, currentStats);
57    }
58  
59    public ServerStatistics getStats(ServerName server) {
60      return this.stats.get(server);
61    }
62  
63    public static ServerStatisticTracker create(Configuration conf) {
64      if (!conf.getBoolean(HConstants.ENABLE_CLIENT_BACKPRESSURE,
65          HConstants.DEFAULT_ENABLE_CLIENT_BACKPRESSURE)) {
66        return null;
67      }
68      return new ServerStatisticTracker();
69    }
70  
71    @VisibleForTesting
72    ServerStatistics getServerStatsForTesting(ServerName server) {
73      return stats.get(server);
74    }
75  }