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;
20
21 import static org.apache.hadoop.hbase.HBaseTestingUtility.START_KEY_BYTES;
22 import static org.apache.hadoop.hbase.HBaseTestingUtility.fam1;
23 import static org.apache.hadoop.hbase.HBaseTestingUtility.fam2;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseTestCase;
31 import org.apache.hadoop.hbase.HBaseTestCase.HRegionIncommon;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.HTableDescriptor;
35 import org.apache.hadoop.hbase.testclassification.MediumTests;
36 import org.apache.hadoop.hbase.client.Delete;
37 import org.apache.hadoop.hbase.client.Get;
38 import org.apache.hadoop.hbase.client.Result;
39 import org.apache.hadoop.hbase.wal.WAL;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.junit.rules.TestName;
47
48
49
50
51
52 @Category(MediumTests.class)
53 public class TestMinorCompaction {
54 @Rule public TestName name = new TestName();
55 private static final Log LOG = LogFactory.getLog(TestMinorCompaction.class.getName());
56 private static final HBaseTestingUtility UTIL = HBaseTestingUtility.createLocalHTU();
57 protected Configuration conf = UTIL.getConfiguration();
58
59 private Region r = null;
60 private HTableDescriptor htd = null;
61 private int compactionThreshold;
62 private byte[] firstRowBytes, secondRowBytes, thirdRowBytes;
63 final private byte[] col1, col2;
64
65
66 public TestMinorCompaction() {
67 super();
68
69
70 conf.setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 1024*1024);
71 conf.setInt(HConstants.HREGION_MEMSTORE_BLOCK_MULTIPLIER, 100);
72 compactionThreshold = conf.getInt("hbase.hstore.compactionThreshold", 3);
73
74 firstRowBytes = START_KEY_BYTES;
75 secondRowBytes = START_KEY_BYTES.clone();
76
77 secondRowBytes[START_KEY_BYTES.length - 1]++;
78 thirdRowBytes = START_KEY_BYTES.clone();
79 thirdRowBytes[START_KEY_BYTES.length - 1] += 2;
80 col1 = Bytes.toBytes("column1");
81 col2 = Bytes.toBytes("column2");
82 }
83
84 @Before
85 public void setUp() throws Exception {
86 this.htd = UTIL.createTableDescriptor(name.getMethodName());
87 this.r = UTIL.createLocalHRegion(htd, null, null);
88 }
89
90 @After
91 public void tearDown() throws Exception {
92 WAL wal = ((HRegion)r).getWAL();
93 ((HRegion)r).close();
94 wal.close();
95 }
96
97 @Test
98 public void testMinorCompactionWithDeleteRow() throws Exception {
99 Delete deleteRow = new Delete(secondRowBytes);
100 testMinorCompactionWithDelete(deleteRow);
101 }
102
103 @Test
104 public void testMinorCompactionWithDeleteColumn1() throws Exception {
105 Delete dc = new Delete(secondRowBytes);
106
107 dc.deleteColumns(fam2, col2);
108 testMinorCompactionWithDelete(dc);
109 }
110
111 @Test
112 public void testMinorCompactionWithDeleteColumn2() throws Exception {
113 Delete dc = new Delete(secondRowBytes);
114 dc.deleteColumn(fam2, col2);
115
116
117
118
119
120
121 testMinorCompactionWithDelete(dc, 3);
122 }
123
124 @Test
125 public void testMinorCompactionWithDeleteColumnFamily() throws Exception {
126 Delete deleteCF = new Delete(secondRowBytes);
127 deleteCF.deleteFamily(fam2);
128 testMinorCompactionWithDelete(deleteCF);
129 }
130
131 @Test
132 public void testMinorCompactionWithDeleteVersion1() throws Exception {
133 Delete deleteVersion = new Delete(secondRowBytes);
134 deleteVersion.deleteColumns(fam2, col2, 2);
135
136
137
138 testMinorCompactionWithDelete(deleteVersion, 1);
139 }
140
141 @Test
142 public void testMinorCompactionWithDeleteVersion2() throws Exception {
143 Delete deleteVersion = new Delete(secondRowBytes);
144 deleteVersion.deleteColumn(fam2, col2, 1);
145
146
147
148
149
150 testMinorCompactionWithDelete(deleteVersion, 3);
151 }
152
153
154
155
156
157
158
159 private void testMinorCompactionWithDelete(Delete delete) throws Exception {
160 testMinorCompactionWithDelete(delete, 0);
161 }
162
163 private void testMinorCompactionWithDelete(Delete delete, int expectedResultsAfterDelete) throws Exception {
164 HRegionIncommon loader = new HRegionIncommon(r);
165 for (int i = 0; i < compactionThreshold + 1; i++) {
166 HBaseTestCase.addContent(loader, Bytes.toString(fam1), Bytes.toString(col1), firstRowBytes,
167 thirdRowBytes, i);
168 HBaseTestCase.addContent(loader, Bytes.toString(fam1), Bytes.toString(col2), firstRowBytes,
169 thirdRowBytes, i);
170 HBaseTestCase.addContent(loader, Bytes.toString(fam2), Bytes.toString(col1), firstRowBytes,
171 thirdRowBytes, i);
172 HBaseTestCase.addContent(loader, Bytes.toString(fam2), Bytes.toString(col2), firstRowBytes,
173 thirdRowBytes, i);
174 r.flush(true);
175 }
176
177 Result result = r.get(new Get(firstRowBytes).addColumn(fam1, col1).setMaxVersions(100));
178 assertEquals(compactionThreshold, result.size());
179 result = r.get(new Get(secondRowBytes).addColumn(fam2, col2).setMaxVersions(100));
180 assertEquals(compactionThreshold, result.size());
181
182
183
184
185
186 r.delete(delete);
187
188
189 result = r.get(new Get(secondRowBytes).addColumn(fam2, col2).setMaxVersions(100));
190 assertEquals(expectedResultsAfterDelete, result.size());
191
192 result = r.get(new Get(firstRowBytes).addColumn(fam1, col1).setMaxVersions(100));
193 assertEquals(compactionThreshold, result.size());
194
195 r.flush(true);
196
197
198
199
200 result = r.get(new Get(secondRowBytes).addColumn(fam2, col2).setMaxVersions(100));
201 assertEquals(expectedResultsAfterDelete, result.size());
202
203 result = r.get(new Get(firstRowBytes).addColumn(fam1, col1).setMaxVersions(100));
204 assertEquals(compactionThreshold, result.size());
205
206
207 Store store2 = r.getStore(fam2);
208 int numFiles1 = store2.getStorefiles().size();
209 assertTrue("Was expecting to see 4 store files", numFiles1 > compactionThreshold);
210 ((HStore)store2).compactRecentForTestingAssumingDefaultPolicy(compactionThreshold);
211 int numFiles2 = store2.getStorefiles().size();
212
213 assertTrue("Number of store files should go down", numFiles1 > numFiles2);
214
215 assertTrue("Was not supposed to be a major compaction", numFiles2 > 1);
216
217
218 result = r.get(new Get(secondRowBytes).addColumn(fam2, col2).setMaxVersions(100));
219 assertEquals(expectedResultsAfterDelete, result.size());
220
221 result = r.get(new Get(firstRowBytes).addColumn(fam1, col1).setMaxVersions(100));
222 assertEquals(compactionThreshold, result.size());
223 }
224 }