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 static org.apache.hadoop.hbase.HBaseTestingUtil.countRows;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import java.util.List;
026import org.apache.hadoop.hbase.CompareOperator;
027import org.apache.hadoop.hbase.HRegionLocation;
028import org.junit.jupiter.api.TestTemplate;
029
030public class FromClientSideTestFilterAcrossMultipleRegions extends FromClientSideTestBase {
031
032  protected FromClientSideTestFilterAcrossMultipleRegions(
033    Class<? extends ConnectionRegistry> registryImpl, int numHedgedReqs) {
034    super(registryImpl, numHedgedReqs);
035  }
036
037  /**
038   * Test filters when multiple regions. It does counts. Needs eye-balling of logs to ensure that
039   * we're not scanning more regions that we're supposed to. Related to the TestFilterAcrossRegions
040   * over in the o.a.h.h.filter package.
041   */
042  @TestTemplate
043  public void testFilterAcrossMultipleRegions() throws IOException {
044    TEST_UTIL.createTable(tableName, FAMILY);
045    try (Connection conn = getConnection(); Table t = conn.getTable(tableName)) {
046      int rowCount = TEST_UTIL.loadTable(t, FAMILY, false);
047      assertRowCount(t, rowCount);
048      // Split the table. Should split on a reasonable key; 'lqj'
049      List<HRegionLocation> regions = splitTable(t);
050      assertRowCount(t, rowCount);
051      // Get end key of first region.
052      byte[] endKey = regions.get(0).getRegion().getEndKey();
053      // Count rows with a filter that stops us before passed 'endKey'.
054      // Should be count of rows in first region.
055      int endKeyCount = countRows(t, createScanWithRowFilter(endKey));
056      assertTrue(endKeyCount < rowCount);
057
058      // How do I know I did not got to second region? Thats tough. Can't really
059      // do that in client-side region test. I verified by tracing in debugger.
060      // I changed the messages that come out when set to DEBUG so should see
061      // when scanner is done. Says "Finished with scanning..." with region name.
062      // Check that its finished in right region.
063
064      // New test. Make it so scan goes into next region by one and then two.
065      // Make sure count comes out right.
066      byte[] key = new byte[] { endKey[0], endKey[1], (byte) (endKey[2] + 1) };
067      int plusOneCount = countRows(t, createScanWithRowFilter(key));
068      assertEquals(endKeyCount + 1, plusOneCount);
069      key = new byte[] { endKey[0], endKey[1], (byte) (endKey[2] + 2) };
070      int plusTwoCount = countRows(t, createScanWithRowFilter(key));
071      assertEquals(endKeyCount + 2, plusTwoCount);
072
073      // New test. Make it so I scan one less than endkey.
074      key = new byte[] { endKey[0], endKey[1], (byte) (endKey[2] - 1) };
075      int minusOneCount = countRows(t, createScanWithRowFilter(key));
076      assertEquals(endKeyCount - 1, minusOneCount);
077      // For above test... study logs. Make sure we do "Finished with scanning.."
078      // in first region and that we do not fall into the next region.
079
080      key = new byte[] { 'a', 'a', 'a' };
081      int countBBB = countRows(t, createScanWithRowFilter(key, null, CompareOperator.EQUAL));
082      assertEquals(1, countBBB);
083
084      int countGreater =
085        countRows(t, createScanWithRowFilter(endKey, null, CompareOperator.GREATER_OR_EQUAL));
086      // Because started at start of table.
087      assertEquals(0, countGreater);
088      countGreater =
089        countRows(t, createScanWithRowFilter(endKey, endKey, CompareOperator.GREATER_OR_EQUAL));
090      assertEquals(rowCount - endKeyCount, countGreater);
091    }
092  }
093}