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