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.*;
31 import org.apache.hadoop.hbase.client.Increment;
32 import org.apache.hadoop.hbase.client.Result;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36
37 @Category(SmallTests.class)
38 public class TestResettingCounters {
39
40 @Test
41 public void testResettingCounters() throws Exception {
42
43 HBaseTestingUtility htu = new HBaseTestingUtility();
44 Configuration conf = htu.getConfiguration();
45 FileSystem fs = FileSystem.get(conf);
46 byte [] table = Bytes.toBytes("table");
47 byte [][] families = new byte [][] {
48 Bytes.toBytes("family1"),
49 Bytes.toBytes("family2"),
50 Bytes.toBytes("family3")
51 };
52 int numQualifiers = 10;
53 byte [][] qualifiers = new byte [numQualifiers][];
54 for (int i=0; i<numQualifiers; i++) qualifiers[i] = Bytes.toBytes("qf" + i);
55 int numRows = 10;
56 byte [][] rows = new byte [numRows][];
57 for (int i=0; i<numRows; i++) rows[i] = Bytes.toBytes("r" + i);
58
59 HTableDescriptor htd = new HTableDescriptor(table);
60 for (byte [] family : families) htd.addFamily(new HColumnDescriptor(family));
61
62 HRegionInfo hri = new HRegionInfo(htd.getName(), null, null, false);
63 String testDir = htu.getDataTestDir() + "/TestResettingCounters/";
64 Path path = new Path(testDir);
65 if (fs.exists(path)) {
66 if (!fs.delete(path, true)) {
67 throw new IOException("Failed delete of " + path);
68 }
69 }
70 HRegion region = HRegion.createHRegion(hri, path, conf, htd);
71 try {
72 Increment odd = new Increment(rows[0]);
73 Increment even = new Increment(rows[0]);
74 Increment all = new Increment(rows[0]);
75 for (int i=0;i<numQualifiers;i++) {
76 if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1);
77 else odd.addColumn(families[0], qualifiers[i], 1);
78 all.addColumn(families[0], qualifiers[i], 1);
79 }
80
81
82 for (int i=0;i<5;i++) region.increment(odd, null, false);
83 region.flushcache();
84
85
86 for (int i=0;i<5;i++) region.increment(even, null, false);
87
88
89 Result result = region.increment(all, null, false);
90 assertEquals(numQualifiers, result.size());
91 KeyValue [] kvs = result.raw();
92 for (int i=0;i<kvs.length;i++) {
93 System.out.println(kvs[i].toString());
94 assertTrue(Bytes.equals(kvs[i].getQualifier(), qualifiers[i]));
95 assertEquals(6, Bytes.toLong(kvs[i].getValue()));
96 }
97 } finally {
98 HRegion.closeHRegion(region);
99 }
100 region.close();
101 region.getLog().closeAndDelete();
102 }
103
104 @org.junit.Rule
105 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
106 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
107 }
108