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 */ 018 019package org.apache.hadoop.hbase.master; 020 021import java.io.IOException; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.MiniHBaseCluster; 027import org.apache.hadoop.hbase.StartMiniClusterOption; 028import org.apache.hadoop.hbase.Stoppable; 029import org.apache.hadoop.hbase.testclassification.MasterTests; 030import org.apache.hadoop.hbase.testclassification.MediumTests; 031import org.junit.After; 032import org.junit.Assert; 033import org.junit.Before; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037 038/** 039 * Test for Regions Recovery Config Manager 040 */ 041@Category({MasterTests.class, MediumTests.class}) 042public class TestRegionsRecoveryConfigManager { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestRegionsRecoveryConfigManager.class); 047 048 private static final HBaseTestingUtility HBASE_TESTING_UTILITY = new HBaseTestingUtility(); 049 050 private MiniHBaseCluster cluster; 051 052 private HMaster hMaster; 053 054 private RegionsRecoveryChore regionsRecoveryChore; 055 056 private RegionsRecoveryConfigManager regionsRecoveryConfigManager; 057 058 private Configuration conf; 059 060 @Before 061 public void setup() throws Exception { 062 conf = HBASE_TESTING_UTILITY.getConfiguration(); 063 conf.unset("hbase.regions.recovery.store.file.ref.count"); 064 conf.unset("hbase.master.regions.recovery.check.interval"); 065 StartMiniClusterOption option = StartMiniClusterOption.builder() 066 .masterClass(TestHMaster.class) 067 .numRegionServers(1) 068 .numDataNodes(1).build(); 069 HBASE_TESTING_UTILITY.startMiniCluster(option); 070 cluster = HBASE_TESTING_UTILITY.getMiniHBaseCluster(); 071 } 072 073 @After 074 public void tearDown() throws Exception { 075 HBASE_TESTING_UTILITY.shutdownMiniCluster(); 076 } 077 078 @Test 079 public void testChoreSchedule() throws Exception { 080 081 this.hMaster = cluster.getMaster(); 082 083 Stoppable stoppable = new StoppableImplementation(); 084 this.regionsRecoveryChore = new RegionsRecoveryChore(stoppable, conf, hMaster); 085 086 this.regionsRecoveryConfigManager = new RegionsRecoveryConfigManager(this.hMaster); 087 // not yet scheduled 088 Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 089 090 this.regionsRecoveryConfigManager.onConfigurationChange(conf); 091 // not yet scheduled 092 Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 093 094 conf.setInt("hbase.master.regions.recovery.check.interval", 10); 095 this.regionsRecoveryConfigManager.onConfigurationChange(conf); 096 // not yet scheduled - missing config: hbase.regions.recovery.store.file.ref.count 097 Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 098 099 conf.setInt("hbase.regions.recovery.store.file.ref.count", 10); 100 this.regionsRecoveryConfigManager.onConfigurationChange(conf); 101 // chore scheduled 102 Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 103 104 conf.setInt("hbase.regions.recovery.store.file.ref.count", 20); 105 this.regionsRecoveryConfigManager.onConfigurationChange(conf); 106 // chore re-scheduled 107 Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 108 109 conf.setInt("hbase.regions.recovery.store.file.ref.count", 20); 110 this.regionsRecoveryConfigManager.onConfigurationChange(conf); 111 // chore scheduling untouched 112 Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 113 114 conf.unset("hbase.regions.recovery.store.file.ref.count"); 115 this.regionsRecoveryConfigManager.onConfigurationChange(conf); 116 // chore un-scheduled 117 Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore)); 118 } 119 120 // Make it public so that JVMClusterUtil can access it. 121 public static class TestHMaster extends HMaster { 122 public TestHMaster(Configuration conf) throws IOException { 123 super(conf); 124 } 125 } 126 127 /** 128 * Simple helper class that just keeps track of whether or not its stopped. 129 */ 130 private static class StoppableImplementation implements Stoppable { 131 132 private boolean stop = false; 133 134 @Override 135 public void stop(String why) { 136 this.stop = true; 137 } 138 139 @Override 140 public boolean isStopped() { 141 return this.stop; 142 } 143 144 } 145 146}