View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.MiniHBaseCluster;
24  import org.apache.hadoop.hbase.Stoppable;
25  import org.apache.hadoop.hbase.testclassification.MasterTests;
26  import org.apache.hadoop.hbase.testclassification.MediumTests;
27  import org.junit.After;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  /**
34   * Test for Regions Recovery Config Manager
35   */
36  @Category({MasterTests.class, MediumTests.class})
37  public class TestRegionsRecoveryConfigManager {
38  
39    private static final HBaseTestingUtility HBASE_TESTING_UTILITY = new HBaseTestingUtility();
40  
41    private MiniHBaseCluster cluster;
42  
43    private HMaster hMaster;
44  
45    private RegionsRecoveryChore regionsRecoveryChore;
46  
47    private RegionsRecoveryConfigManager regionsRecoveryConfigManager;
48  
49    private Configuration conf;
50  
51    @Before
52    public void setup() throws Exception {
53      conf = HBASE_TESTING_UTILITY.getConfiguration();
54      conf.unset("hbase.regions.recovery.store.file.ref.count");
55      conf.unset("hbase.master.regions.recovery.check.interval");
56      HBASE_TESTING_UTILITY.startMiniCluster(1, 1);
57      cluster = HBASE_TESTING_UTILITY.getMiniHBaseCluster();
58    }
59  
60    @After
61    public void tearDown() throws Exception {
62      HBASE_TESTING_UTILITY.shutdownMiniCluster();
63    }
64  
65    @Test
66    public void testChoreSchedule() throws Exception {
67  
68      this.hMaster = cluster.getMaster();
69  
70      Stoppable stoppable = new StoppableImplementation();
71      this.regionsRecoveryChore = new RegionsRecoveryChore(stoppable, conf, hMaster);
72  
73      this.regionsRecoveryConfigManager = new RegionsRecoveryConfigManager(this.hMaster);
74      // not yet scheduled
75      Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
76  
77      this.regionsRecoveryConfigManager.onConfigurationChange(conf);
78      // not yet scheduled
79      Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
80  
81      conf.setInt("hbase.master.regions.recovery.check.interval", 10);
82      this.regionsRecoveryConfigManager.onConfigurationChange(conf);
83      // not yet scheduled - missing config: hbase.regions.recovery.store.file.ref.count
84      Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
85  
86      conf.setInt("hbase.regions.recovery.store.file.ref.count", 10);
87      this.regionsRecoveryConfigManager.onConfigurationChange(conf);
88      // chore scheduled
89      Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
90  
91      conf.setInt("hbase.regions.recovery.store.file.ref.count", 20);
92      this.regionsRecoveryConfigManager.onConfigurationChange(conf);
93      // chore re-scheduled
94      Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
95  
96      conf.setInt("hbase.regions.recovery.store.file.ref.count", 20);
97      this.regionsRecoveryConfigManager.onConfigurationChange(conf);
98      // chore scheduling untouched
99      Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
100 
101     conf.unset("hbase.regions.recovery.store.file.ref.count");
102     this.regionsRecoveryConfigManager.onConfigurationChange(conf);
103     // chore un-scheduled
104     Assert.assertFalse(hMaster.getChoreService().isChoreScheduled(regionsRecoveryChore));
105   }
106 
107   /**
108    * Simple helper class that just keeps track of whether or not its stopped.
109    */
110   private static class StoppableImplementation implements Stoppable {
111 
112     private boolean stop = false;
113 
114     @Override
115     public void stop(String why) {
116       this.stop = true;
117     }
118 
119     @Override
120     public boolean isStopped() {
121       return this.stop;
122     }
123 
124   }
125 }