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.regionserver;
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.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.junit.AfterClass;
030import org.junit.Assert;
031import org.junit.BeforeClass;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036/**
037 * Tests to validate if HRegionServer default chores are scheduled
038 */
039@Category({RegionServerTests.class, MediumTests.class})
040public class TestRSChoresScheduled {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestRSChoresScheduled.class);
045
046  private static HRegionServer hRegionServer;
047
048  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
049
050  @BeforeClass
051  public static void setUp() throws Exception {
052    UTIL.startMiniCluster(StartMiniClusterOption.builder().numRegionServers(1).build());
053    hRegionServer = UTIL.getMiniHBaseCluster().getRegionServer(0);
054  }
055
056  @AfterClass
057  public static void tearDown() throws Exception {
058    UTIL.shutdownMiniCluster();
059  }
060
061  private static class TestChoreField<E extends ScheduledChore> {
062
063    private E getChoreObj(String fieldName) throws NoSuchFieldException,
064      IllegalAccessException {
065      Field hRegionServerField = HRegionServer.class.getDeclaredField(fieldName);
066      hRegionServerField.setAccessible(true);
067      E choreFieldVal = (E) hRegionServerField.get(hRegionServer);
068      return choreFieldVal;
069    }
070
071    private void testIfChoreScheduled(E choreObj) {
072      Assert.assertNotNull(choreObj);
073      Assert.assertTrue(hRegionServer.getChoreService().isChoreScheduled(choreObj));
074    }
075
076  }
077
078  @Test
079  public void testDefaultScheduledChores() throws Exception {
080    // test if compactedHFilesDischarger chore is scheduled by default in HRegionServer init
081    TestChoreField<CompactedHFilesDischarger> compactedHFilesDischargerTestChoreField =
082      new TestChoreField<>();
083    CompactedHFilesDischarger compactedHFilesDischarger =
084      compactedHFilesDischargerTestChoreField.getChoreObj("compactedFileDischarger");
085    compactedHFilesDischargerTestChoreField.testIfChoreScheduled(compactedHFilesDischarger);
086
087    // test if compactionChecker chore is scheduled by default in HRegionServer init
088    TestChoreField<ScheduledChore> compactionCheckerTestChoreField = new TestChoreField<>();
089    ScheduledChore compactionChecker =
090      compactionCheckerTestChoreField.getChoreObj("compactionChecker");
091    compactionCheckerTestChoreField.testIfChoreScheduled(compactionChecker);
092
093    // test if periodicFlusher chore is scheduled by default in HRegionServer init
094    TestChoreField<ScheduledChore> periodicMemstoreFlusherTestChoreField =
095      new TestChoreField<>();
096    ScheduledChore periodicFlusher =
097      periodicMemstoreFlusherTestChoreField.getChoreObj("periodicFlusher");
098    periodicMemstoreFlusherTestChoreField.testIfChoreScheduled(periodicFlusher);
099
100    // test if nonceManager chore is scheduled by default in HRegionServer init
101    TestChoreField<ScheduledChore> nonceManagerTestChoreField = new TestChoreField<>();
102    ScheduledChore nonceManagerChore =
103      nonceManagerTestChoreField.getChoreObj("nonceManagerChore");
104    nonceManagerTestChoreField.testIfChoreScheduled(nonceManagerChore);
105
106  }
107
108}