001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.List; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.Cell; 026import org.apache.hadoop.hbase.CellUtil; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.HTestConst; 030import org.apache.hadoop.hbase.KeyValue; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.filter.Filter; 033import org.apache.hadoop.hbase.filter.FilterBase; 034import org.apache.hadoop.hbase.regionserver.StoreScanner; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.hadoop.hbase.util.Threads; 037import org.junit.AfterClass; 038import org.junit.BeforeClass; 039 040public abstract class AbstractTestScanCursor { 041 042 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 043 044 /** 045 * Table configuration 046 */ 047 protected static TableName TABLE_NAME = TableName.valueOf("TestScanCursor"); 048 049 protected static int NUM_ROWS = 5; 050 protected static byte[] ROW = Bytes.toBytes("testRow"); 051 protected static byte[][] ROWS = HTestConst.makeNAscii(ROW, NUM_ROWS); 052 053 protected static int NUM_FAMILIES = 2; 054 protected static byte[] FAMILY = Bytes.toBytes("testFamily"); 055 protected static byte[][] FAMILIES = HTestConst.makeNAscii(FAMILY, NUM_FAMILIES); 056 057 protected static int NUM_QUALIFIERS = 2; 058 protected static byte[] QUALIFIER = Bytes.toBytes("testQualifier"); 059 protected static byte[][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, NUM_QUALIFIERS); 060 061 protected static int VALUE_SIZE = 10; 062 protected static byte[] VALUE = Bytes.createMaxByteArray(VALUE_SIZE); 063 064 protected static final int TIMEOUT = 4000; 065 066 @BeforeClass 067 public static void setUpBeforeClass() throws Exception { 068 Configuration conf = TEST_UTIL.getConfiguration(); 069 070 conf.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, TIMEOUT); 071 conf.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, TIMEOUT); 072 073 // Check the timeout condition after every cell 074 conf.setLong(StoreScanner.HBASE_CELLS_SCANNED_PER_HEARTBEAT_CHECK, 1); 075 TEST_UTIL.startMiniCluster(1); 076 077 createTestTable(TABLE_NAME, ROWS, FAMILIES, QUALIFIERS, VALUE); 078 } 079 080 private static void createTestTable(TableName name, byte[][] rows, byte[][] families, 081 byte[][] qualifiers, byte[] cellValue) throws IOException { 082 TEST_UTIL.createTable(name, families).put(createPuts(rows, families, qualifiers, cellValue)); 083 } 084 085 private static List<Put> createPuts(byte[][] rows, byte[][] families, byte[][] qualifiers, 086 byte[] value) throws IOException { 087 List<Put> puts = new ArrayList<>(); 088 for (int row = 0; row < rows.length; row++) { 089 Put put = new Put(rows[row]); 090 for (int fam = 0; fam < families.length; fam++) { 091 for (int qual = 0; qual < qualifiers.length; qual++) { 092 KeyValue kv = new KeyValue(rows[row], families[fam], qualifiers[qual], qual, value); 093 put.add(kv); 094 } 095 } 096 puts.add(put); 097 } 098 return puts; 099 } 100 101 @AfterClass 102 public static void tearDownAfterClass() throws Exception { 103 TEST_UTIL.shutdownMiniCluster(); 104 } 105 106 public static final class SparseFilter extends FilterBase { 107 108 private final boolean reversed; 109 110 public SparseFilter(boolean reversed) { 111 this.reversed = reversed; 112 } 113 114 @Override 115 public ReturnCode filterCell(final Cell c) throws IOException { 116 Threads.sleep(TIMEOUT / 2 + 100); 117 return Bytes.equals(CellUtil.cloneRow(c), ROWS[reversed ? 0 : NUM_ROWS - 1]) 118 ? ReturnCode.INCLUDE 119 : ReturnCode.SKIP; 120 } 121 122 @Override 123 public byte[] toByteArray() throws IOException { 124 return reversed ? new byte[] { 1 } : new byte[] { 0 }; 125 } 126 127 public static Filter parseFrom(final byte[] pbBytes) { 128 return new SparseFilter(pbBytes[0] != 0); 129 } 130 } 131 132 protected Scan createScanWithSparseFilter() { 133 return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE) 134 .setNeedCursorResult(true).setAllowPartialResults(true).setFilter(new SparseFilter(false)); 135 } 136 137 protected Scan createReversedScanWithSparseFilter() { 138 return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE) 139 .setReversed(true).setNeedCursorResult(true).setAllowPartialResults(true) 140 .setFilter(new SparseFilter(true)); 141 } 142 143 protected Scan createScanWithSizeLimit() { 144 return new Scan().setMaxResultSize(1).setCaching(Integer.MAX_VALUE).setNeedCursorResult(true); 145 } 146}