View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * TestGet is a medley of tests of get all done up as a single test.
49   * This class
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      // Up flush size else we bind up when we use default catalog flush of 16k.
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      // Write rows for three tables 'A', 'B', and 'C'.
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     // Now delete 'C' and make sure I don't get entries from 'B'.
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     // Assert we get null back (pass -1).
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    * @param mr
149    * @param table
150    * @param rowToFind
151    * @param answer Pass -1 if we're not to find anything.
152    * @return Row found.
153    * @throws IOException
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     // Find the row.
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    * Test file of multiple deletes and with deletes as final key.
188    * @see <a href="https://issues.apache.org/jira/browse/HBASE-751">HBASE-751</a>
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       // try finding "010" after flush
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       // Put into a different column family.  Should make it so I still get t10
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       // Now try combo of memcache and mapfiles.  Delete the t20 COLUMS[1]
262       // in memory; make sure we get back t10 again.
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       // Ask for a value off the end of the file.  Should return t10.
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       // Ok.  Let the candidate come out of hfile but have delete of
277       // the candidate be in memory.
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   /** For HBASE-694 */
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       // try finding "035"
318       Result r = region.getClosestRowBefore(T35, c0);
319       assertTrue(Bytes.equals(T30, r.getRow()));
320 
321       region.flushcache();
322 
323       // try finding "035"
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       // try finding "035"
332       r = region.getClosestRowBefore(T35, c0);
333       assertTrue(Bytes.equals(T30, r.getRow()));
334 
335       region.flushcache();
336 
337       // try finding "035"
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