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.lang.reflect.Field;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.ScheduledChore;
026import org.apache.hadoop.hbase.StartMiniClusterOption;
027import org.apache.hadoop.hbase.master.balancer.BalancerChore;
028import org.apache.hadoop.hbase.master.balancer.ClusterStatusChore;
029import org.apache.hadoop.hbase.master.cleaner.HFileCleaner;
030import org.apache.hadoop.hbase.master.cleaner.LogCleaner;
031import org.apache.hadoop.hbase.master.cleaner.ReplicationBarrierCleaner;
032import org.apache.hadoop.hbase.master.normalizer.RegionNormalizerChore;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.junit.AfterClass;
036import org.junit.Assert;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042/**
043 * Tests to validate if HMaster default chores are scheduled
044 */
045@Category({MasterTests.class, MediumTests.class})
046public class TestMasterChoreScheduled {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestMasterChoreScheduled.class);
051
052  private static HMaster hMaster;
053
054  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
055
056  @BeforeClass
057  public static void setUp() throws Exception {
058    UTIL.startMiniCluster(StartMiniClusterOption.builder().numRegionServers(1).build());
059    hMaster = UTIL.getMiniHBaseCluster().getMaster();
060  }
061
062  @AfterClass
063  public static void tearDown() throws Exception {
064    UTIL.shutdownMiniCluster();
065  }
066
067  @Test
068  public void testDefaultScheduledChores() throws Exception {
069    // test if logCleaner chore is scheduled by default in HMaster init
070    TestChoreField<LogCleaner> logCleanerTestChoreField = new TestChoreField<>();
071    LogCleaner logCleaner = logCleanerTestChoreField.getChoreObj("logCleaner");
072    logCleanerTestChoreField.testIfChoreScheduled(logCleaner);
073
074    // test if hfileCleaner chore is scheduled by default in HMaster init
075    TestChoreField<HFileCleaner> hFileCleanerTestChoreField = new TestChoreField<>();
076    HFileCleaner hFileCleaner = hFileCleanerTestChoreField.getChoreObj("hfileCleaner");
077    hFileCleanerTestChoreField.testIfChoreScheduled(hFileCleaner);
078
079    // test if replicationBarrierCleaner chore is scheduled by default in HMaster init
080    TestChoreField<ReplicationBarrierCleaner> replicationBarrierCleanerTestChoreField =
081      new TestChoreField<>();
082    ReplicationBarrierCleaner replicationBarrierCleaner =
083      replicationBarrierCleanerTestChoreField.getChoreObj("replicationBarrierCleaner");
084    replicationBarrierCleanerTestChoreField.testIfChoreScheduled(replicationBarrierCleaner);
085
086    // test if clusterStatusChore chore is scheduled by default in HMaster init
087    TestChoreField<ClusterStatusChore> clusterStatusChoreTestChoreField = new TestChoreField<>();
088    ClusterStatusChore clusterStatusChore = clusterStatusChoreTestChoreField
089      .getChoreObj("clusterStatusChore");
090    clusterStatusChoreTestChoreField.testIfChoreScheduled(clusterStatusChore);
091
092    // test if balancerChore chore is scheduled by default in HMaster init
093    TestChoreField<BalancerChore> balancerChoreTestChoreField = new TestChoreField<>();
094    BalancerChore balancerChore = balancerChoreTestChoreField.getChoreObj("balancerChore");
095    balancerChoreTestChoreField.testIfChoreScheduled(balancerChore);
096
097    // test if normalizerChore chore is scheduled by default in HMaster init
098    TestChoreField<RegionNormalizerChore> regionNormalizerChoreTestChoreField =
099      new TestChoreField<>();
100    RegionNormalizerChore regionNormalizerChore = regionNormalizerChoreTestChoreField
101      .getChoreObj("normalizerChore");
102    regionNormalizerChoreTestChoreField.testIfChoreScheduled(regionNormalizerChore);
103
104    // test if catalogJanitorChore chore is scheduled by default in HMaster init
105    TestChoreField<CatalogJanitor> catalogJanitorTestChoreField = new TestChoreField<>();
106    CatalogJanitor catalogJanitor = catalogJanitorTestChoreField
107      .getChoreObj("catalogJanitorChore");
108    catalogJanitorTestChoreField.testIfChoreScheduled(catalogJanitor);
109
110    // test if hbckChore chore is scheduled by default in HMaster init
111    TestChoreField<HbckChore> hbckChoreTestChoreField = new TestChoreField<>();
112    HbckChore hbckChore = hbckChoreTestChoreField.getChoreObj("hbckChore");
113    hbckChoreTestChoreField.testIfChoreScheduled(hbckChore);
114  }
115
116
117  private static class TestChoreField<E extends ScheduledChore> {
118
119    private E getChoreObj(String fieldName) throws NoSuchFieldException,
120        IllegalAccessException {
121      Field masterField = HMaster.class.getDeclaredField(fieldName);
122      masterField.setAccessible(true);
123      E choreFieldVal = (E) masterField.get(hMaster);
124      return choreFieldVal;
125    }
126
127    private void testIfChoreScheduled(E choreObj) {
128      Assert.assertNotNull(choreObj);
129      Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(choreObj));
130    }
131
132  }
133
134}