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  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  }