1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.metrics.impl;
19
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.metrics.Counter;
28 import org.apache.hadoop.hbase.metrics.Gauge;
29 import org.apache.hadoop.hbase.metrics.Histogram;
30 import org.apache.hadoop.hbase.metrics.Meter;
31 import org.apache.hadoop.hbase.metrics.Metric;
32 import org.apache.hadoop.hbase.metrics.MetricRegistry;
33 import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
34 import org.apache.hadoop.hbase.metrics.MetricSet;
35 import org.apache.hadoop.hbase.metrics.Timer;
36 import org.apache.hadoop.hbase.util.CollectionUtils;
37
38 import com.google.common.base.Optional;
39
40
41
42
43 @InterfaceAudience.Private
44 public class MetricRegistryImpl implements MetricRegistry {
45
46 private final MetricRegistryInfo info;
47
48 private final ConcurrentMap<String, Metric> metrics;
49
50 public MetricRegistryImpl(MetricRegistryInfo info) {
51 this.info = info;
52 this.metrics = new ConcurrentHashMap<>();
53 }
54
55 @Override
56 public Timer timer(String name) {
57 Timer metric = (Timer) metrics.get(name);
58 if (metric != null) {
59 return metric;
60 }
61
62 Timer newTimer = createTimer();
63 metric = (Timer) metrics.putIfAbsent(name, newTimer);
64 if (metric != null) {
65 return metric;
66 }
67
68 return newTimer;
69 }
70
71 protected Timer createTimer() {
72 return new TimerImpl();
73 }
74
75 @Override
76 public Histogram histogram(String name) {
77 Histogram metric = (Histogram) metrics.get(name);
78 if (metric != null) {
79 return metric;
80 }
81
82 Histogram newHistogram = createHistogram();
83 metric = (Histogram) metrics.putIfAbsent(name, newHistogram);
84 if (metric != null) {
85 return metric;
86 }
87
88 return newHistogram;
89 }
90
91 protected Histogram createHistogram() {
92 return new HistogramImpl();
93 }
94
95 @Override
96 public Meter meter(String name) {
97 Meter metric = (Meter) metrics.get(name);
98 if (metric != null) {
99 return metric;
100 }
101
102 Meter newmeter = createMeter();
103 metric = (Meter) metrics.putIfAbsent(name, newmeter);
104 if (metric != null) {
105 return metric;
106 }
107
108 return newmeter;
109 }
110
111 protected Meter createMeter() {
112 return new DropwizardMeter();
113 }
114
115 @Override
116 public Counter counter(String name) {
117 Counter metric = (Counter) metrics.get(name);
118 if (metric != null) {
119 return metric;
120 }
121
122 Counter newCounter = createCounter();
123 metric = (Counter) metrics.putIfAbsent(name, newCounter);
124 if (metric != null) {
125 return metric;
126 }
127
128 return newCounter;
129 }
130
131 protected Counter createCounter() {
132 return new CounterImpl();
133 }
134
135 @Override
136 public Optional<Metric> get(String name) {
137
138 return Optional.fromNullable(metrics.get(name));
139 }
140
141 @Override
142 public Metric register(String name, Metric metric) {
143 Metric m = metrics.get(name);
144 if (m != null) {
145 return m;
146 }
147
148 Metric oldMetric = metrics.putIfAbsent(name, metric);
149 if (oldMetric != null) {
150 return oldMetric;
151 }
152
153 return metric;
154 }
155
156 @Override
157 public <T> Gauge<T> register(String name, Gauge<T> gauge) {
158 return (Gauge) register(name, (Metric)gauge);
159 }
160
161 @Override
162 public void registerAll(MetricSet metricSet) {
163 Set<Entry<String,Metric>> entrySet = metricSet.getMetrics().entrySet();
164 for (Entry<String, Metric> entry : entrySet) {
165 register(entry.getKey(), entry.getValue());
166 }
167 }
168
169 @Override
170 public Map<String, Metric> getMetrics() {
171 return metrics;
172 }
173
174 @Override
175 public boolean remove(String name) {
176 return metrics.remove(name) != null;
177 }
178
179 @Override
180 public MetricRegistryInfo getMetricRegistryInfo() {
181 return info;
182 }
183 }