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;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.MultithreadedTestUtil.TestContext;
024import org.apache.hadoop.hbase.MultithreadedTestUtil.TestThread;
025import org.apache.hadoop.hbase.master.CachedClusterId;
026import org.apache.hadoop.hbase.master.HMaster;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.junit.AfterClass;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category(MediumTests.class)
035public class TestCachedClusterId {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestCachedClusterId.class);
039
040  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
041
042  private static String clusterId;
043  private static HMaster activeMaster;
044  private static HMaster standByMaster;
045
046  private static class GetClusterIdThread extends TestThread {
047    CachedClusterId cachedClusterId;
048
049    public GetClusterIdThread(TestContext ctx, CachedClusterId clusterId) {
050      super(ctx);
051      cachedClusterId = clusterId;
052    }
053
054    @Override
055    public void doWork() throws Exception {
056      assertEquals(clusterId, cachedClusterId.getFromCacheOrFetch());
057    }
058  }
059
060  @BeforeClass
061  public static void setUp() throws Exception {
062    TEST_UTIL.startMiniCluster(1);
063    activeMaster = TEST_UTIL.getHBaseCluster().getMaster();
064    clusterId = activeMaster.getClusterId();
065    standByMaster = TEST_UTIL.getHBaseCluster().startMaster().getMaster();
066  }
067
068  @AfterClass
069  public static void tearDown() throws Exception {
070    TEST_UTIL.shutdownMiniCluster();
071  }
072
073  @Test
074  public void testClusterIdMatch() {
075    assertEquals(clusterId, standByMaster.getClusterId());
076  }
077
078  @Test
079  public void testMultiThreadedGetClusterId() throws Exception {
080    Configuration conf = TEST_UTIL.getConfiguration();
081    CachedClusterId cachedClusterId =
082      new CachedClusterId(TEST_UTIL.getHBaseCluster().getMaster(), conf);
083    TestContext context = new TestContext(conf);
084    int numThreads = 16;
085    for (int i = 0; i < numThreads; i++) {
086      context.addThread(new GetClusterIdThread(context, cachedClusterId));
087    }
088    context.startThreads();
089    context.stop();
090    int cacheMisses = cachedClusterId.getCacheStats();
091    assertEquals(cacheMisses, 1);
092  }
093}