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