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  
19  package org.apache.hadoop.hbase.client.metrics;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  
29  import com.google.common.collect.ImmutableMap;
30  
31  
32  /**
33   * Provides client-side metrics related to scan operations
34   * The data can be passed to mapreduce framework or other systems.
35   * We use atomic longs so that one thread can increment,
36   * while another atomically resets to zero after the values are reported
37   * to hadoop's counters.
38   *
39   * Some of these metrics are general for any client operation such as put
40   * However, there is no need for this. So they are defined under scan operation
41   * for now.
42   */
43  @InterfaceAudience.Private
44  public class ScanMetrics {
45  
46  
47    private static final Log LOG = LogFactory.getLog(ScanMetrics.class);
48  
49    /**
50     * Hash to hold the String -> Atomic Long mappings.
51     */
52    private final Map<String, AtomicLong> counters = new HashMap<String, AtomicLong>();
53  
54    // AtomicLongs to hold the metrics values.  These are all updated through ClientScanner and
55    // ScannerCallable.  They are atomic longs so that atomic getAndSet can be used to reset the
56    // values after progress is passed to hadoop's counters.
57  
58  
59    /**
60     * number of RPC calls
61     */
62    public final AtomicLong countOfRPCcalls = createCounter("RPC_CALLS");
63  
64    /**
65     * number of remote RPC calls
66     */
67    public final AtomicLong countOfRemoteRPCcalls = createCounter("REMOTE_RPC_CALLS");
68  
69    /**
70     * sum of milliseconds between sequential next calls
71     */
72    public final AtomicLong sumOfMillisSecBetweenNexts = createCounter("MILLIS_BETWEEN_NEXTS");
73  
74    /**
75     * number of NotServingRegionException caught
76     */
77    public final AtomicLong countOfNSRE = createCounter("NOT_SERVING_REGION_EXCEPTION");
78  
79    /**
80     * number of bytes in Result objects from region servers
81     */
82    public final AtomicLong countOfBytesInResults = createCounter("BYTES_IN_RESULTS");
83  
84    /**
85     * number of bytes in Result objects from remote region servers
86     */
87    public final AtomicLong countOfBytesInRemoteResults = createCounter("BYTES_IN_REMOTE_RESULTS");
88  
89    /**
90     * number of regions
91     */
92    public final AtomicLong countOfRegions = createCounter("REGIONS_SCANNED");
93  
94    /**
95     * number of RPC retries
96     */
97    public final AtomicLong countOfRPCRetries = createCounter("RPC_RETRIES");
98  
99    /**
100    * number of remote RPC retries
101    */
102   public final AtomicLong countOfRemoteRPCRetries = createCounter("REMOTE_RPC_RETRIES");
103 
104   /**
105    * constructor
106    */
107   public ScanMetrics() {
108   }
109 
110   private AtomicLong createCounter(String counterName) {
111     AtomicLong c = new AtomicLong(0);
112     counters.put(counterName, c);
113     return c;
114   }
115 
116   public void setCounter(String counterName, long value) {
117     AtomicLong c = this.counters.get(counterName);
118     if (c != null) {
119       c.set(value);
120     }
121   }
122 
123   /**
124    * Get all of the values since the last time this function was called.
125    *
126    * Calling this function will reset all AtomicLongs in the instance back to 0.
127    *
128    * @return A Map of String -> Long for metrics
129    */
130   public Map<String, Long> getMetricsMap() {
131     //Create a builder
132     ImmutableMap.Builder<String, Long> builder = ImmutableMap.builder();
133     //For every entry add the value and reset the AtomicLong back to zero
134     for (Map.Entry<String, AtomicLong> e : this.counters.entrySet()) {
135       builder.put(e.getKey(), e.getValue().getAndSet(0));
136     }
137     //Build the immutable map so that people can't mess around with it.
138     return builder.build();
139   }
140 
141 }