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.apache.hadoop.hbase.regionserver.HRegion.SPLIT_IGNORE_BLOCKING_ENABLED_KEY;
021import static org.apache.hadoop.hbase.regionserver.Store.PRIORITY_USER;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertNotNull;
025import static org.junit.Assert.assertTrue;
026
027import java.util.List;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
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.Put;
035import org.apache.hadoop.hbase.client.ResultScanner;
036import org.apache.hadoop.hbase.client.Scan;
037import org.apache.hadoop.hbase.client.Table;
038import org.apache.hadoop.hbase.client.TableDescriptor;
039import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
040import org.apache.hadoop.hbase.master.assignment.SplitTableRegionProcedure;
041import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
042import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
043import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
044import org.apache.hadoop.hbase.testclassification.MediumTests;
045import org.apache.hadoop.hbase.util.Bytes;
046import org.junit.AfterClass;
047import org.junit.Assert;
048import org.junit.BeforeClass;
049import org.junit.ClassRule;
050import org.junit.Test;
051import org.junit.experimental.categories.Category;
052import org.slf4j.Logger;
053import org.slf4j.LoggerFactory;
054
055import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
056
057@Category({ MediumTests.class })
058public class TestSplitWithBlockingFiles {
059
060  @ClassRule
061  public static final HBaseClassTestRule CLASS_RULE =
062    HBaseClassTestRule.forClass(TestSplitWithBlockingFiles.class);
063
064  private static final Logger LOG = LoggerFactory.getLogger(TestSplitWithBlockingFiles.class);
065
066  protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
067  private static TableName TABLE_NAME = TableName.valueOf("test");
068  private static Admin ADMIN;
069  private static byte[] CF = Bytes.toBytes("cf");
070  private static Table TABLE;
071
072  @BeforeClass
073  public static void setupCluster() throws Exception {
074    UTIL.getConfiguration().setLong(HConstants.HREGION_MAX_FILESIZE, 8 * 2 * 10240L);
075    UTIL.getConfiguration().setInt(HStore.BLOCKING_STOREFILES_KEY, 1);
076    UTIL.getConfiguration().set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
077      ConstantSizeRegionSplitPolicy.class.getName());
078    UTIL.getConfiguration().setBoolean(SPLIT_IGNORE_BLOCKING_ENABLED_KEY, true);
079    UTIL.startMiniCluster(1);
080    ADMIN = UTIL.getAdmin();
081    TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
082      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).setBlocksize(1000).build())
083      .build();
084    TABLE = UTIL.createTable(td, null);
085    UTIL.waitTableAvailable(TABLE_NAME);
086  }
087
088  @AfterClass
089  public static void cleanupTest() throws Exception {
090    Closeables.close(TABLE, true);
091    UTIL.shutdownMiniCluster();
092  }
093
094  @Test
095  public void testSplitIgnoreBlockingFiles() throws Exception {
096    ADMIN.splitSwitch(false, true);
097    byte[] value = new byte[1024];
098    for (int m = 0; m < 10; m++) {
099      String rowPrefix = "row" + m;
100      for (int i = 0; i < 10; i++) {
101        Put p = new Put(Bytes.toBytes(rowPrefix + i));
102        p.addColumn(CF, Bytes.toBytes("qualifier"), value);
103        p.addColumn(CF, Bytes.toBytes("qualifier2"), value);
104        TABLE.put(p);
105      }
106      ADMIN.flush(TABLE_NAME);
107    }
108    Scan scan = new Scan();
109    ResultScanner results = TABLE.getScanner(scan);
110    int count = 0;
111    while (results.next() != null) {
112      count++;
113    }
114    Assert.assertEquals("There should be 100 rows!", 100, count);
115    List<HRegion> regions = UTIL.getMiniHBaseCluster().getRegionServer(0).getRegions();
116    regions.removeIf(r -> !r.getRegionInfo().getTable().equals(TABLE_NAME));
117    assertEquals(1, regions.size());
118    assertNotNull(regions.get(0).getSplitPolicy().getSplitPoint());
119    assertTrue(regions.get(0).getCompactPriority() >= PRIORITY_USER);
120    assertTrue(UTIL.getMiniHBaseCluster().getRegionServer(0).getCompactSplitThread()
121      .requestSplit(regions.get(0)));
122
123    // split region
124    ADMIN.splitSwitch(true, true);
125    MasterProcedureEnv env =
126      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment();
127    final ProcedureExecutor<MasterProcedureEnv> executor =
128      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
129    SplitTableRegionProcedure splitProcedure =
130      new SplitTableRegionProcedure(env, regions.get(0).getRegionInfo(), Bytes.toBytes("row5"));
131    executor.submitProcedure(splitProcedure);
132    ProcedureTestingUtility.waitProcedure(executor, splitProcedure.getProcId());
133
134    regions = UTIL.getMiniHBaseCluster().getRegionServer(0).getRegions();
135    regions.removeIf(r -> !r.getRegionInfo().getTable().equals(TABLE_NAME));
136    assertEquals(2, regions.size());
137    scan = new Scan();
138    results = TABLE.getScanner(scan);
139    count = 0;
140    while (results.next() != null) {
141      count++;
142    }
143    Assert.assertEquals("There should be 100 rows!", 100, count);
144    for (HRegion region : regions) {
145      assertTrue(region.getCompactPriority() < PRIORITY_USER);
146      assertFalse(
147        UTIL.getMiniHBaseCluster().getRegionServer(0).getCompactSplitThread().requestSplit(region));
148    }
149  }
150}