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.jupiter.api.Assertions.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.apache.hadoop.hbase.testclassification.MiscTests; 029import org.junit.jupiter.api.AfterAll; 030import org.junit.jupiter.api.BeforeAll; 031import org.junit.jupiter.api.Tag; 032import org.junit.jupiter.api.Test; 033 034@Tag(MiscTests.TAG) 035@Tag(MediumTests.TAG) 036public class TestCachedClusterId { 037 038 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 039 040 private static String clusterId; 041 private static HMaster activeMaster; 042 private static HMaster standByMaster; 043 044 private static class GetClusterIdThread extends TestThread { 045 CachedClusterId cachedClusterId; 046 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 @BeforeAll 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 @AfterAll 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 = 080 new CachedClusterId(TEST_UTIL.getHBaseCluster().getMaster(), 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}