View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.client;
18  
19  import static org.junit.Assert.assertArrayEquals;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.Cell;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HConstants;
36  import org.apache.hadoop.hbase.HRegionInfo;
37  import org.apache.hadoop.hbase.HRegionLocation;
38  import org.apache.hadoop.hbase.HTestConst;
39  import org.apache.hadoop.hbase.KeyValue;
40  import org.apache.hadoop.hbase.testclassification.MediumTests;
41  import org.apache.hadoop.hbase.MiniHBaseCluster;
42  import org.apache.hadoop.hbase.TableName;
43  import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
44  import org.apache.hadoop.hbase.filter.ColumnRangeFilter;
45  import org.apache.hadoop.hbase.master.HMaster;
46  import org.apache.hadoop.hbase.master.RegionState.State;
47  import org.apache.hadoop.hbase.master.RegionStates;
48  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
49  import org.apache.hadoop.hbase.regionserver.HRegionServer;
50  import org.apache.hadoop.hbase.util.Bytes;
51  import org.apache.hadoop.hbase.util.ConfigUtil;
52  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
53  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
54  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
55  import org.junit.After;
56  import org.junit.AfterClass;
57  import org.junit.Before;
58  import org.junit.BeforeClass;
59  import org.junit.Test;
60  import org.junit.experimental.categories.Category;
61  
62  /**
63   * A client-side test, mostly testing scanners with various parameters.
64   */
65  @Category(MediumTests.class)
66  public class TestScannersFromClientSide {
67    private static final Log LOG = LogFactory.getLog(TestScannersFromClientSide.class);
68  
69    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
70    private static byte [] ROW = Bytes.toBytes("testRow");
71    private static byte [] FAMILY = Bytes.toBytes("testFamily");
72    private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
73    private static byte [] VALUE = Bytes.toBytes("testValue");
74  
75    /**
76     * @throws java.lang.Exception
77     */
78    @BeforeClass
79    public static void setUpBeforeClass() throws Exception {
80      Configuration conf = TEST_UTIL.getConfiguration();
81      conf.setLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY, 10 * 1024 * 1024);
82      TEST_UTIL.startMiniCluster(3);
83    }
84  
85    /**
86     * @throws java.lang.Exception
87     */
88    @AfterClass
89    public static void tearDownAfterClass() throws Exception {
90      TEST_UTIL.shutdownMiniCluster();
91    }
92  
93    /**
94     * @throws java.lang.Exception
95     */
96    @Before
97    public void setUp() throws Exception {
98      // Nothing to do.
99    }
100 
101   /**
102    * @throws java.lang.Exception
103    */
104   @After
105   public void tearDown() throws Exception {
106     // Nothing to do.
107   }
108 
109   /**
110    * Test from client side for batch of scan
111    *
112    * @throws Exception
113    */
114   @Test
115   public void testScanBatch() throws Exception {
116     TableName TABLE = TableName.valueOf("testScanBatch");
117     byte [][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, 8);
118 
119     Table ht = TEST_UTIL.createTable(TABLE, FAMILY);
120 
121     Put put;
122     Scan scan;
123     Delete delete;
124     Result result;
125     ResultScanner scanner;
126     boolean toLog = true;
127     List<Cell> kvListExp;
128 
129     // table: row, family, c0:0, c1:1, ... , c7:7
130     put = new Put(ROW);
131     for (int i=0; i < QUALIFIERS.length; i++) {
132       KeyValue kv = new KeyValue(ROW, FAMILY, QUALIFIERS[i], i, VALUE);
133       put.add(kv);
134     }
135     ht.put(put);
136 
137     // table: row, family, c0:0, c1:1, ..., c6:2, c6:6 , c7:7
138     put = new Put(ROW);
139     KeyValue kv = new KeyValue(ROW, FAMILY, QUALIFIERS[6], 2, VALUE);
140     put.add(kv);
141     ht.put(put);
142 
143     // delete upto ts: 3
144     delete = new Delete(ROW);
145     delete.deleteFamily(FAMILY, 3);
146     ht.delete(delete);
147 
148     // without batch
149     scan = new Scan().withStartRow(ROW);
150     scan.setMaxVersions();
151     scanner = ht.getScanner(scan);
152 
153     // c4:4, c5:5, c6:6, c7:7
154     kvListExp = new ArrayList<Cell>();
155     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[4], 4, VALUE));
156     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[5], 5, VALUE));
157     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[6], 6, VALUE));
158     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[7], 7, VALUE));
159     result = scanner.next();
160     verifyResult(result, kvListExp, toLog, "Testing first batch of scan");
161 
162     // with batch
163     scan =  new Scan().withStartRow(ROW);
164     scan.setMaxVersions();
165     scan.setBatch(2);
166     scanner = ht.getScanner(scan);
167 
168     // First batch: c4:4, c5:5
169     kvListExp = new ArrayList<Cell>();
170     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[4], 4, VALUE));
171     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[5], 5, VALUE));
172     result = scanner.next();
173     verifyResult(result, kvListExp, toLog, "Testing first batch of scan");
174 
175     // Second batch: c6:6, c7:7
176     kvListExp = new ArrayList<Cell>();
177     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[6], 6, VALUE));
178     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[7], 7, VALUE));
179     result = scanner.next();
180     verifyResult(result, kvListExp, toLog, "Testing second batch of scan");
181 
182   }
183 
184   @Test
185   public void testMaxResultSizeIsSetToDefault() throws Exception {
186     TableName TABLE = TableName.valueOf("testMaxResultSizeIsSetToDefault");
187     Table ht = TEST_UTIL.createTable(TABLE, FAMILY);
188 
189     // The max result size we expect the scan to use by default.
190     long expectedMaxResultSize =
191         TEST_UTIL.getConfiguration().getLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY,
192           HConstants.DEFAULT_HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE);
193 
194     int numRows = 5;
195     byte[][] ROWS = HTestConst.makeNAscii(ROW, numRows);
196 
197     int numQualifiers = 10;
198     byte[][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, numQualifiers);
199 
200     // Specify the cell size such that a single row will be larger than the default
201     // value of maxResultSize. This means that Scan RPCs should return at most a single
202     // result back to the client.
203     int cellSize = (int) (expectedMaxResultSize / (numQualifiers - 1));
204     byte[] cellValue = Bytes.createMaxByteArray(cellSize);
205 
206     Put put;
207     List<Put> puts = new ArrayList<Put>();
208     for (int row = 0; row < ROWS.length; row++) {
209       put = new Put(ROWS[row]);
210       for (int qual = 0; qual < QUALIFIERS.length; qual++) {
211         KeyValue kv = new KeyValue(ROWS[row], FAMILY, QUALIFIERS[qual], cellValue);
212         put.add(kv);
213       }
214       puts.add(put);
215     }
216     ht.put(puts);
217 
218     // Create a scan with the default configuration.
219     Scan scan = new Scan();
220 
221     ResultScanner scanner = ht.getScanner(scan);
222     assertTrue(scanner instanceof ClientScanner);
223     ClientScanner clientScanner = (ClientScanner) scanner;
224 
225     // Call next to issue a single RPC to the server
226     scanner.next();
227 
228     // The scanner should have, at most, a single result in its cache. If there more results exists
229     // in the cache it means that more than the expected max result size was fetched.
230     assertTrue("The cache contains: " + clientScanner.getCacheSize() + " results",
231       clientScanner.getCacheSize() <= 1);
232   }
233 
234   @Test
235   public void testSmallScan() throws Exception {
236     TableName TABLE = TableName.valueOf("testSmallScan");
237 
238     int numRows = 10;
239     byte[][] ROWS = HTestConst.makeNAscii(ROW, numRows);
240 
241     int numQualifiers = 10;
242     byte[][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, numQualifiers);
243 
244     Table ht = TEST_UTIL.createTable(TABLE, FAMILY);
245 
246     Put put;
247     List<Put> puts = new ArrayList<Put>();
248     for (int row = 0; row < ROWS.length; row++) {
249       put = new Put(ROWS[row]);
250       for (int qual = 0; qual < QUALIFIERS.length; qual++) {
251         KeyValue kv = new KeyValue(ROWS[row], FAMILY, QUALIFIERS[qual], VALUE);
252         put.add(kv);
253       }
254       puts.add(put);
255     }
256     ht.put(puts);
257 
258     int expectedRows = numRows;
259     int expectedCols = numRows * numQualifiers;
260 
261     // Test normal and reversed
262     testSmallScan(ht, true, expectedRows, expectedCols);
263     testSmallScan(ht, false, expectedRows, expectedCols);
264   }
265 
266   /**
267    * Run through a variety of test configurations with a small scan
268    * @param table
269    * @param reversed
270    * @param rows
271    * @param columns
272    * @throws Exception
273    */
274   private void testSmallScan(Table table, boolean reversed, int rows, int columns) throws Exception {
275     Scan baseScan = new Scan();
276     baseScan.setReversed(reversed);
277     baseScan.setSmall(true);
278 
279     Scan scan = new Scan(baseScan);
280     verifyExpectedCounts(table, scan, rows, columns);
281 
282     scan = new Scan(baseScan);
283     scan.setMaxResultSize(1);
284     verifyExpectedCounts(table, scan, rows, columns);
285 
286     scan = new Scan(baseScan);
287     scan.setMaxResultSize(1);
288     scan.setCaching(Integer.MAX_VALUE);
289     verifyExpectedCounts(table, scan, rows, columns);
290   }
291 
292   private void verifyExpectedCounts(Table table, Scan scan, int expectedRowCount,
293       int expectedCellCount) throws Exception {
294     ResultScanner scanner = table.getScanner(scan);
295 
296     int rowCount = 0;
297     int cellCount = 0;
298     Result r = null;
299     while ((r = scanner.next()) != null) {
300       rowCount++;
301       cellCount += r.rawCells().length;
302     }
303 
304     assertTrue("Expected row count: " + expectedRowCount + " Actual row count: " + rowCount,
305       expectedRowCount == rowCount);
306     assertTrue("Expected cell count: " + expectedCellCount + " Actual cell count: " + cellCount,
307       expectedCellCount == cellCount);
308     scanner.close();
309   }
310 
311   /**
312    * Test from client side for get with maxResultPerCF set
313    *
314    * @throws Exception
315    */
316   @Test
317   public void testGetMaxResults() throws Exception {
318     byte [] TABLE = Bytes.toBytes("testGetMaxResults");
319     byte [][] FAMILIES = HTestConst.makeNAscii(FAMILY, 3);
320     byte [][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, 20);
321 
322     Table ht = TEST_UTIL.createTable(TABLE, FAMILIES);
323 
324     Get get;
325     Put put;
326     Result result;
327     boolean toLog = true;
328     List<Cell> kvListExp;
329 
330     kvListExp = new ArrayList<Cell>();
331     // Insert one CF for row[0]
332     put = new Put(ROW);
333     for (int i=0; i < 10; i++) {
334       KeyValue kv = new KeyValue(ROW, FAMILIES[0], QUALIFIERS[i], 1, VALUE);
335       put.add(kv);
336       kvListExp.add(kv);
337     }
338     ht.put(put);
339 
340     get = new Get(ROW);
341     result = ht.get(get);
342     verifyResult(result, kvListExp, toLog, "Testing without setting maxResults");
343 
344     get = new Get(ROW);
345     get.setMaxResultsPerColumnFamily(2);
346     result = ht.get(get);
347     kvListExp = new ArrayList<Cell>();
348     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[0], 1, VALUE));
349     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[1], 1, VALUE));
350     verifyResult(result, kvListExp, toLog, "Testing basic setMaxResults");
351 
352     // Filters: ColumnRangeFilter
353     get = new Get(ROW);
354     get.setMaxResultsPerColumnFamily(5);
355     get.setFilter(new ColumnRangeFilter(QUALIFIERS[2], true, QUALIFIERS[5],
356                                         true));
357     result = ht.get(get);
358     kvListExp = new ArrayList<Cell>();
359     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[2], 1, VALUE));
360     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[3], 1, VALUE));
361     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[4], 1, VALUE));
362     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[5], 1, VALUE));
363     verifyResult(result, kvListExp, toLog, "Testing single CF with CRF");
364 
365     // Insert two more CF for row[0]
366     // 20 columns for CF2, 10 columns for CF1
367     put = new Put(ROW);
368     for (int i=0; i < QUALIFIERS.length; i++) {
369       KeyValue kv = new KeyValue(ROW, FAMILIES[2], QUALIFIERS[i], 1, VALUE);
370       put.add(kv);
371     }
372     ht.put(put);
373 
374     put = new Put(ROW);
375     for (int i=0; i < 10; i++) {
376       KeyValue kv = new KeyValue(ROW, FAMILIES[1], QUALIFIERS[i], 1, VALUE);
377       put.add(kv);
378     }
379     ht.put(put);
380 
381     get = new Get(ROW);
382     get.setMaxResultsPerColumnFamily(12);
383     get.addFamily(FAMILIES[1]);
384     get.addFamily(FAMILIES[2]);
385     result = ht.get(get);
386     kvListExp = new ArrayList<Cell>();
387     //Exp: CF1:q0, ..., q9, CF2: q0, q1, q10, q11, ..., q19
388     for (int i=0; i < 10; i++) {
389       kvListExp.add(new KeyValue(ROW, FAMILIES[1], QUALIFIERS[i], 1, VALUE));
390     }
391     for (int i=0; i < 2; i++) {
392         kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[i], 1, VALUE));
393       }
394     for (int i=10; i < 20; i++) {
395       kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[i], 1, VALUE));
396     }
397     verifyResult(result, kvListExp, toLog, "Testing multiple CFs");
398 
399     // Filters: ColumnRangeFilter and ColumnPrefixFilter
400     get = new Get(ROW);
401     get.setMaxResultsPerColumnFamily(3);
402     get.setFilter(new ColumnRangeFilter(QUALIFIERS[2], true, null, true));
403     result = ht.get(get);
404     kvListExp = new ArrayList<Cell>();
405     for (int i=2; i < 5; i++) {
406       kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[i], 1, VALUE));
407     }
408     for (int i=2; i < 5; i++) {
409       kvListExp.add(new KeyValue(ROW, FAMILIES[1], QUALIFIERS[i], 1, VALUE));
410     }
411     for (int i=2; i < 5; i++) {
412       kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[i], 1, VALUE));
413     }
414     verifyResult(result, kvListExp, toLog, "Testing multiple CFs + CRF");
415 
416     get = new Get(ROW);
417     get.setMaxResultsPerColumnFamily(7);
418     get.setFilter(new ColumnPrefixFilter(QUALIFIERS[1]));
419     result = ht.get(get);
420     kvListExp = new ArrayList<Cell>();
421     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[1], 1, VALUE));
422     kvListExp.add(new KeyValue(ROW, FAMILIES[1], QUALIFIERS[1], 1, VALUE));
423     kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[1], 1, VALUE));
424     for (int i=10; i < 16; i++) {
425       kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[i], 1, VALUE));
426     }
427     verifyResult(result, kvListExp, toLog, "Testing multiple CFs + PFF");
428 
429   }
430 
431   /**
432    * Test from client side for scan with maxResultPerCF set
433    *
434    * @throws Exception
435    */
436   @Test
437   public void testScanMaxResults() throws Exception {
438     byte [] TABLE = Bytes.toBytes("testScanLimit");
439     byte [][] ROWS = HTestConst.makeNAscii(ROW, 2);
440     byte [][] FAMILIES = HTestConst.makeNAscii(FAMILY, 3);
441     byte [][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, 10);
442 
443     Table ht = TEST_UTIL.createTable(TABLE, FAMILIES);
444 
445     Put put;
446     Scan scan;
447     Result result;
448     boolean toLog = true;
449     List<Cell> kvListExp, kvListScan;
450 
451     kvListExp = new ArrayList<Cell>();
452 
453     for (int r=0; r < ROWS.length; r++) {
454       put = new Put(ROWS[r]);
455       for (int c=0; c < FAMILIES.length; c++) {
456         for (int q=0; q < QUALIFIERS.length; q++) {
457           KeyValue kv = new KeyValue(ROWS[r], FAMILIES[c], QUALIFIERS[q], 1, VALUE);
458           put.add(kv);
459           if (q < 4) {
460             kvListExp.add(kv);
461           }
462         }
463       }
464       ht.put(put);
465     }
466 
467     scan = new Scan();
468     scan.setMaxResultsPerColumnFamily(4);
469     ResultScanner scanner = ht.getScanner(scan);
470     kvListScan = new ArrayList<Cell>();
471     while ((result = scanner.next()) != null) {
472       for (Cell kv : result.listCells()) {
473         kvListScan.add(kv);
474       }
475     }
476     result = Result.create(kvListScan);
477     verifyResult(result, kvListExp, toLog, "Testing scan with maxResults");
478 
479   }
480 
481   /**
482    * Test from client side for get with rowOffset
483    *
484    * @throws Exception
485    */
486   @Test
487   public void testGetRowOffset() throws Exception {
488     byte [] TABLE = Bytes.toBytes("testGetRowOffset");
489     byte [][] FAMILIES = HTestConst.makeNAscii(FAMILY, 3);
490     byte [][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, 20);
491 
492     Table ht = TEST_UTIL.createTable(TABLE, FAMILIES);
493 
494     Get get;
495     Put put;
496     Result result;
497     boolean toLog = true;
498     List<Cell> kvListExp;
499 
500     // Insert one CF for row
501     kvListExp = new ArrayList<Cell>();
502     put = new Put(ROW);
503     for (int i=0; i < 10; i++) {
504       KeyValue kv = new KeyValue(ROW, FAMILIES[0], QUALIFIERS[i], 1, VALUE);
505       put.add(kv);
506       // skipping first two kvs
507       if (i < 2) continue;
508       kvListExp.add(kv);
509     }
510     ht.put(put);
511 
512     //setting offset to 2
513     get = new Get(ROW);
514     get.setRowOffsetPerColumnFamily(2);
515     result = ht.get(get);
516     verifyResult(result, kvListExp, toLog, "Testing basic setRowOffset");
517 
518     //setting offset to 20
519     get = new Get(ROW);
520     get.setRowOffsetPerColumnFamily(20);
521     result = ht.get(get);
522     kvListExp = new ArrayList<Cell>();
523     verifyResult(result, kvListExp, toLog, "Testing offset > #kvs");
524 
525     //offset + maxResultPerCF
526     get = new Get(ROW);
527     get.setRowOffsetPerColumnFamily(4);
528     get.setMaxResultsPerColumnFamily(5);
529     result = ht.get(get);
530     kvListExp = new ArrayList<Cell>();
531     for (int i=4; i < 9; i++) {
532       kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[i], 1, VALUE));
533     }
534     verifyResult(result, kvListExp, toLog,
535       "Testing offset + setMaxResultsPerCF");
536 
537     // Filters: ColumnRangeFilter
538     get = new Get(ROW);
539     get.setRowOffsetPerColumnFamily(1);
540     get.setFilter(new ColumnRangeFilter(QUALIFIERS[2], true, QUALIFIERS[5],
541                                         true));
542     result = ht.get(get);
543     kvListExp = new ArrayList<Cell>();
544     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[3], 1, VALUE));
545     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[4], 1, VALUE));
546     kvListExp.add(new KeyValue(ROW, FAMILIES[0], QUALIFIERS[5], 1, VALUE));
547     verifyResult(result, kvListExp, toLog, "Testing offset with CRF");
548 
549     // Insert into two more CFs for row
550     // 10 columns for CF2, 10 columns for CF1
551     for(int j=2; j > 0; j--) {
552       put = new Put(ROW);
553       for (int i=0; i < 10; i++) {
554         KeyValue kv = new KeyValue(ROW, FAMILIES[j], QUALIFIERS[i], 1, VALUE);
555         put.add(kv);
556       }
557       ht.put(put);
558     }
559 
560     get = new Get(ROW);
561     get.setRowOffsetPerColumnFamily(4);
562     get.setMaxResultsPerColumnFamily(2);
563     get.addFamily(FAMILIES[1]);
564     get.addFamily(FAMILIES[2]);
565     result = ht.get(get);
566     kvListExp = new ArrayList<Cell>();
567     //Exp: CF1:q4, q5, CF2: q4, q5
568     kvListExp.add(new KeyValue(ROW, FAMILIES[1], QUALIFIERS[4], 1, VALUE));
569     kvListExp.add(new KeyValue(ROW, FAMILIES[1], QUALIFIERS[5], 1, VALUE));
570     kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[4], 1, VALUE));
571     kvListExp.add(new KeyValue(ROW, FAMILIES[2], QUALIFIERS[5], 1, VALUE));
572     verifyResult(result, kvListExp, toLog,
573        "Testing offset + multiple CFs + maxResults");
574   }
575 
576   /**
577    * Test from client side for scan while the region is reopened
578    * on the same region server.
579    *
580    * @throws Exception
581    */
582   @Test
583   public void testScanOnReopenedRegion() throws Exception {
584     TableName TABLE = TableName.valueOf("testScanOnReopenedRegion");
585     byte [][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, 2);
586 
587     HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
588 
589     Put put;
590     Scan scan;
591     Result result;
592     ResultScanner scanner;
593     boolean toLog = false;
594     List<Cell> kvListExp;
595 
596     // table: row, family, c0:0, c1:1
597     put = new Put(ROW);
598     for (int i=0; i < QUALIFIERS.length; i++) {
599       KeyValue kv = new KeyValue(ROW, FAMILY, QUALIFIERS[i], i, VALUE);
600       put.add(kv);
601     }
602     ht.put(put);
603 
604     scan = new Scan().withStartRow(ROW);
605     scanner = ht.getScanner(scan);
606 
607     HRegionLocation loc = ht.getRegionLocation(ROW);
608     HRegionInfo hri = loc.getRegionInfo();
609     MiniHBaseCluster cluster = TEST_UTIL.getMiniHBaseCluster();
610     byte[] regionName = hri.getRegionName();
611     int i = cluster.getServerWith(regionName);
612     HRegionServer rs = cluster.getRegionServer(i);
613     ProtobufUtil.closeRegion(null,
614       rs.getRSRpcServices(), rs.getServerName(), regionName, false);
615     long startTime = EnvironmentEdgeManager.currentTime();
616     long timeOut = 300000;
617     while (true) {
618       if (rs.getOnlineRegion(regionName) == null) {
619         break;
620       }
621       assertTrue("Timed out in closing the testing region",
622         EnvironmentEdgeManager.currentTime() < startTime + timeOut);
623       Thread.sleep(500);
624     }
625 
626     // Now open the region again.
627     ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
628     try {
629       HMaster master = cluster.getMaster();
630       RegionStates states = master.getAssignmentManager().getRegionStates();
631       states.regionOffline(hri);
632       states.updateRegionState(hri, State.OPENING);
633       if (ConfigUtil.useZKForAssignment(TEST_UTIL.getConfiguration())) {
634         ZKAssign.createNodeOffline(zkw, hri, loc.getServerName());
635       }
636       ProtobufUtil.openRegion(null, rs.getRSRpcServices(), rs.getServerName(), hri);
637       startTime = EnvironmentEdgeManager.currentTime();
638       while (true) {
639         if (rs.getOnlineRegion(regionName) != null) {
640           break;
641         }
642         assertTrue("Timed out in open the testing region",
643           EnvironmentEdgeManager.currentTime() < startTime + timeOut);
644         Thread.sleep(500);
645       }
646     } finally {
647       ZKAssign.deleteNodeFailSilent(zkw, hri);
648     }
649 
650     // c0:0, c1:1
651     kvListExp = new ArrayList<Cell>();
652     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[0], 0, VALUE));
653     kvListExp.add(new KeyValue(ROW, FAMILY, QUALIFIERS[1], 1, VALUE));
654     result = scanner.next();
655     verifyResult(result, kvListExp, toLog, "Testing scan on re-opened region");
656   }
657 
658   static void verifyResult(Result result, List<Cell> expKvList, boolean toLog,
659       String msg) {
660 
661     LOG.info(msg);
662     LOG.info("Expected count: " + expKvList.size());
663     LOG.info("Actual count: " + result.size());
664     if (expKvList.size() == 0)
665       return;
666 
667     int i = 0;
668     for (Cell kv : result.rawCells()) {
669       if (i >= expKvList.size()) {
670         break;  // we will check the size later
671       }
672 
673       Cell kvExp = expKvList.get(i++);
674       if (toLog) {
675         LOG.info("get kv is: " + kv.toString());
676         LOG.info("exp kv is: " + kvExp.toString());
677       }
678       assertTrue("Not equal", kvExp.equals(kv));
679     }
680 
681     assertEquals(expKvList.size(), result.size());
682   }
683 
684   @Test
685   public void testReadExpiredDataForRawScan() throws IOException {
686     TableName tableName = TableName.valueOf("testReadExpiredDataForRawScan");
687     long ts = System.currentTimeMillis() - 10000;
688     byte[] value = Bytes.toBytes("expired");
689     try (Table table = TEST_UTIL.createTable(tableName, FAMILY)) {
690       table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, ts, value));
691       assertArrayEquals(value, table.get(new Get(ROW)).getValue(FAMILY, QUALIFIER));
692       TEST_UTIL.getHBaseAdmin().modifyColumn(tableName,
693         new HColumnDescriptor(FAMILY).setTimeToLive(5));
694       try (ResultScanner scanner = table.getScanner(FAMILY)) {
695         assertNull(scanner.next());
696       }
697       try (ResultScanner scanner = table.getScanner(new Scan().setRaw(true))) {
698         assertArrayEquals(value, scanner.next().getValue(FAMILY, QUALIFIER));
699         assertNull(scanner.next());
700       }
701     }
702   }
703 
704   @Test
705   public void testScanWithSameStartRowStopRow() throws IOException {
706     TableName tableName = TableName.valueOf("testScanWithSameStartRowStopRow");
707     try (Table table = TEST_UTIL.createTable(tableName, FAMILY)) {
708       table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE));
709 
710       Scan scan = new Scan().withStartRow(ROW).withStopRow(ROW);
711       try (ResultScanner scanner = table.getScanner(scan)) {
712         assertNull(scanner.next());
713       }
714 
715       scan = new Scan().withStartRow(ROW, true).withStopRow(ROW, true);
716       try (ResultScanner scanner = table.getScanner(scan)) {
717         Result result = scanner.next();
718         assertNotNull(result);
719         assertArrayEquals(ROW, result.getRow());
720         assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER));
721         assertNull(scanner.next());
722       }
723 
724       scan = new Scan().withStartRow(ROW, true).withStopRow(ROW, false);
725       try (ResultScanner scanner = table.getScanner(scan)) {
726         assertNull(scanner.next());
727       }
728 
729       scan = new Scan().withStartRow(ROW, false).withStopRow(ROW, false);
730       try (ResultScanner scanner = table.getScanner(scan)) {
731         assertNull(scanner.next());
732       }
733 
734       scan = new Scan().withStartRow(ROW, false).withStopRow(ROW, true);
735       try (ResultScanner scanner = table.getScanner(scan)) {
736         assertNull(scanner.next());
737       }
738     }
739   }
740 }