HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestMasterMetricsWrapper.java
blob1241e261332f9e072ae7bb808a0503863a65b2b5
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
23 import java.util.AbstractMap.SimpleImmutableEntry;
24 import java.util.List;
25 import org.apache.hadoop.hbase.HBaseClassTestRule;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
29 import org.apache.hadoop.hbase.client.RegionInfo;
30 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
31 import org.apache.hadoop.hbase.master.assignment.RegionStates;
32 import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
33 import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
34 import org.apache.hadoop.hbase.quotas.SpaceViolationPolicy;
35 import org.apache.hadoop.hbase.testclassification.MasterTests;
36 import org.apache.hadoop.hbase.testclassification.MediumTests;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.apache.hadoop.hbase.util.PairOfSameType;
39 import org.apache.hadoop.hbase.util.Threads;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.ClassRule;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 @Category({MasterTests.class, MediumTests.class})
49 public class TestMasterMetricsWrapper {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestMasterMetricsWrapper.class);
55 private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetricsWrapper.class);
57 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
58 private static final int NUM_RS = 4;
60 @BeforeClass
61 public static void setup() throws Exception {
62 TEST_UTIL.startMiniCluster(NUM_RS);
65 @AfterClass
66 public static void teardown() throws Exception {
67 TEST_UTIL.shutdownMiniCluster();
70 @Test
71 public void testInfo() {
72 HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
73 MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
74 assertEquals(master.getSplitPlanCount(), info.getSplitPlanCount(), 0);
75 assertEquals(master.getMergePlanCount(), info.getMergePlanCount(), 0);
76 assertEquals(master.getAverageLoad(), info.getAverageLoad(), 0);
77 assertEquals(master.getClusterId(), info.getClusterId());
78 assertEquals(master.getMasterActiveTime(), info.getActiveTime());
79 assertEquals(master.getMasterStartTime(), info.getStartTime());
80 assertEquals(master.getMasterCoprocessors().length, info.getCoprocessors().length);
81 assertEquals(master.getServerManager().getOnlineServersList().size(), info.getNumRegionServers());
82 int regionServerCount =
83 NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 1: 0);
84 assertEquals(regionServerCount, info.getNumRegionServers());
86 String zkServers = info.getZookeeperQuorum();
87 assertEquals(zkServers.split(",").length, TEST_UTIL.getZkCluster().getZooKeeperServerNum());
89 final int index = 3;
90 LOG.info("Stopping " + TEST_UTIL.getMiniHBaseCluster().getRegionServer(index));
91 TEST_UTIL.getMiniHBaseCluster().stopRegionServer(index, false);
92 TEST_UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
93 // We stopped the regionserver but could take a while for the master to notice it so hang here
94 // until it does... then move forward to see if metrics wrapper notices.
95 while (TEST_UTIL.getHBaseCluster().getMaster().getServerManager().getOnlineServers().size() ==
96 regionServerCount ) {
97 Threads.sleep(10);
99 assertEquals(regionServerCount - 1, info.getNumRegionServers());
100 assertEquals(1, info.getNumDeadRegionServers());
101 // now we do not expose this information as WALProcedureStore is not the only ProcedureStore
102 // implementation any more.
103 assertEquals(0, info.getNumWALFiles());
106 @Test
107 public void testQuotaSnapshotConversion() {
108 MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(
109 TEST_UTIL.getHBaseCluster().getMaster());
110 assertEquals(new SimpleImmutableEntry<Long,Long>(1024L, 2048L),
111 info.convertSnapshot(new SpaceQuotaSnapshot(
112 SpaceQuotaStatus.notInViolation(), 1024L, 2048L)));
113 assertEquals(new SimpleImmutableEntry<Long,Long>(4096L, 2048L),
114 info.convertSnapshot(new SpaceQuotaSnapshot(
115 new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 2048L)));
119 * tests online and offline region number
121 @Test
122 public void testOfflineRegion() throws Exception {
123 HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
124 MetricsMasterWrapperImpl info = new MetricsMasterWrapperImpl(master);
125 TableName table = TableName.valueOf("testRegionNumber");
126 try {
127 RegionInfo hri;
128 TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
129 new TableDescriptorBuilder.ModifyableTableDescriptor(table);
131 byte[] FAMILY = Bytes.toBytes("FAMILY");
132 tableDescriptor.setColumnFamily(
133 new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY));
134 TEST_UTIL.getAdmin().createTable(tableDescriptor, Bytes.toBytes("A"),
135 Bytes.toBytes("Z"), 5);
137 // wait till the table is assigned
138 long timeoutTime = System.currentTimeMillis() + 1000;
139 while (true) {
140 List<RegionInfo> regions = master.getAssignmentManager().
141 getRegionStates().getRegionsOfTable(table);
142 if (regions.size() > 3) {
143 hri = regions.get(2);
144 break;
146 long now = System.currentTimeMillis();
147 if (now > timeoutTime) {
148 fail("Could not find an online region");
150 Thread.sleep(10);
153 PairOfSameType<Integer> regionNumberPair = info.getRegionCounts();
154 assertEquals(5, regionNumberPair.getFirst().intValue());
155 assertEquals(0, regionNumberPair.getSecond().intValue());
157 TEST_UTIL.getAdmin().offline(hri.getRegionName());
159 timeoutTime = System.currentTimeMillis() + 800;
160 RegionStates regionStates = master.getAssignmentManager().getRegionStates();
161 while (true) {
162 if (regionStates.getRegionByStateOfTable(table)
163 .get(RegionState.State.OFFLINE).contains(hri)) {
164 break;
166 long now = System.currentTimeMillis();
167 if (now > timeoutTime) {
168 fail("Failed to offline the region in time");
169 break;
171 Thread.sleep(10);
173 regionNumberPair = info.getRegionCounts();
174 assertEquals(4, regionNumberPair.getFirst().intValue());
175 assertEquals(1, regionNumberPair.getSecond().intValue());
176 } finally {
177 TEST_UTIL.deleteTable(table);