Skip to content

Commit

Permalink
HBASE-26242 Region never split when store file count larger than the …
Browse files Browse the repository at this point in the history
…configed blocking file count
  • Loading branch information
sunhelly committed Sep 6, 2021
1 parent a0864ed commit 1a866f4
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 9 deletions.
Expand Up @@ -188,10 +188,13 @@ public String dumpQueue() {
}

public synchronized boolean requestSplit(final Region r) {
// don't split regions that are blocking
// We should split regions ignore blocking when it should split, if not a region will
// never split if its speed of reducing the number of files through compaction is slower
// than the speed of flush. Another reason is that, 1 parent compaction + 2 child compaction
// can be reduced to 2 child compaction, which is faster and uses less IO.
HRegion hr = (HRegion)r;
try {
if (shouldSplitRegion() && hr.getCompactPriority() >= PRIORITY_USER) {
if (allowSplitRegion() && hr.getCompactPriority() >= PRIORITY_USER) {
byte[] midKey = hr.checkSplit().orElse(null);
if (midKey != null) {
requestSplit(r, midKey);
Expand All @@ -206,14 +209,14 @@ public synchronized boolean requestSplit(final Region r) {
return false;
}

public synchronized void requestSplit(final Region r, byte[] midKey) {
private synchronized void requestSplit(final Region r, byte[] midKey) {
requestSplit(r, midKey, null);
}

/*
* The User parameter allows the split thread to assume the correct user identity
*/
public synchronized void requestSplit(final Region r, byte[] midKey, User user) {
private synchronized void requestSplit(final Region r, byte[] midKey, User user) {
if (midKey == null) {
LOG.debug("Region " + r.getRegionInfo().getRegionNameAsString() +
" not splittable because midkey=null");
Expand Down Expand Up @@ -456,10 +459,10 @@ public int getSplitQueueSize() {
return splits.getQueue().size();
}

private boolean shouldSplitRegion() {
if(server.getNumberOfOnlineRegions() > 0.9*regionSplitLimit) {
private boolean allowSplitRegion() {
if (server.getNumberOfOnlineRegions() > 0.9 * regionSplitLimit) {
LOG.warn("Total number of regions is approaching the upper limit " + regionSplitLimit + ". "
+ "Please consider taking a look at http://hbase.apache.org/book.html#ops.regionmgt");
+ "Please consider taking a look at http://hbase.apache.org/book.html#ops.regionmgt");
}
return (regionSplitLimit > server.getNumberOfOnlineRegions());
}
Expand Down Expand Up @@ -620,11 +623,14 @@ private void doCompaction(User user) {
this + "; duration=" + StringUtils.formatTimeDiff(now, start));
if (completed) {
// degenerate case: blocked regions require recursive enqueues
if (store.getCompactPriority() <= 0) {
if (region.getCompactPriority() < Store.PRIORITY_USER
&& store.getCompactPriority() <= 0) {
requestSystemCompaction(region, store, "Recursive enqueue");
} else {
// see if the compaction has caused us to exceed max region size
requestSplit(region);
if (!requestSplit(region) && store.getCompactPriority() <= 0) {
requestSystemCompaction(region, store, "Recursive enqueue");
}
}
}
} catch (IOException ex) {
Expand Down
Expand Up @@ -7938,6 +7938,10 @@ public Optional<byte[]> checkSplit(boolean force) {
* @return The priority that this region should have in the compaction queue
*/
public int getCompactPriority() {
if (checkSplit().isPresent()) {
// if a region should split, split it before compact
return Store.PRIORITY_USER;
}
return stores.values().stream().mapToInt(HStore::getCompactPriority).min()
.orElse(Store.NO_PRIORITY);
}
Expand Down
@@ -0,0 +1,147 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.regionserver;

import static org.apache.hadoop.hbase.regionserver.Store.PRIORITY_USER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtil;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.master.assignment.SplitTableRegionProcedure;
import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;

@Category({ MediumTests.class})
public class TestSplitWithBlockingFiles {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestSplitWithBlockingFiles.class);

private static final Logger LOG = LoggerFactory.getLogger(TestSplitWithBlockingFiles.class);

protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
private static TableName TABLE_NAME = TableName.valueOf("test");
private static Admin ADMIN;
private static byte[] CF = Bytes.toBytes("cf");
private static Table TABLE;


@BeforeClass
public static void setupCluster() throws Exception {
UTIL.getConfiguration().setLong(HConstants.HREGION_MAX_FILESIZE, 8 * 2 * 10240L);
UTIL.getConfiguration().setInt(HStore.BLOCKING_STOREFILES_KEY, 1);
UTIL.getConfiguration().set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
ConstantSizeRegionSplitPolicy.class.getName());
UTIL.startMiniCluster(1);
ADMIN = UTIL.getAdmin();
TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
.setColumnFamily(
ColumnFamilyDescriptorBuilder.newBuilder(CF).setBlocksize(1000).build()).build();
TABLE = UTIL.createTable(td, null);
UTIL.waitTableAvailable(TABLE_NAME);
}

@AfterClass
public static void cleanupTest() throws Exception {
Closeables.close(TABLE, true);
UTIL.shutdownMiniCluster();
}

@Test
public void test() throws Exception {
ADMIN.splitSwitch(false, true);
byte[] value = new byte[1024];
for (int m = 0; m < 10; m++) {
String rowPrefix = "row" + m;
for (int i = 0; i < 10; i++) {
Put p = new Put(Bytes.toBytes(rowPrefix + i));
p.addColumn(CF, Bytes.toBytes("qualifier"), value);
p.addColumn(CF, Bytes.toBytes("qualifier2"), value);
TABLE.put(p);
}
ADMIN.flush(TABLE_NAME);
}
Scan scan = new Scan();
ResultScanner results = TABLE.getScanner(scan);
int count = 0;
while (results.next() != null) {
count++;
}
Assert.assertEquals("There should be 100 rows!", 100, count);
List<HRegion> regions = UTIL.getMiniHBaseCluster().getRegionServer(0).getRegions();
regions.removeIf(r -> !r.getRegionInfo().getTable().equals(TABLE_NAME));
assertEquals(1, regions.size());
assertNotNull(regions.get(0).getSplitPolicy().getSplitPoint());
assertTrue(regions.get(0).getCompactPriority() >= PRIORITY_USER);
assertTrue(UTIL.getMiniHBaseCluster().getRegionServer(0).getCompactSplitThread()
.requestSplit(regions.get(0)));

// split region
ADMIN.splitSwitch(true, true);
MasterProcedureEnv env =
UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment();
final ProcedureExecutor<MasterProcedureEnv> executor =
UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
SplitTableRegionProcedure splitProcedure =
new SplitTableRegionProcedure(env, regions.get(0).getRegionInfo(), Bytes.toBytes("row5"));
executor.submitProcedure(splitProcedure);
ProcedureTestingUtility.waitProcedure(executor, splitProcedure.getProcId());

regions = UTIL.getMiniHBaseCluster().getRegionServer(0).getRegions();
regions.removeIf(r -> !r.getRegionInfo().getTable().equals(TABLE_NAME));
assertEquals(2, regions.size());
scan = new Scan();
results = TABLE.getScanner(scan);
count = 0;
while (results.next() != null) {
count++;
}
Assert.assertEquals("There should be 100 rows!", 100, count);
for (HRegion region : regions) {
assertTrue(region.getCompactPriority() < PRIORITY_USER);
assertFalse(
UTIL.getMiniHBaseCluster().getRegionServer(0).getCompactSplitThread().requestSplit(region));
}
}
}

0 comments on commit 1a866f4

Please sign in to comment.