1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.metrics;
20
21 import java.util.Set;
22
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HRegionInfo;
25 import org.apache.hadoop.hbase.client.Append;
26 import org.apache.hadoop.hbase.client.Delete;
27 import org.apache.hadoop.hbase.client.Get;
28 import org.apache.hadoop.hbase.client.HTable;
29 import org.apache.hadoop.hbase.client.Increment;
30 import org.apache.hadoop.hbase.client.Put;
31 import org.apache.hadoop.hbase.util.Bytes;
32
33
34
35
36
37
38
39 public class OperationMetrics {
40
41 private static final String DELETE_KEY = "delete_";
42 private static final String PUT_KEY = "put_";
43 private static final String GET_KEY = "get_";
44 private static final String ICV_KEY = "incrementColumnValue_";
45 private static final String INCREMENT_KEY = "increment_";
46 private static final String MULTIPUT_KEY = "multiput_";
47 private static final String MULTIDELETE_KEY = "multidelete_";
48 private static final String APPEND_KEY = "append_";
49 private static final String READREQUESTCOUNT_KEY = "readrequestcount";
50 private static final String WRITEREQUESTCOUNT_KEY = "writerequestcount";
51
52
53 private static final String CONF_KEY =
54 "hbase.metrics.exposeOperationTimes";
55
56 private final String tableName;
57 private final String regionName;
58 private final String regionMetrixPrefix;
59 private final Configuration conf;
60 private final boolean exposeTimes;
61
62
63
64
65
66
67
68 public OperationMetrics(Configuration conf, HRegionInfo regionInfo) {
69
70
71 if (conf != null) {
72 SchemaMetrics.configureGlobally(conf);
73
74 this.conf = conf;
75 if (regionInfo != null) {
76 this.tableName = regionInfo.getTableNameAsString();
77 this.regionName = regionInfo.getEncodedName();
78 } else {
79 this.tableName = SchemaMetrics.UNKNOWN;
80 this.regionName = SchemaMetrics.UNKNOWN;
81 }
82 this.regionMetrixPrefix =
83 SchemaMetrics.generateRegionMetricsPrefix(this.tableName, this.regionName);
84 this.exposeTimes = this.conf.getBoolean(CONF_KEY, true);
85 } else {
86
87 this.conf = null;
88 this.tableName = null;
89 this.regionName = null;
90 this.regionMetrixPrefix = null;
91 this.exposeTimes = false;
92 }
93 }
94
95
96
97
98
99 public OperationMetrics() {
100 this(null, null);
101 }
102
103
104
105
106
107
108 public void setReadRequestCountMetrics(long value) {
109 doSetNumericPersistentMetrics(READREQUESTCOUNT_KEY, value);
110 }
111
112
113
114
115
116
117 public void setWriteRequestCountMetrics(long value) {
118 doSetNumericPersistentMetrics(WRITEREQUESTCOUNT_KEY, value);
119 }
120
121 private void doSetNumericPersistentMetrics(String key, long value) {
122 RegionMetricsStorage.setNumericPersistentMetric(this.regionMetrixPrefix+key, value);
123 }
124
125
126
127
128
129
130
131 public void updateMultiPutMetrics(Set<byte[]> columnFamilies, long value) {
132 doUpdateTimeVarying(columnFamilies, MULTIPUT_KEY, value);
133 }
134
135
136
137
138
139
140
141 public void updateMultiDeleteMetrics(Set<byte[]> columnFamilies, long value) {
142 doUpdateTimeVarying(columnFamilies, MULTIDELETE_KEY, value);
143 }
144
145
146
147
148
149
150
151
152
153 public void updateGetMetrics(Set<byte[]> columnFamilies, long value) {
154 doUpdateTimeVarying(columnFamilies, GET_KEY, value);
155 }
156
157
158
159
160
161
162 public void updateIncrementMetrics(Set<byte[]> columnFamilies, long value) {
163 doUpdateTimeVarying(columnFamilies, INCREMENT_KEY, value);
164 }
165
166
167
168
169
170
171
172 public void updateAppendMetrics(Set<byte[]> columnFamilies, long value) {
173 doUpdateTimeVarying(columnFamilies, APPEND_KEY, value);
174 }
175
176
177
178
179
180
181
182
183
184
185
186 public void updateIncrementColumnValueMetrics(byte[] columnFamily, long value) {
187 String cfMetricPrefix =
188 SchemaMetrics.generateSchemaMetricsPrefix(this.tableName, Bytes.toString(columnFamily));
189 doSafeIncTimeVarying(cfMetricPrefix, ICV_KEY, value);
190 doSafeIncTimeVarying(this.regionMetrixPrefix, ICV_KEY, value);
191 }
192
193
194
195
196
197
198
199
200
201 public void updatePutMetrics(Set<byte[]> columnFamilies, long value) {
202 doUpdateTimeVarying(columnFamilies, PUT_KEY, value);
203 }
204
205
206
207
208
209
210
211
212 public void updateDeleteMetrics(Set<byte[]> columnFamilies, long value) {
213 doUpdateTimeVarying(columnFamilies, DELETE_KEY, value);
214 }
215
216
217
218
219
220
221
222
223 public void closeMetrics(String regionEncodedName) {
224 RegionMetricsStorage.clear(regionEncodedName);
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238 private void doUpdateTimeVarying(Set<byte[]> columnFamilies, String key, long value) {
239 String cfPrefix = null;
240 if (columnFamilies != null) {
241 cfPrefix = SchemaMetrics.generateSchemaMetricsPrefix(tableName, columnFamilies);
242 } else {
243 cfPrefix = SchemaMetrics.generateSchemaMetricsPrefix(tableName, SchemaMetrics.UNKNOWN);
244 }
245
246 doSafeIncTimeVarying(cfPrefix, key, value);
247 doSafeIncTimeVarying(this.regionMetrixPrefix, key, value);
248 }
249
250 private void doSafeIncTimeVarying(String prefix, String key, long value) {
251 if (exposeTimes) {
252 if (prefix != null && !prefix.isEmpty() && key != null && !key.isEmpty()) {
253 String m = prefix + key;
254 RegionMetricsStorage.incrTimeVaryingMetric(m, value);
255 }
256 }
257 }
258
259 }