1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HColumnDescriptor;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.client.Increment;
36 import org.apache.hadoop.hbase.client.Result;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.Test;
39
40 public class TestResettingCounters {
41
42 @Test
43 public void testResettingCounters() throws Exception {
44
45 HBaseTestingUtility htu = new HBaseTestingUtility();
46 Configuration conf = htu.getConfiguration();
47 FileSystem fs = FileSystem.get(conf);
48 byte [] table = Bytes.toBytes("table");
49 byte [][] families = new byte [][] {
50 Bytes.toBytes("family1"),
51 Bytes.toBytes("family2"),
52 Bytes.toBytes("family3")
53 };
54 int numQualifiers = 10;
55 byte [][] qualifiers = new byte [numQualifiers][];
56 for (int i=0; i<numQualifiers; i++) qualifiers[i] = Bytes.toBytes("qf" + i);
57 int numRows = 10;
58 byte [][] rows = new byte [numRows][];
59 for (int i=0; i<numRows; i++) rows[i] = Bytes.toBytes("r" + i);
60
61 HTableDescriptor htd = new HTableDescriptor(table);
62 for (byte [] family : families) htd.addFamily(new HColumnDescriptor(family));
63
64 HRegionInfo hri = new HRegionInfo(htd, null, null, false);
65 String testDir = HBaseTestingUtility.getTestDir() + "/TestResettingCounters/";
66 Path path = new Path(testDir);
67 if (fs.exists(path)) {
68 if (!fs.delete(path, true)) {
69 throw new IOException("Failed delete of " + path);
70 }
71 }
72 HRegion region = HRegion.createHRegion(hri, path, conf);
73
74 Increment odd = new Increment(rows[0]);
75 Increment even = new Increment(rows[0]);
76 Increment all = new Increment(rows[0]);
77 for (int i=0;i<numQualifiers;i++) {
78 if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1);
79 else odd.addColumn(families[0], qualifiers[i], 1);
80 all.addColumn(families[0], qualifiers[i], 1);
81 }
82
83
84 for (int i=0;i<5;i++) region.increment(odd, null, false);
85 region.flushcache();
86
87
88 for (int i=0;i<5;i++) region.increment(even, null, false);
89
90
91 Result result = region.increment(all, null, false);
92 assertEquals(numQualifiers, result.size());
93 KeyValue [] kvs = result.raw();
94 for (int i=0;i<kvs.length;i++) {
95 System.out.println(kvs[i].toString());
96 assertTrue(Bytes.equals(kvs[i].getQualifier(), qualifiers[i]));
97 assertEquals(6, Bytes.toLong(kvs[i].getValue()));
98 }
99 }
100 }