diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/RatioBasedCompactionPolicy.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/RatioBasedCompactionPolicy.java index 3386bfd97d9..a3e10f8165d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/RatioBasedCompactionPolicy.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/RatioBasedCompactionPolicy.java @@ -32,6 +32,7 @@ import org.apache.hadoop.hbase.regionserver.RSRpcServices; import org.apache.hadoop.hbase.regionserver.StoreConfigInformation; import org.apache.hadoop.hbase.regionserver.StoreFile; import org.apache.hadoop.hbase.regionserver.StoreUtils; +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; /** * The default algorithm for selecting files for compaction. @@ -61,7 +62,7 @@ public class RatioBasedCompactionPolicy extends SortedCompactionPolicy { } // TODO: Use better method for determining stamp of last major (HBASE-2990) long lowTimestamp = StoreUtils.getLowestTimestamp(filesToCompact); - long now = System.currentTimeMillis(); + long now = EnvironmentEdgeManager.currentTime(); if (lowTimestamp > 0L && lowTimestamp < (now - mcTime)) { // Major compaction time has elapsed. long cfTTL = this.storeConfigInfo.getStoreFileTtl(); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestDefaultCompactSelection.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestDefaultCompactSelection.java index dbd6f1103a3..1513cd02c94 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestDefaultCompactSelection.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestDefaultCompactSelection.java @@ -25,6 +25,8 @@ import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest; import org.apache.hadoop.hbase.regionserver.compactions.RatioBasedCompactionPolicy; import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; +import org.apache.hadoop.hbase.util.TimeOffsetEnvironmentEdge; import org.junit.Assert; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -34,6 +36,8 @@ public class TestDefaultCompactSelection extends TestCompactionPolicy { @Test public void testCompactionRatio() throws IOException { + TimeOffsetEnvironmentEdge edge = new TimeOffsetEnvironmentEdge(); + EnvironmentEdgeManager.injectEdge(edge); /** * NOTE: these tests are specific to describe the implementation of the * current compaction algorithm. Developed to ensure that refactoring @@ -90,10 +94,17 @@ public class TestDefaultCompactSelection extends TestCompactionPolicy { conf.setFloat("hbase.hregion.majorcompaction.jitter", 0); store.storeEngine.getCompactionPolicy().setConf(conf); try { + // The modTime of the mocked store file is currentTimeMillis, so we need to increase the + // timestamp a bit to make sure that now - lowestModTime is greater than major compaction + // period(1ms). // trigger an aged major compaction - compactEquals(sfCreate(50,25,12,12), 50, 25, 12, 12); + List candidates = sfCreate(50, 25, 12, 12); + edge.increment(2); + compactEquals(candidates, 50, 25, 12, 12); // major sure exceeding maxCompactSize also downgrades aged minors - compactEquals(sfCreate(100,50,23,12,12), 23, 12, 12); + candidates = sfCreate(100, 50, 23, 12, 12); + edge.increment(2); + compactEquals(candidates, 23, 12, 12); } finally { conf.setLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24); conf.setFloat("hbase.hregion.majorcompaction.jitter", 0.20F);