From c2c918ab8320cc239276d67ea321215e9200b035 Mon Sep 17 00:00:00 2001 From: zhangduo Date: Wed, 12 Nov 2014 10:29:42 +0800 Subject: [PATCH] HBASE-8329 Limit compaction speed --- .../apache/hadoop/hbase/regionserver/HStore.java | 24 ++-- .../compactions/CompactionConfiguration.java | 6 +- .../hbase/regionserver/compactions/Compactor.java | 12 +- .../regionserver/compactions/OffPeakHours.java | 105 ---------------- .../compactions/PeakCompactionsThrottle.java | 133 +++++++++++++++++++++ .../regionserver/compactions/TimeOfDayTracker.java | 103 ++++++++++++++++ .../TestRegionServerOnlineConfigChange.java | 2 +- .../regionserver/compactions/TestOffPeakHours.java | 12 +- .../compactions/TestPeakCompactionsThrottle.java | 70 +++++++++++ 9 files changed, 339 insertions(+), 128 deletions(-) delete mode 100644 hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/OffPeakHours.java create mode 100644 hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/PeakCompactionsThrottle.java create mode 100644 hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/TimeOfDayTracker.java create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestPeakCompactionsThrottle.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java index 8b41401..e42ae8e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java @@ -77,7 +77,7 @@ import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext; import org.apache.hadoop.hbase.regionserver.compactions.CompactionProgress; import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest; import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactor; -import org.apache.hadoop.hbase.regionserver.compactions.OffPeakHours; +import org.apache.hadoop.hbase.regionserver.compactions.TimeOfDayTracker; import org.apache.hadoop.hbase.regionserver.wal.HLogUtil; import org.apache.hadoop.hbase.security.EncryptionUtil; import org.apache.hadoop.hbase.security.User; @@ -179,7 +179,7 @@ public class HStore implements Store { final StoreEngine storeEngine; private static final AtomicBoolean offPeakCompactionTracker = new AtomicBoolean(); - private volatile OffPeakHours offPeakHours; + private volatile TimeOfDayTracker offPeakHours; private static final int DEFAULT_FLUSH_RETRIES_NUMBER = 10; private int flushRetriesNumber; @@ -242,7 +242,7 @@ public class HStore implements Store { String className = conf.get(MEMSTORE_CLASS_NAME, DefaultMemStore.class.getName()); this.memstore = ReflectionUtils.instantiateWithCustomCtor(className, new Class[] { Configuration.class, KeyValue.KVComparator.class }, new Object[] { conf, this.comparator }); - this.offPeakHours = OffPeakHours.getInstance(conf); + this.offPeakHours = TimeOfDayTracker.getInstance(conf); // Setting up cache configuration for this family this.cacheConf = new CacheConfig(conf, family); @@ -923,7 +923,7 @@ public class HStore implements Store { if (LOG.isInfoEnabled()) { LOG.info("Added " + sf + ", entries=" + r.getEntries() + ", sequenceid=" + logCacheFlushId + - ", filesize=" + StringUtils.humanReadableInt(r.length())); + ", filesize=" + StringUtils.byteDesc(r.length())); } return sf; } @@ -1146,7 +1146,7 @@ public class HStore implements Store { LOG.info("Starting compaction of " + filesToCompact.size() + " file(s) in " + this + " of " + this.getRegionInfo().getRegionNameAsString() + " into tmpdir=" + fs.getTempDir() + ", totalSize=" - + StringUtils.humanReadableInt(cr.getSize())); + + StringUtils.byteDesc(cr.getSize())); long compactionStartTime = EnvironmentEdgeManager.currentTime(); List sfs = null; @@ -1263,12 +1263,12 @@ public class HStore implements Store { for (StoreFile sf: sfs) { message.append(sf.getPath().getName()); message.append("(size="); - message.append(StringUtils.humanReadableInt(sf.getReader().length())); + message.append(StringUtils.byteDesc(sf.getReader().length())); message.append("), "); } } message.append("total size for store is ") - .append(StringUtils.humanReadableInt(storeSize)) + .append(StringUtils.byteDesc(storeSize)) .append(". This selection was in queue for ") .append(StringUtils.formatTimeDiff(compactionStartTime, cr.getSelectionTime())) .append(", and took ").append(StringUtils.formatTimeDiff(now, compactionStartTime)) @@ -1328,7 +1328,7 @@ public class HStore implements Store { } } - this.replaceStoreFiles(inputStoreFiles, Collections.EMPTY_LIST); + this.replaceStoreFiles(inputStoreFiles, Collections.emptyList()); this.completeCompaction(inputStoreFiles); } @@ -1447,7 +1447,7 @@ public class HStore implements Store { // Normal case - coprocessor is not overriding file selection. if (!compaction.hasSelection()) { boolean isUserCompaction = priority == Store.PRIORITY_USER; - boolean mayUseOffPeak = offPeakHours.isOffPeakHour() && + boolean mayUseOffPeak = offPeakHours.isHourInInterval() && offPeakCompactionTracker.compareAndSet(false, true); try { compaction.select(this.filesCompacting, isUserCompaction, @@ -1539,7 +1539,7 @@ public class HStore implements Store { completeCompaction(delSfs); LOG.info("Completed removal of " + delSfs.size() + " unnecessary (expired) file(s) in " + this + " of " + this.getRegionInfo().getRegionNameAsString() - + "; total size for store is " + StringUtils.humanReadableInt(storeSize)); + + "; total size for store is " + StringUtils.byteDesc(storeSize)); } @Override @@ -2212,7 +2212,7 @@ public class HStore implements Store { return this.storeEngine; } - protected OffPeakHours getOffPeakHours() { + protected TimeOfDayTracker getOffPeakHours() { return this.offPeakHours; } @@ -2225,7 +2225,7 @@ public class HStore implements Store { .add(conf) .addBytesMap(family.getValues()); this.storeEngine.compactionPolicy.setConf(conf); - this.offPeakHours = OffPeakHours.getInstance(conf); + this.offPeakHours = TimeOfDayTracker.getInstance(conf); } /** diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactionConfiguration.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactionConfiguration.java index 8d8fcd0..bc23e49 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactionConfiguration.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactionConfiguration.java @@ -56,7 +56,11 @@ public class CompactionConfiguration { "hbase.hstore.compaction.max.size"; public static final String HBASE_HSTORE_OFFPEAK_END_HOUR = "hbase.offpeak.end.hour"; public static final String HBASE_HSTORE_OFFPEAK_START_HOUR = "hbase.offpeak.start.hour"; - + public static final String HBASE_HSTORE_PEAK_COMPACTION_SPEED_ALLOWED = + "hbase.regionserver.compaction.peak.maxspeed"; + public static final String HBASE_HSTORE_PEAK_COMPACTION_SPEED_CHECK_INTERVAL = + "hbase.regionserver.compaction.speed.check.interval"; + Configuration conf; StoreConfigInformation storeConfigInfo; diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/Compactor.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/Compactor.java index 2ddc06a..5219392 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/Compactor.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/Compactor.java @@ -25,12 +25,12 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.KeyValueUtil; +import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.io.compress.Compression; import org.apache.hadoop.hbase.io.hfile.HFile.FileInfo; @@ -59,7 +59,8 @@ public abstract class Compactor { private int compactionKVMax; protected Compression.Algorithm compactionCompression; - + private PeakCompactionsThrottle peakCompactionsThrottle; + /** specify how many days to keep MVCC values during major compaction **/ protected int keepSeqIdPeriod; @@ -71,6 +72,7 @@ public abstract class Compactor { this.conf.getInt(HConstants.COMPACTION_KV_MAX, HConstants.COMPACTION_KV_MAX_DEFAULT); this.compactionCompression = (this.store.getFamily() == null) ? Compression.Algorithm.NONE : this.store.getFamily().getCompactionCompression(); + this.peakCompactionsThrottle = new PeakCompactionsThrottle(conf); this.keepSeqIdPeriod = Math.max(this.conf.getInt(HConstants.KEEP_SEQID_PERIOD, HConstants.MIN_KEEP_SEQID_PERIOD), HConstants.MIN_KEEP_SEQID_PERIOD); } @@ -167,7 +169,7 @@ public abstract class Compactor { LOG.debug("Compacting " + file + ", keycount=" + keyCount + ", bloomtype=" + r.getBloomFilterType().toString() + - ", size=" + StringUtils.humanReadableInt(r.length()) + + ", size=" + StringUtils.byteDesc(r.length()) + ", encoding=" + r.getHFileReader().getDataBlockEncoding() + ", seqNum=" + seqNum + (allFiles ? ", earliestPutTs=" + earliestPutTs: "")); @@ -241,6 +243,7 @@ public abstract class Compactor { } long now = 0; boolean hasMore; + peakCompactionsThrottle.startCompaction(); do { hasMore = scanner.next(cells, compactionKVMax); if (LOG.isDebugEnabled()) { @@ -269,6 +272,7 @@ public abstract class Compactor { } } } + peakCompactionsThrottle.throttle(len); } // Log the progress of long running compactions every minute if // logging at DEBUG level @@ -282,6 +286,8 @@ public abstract class Compactor { } cells.clear(); } while (hasMore); + peakCompactionsThrottle.finishCompaction(this.store.getRegionInfo() + .getRegionNameAsString(), this.store.getFamily().getNameAsString()); progress.complete(); return true; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/OffPeakHours.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/OffPeakHours.java deleted file mode 100644 index 37b997b..0000000 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/OffPeakHours.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.compactions; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.classification.InterfaceAudience; -import org.apache.hadoop.conf.Configuration; - -@InterfaceAudience.Private -public abstract class OffPeakHours { - private static final Log LOG = LogFactory.getLog(OffPeakHours.class); - - public static final OffPeakHours DISABLED = new OffPeakHours() { - @Override public boolean isOffPeakHour() { return false; } - @Override public boolean isOffPeakHour(int targetHour) { return false; } - }; - - public static OffPeakHours getInstance(Configuration conf) { - int startHour = conf.getInt(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, -1); - int endHour = conf.getInt(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, -1); - return getInstance(startHour, endHour); - } - - /** - * @param startHour inclusive - * @param endHour exclusive - */ - public static OffPeakHours getInstance(int startHour, int endHour) { - if (startHour == -1 && endHour == -1) { - return DISABLED; - } - - if (! isValidHour(startHour) || ! isValidHour(endHour)) { - if (LOG.isWarnEnabled()) { - LOG.warn("Ignoring invalid start/end hour for peak hour : start = " + - startHour + " end = " + endHour + - ". Valid numbers are [0-23]"); - } - return DISABLED; - } - - if (startHour == endHour) { - return DISABLED; - } - - return new OffPeakHoursImpl(startHour, endHour); - } - - private static boolean isValidHour(int hour) { - return 0 <= hour && hour <= 23; - } - - /** - * @return whether {@code targetHour} is off-peak hour - */ - public abstract boolean isOffPeakHour(int targetHour); - - /** - * @return whether it is off-peak hour - */ - public abstract boolean isOffPeakHour(); - - private static class OffPeakHoursImpl extends OffPeakHours { - final int startHour; - final int endHour; - - /** - * @param startHour inclusive - * @param endHour exclusive - */ - OffPeakHoursImpl(int startHour, int endHour) { - this.startHour = startHour; - this.endHour = endHour; - } - - @Override - public boolean isOffPeakHour() { - return isOffPeakHour(CurrentHourProvider.getCurrentHour()); - } - - @Override - public boolean isOffPeakHour(int targetHour) { - if (startHour <= endHour) { - return startHour <= targetHour && targetHour < endHour; - } - return targetHour < endHour || startHour <= targetHour; - } - } -} diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/PeakCompactionsThrottle.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/PeakCompactionsThrottle.java new file mode 100644 index 0000000..8fb36b1 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/PeakCompactionsThrottle.java @@ -0,0 +1,133 @@ +/** + * + * 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.compactions; + +import java.io.IOException; +import java.io.InterruptedIOException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.util.StringUtils; + +/** + * Limit peak hour compaction speed , slow down when it is too fast. Some applications have a + * typically peak hour at which time have most request. + * hbase.regionserver.compaction.peak.start.hour means peak start hour. + * hbase.regionserver.compaction.peak.end.hour means peak end hour. Between the peak start hour and + * the peak end hour the request from the client is the most at a day. + * hbase.regionserver.compaction.speed.check.interval means at which time when we check compact + * speed. hbase.regionserver.compaction.peak.maxspeed means max speed at peak hour. + */ +@InterfaceAudience.Private +public class PeakCompactionsThrottle { + private static final Log LOG = LogFactory.getLog(PeakCompactionsThrottle.class); + + TimeOfDayTracker peakHours; + + private final long maxSpeedInPeak; + private final int checkInterval; + private int numberOfThrottles = 0; + private int timeOfThrottles = 0; + private long start; + private long end; + private int bytesWritten = 0; + + public PeakCompactionsThrottle(Configuration conf) { + peakHours = TimeOfDayTracker.getInstance(conf); + maxSpeedInPeak = conf + .getInt(CompactionConfiguration.HBASE_HSTORE_PEAK_COMPACTION_SPEED_ALLOWED, + 30 * 1024 * 1024 /* 30 MB/s */); + checkInterval = conf.getInt( + CompactionConfiguration.HBASE_HSTORE_PEAK_COMPACTION_SPEED_CHECK_INTERVAL, + 30 * 1024 * 1024 /* 30 MB */); + } + + /** + * start compaction + */ + public void startCompaction() { + start = System.currentTimeMillis(); + } + + /** + * finish compaction + */ + public void finishCompaction(String region, String family) { + if (numberOfThrottles > 0) { + LOG.info("Region '" + region + "' family '" + family + "' 's maxSpeedInPeak is " + + StringUtils.byteDesc(maxSpeedInPeak) + "/s compaction throttle: sleep number " + + numberOfThrottles + " sleep time " + timeOfThrottles + "(ms)"); + } + } + + /** + * reset start time + */ + void resetStartTime() { + start = System.currentTimeMillis(); + } + + /** + * Peak compaction throttle, if it is peak time and the compaction speed is too fast, + * sleep for a while to slow down. + */ + public void throttle(long numOfBytes) throws IOException { + bytesWritten += numOfBytes; + if (bytesWritten >= checkInterval) { + checkAndSlowFastCompact(bytesWritten); + bytesWritten = 0; + } + } + + private void checkAndSlowFastCompact(long numOfBytes) throws IOException { + if (!peakHours.isHourInInterval()) { + // not peak hour, just return. + return; + } + if (maxSpeedInPeak <= 0) { + return; + } + end = System.currentTimeMillis(); + long minTimeAllowed = numOfBytes * 1000 / maxSpeedInPeak; // ms + long elapsed = end - start; + if (elapsed < minTimeAllowed) { + // too fast + try { + // sleep for a while to slow down. + Thread.sleep(minTimeAllowed - elapsed); + numberOfThrottles++; + timeOfThrottles += (minTimeAllowed - elapsed); + } catch (InterruptedException ie) { + IOException iie = new InterruptedIOException(); + iie.initCause(ie); + throw iie; + } + } + resetStartTime(); + } + + /** + * For test + */ + public int getNumberOfThrottles() { + return numberOfThrottles; + } +} diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/TimeOfDayTracker.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/TimeOfDayTracker.java new file mode 100644 index 0000000..5e56d52 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/TimeOfDayTracker.java @@ -0,0 +1,103 @@ +/* + * 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.compactions; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.conf.Configuration; + +@InterfaceAudience.Private +public abstract class TimeOfDayTracker { + private static final Log LOG = LogFactory.getLog(TimeOfDayTracker.class); + + public static final TimeOfDayTracker DISABLED = new TimeOfDayTracker() { + @Override public boolean isHourInInterval() { return false; } + @Override public boolean isHourInInterval(int targetHour) { return false; } + }; + + public static TimeOfDayTracker getInstance(Configuration conf) { + int startHour = conf.getInt(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, -1); + int endHour = conf.getInt(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, -1); + return getInstance(startHour, endHour); + } + + /** + * @param startHour inclusive + * @param endHour exclusive + */ + public static TimeOfDayTracker getInstance(int startHour, int endHour) { + if (startHour == -1 && endHour == -1) { + return DISABLED; + } + + if (! isValidHour(startHour) || ! isValidHour(endHour)) { + LOG.warn("Ignoring invalid start/end hour for peak hour : start = " + + startHour + " end = " + endHour + + ". Valid numbers are [0-23]"); + return DISABLED; + } + + if (startHour == endHour) { + return DISABLED; + } + + return new TimeOfDayTrackerImpl(startHour, endHour); + } + + private static boolean isValidHour(int hour) { + return 0 <= hour && hour <= 23; + } + + /** + * @return whether the hour is within tracked interval + */ + public abstract boolean isHourInInterval(int targetHour); + + /** + * @return whether the hour is within tracked interval + */ + public abstract boolean isHourInInterval(); + + private static class TimeOfDayTrackerImpl extends TimeOfDayTracker { + final int startHour; + final int endHour; + + /** + * @param startHour inclusive + * @param endHour exclusive + */ + TimeOfDayTrackerImpl(int startHour, int endHour) { + this.startHour = startHour; + this.endHour = endHour; + } + + @Override + public boolean isHourInInterval() { + return isHourInInterval(CurrentHourProvider.getCurrentHour()); + } + + @Override + public boolean isHourInInterval(int targetHour) { + if (startHour <= endHour) { + return startHour <= targetHour && targetHour < endHour; + } + return targetHour < endHour || startHour <= targetHour; + } + } +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerOnlineConfigChange.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerOnlineConfigChange.java index 6aa2526..609373d 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerOnlineConfigChange.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerOnlineConfigChange.java @@ -167,7 +167,7 @@ public class TestRegionServerOnlineConfigChange extends TestCase { conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, 6); conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, 7); rs1.getConfigurationManager().notifyAllObservers(conf); - assertFalse(hstore.getOffPeakHours().isOffPeakHour(4)); + assertFalse(hstore.getOffPeakHours().isHourInInterval(4)); // Check if the minCompactSize gets updated. long newMinCompactSize = diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestOffPeakHours.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestOffPeakHours.java index f43c29a..45756f8 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestOffPeakHours.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestOffPeakHours.java @@ -56,23 +56,23 @@ public class TestOffPeakHours { @Test public void testWithoutSettings() { Configuration conf = testUtil.getConfiguration(); - OffPeakHours target = OffPeakHours.getInstance(conf); - assertFalse(target.isOffPeakHour(hourOfDay)); + TimeOfDayTracker target = TimeOfDayTracker.getInstance(conf); + assertFalse(target.isHourInInterval(hourOfDay)); } @Test public void testSetPeakHourToTargetTime() { conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, hourMinusOne); conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, hourPlusOne); - OffPeakHours target = OffPeakHours.getInstance(conf); - assertTrue(target.isOffPeakHour(hourOfDay)); + TimeOfDayTracker target = TimeOfDayTracker.getInstance(conf); + assertTrue(target.isHourInInterval(hourOfDay)); } @Test public void testSetPeakHourOutsideCurrentSelection() { conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, hourMinusTwo); conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, hourMinusOne); - OffPeakHours target = OffPeakHours.getInstance(conf); - assertFalse(target.isOffPeakHour(hourOfDay)); + TimeOfDayTracker target = TimeOfDayTracker.getInstance(conf); + assertFalse(target.isHourInInterval(hourOfDay)); } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestPeakCompactionsThrottle.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestPeakCompactionsThrottle.java new file mode 100644 index 0000000..12b2103 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/compactions/TestPeakCompactionsThrottle.java @@ -0,0 +1,70 @@ +/* + * 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.compactions; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(SmallTests.class) +public class TestPeakCompactionsThrottle { + private static HBaseTestingUtility testUtil; + private Configuration conf; + + @BeforeClass + public static void setUpClass() { + testUtil = new HBaseTestingUtility(); + } + + @Before + public void setUp() { + conf = testUtil.getConfiguration(); + } + + @Test + public void testSetPeakHourToTargetTime() throws IOException { + conf.set(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, "0"); + conf.set(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, "23"); + PeakCompactionsThrottle peakCompactionsThrottle = new PeakCompactionsThrottle(conf); + peakCompactionsThrottle.startCompaction(); + long numOfBytes = 60 * 1024 * 1024; + peakCompactionsThrottle.throttle(numOfBytes); + peakCompactionsThrottle.finishCompaction("region", "family"); + assertTrue(peakCompactionsThrottle.getNumberOfThrottles() > 0); + } + + @Test + public void testSetPeakHourOutsideCurrentSelection() throws IOException { + conf.set(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, "-1"); + conf.set(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, "-1"); + PeakCompactionsThrottle peakCompactionsThrottle = new PeakCompactionsThrottle(conf); + peakCompactionsThrottle.startCompaction(); + long numOfBytes = 30 * 1024 * 1024; + peakCompactionsThrottle.throttle(numOfBytes); + peakCompactionsThrottle.finishCompaction("region", "family"); + assertTrue(peakCompactionsThrottle.getNumberOfThrottles() == 0); + } +} -- 1.9.1