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;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.HTestConst;
029import org.apache.hadoop.hbase.KeyValue;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.filter.Filter;
032import org.apache.hadoop.hbase.filter.FilterBase;
033import org.apache.hadoop.hbase.regionserver.StoreScanner;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.Threads;
036
037public abstract class AbstractTestScanCursor {
038
039  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
040
041  /**
042   * Table configuration
043   */
044  protected static TableName TABLE_NAME = TableName.valueOf("TestScanCursor");
045
046  protected static int NUM_ROWS = 5;
047  protected static byte[] ROW = Bytes.toBytes("testRow");
048  protected static byte[][] ROWS = HTestConst.makeNAscii(ROW, NUM_ROWS);
049
050  protected static int NUM_FAMILIES = 2;
051  protected static byte[] FAMILY = Bytes.toBytes("testFamily");
052  protected static byte[][] FAMILIES = HTestConst.makeNAscii(FAMILY, NUM_FAMILIES);
053
054  protected static int NUM_QUALIFIERS = 2;
055  protected static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
056  protected static byte[][] QUALIFIERS = HTestConst.makeNAscii(QUALIFIER, NUM_QUALIFIERS);
057
058  protected static int VALUE_SIZE = 10;
059  protected static byte[] VALUE = Bytes.createMaxByteArray(VALUE_SIZE);
060
061  protected static final int TIMEOUT = 4000;
062
063  protected static void startCluster() throws Exception {
064    Configuration conf = TEST_UTIL.getConfiguration();
065
066    conf.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, TIMEOUT);
067    conf.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, TIMEOUT);
068
069    // Check the timeout condition after every cell
070    conf.setLong(StoreScanner.HBASE_CELLS_SCANNED_PER_HEARTBEAT_CHECK, 1);
071    TEST_UTIL.startMiniCluster(1);
072
073    createTestTable(TABLE_NAME, ROWS, FAMILIES, QUALIFIERS, VALUE);
074  }
075
076  private static void createTestTable(TableName name, byte[][] rows, byte[][] families,
077    byte[][] qualifiers, byte[] cellValue) throws IOException {
078    TEST_UTIL.createTable(name, families).put(createPuts(rows, families, qualifiers, cellValue));
079  }
080
081  private static List<Put> createPuts(byte[][] rows, byte[][] families, byte[][] qualifiers,
082    byte[] value) throws IOException {
083    List<Put> puts = new ArrayList<>();
084    for (int row = 0; row < rows.length; row++) {
085      Put put = new Put(rows[row]);
086      for (int fam = 0; fam < families.length; fam++) {
087        for (int qual = 0; qual < qualifiers.length; qual++) {
088          KeyValue kv = new KeyValue(rows[row], families[fam], qualifiers[qual], qual, value);
089          put.add(kv);
090        }
091      }
092      puts.add(put);
093    }
094    return puts;
095  }
096
097  protected static void stopCluster() throws Exception {
098    TEST_UTIL.shutdownMiniCluster();
099  }
100
101  public static final class SparseFilter extends FilterBase {
102
103    private final boolean reversed;
104
105    public SparseFilter(boolean reversed) {
106      this.reversed = reversed;
107    }
108
109    @Override
110    public ReturnCode filterCell(final Cell c) throws IOException {
111      Threads.sleep(TIMEOUT / 2 + 100);
112      return Bytes.equals(CellUtil.cloneRow(c), ROWS[reversed ? 0 : NUM_ROWS - 1])
113        ? ReturnCode.INCLUDE
114        : ReturnCode.SKIP;
115    }
116
117    @Override
118    public byte[] toByteArray() throws IOException {
119      return reversed ? new byte[] { 1 } : new byte[] { 0 };
120    }
121
122    public static Filter parseFrom(final byte[] pbBytes) {
123      return new SparseFilter(pbBytes[0] != 0);
124    }
125  }
126
127  protected Scan createScanWithSparseFilter() {
128    return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE)
129      .setNeedCursorResult(true).setAllowPartialResults(true).setFilter(new SparseFilter(false));
130  }
131
132  protected Scan createReversedScanWithSparseFilter() {
133    return new Scan().setMaxResultSize(Long.MAX_VALUE).setCaching(Integer.MAX_VALUE)
134      .setReversed(true).setNeedCursorResult(true).setAllowPartialResults(true)
135      .setFilter(new SparseFilter(true));
136  }
137
138  protected Scan createScanWithSizeLimit() {
139    return new Scan().setMaxResultSize(1).setCaching(Integer.MAX_VALUE).setNeedCursorResult(true);
140  }
141}