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