Skip to content

Commit 37b8af7

Browse files
author
Little K
committed
HBASE-28750 Region normalizer should work in off peak if config
1 parent d85574a commit 37b8af7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/master/normalizer/RegionNormalizerWorker.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.time.Duration;
2222
import java.util.ArrayList;
23+
import java.util.Calendar;
2324
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.concurrent.atomic.AtomicLong;
@@ -33,6 +34,7 @@
3334
import org.apache.hadoop.hbase.conf.ConfigurationObserver;
3435
import org.apache.hadoop.hbase.conf.PropagatingConfigurationObserver;
3536
import org.apache.hadoop.hbase.master.MasterServices;
37+
import org.apache.hadoop.hbase.regionserver.compactions.OffPeakHours;
3638
import org.apache.yetus.audience.InterfaceAudience;
3739
import org.slf4j.Logger;
3840
import org.slf4j.LoggerFactory;
@@ -68,6 +70,7 @@ class RegionNormalizerWorker implements PropagatingConfigurationObserver, Runnab
6870
private long splitPlanCount;
6971
private long mergePlanCount;
7072
private final AtomicLong cumulativePlansSizeLimitMb;
73+
private final OffPeakHours offPeakHours;
7174

7275
RegionNormalizerWorker(final Configuration configuration, final MasterServices masterServices,
7376
final RegionNormalizer regionNormalizer, final RegionNormalizerWorkQueue<TableName> workQueue) {
@@ -81,6 +84,7 @@ class RegionNormalizerWorker implements PropagatingConfigurationObserver, Runnab
8184
this.defaultNormalizerTableLevel = extractDefaultNormalizerValue(configuration);
8285
this.cumulativePlansSizeLimitMb = new AtomicLong(
8386
configuration.getLong(CUMULATIVE_SIZE_LIMIT_MB_KEY, DEFAULT_CUMULATIVE_SIZE_LIMIT_MB));
87+
this.offPeakHours = OffPeakHours.getInstance(configuration);
8488
}
8589

8690
private boolean extractDefaultNormalizerValue(final Configuration configuration) {
@@ -186,6 +190,10 @@ public void run() {
186190
LOG.debug("interrupt detected. terminating.");
187191
break;
188192
}
193+
if (!offPeakHours.equals(OffPeakHours.DISABLED) && !offPeakHours.isOffPeakHour()) {
194+
sleepToNextHour();
195+
continue;
196+
}
189197
final TableName tableName;
190198
try {
191199
tableName = workQueue.take();
@@ -199,6 +207,22 @@ public void run() {
199207
}
200208
}
201209

210+
private void sleepToNextHour() {
211+
LOG.info("offpeak hours is configured and we'll wait for offpeak hours to continue normalising.");
212+
Calendar now = Calendar.getInstance();
213+
Calendar nextHour = (Calendar) now.clone();
214+
nextHour.add(Calendar.HOUR_OF_DAY, 1);
215+
nextHour.set(Calendar.MINUTE, 0);
216+
nextHour.set(Calendar.SECOND, 0);
217+
nextHour.set(Calendar.MILLISECOND, 0);
218+
try {
219+
Thread.sleep(nextHour.getTimeInMillis() - now.getTimeInMillis());
220+
} catch (InterruptedException e) {
221+
LOG.warn("Interrupted while waiting to next hour.");
222+
e.printStackTrace();
223+
}
224+
}
225+
202226
private List<NormalizationPlan> calculatePlans(final TableName tableName) {
203227
if (masterServices.skipRegionManagementAction("region normalizer")) {
204228
return Collections.emptyList();

hbase-server/src/test/java/org/apache/hadoop/hbase/master/normalizer/TestRegionNormalizerWorker.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.Collections.singletonList;
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222
import static org.hamcrest.Matchers.comparesEqualTo;
23+
import static org.hamcrest.Matchers.equalTo;
2324
import static org.hamcrest.Matchers.greaterThan;
2425
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
2526
import static org.hamcrest.Matchers.nullValue;
@@ -48,6 +49,8 @@
4849
import org.apache.hadoop.hbase.client.TableDescriptor;
4950
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
5051
import org.apache.hadoop.hbase.master.MasterServices;
52+
import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
53+
import org.apache.hadoop.hbase.regionserver.compactions.OffPeakHours;
5154
import org.apache.hadoop.hbase.testclassification.MasterTests;
5255
import org.apache.hadoop.hbase.testclassification.SmallTests;
5356
import org.hamcrest.Description;
@@ -234,6 +237,40 @@ public void testPlansSizeLimit() throws Exception {
234237
comparesEqualTo(1L));
235238
}
236239

240+
@Test
241+
public void testNormalizerWorkInOffpeak() throws Exception {
242+
final TableName tn = tableName.getTableName();
243+
final TableDescriptor tnDescriptor =
244+
TableDescriptorBuilder.newBuilder(tn).setNormalizationEnabled(true).build();
245+
when(masterServices.getTableDescriptors().get(tn)).thenReturn(tnDescriptor);
246+
when(masterServices.mergeRegions(any(), anyBoolean(), anyLong(), anyLong())).thenReturn(1L);
247+
when(regionNormalizer.computePlansForTable(tnDescriptor)).thenReturn(singletonList(
248+
new MergeNormalizationPlan.Builder().addTarget(RegionInfoBuilder.newBuilder(tn).build(), 10)
249+
.addTarget(RegionInfoBuilder.newBuilder(tn).build(), 20).build()));
250+
251+
Configuration configuration = testingUtility.getConfiguration();
252+
configuration.set(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, "18");
253+
configuration.set(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, "19");
254+
255+
RegionNormalizerWorker worker =
256+
new RegionNormalizerWorker(configuration, masterServices, regionNormalizer, queue);
257+
long beforeMergePlanCount = worker.getMergePlanCount();
258+
workerPool.submit(worker);
259+
queue.put(tn);
260+
261+
Thread.sleep(5000);
262+
263+
OffPeakHours offPeakHours = OffPeakHours.getInstance(configuration);
264+
if (offPeakHours.isOffPeakHour()) {
265+
assertThatEventually("executing work should see plan count increase",
266+
worker::getMergePlanCount, greaterThan(beforeMergePlanCount));
267+
} else {
268+
assertThatEventually("executing work should see plan count unchanged",
269+
worker::getMergePlanCount, equalTo(beforeMergePlanCount));
270+
}
271+
workerPool.shutdownNow();
272+
}
273+
237274
/**
238275
* Repeatedly evaluates {@code matcher} against the result of calling {@code actualSupplier} until
239276
* the matcher succeeds or the timeout period of 30 seconds is exhausted.

0 commit comments

Comments
 (0)