HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestCachedClusterId.java
blob932cb3bb7e15a530db1fa79841b3b6e59b26a05e
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;
20 import static org.junit.Assert.assertEquals;
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext;
23 import org.apache.hadoop.hbase.MultithreadedTestUtil.TestThread;
24 import org.apache.hadoop.hbase.master.CachedClusterId;
25 import org.apache.hadoop.hbase.master.HMaster;
26 import org.apache.hadoop.hbase.testclassification.MediumTests;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.ClassRule;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
33 @Category(MediumTests.class)
34 public class TestCachedClusterId {
35 @ClassRule
36 public static final HBaseClassTestRule CLASS_RULE =
37 HBaseClassTestRule.forClass(TestCachedClusterId.class);
39 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
41 private static String clusterId;
42 private static HMaster activeMaster;
43 private static HMaster standByMaster;
45 private static class GetClusterIdThread extends TestThread {
46 CachedClusterId cachedClusterId;
47 public GetClusterIdThread(TestContext ctx, CachedClusterId clusterId) {
48 super(ctx);
49 cachedClusterId = clusterId;
52 @Override
53 public void doWork() throws Exception {
54 assertEquals(clusterId, cachedClusterId.getFromCacheOrFetch());
58 @BeforeClass
59 public static void setUp() throws Exception {
60 TEST_UTIL.startMiniCluster(1);
61 activeMaster = TEST_UTIL.getHBaseCluster().getMaster();
62 clusterId = activeMaster.getClusterId();
63 standByMaster = TEST_UTIL.getHBaseCluster().startMaster().getMaster();
66 @AfterClass
67 public static void tearDown() throws Exception {
68 TEST_UTIL.shutdownMiniCluster();
71 @Test
72 public void testClusterIdMatch() {
73 assertEquals(clusterId, standByMaster.getClusterId());
76 @Test
77 public void testMultiThreadedGetClusterId() throws Exception {
78 Configuration conf = TEST_UTIL.getConfiguration();
79 CachedClusterId cachedClusterId = new CachedClusterId(conf);
80 TestContext context = new TestContext(conf);
81 int numThreads = 100;
82 for (int i = 0; i < numThreads; i++) {
83 context.addThread(new GetClusterIdThread(context, cachedClusterId));
85 context.startThreads();
86 context.stop();
87 int cacheMisses = cachedClusterId.getCacheStats();
88 assertEquals(cacheMisses, 1);