001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver.compactions;
019
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.testclassification.RegionServerTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.jupiter.api.BeforeAll;
028import org.junit.jupiter.api.BeforeEach;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(RegionServerTests.TAG)
033@Tag(SmallTests.TAG)
034public class TestOffPeakHours {
035
036  private static HBaseTestingUtil testUtil;
037
038  @BeforeAll
039  public static void setUpClass() {
040    testUtil = new HBaseTestingUtil();
041  }
042
043  private int hourOfDay;
044  private int hourPlusOne;
045  private int hourMinusOne;
046  private int hourMinusTwo;
047  private Configuration conf;
048
049  @BeforeEach
050  public void setUp() {
051    hourOfDay = 15;
052    hourPlusOne = ((hourOfDay + 1) % 24);
053    hourMinusOne = ((hourOfDay - 1 + 24) % 24);
054    hourMinusTwo = ((hourOfDay - 2 + 24) % 24);
055    conf = testUtil.getConfiguration();
056  }
057
058  @Test
059  public void testWithoutSettings() {
060    Configuration conf = testUtil.getConfiguration();
061    OffPeakHours target = OffPeakHours.getInstance(conf);
062    assertFalse(target.isOffPeakHour(hourOfDay));
063  }
064
065  @Test
066  public void testSetPeakHourToTargetTime() {
067    conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, hourMinusOne);
068    conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, hourPlusOne);
069    OffPeakHours target = OffPeakHours.getInstance(conf);
070    assertTrue(target.isOffPeakHour(hourOfDay));
071  }
072
073  @Test
074  public void testSetPeakHourOutsideCurrentSelection() {
075    conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_START_HOUR, hourMinusTwo);
076    conf.setLong(CompactionConfiguration.HBASE_HSTORE_OFFPEAK_END_HOUR, hourMinusOne);
077    OffPeakHours target = OffPeakHours.getInstance(conf);
078    assertFalse(target.isOffPeakHour(hourOfDay));
079  }
080}