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 static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.Iterator;
027import java.util.List;
028import java.util.concurrent.ThreadLocalRandom;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.CellUtil;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
035import org.apache.hadoop.hbase.client.Durability;
036import org.apache.hadoop.hbase.client.Put;
037import org.apache.hadoop.hbase.client.RegionInfo;
038import org.apache.hadoop.hbase.client.RegionInfoBuilder;
039import org.apache.hadoop.hbase.client.Scan;
040import org.apache.hadoop.hbase.client.TableDescriptor;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.testclassification.RegionServerTests;
043import org.apache.hadoop.hbase.testclassification.SmallTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
046import org.junit.jupiter.api.AfterAll;
047import org.junit.jupiter.api.BeforeAll;
048import org.junit.jupiter.api.Tag;
049import org.junit.jupiter.api.Test;
050import org.slf4j.Logger;
051import org.slf4j.LoggerFactory;
052
053@Tag(RegionServerTests.TAG)
054@Tag(SmallTests.TAG)
055public class TestWideScanner {
056  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestWideScanner.class);
059
060  private static final byte[] A = Bytes.toBytes("A");
061  private static final byte[] B = Bytes.toBytes("B");
062  private static final byte[] C = Bytes.toBytes("C");
063  private static byte[][] COLUMNS = { A, B, C };
064
065  private static final TableDescriptor TESTTABLEDESC;
066  static {
067    TableDescriptorBuilder builder =
068      TableDescriptorBuilder.newBuilder(TableName.valueOf("testwidescan"));
069    for (byte[] cfName : new byte[][] { A, B, C }) {
070      // Keep versions to help debugging.
071      builder.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(cfName).setMaxVersions(100)
072        .setBlocksize(8 * 1024).build());
073    }
074    TESTTABLEDESC = builder.build();
075  }
076
077  /** HRegionInfo for root region */
078  private static HRegion REGION;
079
080  @BeforeAll
081  public static void setUp() throws IOException {
082    Path testDir = UTIL.getDataTestDir();
083    RegionInfo hri = RegionInfoBuilder.newBuilder(TESTTABLEDESC.getTableName()).build();
084    REGION =
085      HBaseTestingUtil.createRegionAndWAL(hri, testDir, UTIL.getConfiguration(), TESTTABLEDESC);
086  }
087
088  @AfterAll
089  public static void tearDown() throws IOException {
090    if (REGION != null) {
091      HBaseTestingUtil.closeRegionAndWAL(REGION);
092      REGION = null;
093    }
094    UTIL.cleanupTestDir();
095  }
096
097  private int addWideContent(HRegion region) throws IOException {
098    int count = 0;
099    for (char c = 'a'; c <= 'c'; c++) {
100      byte[] row = Bytes.toBytes("ab" + c);
101      int i, j;
102      long ts = EnvironmentEdgeManager.currentTime();
103      for (i = 0; i < 100; i++) {
104        byte[] b = Bytes.toBytes(String.format("%10d", i));
105        for (j = 0; j < 100; j++) {
106          Put put = new Put(row);
107          put.setDurability(Durability.SKIP_WAL);
108          long ts1 = ++ts;
109          put.addColumn(COLUMNS[ThreadLocalRandom.current().nextInt(COLUMNS.length)], b, ts1, b);
110          region.put(put);
111          count++;
112        }
113      }
114    }
115    return count;
116  }
117
118  @Test
119  public void testWideScanBatching() throws IOException {
120    final int batch = 256;
121    int inserted = addWideContent(REGION);
122    List<Cell> results = new ArrayList<>();
123    Scan scan = new Scan();
124    scan.addFamily(A);
125    scan.addFamily(B);
126    scan.addFamily(C);
127    scan.readVersions(100);
128    scan.setBatch(batch);
129    try (InternalScanner s = REGION.getScanner(scan)) {
130      int total = 0;
131      int i = 0;
132      boolean more;
133      do {
134        more = s.next(results);
135        i++;
136        LOG.info("iteration #" + i + ", results.size=" + results.size());
137
138        // assert that the result set is no larger
139        assertTrue(results.size() <= batch);
140
141        total += results.size();
142
143        if (results.size() > 0) {
144          // assert that all results are from the same row
145          byte[] row = CellUtil.cloneRow(results.get(0));
146          for (Cell kv : results) {
147            assertTrue(Bytes.equals(row, CellUtil.cloneRow(kv)));
148          }
149        }
150
151        results.clear();
152
153        // trigger ChangedReadersObservers
154        Iterator<KeyValueScanner> scanners = ((RegionScannerImpl) s).storeHeap.getHeap().iterator();
155        while (scanners.hasNext()) {
156          StoreScanner ss = (StoreScanner) scanners.next();
157          ss.updateReaders(Collections.emptyList(), Collections.emptyList());
158        }
159      } while (more);
160
161      // assert that the scanner returned all values
162      LOG.info("inserted " + inserted + ", scanned " + total);
163      assertEquals(total, inserted);
164    }
165  }
166}