1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
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.Test;
30 import org.junit.experimental.categories.Category;
31
32 @Category(MediumTests.class)
33 public class TestCachedClusterId {
34
35 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
36
37 private static String clusterId;
38 private static HMaster activeMaster;
39 private static HMaster standByMaster;
40
41 private static class GetClusterIdThread extends TestThread {
42 CachedClusterId cachedClusterId;
43 public GetClusterIdThread(TestContext ctx, CachedClusterId clusterId) {
44 super(ctx);
45 cachedClusterId = clusterId;
46 }
47
48 @Override
49 public void doWork() throws Exception {
50 assertEquals(clusterId, cachedClusterId.getFromCacheOrFetch());
51 }
52 }
53
54 @BeforeClass
55 public static void setUp() throws Exception {
56 TEST_UTIL.startMiniCluster(1);
57 activeMaster = TEST_UTIL.getHBaseCluster().getMaster();
58 clusterId = activeMaster.getClusterId();
59 standByMaster = TEST_UTIL.getHBaseCluster().startMaster().getMaster();
60 }
61
62 @AfterClass
63 public static void tearDown() throws Exception {
64 TEST_UTIL.shutdownMiniCluster();
65 }
66
67 @Test
68 public void testClusterIdMatch() {
69 assertEquals(clusterId, standByMaster.getClusterId());
70 }
71
72 @Test
73 public void testMultiThreadedGetClusterId() throws Exception {
74 Configuration conf = TEST_UTIL.getConfiguration();
75 CachedClusterId cachedClusterId = new CachedClusterId(conf);
76 TestContext context = new TestContext(conf);
77 int numThreads = 100;
78 for (int i = 0; i < numThreads; i++) {
79 context.addThread(new GetClusterIdThread(context, cachedClusterId));
80 }
81 context.startThreads();
82 context.stop();
83 int cacheMisses = cachedClusterId.getCacheStats();
84 assertEquals(cacheMisses, 1);
85 }
86 }