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 java.lang.reflect.Field;
22  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.ScheduledChore;
25  import org.apache.hadoop.hbase.master.balancer.BalancerChore;
26  import org.apache.hadoop.hbase.master.balancer.ClusterStatusChore;
27  import org.apache.hadoop.hbase.master.cleaner.HFileCleaner;
28  import org.apache.hadoop.hbase.master.cleaner.LogCleaner;
29  import org.apache.hadoop.hbase.master.normalizer.RegionNormalizerChore;
30  import org.apache.hadoop.hbase.testclassification.MasterTests;
31  import org.apache.hadoop.hbase.testclassification.MediumTests;
32  import org.junit.AfterClass;
33  import org.junit.Assert;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  /**
39   * Tests to validate if HMaster default chores are scheduled
40   */
41  @Category({MasterTests.class, MediumTests.class})
42  public class TestMasterChoreScheduled {
43  
44    private static HMaster hMaster;
45  
46    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
47  
48    @BeforeClass
49    public static void setUp() throws Exception {
50      UTIL.startMiniCluster(1, 1);
51      hMaster = UTIL.getMiniHBaseCluster().getMaster();
52    }
53  
54    @AfterClass
55    public static void tearDown() throws Exception {
56      UTIL.shutdownMiniCluster();
57    }
58  
59    @Test
60    public void testDefaultScheduledChores() throws Exception {
61      // test if logCleaner chore is scheduled by default in HMaster init
62      TestChoreField<LogCleaner> logCleanerTestChoreField = new TestChoreField<>();
63      LogCleaner logCleaner = logCleanerTestChoreField.getChoreObj("logCleaner");
64      logCleanerTestChoreField.testIfChoreScheduled(logCleaner);
65  
66      // test if hfileCleaner chore is scheduled by default in HMaster init
67      TestChoreField<HFileCleaner> hFileCleanerTestChoreField = new TestChoreField<>();
68      HFileCleaner hFileCleaner = hFileCleanerTestChoreField.getChoreObj("hfileCleaner");
69      hFileCleanerTestChoreField.testIfChoreScheduled(hFileCleaner);
70  
71      // test if clusterStatusChore chore is scheduled by default in HMaster init
72      TestChoreField<ClusterStatusChore> clusterStatusChoreTestChoreField = new TestChoreField<>();
73      ClusterStatusChore clusterStatusChore = clusterStatusChoreTestChoreField
74        .getChoreObj("clusterStatusChore");
75      clusterStatusChoreTestChoreField.testIfChoreScheduled(clusterStatusChore);
76  
77      // test if balancerChore chore is scheduled by default in HMaster init
78      TestChoreField<BalancerChore> balancerChoreTestChoreField = new TestChoreField<>();
79      BalancerChore balancerChore = balancerChoreTestChoreField.getChoreObj("balancerChore");
80      balancerChoreTestChoreField.testIfChoreScheduled(balancerChore);
81  
82      // test if normalizerChore chore is scheduled by default in HMaster init
83      TestChoreField<RegionNormalizerChore> regionNormalizerChoreTestChoreField =
84        new TestChoreField<>();
85      RegionNormalizerChore regionNormalizerChore = regionNormalizerChoreTestChoreField
86        .getChoreObj("normalizerChore");
87      regionNormalizerChoreTestChoreField.testIfChoreScheduled(regionNormalizerChore);
88  
89      // test if catalogJanitorChore chore is scheduled by default in HMaster init
90      TestChoreField<CatalogJanitor> catalogJanitorTestChoreField = new TestChoreField<>();
91      CatalogJanitor catalogJanitor = catalogJanitorTestChoreField
92        .getChoreObj("catalogJanitorChore");
93      catalogJanitorTestChoreField.testIfChoreScheduled(catalogJanitor);
94  
95    }
96  
97  
98    private static class TestChoreField<E extends ScheduledChore> {
99  
100     private E getChoreObj(String fieldName) throws NoSuchFieldException,
101         IllegalAccessException {
102       Field masterField = HMaster.class.getDeclaredField(fieldName);
103       masterField.setAccessible(true);
104       E choreFieldVal = (E) masterField.get(hMaster);
105       return choreFieldVal;
106     }
107 
108     private void testIfChoreScheduled(E choreObj) {
109       Assert.assertNotNull(choreObj);
110       Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(choreObj));
111     }
112 
113   }
114 
115 }