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