Skip to content

Commit 48e9835

Browse files
authored
HBASE-24428 : Update compaction priority for recently split daughter regions (#1784)
Signed-off-by: Andrew Purtell <apurtell@apache.org>
1 parent f520c9d commit 48e9835

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public class HStore implements Store, HeapSize, StoreConfigInformation,
143143
public static final int DEFAULT_COMPACTCHECKER_INTERVAL_MULTIPLIER = 1000;
144144
public static final int DEFAULT_BLOCKING_STOREFILE_COUNT = 16;
145145

146+
// HBASE-24428 : Update compaction priority for recently split daughter regions
147+
// so as to prioritize their compaction.
148+
// Any compaction candidate with higher priority than compaction of newly split daugher regions
149+
// should have priority value < (Integer.MIN_VALUE + 1000)
150+
private static final int SPLIT_REGION_COMPACTION_PRIORITY = Integer.MIN_VALUE + 1000;
151+
146152
private static final Logger LOG = LoggerFactory.getLogger(HStore.class);
147153

148154
protected final MemStore memstore;
@@ -1923,7 +1929,22 @@ public Optional<CompactionContext> requestCompaction(int priority,
19231929

19241930
// Set common request properties.
19251931
// Set priority, either override value supplied by caller or from store.
1926-
request.setPriority((priority != Store.NO_PRIORITY) ? priority : getCompactPriority());
1932+
final int compactionPriority =
1933+
(priority != Store.NO_PRIORITY) ? priority : getCompactPriority();
1934+
request.setPriority(compactionPriority);
1935+
1936+
if (request.isAfterSplit()) {
1937+
// If the store belongs to recently splitted daughter regions, better we consider
1938+
// them with the higher priority in the compaction queue.
1939+
// Override priority if it is lower (higher int value) than
1940+
// SPLIT_REGION_COMPACTION_PRIORITY
1941+
final int splitHousekeepingPriority =
1942+
Math.min(compactionPriority, SPLIT_REGION_COMPACTION_PRIORITY);
1943+
request.setPriority(splitHousekeepingPriority);
1944+
LOG.info("Keeping/Overriding Compaction request priority to {} for CF {} since it"
1945+
+ " belongs to recently split daughter region {}", splitHousekeepingPriority,
1946+
this.getColumnFamilyName(), getRegionInfo().getRegionNameAsString());
1947+
}
19271948
request.setDescription(getRegionInfo().getRegionNameAsString(), getColumnFamilyName());
19281949
request.setTracker(tracker);
19291950
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static OptionalInt getDeterministicRandomSeed(Collection<HStoreFile> file
5252
*/
5353
public static boolean hasReferences(Collection<HStoreFile> files) {
5454
// TODO: make sure that we won't pass null here in the future.
55-
return files != null ? files.stream().anyMatch(HStoreFile::isReference) : false;
55+
return files != null && files.stream().anyMatch(HStoreFile::isReference);
5656
}
5757

5858
/**

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactionRequestImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private enum DisplayCompactionType { MINOR, ALL_FILES, MAJOR }
4343
private DisplayCompactionType isMajor = DisplayCompactionType.MINOR;
4444
private int priority = NO_PRIORITY;
4545
private Collection<HStoreFile> filesToCompact;
46+
private boolean isAfterSplit = false;
4647

4748
// CompactRequest object creation time.
4849
private long selectionTime;
@@ -136,6 +137,14 @@ public CompactionLifeCycleTracker getTracker() {
136137
return tracker;
137138
}
138139

140+
public boolean isAfterSplit() {
141+
return isAfterSplit;
142+
}
143+
144+
public void setAfterSplit(boolean afterSplit) {
145+
isAfterSplit = afterSplit;
146+
}
147+
139148
@Override
140149
public int hashCode() {
141150
final int prime = 31;
@@ -149,6 +158,7 @@ public int hashCode() {
149158
result = prime * result + ((storeName == null) ? 0 : storeName.hashCode());
150159
result = prime * result + (int) (totalSize ^ (totalSize >>> 32));
151160
result = prime * result + ((tracker == null) ? 0 : tracker.hashCode());
161+
result = prime * result + (isAfterSplit ? 1231 : 1237);
152162
return result;
153163
}
154164

@@ -200,6 +210,9 @@ public boolean equals(Object obj) {
200210
if (totalSize != other.totalSize) {
201211
return false;
202212
}
213+
if (isAfterSplit != other.isAfterSplit) {
214+
return false;
215+
}
203216
if (tracker == null) {
204217
if (other.tracker != null) {
205218
return false;

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/SortedCompactionPolicy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public CompactionRequestImpl selectCompaction(Collection<HStoreFile> candidateFi
8484

8585
CompactionRequestImpl result = createCompactionRequest(candidateSelection,
8686
isTryingMajor || isAfterSplit, mayUseOffPeak, mayBeStuck);
87+
result.setAfterSplit(isAfterSplit);
8788

8889
ArrayList<HStoreFile> filesToCompact = Lists.newArrayList(result.getFiles());
8990
removeExcessFiles(filesToCompact, isUserCompaction, isTryingMajor);

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/StripeCompactionPolicy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public StripeCompactionRequest selectCompaction(StripeInformationProvider si,
122122
SplitStripeCompactionRequest request = new SplitStripeCompactionRequest(
123123
allFiles, OPEN_KEY, OPEN_KEY, targetKvs);
124124
request.setMajorRangeFull();
125+
request.getRequest().setAfterSplit(true);
125126
return request;
126127
}
127128

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestSplitTransactionOnCluster.java

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNotEquals;
2223
import static org.junit.Assert.assertNotNull;
2324
import static org.junit.Assert.assertNotSame;
2425
import static org.junit.Assert.assertNull;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.junit.Assert.fail;
2728

2829
import java.io.IOException;
30+
import java.lang.reflect.Field;
31+
import java.util.ArrayList;
2932
import java.util.Collection;
3033
import java.util.List;
3134
import java.util.Map;
@@ -75,6 +78,7 @@
7578
import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
7679
import org.apache.hadoop.hbase.coprocessor.MasterObserver;
7780
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
81+
import org.apache.hadoop.hbase.io.Reference;
7882
import org.apache.hadoop.hbase.master.HMaster;
7983
import org.apache.hadoop.hbase.master.LoadBalancer;
8084
import org.apache.hadoop.hbase.master.MasterRpcServices;
@@ -86,6 +90,7 @@
8690
import org.apache.hadoop.hbase.master.assignment.RegionStates;
8791
import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
8892
import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
93+
import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker;
8994
import org.apache.hadoop.hbase.regionserver.compactions.CompactionProgress;
9095
import org.apache.hadoop.hbase.regionserver.throttle.NoLimitThroughputController;
9196
import org.apache.hadoop.hbase.testclassification.LargeTests;
@@ -110,6 +115,7 @@
110115
import org.junit.Test;
111116
import org.junit.experimental.categories.Category;
112117
import org.junit.rules.TestName;
118+
import org.mockito.Mockito;
113119
import org.slf4j.Logger;
114120
import org.slf4j.LoggerFactory;
115121

@@ -280,6 +286,79 @@ public void testSplitFailedCompactionAndSplit() throws Exception {
280286
assertEquals(2, cluster.getRegions(tableName).size());
281287
}
282288

289+
@Test
290+
public void testSplitCompactWithPriority() throws Exception {
291+
final TableName tableName = TableName.valueOf(name.getMethodName());
292+
// Create table then get the single region for our new table.
293+
byte[] cf = Bytes.toBytes("cf");
294+
TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName)
295+
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(cf)).build();
296+
admin.createTable(htd);
297+
298+
assertNotEquals("Unable to retrieve regions of the table", -1,
299+
TESTING_UTIL.waitFor(10000, () -> cluster.getRegions(tableName).size() == 1));
300+
301+
HRegion region = cluster.getRegions(tableName).get(0);
302+
HStore store = region.getStore(cf);
303+
int regionServerIndex = cluster.getServerWith(region.getRegionInfo().getRegionName());
304+
HRegionServer regionServer = cluster.getRegionServer(regionServerIndex);
305+
306+
Table table = TESTING_UTIL.getConnection().getTable(tableName);
307+
// insert data
308+
insertData(tableName, admin, table);
309+
insertData(tableName, admin, table, 20);
310+
insertData(tableName, admin, table, 40);
311+
312+
// Compaction Request
313+
store.triggerMajorCompaction();
314+
Optional<CompactionContext> compactionContext = store.requestCompaction();
315+
assertTrue(compactionContext.isPresent());
316+
assertFalse(compactionContext.get().getRequest().isAfterSplit());
317+
assertEquals(compactionContext.get().getRequest().getPriority(), 13);
318+
319+
// Split
320+
long procId =
321+
cluster.getMaster().splitRegion(region.getRegionInfo(), Bytes.toBytes("row4"), 0, 0);
322+
323+
// wait for the split to complete or get interrupted. If the split completes successfully,
324+
// the procedure will return true; if the split fails, the procedure would throw exception.
325+
ProcedureTestingUtility.waitProcedure(cluster.getMaster().getMasterProcedureExecutor(),
326+
procId);
327+
328+
assertEquals(2, cluster.getRegions(tableName).size());
329+
// we have 2 daughter regions
330+
HRegion hRegion1 = cluster.getRegions(tableName).get(0);
331+
HRegion hRegion2 = cluster.getRegions(tableName).get(1);
332+
HStore hStore1 = hRegion1.getStore(cf);
333+
HStore hStore2 = hRegion2.getStore(cf);
334+
335+
// For hStore1 && hStore2, set mock reference to one of the storeFiles
336+
StoreFileInfo storeFileInfo1 = new ArrayList<>(hStore1.getStorefiles()).get(0).getFileInfo();
337+
StoreFileInfo storeFileInfo2 = new ArrayList<>(hStore2.getStorefiles()).get(0).getFileInfo();
338+
Field field = StoreFileInfo.class.getDeclaredField("reference");
339+
field.setAccessible(true);
340+
field.set(storeFileInfo1, Mockito.mock(Reference.class));
341+
field.set(storeFileInfo2, Mockito.mock(Reference.class));
342+
hStore1.triggerMajorCompaction();
343+
hStore2.triggerMajorCompaction();
344+
345+
compactionContext = hStore1.requestCompaction();
346+
assertTrue(compactionContext.isPresent());
347+
// since we set mock reference to one of the storeFiles, we will get isAfterSplit=true &&
348+
// highest priority for hStore1's compactionContext
349+
assertTrue(compactionContext.get().getRequest().isAfterSplit());
350+
assertEquals(compactionContext.get().getRequest().getPriority(), Integer.MIN_VALUE + 1000);
351+
352+
compactionContext =
353+
hStore2.requestCompaction(Integer.MIN_VALUE + 10, CompactionLifeCycleTracker.DUMMY, null);
354+
assertTrue(compactionContext.isPresent());
355+
// compaction request contains higher priority than default priority of daughter region
356+
// compaction (Integer.MIN_VALUE + 1000), hence we are expecting request priority to
357+
// be accepted.
358+
assertTrue(compactionContext.get().getRequest().isAfterSplit());
359+
assertEquals(compactionContext.get().getRequest().getPriority(), Integer.MIN_VALUE + 10);
360+
}
361+
283362
public static class FailingSplitMasterObserver implements MasterCoprocessor, MasterObserver {
284363
volatile CountDownLatch latch;
285364

@@ -641,18 +720,21 @@ public void testSplitWithRegionReplicas() throws Exception {
641720
}
642721
}
643722

644-
private void insertData(final TableName tableName, Admin admin, Table t) throws IOException,
645-
InterruptedException {
646-
Put p = new Put(Bytes.toBytes("row1"));
723+
private void insertData(final TableName tableName, Admin admin, Table t) throws IOException {
724+
insertData(tableName, admin, t, 1);
725+
}
726+
727+
private void insertData(TableName tableName, Admin admin, Table t, int i) throws IOException {
728+
Put p = new Put(Bytes.toBytes("row" + i));
647729
p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q1"), Bytes.toBytes("1"));
648730
t.put(p);
649-
p = new Put(Bytes.toBytes("row2"));
731+
p = new Put(Bytes.toBytes("row" + (i + 1)));
650732
p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q1"), Bytes.toBytes("2"));
651733
t.put(p);
652-
p = new Put(Bytes.toBytes("row3"));
734+
p = new Put(Bytes.toBytes("row" + (i + 2)));
653735
p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q1"), Bytes.toBytes("3"));
654736
t.put(p);
655-
p = new Put(Bytes.toBytes("row4"));
737+
p = new Put(Bytes.toBytes("row" + (i + 3)));
656738
p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("q1"), Bytes.toBytes("4"));
657739
t.put(p);
658740
admin.flush(tableName);

0 commit comments

Comments
 (0)