1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.*;
24 import org.apache.hadoop.hbase.client.*;
25 import org.apache.hadoop.hbase.test.MetricsAssertHelper;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.apache.hadoop.hbase.util.Threads;
28 import org.apache.log4j.Level;
29 import org.apache.log4j.Logger;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.experimental.categories.Category;
34 import static org.junit.Assert.*;
35
36 import java.io.IOException;
37
38
39 @Category(MediumTests.class)
40 public class TestRegionServerMetrics {
41 private static final Log LOG = LogFactory.getLog(TestRegionServerMetrics.class);
42 private static MetricsAssertHelper metricsHelper;
43
44 static {
45 Logger.getLogger("org.apache.hadoop.hbase").setLevel(Level.DEBUG);
46 }
47
48 private static MiniHBaseCluster cluster;
49 private static HRegionServer rs;
50 private static Configuration conf;
51 private static HBaseTestingUtility TEST_UTIL;
52 private static MetricsRegionServer metricsRegionServer;
53 private static MetricsRegionServerSource serverSource;
54
55 @BeforeClass
56 public static void startCluster() throws Exception {
57 metricsHelper = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
58 TEST_UTIL = new HBaseTestingUtility();
59 conf = TEST_UTIL.getConfiguration();
60 conf.getLong("hbase.splitlog.max.resubmit", 0);
61
62 conf.setInt("zookeeper.recovery.retry", 0);
63 conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1);
64
65 TEST_UTIL.startMiniCluster(1, 1);
66 cluster = TEST_UTIL.getHBaseCluster();
67
68 cluster.waitForActiveAndReadyMaster();
69
70 while (cluster.getLiveRegionServerThreads().size() < 1) {
71 Threads.sleep(100);
72 }
73
74 rs = cluster.getRegionServer(0);
75 metricsRegionServer = rs.getMetrics();
76 serverSource = metricsRegionServer.getMetricsSource();
77 }
78
79 @AfterClass
80 public static void after() throws Exception {
81 if (TEST_UTIL != null) {
82 TEST_UTIL.shutdownMiniCluster();
83 }
84 }
85
86 @Test(timeout = 300000)
87 public void testRegionCount() throws Exception {
88 String regionMetricsKey = "regionCount";
89 long regions = metricsHelper.getGaugeLong(regionMetricsKey, serverSource);
90
91 TEST_UTIL.createTable(Bytes.toBytes("table"), Bytes.toBytes("cf"));
92 metricsHelper.assertGaugeGt(regionMetricsKey, regions, serverSource);
93 }
94
95 @Test
96 public void testLocalFiles() throws Exception {
97 metricsHelper.assertGauge("percentFilesLocal", 0, serverSource);
98 }
99
100 @Test
101 public void testRequestCount() throws Exception {
102 String tableNameString = "testRequestCount";
103 byte[] tName = Bytes.toBytes(tableNameString);
104 byte[] cfName = Bytes.toBytes("d");
105 byte[] row = Bytes.toBytes("rk");
106 byte[] qualifier = Bytes.toBytes("qual");
107 byte[] initValue = Bytes.toBytes("Value");
108 byte[] nextValue = Bytes.toBytes("NEXT VAL");
109
110
111 TEST_UTIL.createTable(tName, cfName);
112
113 new HTable(conf, tName).close();
114 metricsRegionServer.getRegionServerWrapper().forceRecompute();
115 long requests = metricsHelper.getCounter("totalRequestCount", serverSource);
116 long readRequests = metricsHelper.getCounter("readRequestCount", serverSource);
117 long writeRequests = metricsHelper.getCounter("writeRequestCount", serverSource);
118
119 HTable table = new HTable(conf, tName);
120
121 Put p = new Put(row);
122
123
124 p.add(cfName, qualifier, initValue);
125
126 for (int i=0; i< 30; i++) {
127 table.put(p);
128 }
129
130
131 table.flushCommits();
132
133 Get g = new Get(row);
134 for (int i=0; i< 10; i++) {
135 table.get(g);
136 }
137
138
139 for ( HRegionInfo i:table.getRegionLocations().keySet()) {
140 MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
141 .getMetrics()
142 .getSource()
143 .getAggregateSource();
144 String prefix = "table."+tableNameString + ".region." + i.getEncodedName();
145 metricsHelper.assertCounter(prefix + ".getNumOps", 10, agg);
146 metricsHelper.assertCounter(prefix + ".mutateCount", 30, agg);
147 }
148
149
150 metricsRegionServer.getRegionServerWrapper().forceRecompute();
151 metricsHelper.assertCounterGt("totalRequestCount", requests + 39, serverSource);
152 metricsHelper.assertCounterGt("readRequestCount", readRequests + 9, serverSource);
153 metricsHelper.assertCounterGt("writeRequestCount", writeRequests + 29, serverSource);
154
155 table.close();
156 }
157
158 @Test
159 public void testMutationsWithoutWal() throws Exception {
160 byte[] tableName = Bytes.toBytes("testMutationsWithoutWal");
161 byte[] cf = Bytes.toBytes("d");
162 byte[] row = Bytes.toBytes("rk");
163 byte[] qualifier = Bytes.toBytes("qual");
164 byte[] val = Bytes.toBytes("Value");
165
166 metricsRegionServer.getRegionServerWrapper().forceRecompute();
167
168 TEST_UTIL.createTable(tableName, cf);
169
170 HTable t = new HTable(conf, tableName);
171
172 Put p = new Put(row);
173 p.add(cf, qualifier, val);
174 p.setDurability(Durability.SKIP_WAL);
175
176 t.put(p);
177 t.flushCommits();
178
179 metricsRegionServer.getRegionServerWrapper().forceRecompute();
180 metricsHelper.assertGauge("mutationsWithoutWALCount", 1, serverSource);
181 long minLength = row.length + cf.length + qualifier.length + val.length;
182 metricsHelper.assertGaugeGt("mutationsWithoutWALSize", minLength, serverSource);
183
184 t.close();
185 }
186
187 @Test
188 public void testStoreCount() throws Exception {
189 byte[] tableName = Bytes.toBytes("testStoreCount");
190 byte[] cf = Bytes.toBytes("d");
191 byte[] row = Bytes.toBytes("rk");
192 byte[] qualifier = Bytes.toBytes("qual");
193 byte[] val = Bytes.toBytes("Value");
194
195 metricsRegionServer.getRegionServerWrapper().forceRecompute();
196 long stores = metricsHelper.getGaugeLong("storeCount", serverSource);
197 long storeFiles = metricsHelper.getGaugeLong("storeFileCount", serverSource);
198
199 TEST_UTIL.createTable(tableName, cf);
200
201
202 HTable t = new HTable(conf, tableName);
203 Put p = new Put(row);
204 p.add(cf, qualifier, val);
205 t.put(p);
206 t.flushCommits();
207 TEST_UTIL.getHBaseAdmin().flush(tableName);
208
209 metricsRegionServer.getRegionServerWrapper().forceRecompute();
210 metricsHelper.assertGauge("storeCount", stores +1, serverSource);
211 metricsHelper.assertGauge("storeFileCount", storeFiles + 1, serverSource);
212
213 t.close();
214 }
215
216 @Test
217 public void testCheckAndPutCount() throws Exception {
218 String tableNameString = "testCheckAndPutCount";
219 byte[] tableName = Bytes.toBytes(tableNameString);
220 byte[] cf = Bytes.toBytes("d");
221 byte[] row = Bytes.toBytes("rk");
222 byte[] qualifier = Bytes.toBytes("qual");
223 byte[] valOne = Bytes.toBytes("Value");
224 byte[] valTwo = Bytes.toBytes("ValueTwo");
225 byte[] valThree = Bytes.toBytes("ValueThree");
226
227 TEST_UTIL.createTable(tableName, cf);
228 HTable t = new HTable(conf, tableName);
229 Put p = new Put(row);
230 p.add(cf, qualifier, valOne);
231 t.put(p);
232 t.flushCommits();
233
234 Put pTwo = new Put(row);
235 pTwo.add(cf, qualifier, valTwo);
236 t.checkAndPut(row, cf, qualifier, valOne, pTwo);
237 t.flushCommits();
238
239 Put pThree = new Put(row);
240 pThree.add(cf, qualifier, valThree);
241 t.checkAndPut(row, cf, qualifier, valOne, pThree);
242 t.flushCommits();
243
244
245 metricsRegionServer.getRegionServerWrapper().forceRecompute();
246 metricsHelper.assertCounter("checkMutateFailedCount", 1, serverSource);
247 metricsHelper.assertCounter("checkMutatePassedCount", 1, serverSource);
248
249 t.close();
250 }
251
252 @Test
253 public void testIncrement() throws Exception {
254 String tableNameString = "testIncrement";
255 byte[] tableName = Bytes.toBytes(tableNameString);
256 byte[] cf = Bytes.toBytes("d");
257 byte[] row = Bytes.toBytes("rk");
258 byte[] qualifier = Bytes.toBytes("qual");
259 byte[] val = Bytes.toBytes(0l);
260
261
262 TEST_UTIL.createTable(tableName, cf);
263 HTable t = new HTable(conf, tableName);
264
265 Put p = new Put(row);
266 p.add(cf, qualifier, val);
267 t.put(p);
268 t.flushCommits();
269
270 for(int count = 0; count< 13; count++) {
271 Increment inc = new Increment(row);
272 inc.addColumn(cf, qualifier, 100);
273 t.increment(inc);
274 }
275
276 t.flushCommits();
277
278 metricsRegionServer.getRegionServerWrapper().forceRecompute();
279 metricsHelper.assertCounter("incrementNumOps", 13, serverSource);
280
281 t.close();
282 }
283
284 @Test
285 public void testAppend() throws Exception {
286 String tableNameString = "testAppend";
287 byte[] tableName = Bytes.toBytes(tableNameString);
288 byte[] cf = Bytes.toBytes("d");
289 byte[] row = Bytes.toBytes("rk");
290 byte[] qualifier = Bytes.toBytes("qual");
291 byte[] val = Bytes.toBytes("One");
292
293
294 TEST_UTIL.createTable(tableName, cf);
295 HTable t = new HTable(conf, tableName);
296
297 Put p = new Put(row);
298 p.add(cf, qualifier, val);
299 t.put(p);
300 t.flushCommits();
301
302 for(int count = 0; count< 73; count++) {
303 Append append = new Append(row);
304 append.add(cf, qualifier, Bytes.toBytes(",Test"));
305 t.append(append);
306 }
307
308 t.flushCommits();
309
310 metricsRegionServer.getRegionServerWrapper().forceRecompute();
311 metricsHelper.assertCounter("appendNumOps", 73, serverSource);
312
313 t.close();
314 }
315
316 @Test
317 public void testScanNext() throws IOException {
318 String tableNameString = "testScanNext";
319 byte[] tableName = Bytes.toBytes(tableNameString);
320 byte[] cf = Bytes.toBytes("d");
321 byte[] qualifier = Bytes.toBytes("qual");
322 byte[] val = Bytes.toBytes("One");
323
324
325 TEST_UTIL.createTable(tableName, cf);
326 HTable t = new HTable(conf, tableName);
327 t.setAutoFlush(false);
328 for (int insertCount =0; insertCount < 100; insertCount++) {
329 Put p = new Put(Bytes.toBytes("" + insertCount + "row"));
330 p.add(cf, qualifier, val);
331 t.put(p);
332 }
333 t.flushCommits();
334
335 Scan s = new Scan();
336 s.setBatch(1);
337 s.setCaching(1);
338 ResultScanner resultScanners = t.getScanner(s);
339
340 for (int nextCount = 0; nextCount < 30; nextCount++) {
341 Result result = resultScanners.next();
342 assertNotNull(result);
343 assertEquals(1, result.size());
344 }
345 for ( HRegionInfo i:t.getRegionLocations().keySet()) {
346 MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
347 .getMetrics()
348 .getSource()
349 .getAggregateSource();
350 String prefix = "table."+tableNameString + ".region." + i.getEncodedName();
351 metricsHelper.assertCounter(prefix + ".scanNextNumOps", 30, agg);
352 }
353 }
354 }