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 java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.CellUtil;
31 import org.apache.hadoop.hbase.HBaseTestCase;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.HRegionInfo;
34 import org.apache.hadoop.hbase.HTableDescriptor;
35 import org.apache.hadoop.hbase.KeyValue;
36 import org.apache.hadoop.hbase.MediumTests;
37 import org.apache.hadoop.hbase.TableName;
38 import org.apache.hadoop.hbase.catalog.MetaEditor;
39 import org.apache.hadoop.hbase.client.Delete;
40 import org.apache.hadoop.hbase.client.Durability;
41 import org.apache.hadoop.hbase.client.Put;
42 import org.apache.hadoop.hbase.client.Result;
43 import org.apache.hadoop.hbase.client.Scan;
44 import org.apache.hadoop.hbase.util.Bytes;
45 import org.junit.experimental.categories.Category;
46
47
48
49
50
51 @Category(MediumTests.class)
52 public class TestGetClosestAtOrBefore extends HBaseTestCase {
53 private static final Log LOG = LogFactory.getLog(TestGetClosestAtOrBefore.class);
54
55 private static final byte[] T00 = Bytes.toBytes("000");
56 private static final byte[] T10 = Bytes.toBytes("010");
57 private static final byte[] T11 = Bytes.toBytes("011");
58 private static final byte[] T12 = Bytes.toBytes("012");
59 private static final byte[] T20 = Bytes.toBytes("020");
60 private static final byte[] T30 = Bytes.toBytes("030");
61 private static final byte[] T31 = Bytes.toBytes("031");
62 private static final byte[] T35 = Bytes.toBytes("035");
63 private static final byte[] T40 = Bytes.toBytes("040");
64
65
66
67 public void testUsingMetaAndBinary() throws IOException {
68 FileSystem filesystem = FileSystem.get(conf);
69 Path rootdir = testDir;
70
71 HTableDescriptor.META_TABLEDESC.setMemStoreFlushSize(64 * 1024 * 1024);
72
73 HRegion mr = HRegion.createHRegion(HRegionInfo.FIRST_META_REGIONINFO,
74 rootdir, this.conf, HTableDescriptor.META_TABLEDESC);
75 try {
76
77 for (char c = 'A'; c < 'D'; c++) {
78 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("" + c));
79 final int last = 128;
80 final int interval = 2;
81 for (int i = 0; i <= last; i += interval) {
82 HRegionInfo hri = new HRegionInfo(htd.getTableName(),
83 i == 0? HConstants.EMPTY_BYTE_ARRAY: Bytes.toBytes((byte)i),
84 i == last? HConstants.EMPTY_BYTE_ARRAY: Bytes.toBytes((byte)i + interval));
85
86 Put put = MetaEditor.makePutFromRegionInfo(hri);
87 put.setDurability(Durability.SKIP_WAL);
88 mr.put(put);
89 }
90 }
91 InternalScanner s = mr.getScanner(new Scan());
92 try {
93 List<Cell> keys = new ArrayList<Cell>();
94 while(s.next(keys)) {
95 LOG.info(keys);
96 keys.clear();
97 }
98 } finally {
99 s.close();
100 }
101 findRow(mr, 'C', 44, 44);
102 findRow(mr, 'C', 45, 44);
103 findRow(mr, 'C', 46, 46);
104 findRow(mr, 'C', 43, 42);
105 mr.flushcache();
106 findRow(mr, 'C', 44, 44);
107 findRow(mr, 'C', 45, 44);
108 findRow(mr, 'C', 46, 46);
109 findRow(mr, 'C', 43, 42);
110
111 byte [] firstRowInC = HRegionInfo.createRegionName(
112 TableName.valueOf("" + 'C'),
113 HConstants.EMPTY_BYTE_ARRAY, HConstants.ZEROES, false);
114 Scan scan = new Scan(firstRowInC);
115 s = mr.getScanner(scan);
116 try {
117 List<Cell> keys = new ArrayList<Cell>();
118 while (s.next(keys)) {
119 mr.delete(new Delete(CellUtil.cloneRow(keys.get(0))));
120 keys.clear();
121 }
122 } finally {
123 s.close();
124 }
125
126 findRow(mr, 'C', 44, -1);
127 findRow(mr, 'C', 45, -1);
128 findRow(mr, 'C', 46, -1);
129 findRow(mr, 'C', 43, -1);
130 mr.flushcache();
131 findRow(mr, 'C', 44, -1);
132 findRow(mr, 'C', 45, -1);
133 findRow(mr, 'C', 46, -1);
134 findRow(mr, 'C', 43, -1);
135 } finally {
136 if (mr != null) {
137 try {
138 mr.close();
139 } catch (Exception e) {
140 e.printStackTrace();
141 }
142 mr.getLog().closeAndDelete();
143 }
144 }
145 }
146
147
148
149
150
151
152
153
154
155 private byte [] findRow(final HRegion mr, final char table,
156 final int rowToFind, final int answer)
157 throws IOException {
158 TableName tableb = TableName.valueOf("" + table);
159
160 byte [] tofindBytes = Bytes.toBytes((short)rowToFind);
161 byte [] metaKey = HRegionInfo.createRegionName(
162 tableb, tofindBytes,
163 HConstants.NINES, false);
164 LOG.info("find=" + new String(metaKey));
165 Result r = mr.getClosestRowBefore(metaKey);
166 if (answer == -1) {
167 assertNull(r);
168 return null;
169 }
170 assertTrue(Bytes.compareTo(Bytes.toBytes((short)answer),
171 extractRowFromMetaRow(r.getRow())) == 0);
172 return r.getRow();
173 }
174
175 private byte [] extractRowFromMetaRow(final byte [] b) {
176 int firstDelimiter = KeyValue.getDelimiter(b, 0, b.length,
177 HConstants.DELIMITER);
178 int lastDelimiter = KeyValue.getDelimiterInReverse(b, 0, b.length,
179 HConstants.DELIMITER);
180 int length = lastDelimiter - firstDelimiter - 1;
181 byte [] row = new byte[length];
182 System.arraycopy(b, firstDelimiter + 1, row, 0, length);
183 return row;
184 }
185
186
187
188
189
190 public void testGetClosestRowBefore3() throws IOException{
191 HRegion region = null;
192 byte [] c0 = COLUMNS[0];
193 byte [] c1 = COLUMNS[1];
194 try {
195 HTableDescriptor htd = createTableDescriptor(getName());
196 region = createNewHRegion(htd, null, null);
197
198 Put p = new Put(T00);
199 p.add(c0, c0, T00);
200 region.put(p);
201
202 p = new Put(T10);
203 p.add(c0, c0, T10);
204 region.put(p);
205
206 p = new Put(T20);
207 p.add(c0, c0, T20);
208 region.put(p);
209
210 Result r = region.getClosestRowBefore(T20, c0);
211 assertTrue(Bytes.equals(T20, r.getRow()));
212
213 Delete d = new Delete(T20);
214 d.deleteColumn(c0, c0);
215 region.delete(d);
216
217 r = region.getClosestRowBefore(T20, c0);
218 assertTrue(Bytes.equals(T10, r.getRow()));
219
220 p = new Put(T30);
221 p.add(c0, c0, T30);
222 region.put(p);
223
224 r = region.getClosestRowBefore(T30, c0);
225 assertTrue(Bytes.equals(T30, r.getRow()));
226
227 d = new Delete(T30);
228 d.deleteColumn(c0, c0);
229 region.delete(d);
230
231 r = region.getClosestRowBefore(T30, c0);
232 assertTrue(Bytes.equals(T10, r.getRow()));
233 r = region.getClosestRowBefore(T31, c0);
234 assertTrue(Bytes.equals(T10, r.getRow()));
235
236 region.flushcache();
237
238
239 r = region.getClosestRowBefore(T30, c0);
240 assertTrue(Bytes.equals(T10, r.getRow()));
241 r = region.getClosestRowBefore(T31, c0);
242 assertTrue(Bytes.equals(T10, r.getRow()));
243
244
245 p = new Put(T20);
246 p.add(c1, c1, T20);
247 region.put(p);
248
249 r = region.getClosestRowBefore(T30, c0);
250 assertTrue(Bytes.equals(T10, r.getRow()));
251 r = region.getClosestRowBefore(T31, c0);
252 assertTrue(Bytes.equals(T10, r.getRow()));
253
254 region.flushcache();
255
256 r = region.getClosestRowBefore(T30, c0);
257 assertTrue(Bytes.equals(T10, r.getRow()));
258 r = region.getClosestRowBefore(T31, c0);
259 assertTrue(Bytes.equals(T10, r.getRow()));
260
261
262
263 d = new Delete(T20);
264 d.deleteColumn(c1, c1);
265 region.delete(d);
266 r = region.getClosestRowBefore(T30, c0);
267 assertTrue(Bytes.equals(T10, r.getRow()));
268
269
270 r = region.getClosestRowBefore(T31, c0);
271 assertTrue(Bytes.equals(T10, r.getRow()));
272 region.flushcache();
273 r = region.getClosestRowBefore(T31, c0);
274 assertTrue(Bytes.equals(T10, r.getRow()));
275
276
277
278 p = new Put(T11);
279 p.add(c0, c0, T11);
280 region.put(p);
281 d = new Delete(T10);
282 d.deleteColumn(c1, c1);
283 r = region.getClosestRowBefore(T12, c0);
284 assertTrue(Bytes.equals(T11, r.getRow()));
285 } finally {
286 if (region != null) {
287 try {
288 region.close();
289 } catch (Exception e) {
290 e.printStackTrace();
291 }
292 region.getLog().closeAndDelete();
293 }
294 }
295 }
296
297
298 public void testGetClosestRowBefore2() throws IOException{
299 HRegion region = null;
300 byte [] c0 = COLUMNS[0];
301 try {
302 HTableDescriptor htd = createTableDescriptor(getName());
303 region = createNewHRegion(htd, null, null);
304
305 Put p = new Put(T10);
306 p.add(c0, c0, T10);
307 region.put(p);
308
309 p = new Put(T30);
310 p.add(c0, c0, T30);
311 region.put(p);
312
313 p = new Put(T40);
314 p.add(c0, c0, T40);
315 region.put(p);
316
317
318 Result r = region.getClosestRowBefore(T35, c0);
319 assertTrue(Bytes.equals(T30, r.getRow()));
320
321 region.flushcache();
322
323
324 r = region.getClosestRowBefore(T35, c0);
325 assertTrue(Bytes.equals(T30, r.getRow()));
326
327 p = new Put(T20);
328 p.add(c0, c0, T20);
329 region.put(p);
330
331
332 r = region.getClosestRowBefore(T35, c0);
333 assertTrue(Bytes.equals(T30, r.getRow()));
334
335 region.flushcache();
336
337
338 r = region.getClosestRowBefore(T35, c0);
339 assertTrue(Bytes.equals(T30, r.getRow()));
340 } finally {
341 if (region != null) {
342 try {
343 region.close();
344 } catch (Exception e) {
345 e.printStackTrace();
346 }
347 region.getLog().closeAndDelete();
348 }
349 }
350 }
351
352 }
353