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