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.mapreduce;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
026import org.apache.hadoop.hbase.testclassification.MapReduceTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.jupiter.api.AfterAll;
030import org.junit.jupiter.api.BeforeAll;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033import org.junit.jupiter.api.TestInfo;
034
035@Tag(MapReduceTests.TAG)
036@Tag(MediumTests.TAG)
037public class TestHRegionPartitioner {
038
039  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
040
041  @BeforeAll
042  public static void beforeClass() throws Exception {
043    UTIL.startMiniCluster();
044  }
045
046  @AfterAll
047  public static void afterClass() throws Exception {
048    UTIL.shutdownMiniCluster();
049  }
050
051  /**
052   * Test HRegionPartitioner
053   */
054  @Test
055  public void testHRegionPartitioner(TestInfo testInfo) throws Exception {
056
057    byte[][] families = { Bytes.toBytes("familyA"), Bytes.toBytes("familyB") };
058
059    String tableName = testInfo.getTestMethod().get().getName();
060    UTIL.createTable(TableName.valueOf(tableName), families, 1, Bytes.toBytes("aa"),
061      Bytes.toBytes("cc"), 3);
062
063    HRegionPartitioner<Long, Long> partitioner = new HRegionPartitioner<>();
064    Configuration configuration = UTIL.getConfiguration();
065    configuration.set(TableOutputFormat.OUTPUT_TABLE, tableName);
066    partitioner.setConf(configuration);
067    ImmutableBytesWritable writable = new ImmutableBytesWritable(Bytes.toBytes("bb"));
068
069    assertEquals(1, partitioner.getPartition(writable, 10L, 3));
070    assertEquals(0, partitioner.getPartition(writable, 10L, 1));
071  }
072
073  @Test
074  public void testHRegionPartitionerMoreRegions(TestInfo testInfo) throws Exception {
075    byte[][] families = { Bytes.toBytes("familyA"), Bytes.toBytes("familyB") };
076
077    String tableNameStr = testInfo.getTestMethod().get().getName();
078    TableName tableName = TableName.valueOf(tableNameStr);
079    UTIL.createTable(tableName, families, 1, Bytes.toBytes("aa"), Bytes.toBytes("cc"), 5);
080
081    Configuration configuration = UTIL.getConfiguration();
082    int numberOfRegions = UTIL.getMiniHBaseCluster().getRegions(tableName).size();
083    assertEquals(5, numberOfRegions);
084
085    HRegionPartitioner<Long, Long> partitioner = new HRegionPartitioner<>();
086    configuration.set(TableOutputFormat.OUTPUT_TABLE, tableNameStr);
087    partitioner.setConf(configuration);
088
089    // Get some rowKey for the lastRegion
090    ImmutableBytesWritable writable = new ImmutableBytesWritable(Bytes.toBytes("df"));
091
092    // getPartition should return 4 since number of partition = number of reduces.
093    assertEquals(4, partitioner.getPartition(writable, 10L, 5));
094  }
095}