View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.lang.reflect.Field;
22  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.ScheduledChore;
25  import org.apache.hadoop.hbase.testclassification.MediumTests;
26  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
27  import org.junit.AfterClass;
28  import org.junit.Assert;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  /**
34   * Tests to validate if HRegionServer default chores are scheduled
35   */
36  @Category({RegionServerTests.class, MediumTests.class})
37  public class TestRSChoresScheduled {
38  
39    private static HRegionServer hRegionServer;
40  
41    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
42  
43    @BeforeClass
44    public static void setUp() throws Exception {
45      UTIL.startMiniCluster(1, 1);
46      hRegionServer = UTIL.getMiniHBaseCluster().getRegionServer(0);
47    }
48  
49    @AfterClass
50    public static void tearDown() throws Exception {
51      UTIL.shutdownMiniCluster();
52    }
53  
54    private static class TestChoreField<E extends ScheduledChore> {
55  
56      private E getChoreObj(String fieldName) throws NoSuchFieldException,
57        IllegalAccessException {
58        Field hRegionServerField = HRegionServer.class.getDeclaredField(fieldName);
59        hRegionServerField.setAccessible(true);
60        E choreFieldVal = (E) hRegionServerField.get(hRegionServer);
61        return choreFieldVal;
62      }
63  
64      private void testIfChoreScheduled(E choreObj) {
65        Assert.assertNotNull(choreObj);
66        Assert.assertTrue(hRegionServer.getChoreService().isChoreScheduled(choreObj));
67      }
68  
69    }
70  
71    @Test
72    public void testDefaultScheduledChores() throws Exception {
73      // test if movedRegionsCleaner chore is scheduled by default in HRegionServer init
74      TestChoreField<HRegionServer.MovedRegionsCleaner> movedRegionsCleanerTestChoreField =
75        new TestChoreField<>();
76      HRegionServer.MovedRegionsCleaner movedRegionsCleaner = movedRegionsCleanerTestChoreField
77        .getChoreObj("movedRegionsCleaner");
78      movedRegionsCleanerTestChoreField.testIfChoreScheduled(movedRegionsCleaner);
79  
80      // test if compactedHFilesDischarger chore is scheduled by default in HRegionServer init
81      TestChoreField<CompactedHFilesDischarger> compactedHFilesDischargerTestChoreField =
82        new TestChoreField<>();
83      CompactedHFilesDischarger compactedHFilesDischarger =
84        compactedHFilesDischargerTestChoreField.getChoreObj("compactedFileDischarger");
85      compactedHFilesDischargerTestChoreField.testIfChoreScheduled(compactedHFilesDischarger);
86  
87      // test if compactionChecker chore is scheduled by default in HRegionServer init
88      TestChoreField<ScheduledChore> compactionCheckerTestChoreField = new TestChoreField<>();
89      ScheduledChore compactionChecker =
90        compactionCheckerTestChoreField.getChoreObj("compactionChecker");
91      compactionCheckerTestChoreField.testIfChoreScheduled(compactionChecker);
92  
93      // test if periodicFlusher chore is scheduled by default in HRegionServer init
94      TestChoreField<ScheduledChore> periodicMemstoreFlusherTestChoreField =
95        new TestChoreField<>();
96      ScheduledChore periodicFlusher =
97        periodicMemstoreFlusherTestChoreField.getChoreObj("periodicFlusher");
98      periodicMemstoreFlusherTestChoreField.testIfChoreScheduled(periodicFlusher);
99  
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 }