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.master; 019 020import java.lang.reflect.Field; 021import java.util.ArrayList; 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.master.balancer.BalancerChore; 027import org.apache.hadoop.hbase.master.balancer.ClusterStatusChore; 028import org.apache.hadoop.hbase.master.cleaner.HFileCleaner; 029import org.apache.hadoop.hbase.master.cleaner.LogCleaner; 030import org.apache.hadoop.hbase.master.cleaner.ReplicationBarrierCleaner; 031import org.apache.hadoop.hbase.master.hbck.HbckChore; 032import org.apache.hadoop.hbase.master.janitor.CatalogJanitor; 033import org.apache.hadoop.hbase.testclassification.MasterTests; 034import org.apache.hadoop.hbase.testclassification.MediumTests; 035import org.junit.AfterClass; 036import org.junit.Assert; 037import org.junit.BeforeClass; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042/** 043 * Tests to validate if HMaster default chores are scheduled 044 */ 045@Category({ MasterTests.class, MediumTests.class }) 046public class TestMasterChoreScheduled { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestMasterChoreScheduled.class); 051 052 private static HMaster hMaster; 053 054 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 055 056 @BeforeClass 057 public static void setUp() throws Exception { 058 UTIL.startMiniCluster(StartTestingClusterOption.builder().numRegionServers(1).build()); 059 hMaster = UTIL.getMiniHBaseCluster().getMaster(); 060 } 061 062 @AfterClass 063 public static void tearDown() throws Exception { 064 UTIL.shutdownMiniCluster(); 065 } 066 067 @Test 068 public void testDefaultScheduledChores() throws Exception { 069 // test if logCleaner chore is scheduled by default in HMaster init 070 TestChoreField<LogCleaner> logCleanerTestChoreField = new TestChoreField<>(); 071 LogCleaner logCleaner = logCleanerTestChoreField.getChoreObj("logCleaner"); 072 logCleanerTestChoreField.testIfChoreScheduled(logCleaner); 073 074 // test if hfileCleaner chore is scheduled by default in HMaster init 075 TestChoreField<HFileCleaner> hFileCleanerTestChoreField = new TestChoreField<>(); 076 Field masterField = HMaster.class.getDeclaredField("hfileCleaners"); 077 masterField.setAccessible(true); 078 HFileCleaner hFileCleaner = ((ArrayList<HFileCleaner>) masterField.get(hMaster)).get(0); 079 hFileCleanerTestChoreField.testIfChoreScheduled(hFileCleaner); 080 081 // test if replicationBarrierCleaner chore is scheduled by default in HMaster init 082 TestChoreField<ReplicationBarrierCleaner> replicationBarrierCleanerTestChoreField = 083 new TestChoreField<>(); 084 ReplicationBarrierCleaner replicationBarrierCleaner = 085 replicationBarrierCleanerTestChoreField.getChoreObj("replicationBarrierCleaner"); 086 replicationBarrierCleanerTestChoreField.testIfChoreScheduled(replicationBarrierCleaner); 087 088 // test if clusterStatusChore chore is scheduled by default in HMaster init 089 TestChoreField<ClusterStatusChore> clusterStatusChoreTestChoreField = new TestChoreField<>(); 090 ClusterStatusChore clusterStatusChore = 091 clusterStatusChoreTestChoreField.getChoreObj("clusterStatusChore"); 092 clusterStatusChoreTestChoreField.testIfChoreScheduled(clusterStatusChore); 093 094 // test if balancerChore chore is scheduled by default in HMaster init 095 TestChoreField<BalancerChore> balancerChoreTestChoreField = new TestChoreField<>(); 096 BalancerChore balancerChore = balancerChoreTestChoreField.getChoreObj("balancerChore"); 097 balancerChoreTestChoreField.testIfChoreScheduled(balancerChore); 098 099 // test if normalizerChore chore is scheduled by default in HMaster init 100 ScheduledChore regionNormalizerChore = 101 hMaster.getRegionNormalizerManager().getRegionNormalizerChore(); 102 TestChoreField<ScheduledChore> regionNormalizerChoreTestChoreField = new TestChoreField<>(); 103 regionNormalizerChoreTestChoreField.testIfChoreScheduled(regionNormalizerChore); 104 105 // test if catalogJanitorChore chore is scheduled by default in HMaster init 106 TestChoreField<CatalogJanitor> catalogJanitorTestChoreField = new TestChoreField<>(); 107 CatalogJanitor catalogJanitor = catalogJanitorTestChoreField.getChoreObj("catalogJanitorChore"); 108 catalogJanitorTestChoreField.testIfChoreScheduled(catalogJanitor); 109 110 // test if hbckChore chore is scheduled by default in HMaster init 111 TestChoreField<HbckChore> hbckChoreTestChoreField = new TestChoreField<>(); 112 HbckChore hbckChore = hbckChoreTestChoreField.getChoreObj("hbckChore"); 113 hbckChoreTestChoreField.testIfChoreScheduled(hbckChore); 114 } 115 116 /** 117 * Reflect into the {@link HMaster} instance and find by field name a specified instance of 118 * {@link ScheduledChore}. 119 */ 120 private static class TestChoreField<E extends ScheduledChore> { 121 122 @SuppressWarnings("unchecked") 123 private E getChoreObj(String fieldName) { 124 try { 125 Field masterField = HMaster.class.getDeclaredField(fieldName); 126 masterField.setAccessible(true); 127 return (E) masterField.get(hMaster); 128 } catch (Exception e) { 129 throw new AssertionError( 130 "Unable to retrieve field '" + fieldName + "' from HMaster instance.", e); 131 } 132 } 133 134 private void testIfChoreScheduled(E choreObj) { 135 Assert.assertNotNull(choreObj); 136 Assert.assertTrue(hMaster.getChoreService().isChoreScheduled(choreObj)); 137 } 138 } 139}