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