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.master;
019
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
027import org.apache.hadoop.hbase.StartTestingClusterOption;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.junit.jupiter.api.AfterEach;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035/**
036 * Test for Regions Recovery Config Manager
037 */
038@Tag(MasterTests.TAG)
039@Tag(MediumTests.TAG)
040public class TestRegionsRecoveryConfigManager {
041
042  private static final HBaseTestingUtil HBASE_TESTING_UTILITY = new HBaseTestingUtil();
043
044  private SingleProcessHBaseCluster cluster;
045
046  private HMaster hMaster;
047
048  private RegionsRecoveryConfigManager regionsRecoveryConfigManager;
049
050  private Configuration conf;
051
052  @BeforeEach
053  public void setup() throws Exception {
054    conf = HBASE_TESTING_UTILITY.getConfiguration();
055    conf.unset("hbase.regions.recovery.store.file.ref.count");
056    conf.unset("hbase.master.regions.recovery.check.interval");
057    StartTestingClusterOption option = StartTestingClusterOption.builder()
058      .masterClass(TestHMaster.class).numRegionServers(1).numDataNodes(1).build();
059    HBASE_TESTING_UTILITY.startMiniCluster(option);
060    cluster = HBASE_TESTING_UTILITY.getMiniHBaseCluster();
061  }
062
063  @AfterEach
064  public void tearDown() throws Exception {
065    HBASE_TESTING_UTILITY.shutdownMiniCluster();
066  }
067
068  @Test
069  public void testChoreSchedule() throws Exception {
070    this.hMaster = cluster.getMaster();
071
072    this.regionsRecoveryConfigManager = new RegionsRecoveryConfigManager(this.hMaster);
073    // not yet scheduled
074    assertFalse(
075      hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
076
077    this.regionsRecoveryConfigManager.onConfigurationChange(conf);
078    // not yet scheduled
079    assertFalse(
080      hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
081
082    conf.setInt("hbase.master.regions.recovery.check.interval", 10);
083    this.regionsRecoveryConfigManager.onConfigurationChange(conf);
084    // not yet scheduled - missing config: hbase.regions.recovery.store.file.ref.count
085    assertFalse(
086      hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
087
088    conf.setInt("hbase.regions.recovery.store.file.ref.count", 10);
089    this.regionsRecoveryConfigManager.onConfigurationChange(conf);
090    // chore scheduled
091    assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
092
093    conf.setInt("hbase.regions.recovery.store.file.ref.count", 20);
094    this.regionsRecoveryConfigManager.onConfigurationChange(conf);
095    // chore re-scheduled
096    assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
097
098    conf.setInt("hbase.regions.recovery.store.file.ref.count", 20);
099    this.regionsRecoveryConfigManager.onConfigurationChange(conf);
100    // chore scheduling untouched
101    assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
102
103    conf.unset("hbase.regions.recovery.store.file.ref.count");
104    this.regionsRecoveryConfigManager.onConfigurationChange(conf);
105    // chore un-scheduled
106    assertFalse(
107      hMaster.getChoreService().isChoreScheduled(regionsRecoveryConfigManager.getChore()));
108  }
109
110  // Make it public so that JVMClusterUtil can access it.
111  public static class TestHMaster extends HMaster {
112    public TestHMaster(Configuration conf) throws IOException {
113      super(conf);
114    }
115  }
116}