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