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.regionserver;
019
020import java.util.ArrayList;
021import java.util.List;
022import java.util.Random;
023import java.util.concurrent.ThreadLocalRandom;
024import org.apache.hadoop.hbase.CompareOperator;
025import org.apache.hadoop.hbase.DoNotRetryIOException;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HColumnDescriptor;
029import org.apache.hadoop.hbase.HTableDescriptor;
030import org.apache.hadoop.hbase.StartMiniClusterOption;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.client.Put;
036import org.apache.hadoop.hbase.client.Result;
037import org.apache.hadoop.hbase.client.ResultScanner;
038import org.apache.hadoop.hbase.client.Scan;
039import org.apache.hadoop.hbase.client.Table;
040import org.apache.hadoop.hbase.client.TableDescriptor;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.filter.CompareFilter;
043import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
044import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
045import org.apache.hadoop.hbase.testclassification.LargeTests;
046import org.apache.hadoop.hbase.testclassification.RegionServerTests;
047import org.apache.hadoop.hbase.util.Bytes;
048import org.junit.AfterClass;
049import org.junit.BeforeClass;
050import org.junit.ClassRule;
051import org.junit.Rule;
052import org.junit.Test;
053import org.junit.experimental.categories.Category;
054import org.junit.rules.TestName;
055import org.slf4j.Logger;
056import org.slf4j.LoggerFactory;
057
058import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
059import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLineParser;
060import org.apache.hbase.thirdparty.org.apache.commons.cli.GnuParser;
061import org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter;
062import org.apache.hbase.thirdparty.org.apache.commons.cli.Option;
063import org.apache.hbase.thirdparty.org.apache.commons.cli.Options;
064
065/**
066 * Test performance improvement of joined scanners optimization:
067 * https://issues.apache.org/jira/browse/HBASE-5416
068 */
069@Category({ RegionServerTests.class, LargeTests.class })
070public class TestJoinedScanners {
071
072  @ClassRule
073  public static final HBaseClassTestRule CLASS_RULE =
074    HBaseClassTestRule.forClass(TestJoinedScanners.class);
075
076  private static final Logger LOG = LoggerFactory.getLogger(TestJoinedScanners.class);
077
078  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
079
080  private static final byte[] cf_essential = Bytes.toBytes("essential");
081  private static final byte[] cf_joined = Bytes.toBytes("joined");
082  private static final byte[] col_name = Bytes.toBytes("a");
083  private static final byte[] flag_yes = Bytes.toBytes("Y");
084  private static final byte[] flag_no = Bytes.toBytes("N");
085
086  private static DataBlockEncoding blockEncoding = DataBlockEncoding.FAST_DIFF;
087  private static int selectionRatio = 30;
088  private static int valueWidth = 128 * 1024;
089
090  @Rule
091  public TestName name = new TestName();
092
093  @BeforeClass
094  public static void setUpBeforeClass() throws Exception {
095    final int DEFAULT_BLOCK_SIZE = 1024 * 1024;
096    TEST_UTIL.getConfiguration().setLong("dfs.blocksize", DEFAULT_BLOCK_SIZE);
097    TEST_UTIL.getConfiguration().setInt("dfs.replication", 1);
098    TEST_UTIL.getConfiguration().setLong("hbase.hregion.max.filesize", 322122547200L);
099
100    String[] dataNodeHosts = new String[] { "host1", "host2", "host3" };
101    int regionServersCount = 3;
102    StartMiniClusterOption option = StartMiniClusterOption.builder()
103      .numRegionServers(regionServersCount).dataNodeHosts(dataNodeHosts).build();
104    TEST_UTIL.startMiniCluster(option);
105  }
106
107  @AfterClass
108  public static void tearDownAfterClass() throws Exception {
109    TEST_UTIL.shutdownMiniCluster();
110  }
111
112  @Test
113  public void testJoinedScanners() throws Exception {
114    byte[][] families = { cf_essential, cf_joined };
115
116    final TableName tableName = TableName.valueOf(name.getMethodName());
117    HTableDescriptor desc = new HTableDescriptor(tableName);
118    for (byte[] family : families) {
119      HColumnDescriptor hcd = new HColumnDescriptor(family);
120      hcd.setDataBlockEncoding(blockEncoding);
121      desc.addFamily(hcd);
122    }
123    TEST_UTIL.getAdmin().createTable(desc);
124    Table ht = TEST_UTIL.getConnection().getTable(tableName);
125
126    long rows_to_insert = 1000;
127    int insert_batch = 20;
128
129    LOG.info("Make " + Long.toString(rows_to_insert) + " rows, total size = "
130      + Float.toString(rows_to_insert * valueWidth / 1024 / 1024) + " MB");
131
132    long time = System.nanoTime();
133    Random rand = ThreadLocalRandom.current();
134    byte[] val_large = new byte[valueWidth];
135    List<Put> puts = new ArrayList<>();
136    for (long i = 0; i < rows_to_insert; i++) {
137      Put put = new Put(Bytes.toBytes(Long.toString(i)));
138      if (rand.nextInt(100) <= selectionRatio) {
139        put.addColumn(cf_essential, col_name, flag_yes);
140      } else {
141        put.addColumn(cf_essential, col_name, flag_no);
142      }
143      put.addColumn(cf_joined, col_name, val_large);
144      puts.add(put);
145      if (puts.size() >= insert_batch) {
146        ht.put(puts);
147        puts.clear();
148      }
149    }
150    if (!puts.isEmpty()) {
151      ht.put(puts);
152      puts.clear();
153    }
154
155    LOG.info("Data generated in " + Double.toString((System.nanoTime() - time) / 1000000000.0)
156      + " seconds");
157
158    boolean slow = true;
159    for (int i = 0; i < 10; ++i) {
160      runScanner(ht, slow);
161      slow = !slow;
162    }
163
164    ht.close();
165  }
166
167  private void runScanner(Table table, boolean slow) throws Exception {
168    long time = System.nanoTime();
169    Scan scan = new Scan();
170    scan.addColumn(cf_essential, col_name);
171    scan.addColumn(cf_joined, col_name);
172
173    SingleColumnValueFilter filter =
174      new SingleColumnValueFilter(cf_essential, col_name, CompareFilter.CompareOp.EQUAL, flag_yes);
175    filter.setFilterIfMissing(true);
176    scan.setFilter(filter);
177    scan.setLoadColumnFamiliesOnDemand(!slow);
178
179    ResultScanner result_scanner = table.getScanner(scan);
180    Result res;
181    long rows_count = 0;
182    while ((res = result_scanner.next()) != null) {
183      rows_count++;
184    }
185
186    double timeSec = (System.nanoTime() - time) / 1000000000.0;
187    result_scanner.close();
188    LOG.info((slow ? "Slow" : "Joined") + " scanner finished in " + Double.toString(timeSec)
189      + " seconds, got " + Long.toString(rows_count / 2) + " rows");
190  }
191
192  private static Options options = new Options();
193
194  /**
195   * Command line interface:
196   * @throws IOException if there is a bug while reading from disk
197   */
198  public static void main(final String[] args) throws Exception {
199    Option encodingOption =
200      new Option("e", "blockEncoding", true, "Data block encoding; Default: FAST_DIFF");
201    encodingOption.setRequired(false);
202    options.addOption(encodingOption);
203
204    Option ratioOption = new Option("r", "selectionRatio", true,
205      "Ratio of selected rows using essential column family");
206    ratioOption.setRequired(false);
207    options.addOption(ratioOption);
208
209    Option widthOption =
210      new Option("w", "valueWidth", true, "Width of value for non-essential column family");
211    widthOption.setRequired(false);
212    options.addOption(widthOption);
213
214    CommandLineParser parser = new GnuParser();
215    CommandLine cmd = parser.parse(options, args);
216    if (args.length < 1) {
217      HelpFormatter formatter = new HelpFormatter();
218      formatter.printHelp("TestJoinedScanners", options, true);
219    }
220
221    if (cmd.hasOption("e")) {
222      blockEncoding = DataBlockEncoding.valueOf(cmd.getOptionValue("e"));
223    }
224    if (cmd.hasOption("r")) {
225      selectionRatio = Integer.parseInt(cmd.getOptionValue("r"));
226    }
227    if (cmd.hasOption("w")) {
228      valueWidth = Integer.parseInt(cmd.getOptionValue("w"));
229    }
230    // run the test
231    TestJoinedScanners test = new TestJoinedScanners();
232    test.testJoinedScanners();
233  }
234
235  @Test(expected = DoNotRetryIOException.class)
236  public void testWithReverseScan() throws Exception {
237    try (Connection con = TEST_UTIL.getConnection(); Admin admin = con.getAdmin()) {
238      TableName tableName = TableName.valueOf(name.getMethodName());
239
240      TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
241        .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf1"))
242        .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf2")).build();
243      admin.createTable(tableDescriptor);
244
245      try (Table table = con.getTable(tableName)) {
246        SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("cf1"),
247          Bytes.toBytes("col"), CompareOperator.EQUAL, Bytes.toBytes("val"));
248        filter.setFilterIfMissing(true);
249
250        // Reverse scan with loading CFs on demand
251        Scan scan = new Scan();
252        scan.setFilter(filter);
253        scan.setReversed(true);
254        scan.setLoadColumnFamiliesOnDemand(true);
255
256        try (ResultScanner scanner = table.getScanner(scan)) {
257          // DoNotRetryIOException should occur
258          scanner.next();
259        }
260      }
261    }
262  }
263}