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.client; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.HRegionLocation; 031import org.apache.hadoop.hbase.MetaRegionLocationCache; 032import org.apache.hadoop.hbase.MultithreadedTestUtil; 033import org.apache.hadoop.hbase.ServerName; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.master.HMaster; 036import org.apache.hadoop.hbase.master.RegionState; 037import org.apache.hadoop.hbase.security.User; 038import org.apache.hadoop.hbase.testclassification.MasterTests; 039import org.apache.hadoop.hbase.testclassification.SmallTests; 040import org.apache.hadoop.hbase.util.JVMClusterUtil; 041import org.apache.hadoop.hbase.zookeeper.MetaTableLocator; 042import org.apache.hadoop.hbase.zookeeper.ZKUtil; 043import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 044import org.apache.hadoop.hbase.zookeeper.ZNodePaths; 045import org.junit.jupiter.api.AfterAll; 046import org.junit.jupiter.api.BeforeAll; 047import org.junit.jupiter.api.Tag; 048import org.junit.jupiter.api.Test; 049 050import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 051 052@Tag(SmallTests.TAG) 053@Tag(MasterTests.TAG) 054public class TestMetaRegionLocationCache { 055 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 056 private static ConnectionRegistry REGISTRY; 057 058 @BeforeAll 059 public static void setUp() throws Exception { 060 TEST_UTIL.startMiniCluster(3); 061 HBaseTestingUtil.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3); 062 REGISTRY = ConnectionRegistryFactory.create(TEST_UTIL.getConfiguration(), User.getCurrent()); 063 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, REGISTRY); 064 TEST_UTIL.getAdmin().balancerSwitch(false, true); 065 } 066 067 @AfterAll 068 public static void cleanUp() throws Exception { 069 Closeables.close(REGISTRY, true); 070 TEST_UTIL.shutdownMiniCluster(); 071 } 072 073 private List<HRegionLocation> getCurrentMetaLocations(ZKWatcher zk) throws Exception { 074 List<HRegionLocation> result = new ArrayList<>(); 075 for (String znode : zk.getMetaReplicaNodes()) { 076 String path = ZNodePaths.joinZNode(zk.getZNodePaths().baseZNode, znode); 077 int replicaId = zk.getZNodePaths().getMetaReplicaIdFromPath(path); 078 RegionState state = MetaTableLocator.getMetaRegionState(zk, replicaId); 079 result.add(new HRegionLocation(state.getRegion(), state.getServerName())); 080 } 081 return result; 082 } 083 084 // Verifies that the cached meta locations in the given master are in sync with what is in ZK. 085 private void verifyCachedMetaLocations(HMaster master) throws Exception { 086 // Wait until initial meta locations are loaded. 087 int retries = 0; 088 while (master.getMetaRegionLocationCache().getMetaRegionLocations().isEmpty()) { 089 Thread.sleep(1000); 090 if (++retries == 10) { 091 break; 092 } 093 } 094 ZKWatcher zk = master.getZooKeeper(); 095 List<String> metaZnodes = zk.getMetaReplicaNodes(); 096 // Wait till all replicas available. 097 retries = 0; 098 while ( 099 master.getMetaRegionLocationCache().getMetaRegionLocations().size() != metaZnodes.size() 100 ) { 101 Thread.sleep(1000); 102 if (++retries == 10) { 103 break; 104 } 105 } 106 List<HRegionLocation> metaHRLs = master.getMetaRegionLocationCache().getMetaRegionLocations(); 107 assertFalse(metaHRLs.isEmpty()); 108 assertEquals(metaZnodes.size(), metaHRLs.size()); 109 List<HRegionLocation> actualHRLs = getCurrentMetaLocations(zk); 110 Collections.sort(metaHRLs); 111 Collections.sort(actualHRLs); 112 assertEquals(actualHRLs, metaHRLs); 113 } 114 115 @Test 116 public void testInitialMetaLocations() throws Exception { 117 verifyCachedMetaLocations(TEST_UTIL.getMiniHBaseCluster().getMaster()); 118 } 119 120 @Test 121 public void testStandByMetaLocations() throws Exception { 122 HMaster standBy = TEST_UTIL.getMiniHBaseCluster().startMaster().getMaster(); 123 verifyCachedMetaLocations(standBy); 124 } 125 126 /* 127 * Shuffles the meta region replicas around the cluster and makes sure the cache is not stale. 128 */ 129 @Test 130 public void testMetaLocationsChange() throws Exception { 131 List<HRegionLocation> currentMetaLocs = 132 getCurrentMetaLocations(TEST_UTIL.getMiniHBaseCluster().getMaster().getZooKeeper()); 133 // Move these replicas to random servers. 134 for (HRegionLocation location : currentMetaLocs) { 135 RegionReplicaTestHelper.moveRegion(TEST_UTIL, location); 136 } 137 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, REGISTRY); 138 for (JVMClusterUtil.MasterThread masterThread : TEST_UTIL.getMiniHBaseCluster() 139 .getMasterThreads()) { 140 verifyCachedMetaLocations(masterThread.getMaster()); 141 } 142 } 143 144 /** 145 * Tests MetaRegionLocationCache's init procedure to make sure that it correctly watches the base 146 * znode for notifications. 147 */ 148 @Test 149 public void testMetaRegionLocationCache() throws Exception { 150 final String parentZnodeName = "/randomznodename"; 151 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 152 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, parentZnodeName); 153 ServerName sn = ServerName.valueOf("localhost", 1234, 5678); 154 try (ZKWatcher zkWatcher = new ZKWatcher(conf, null, null, true)) { 155 // A thread that repeatedly creates and drops an unrelated child znode. This is to simulate 156 // some ZK activity in the background. 157 MultithreadedTestUtil.TestContext ctx = new MultithreadedTestUtil.TestContext(conf); 158 ctx.addThread(new MultithreadedTestUtil.RepeatingTestThread(ctx) { 159 @Override 160 public void doAnAction() throws Exception { 161 final String testZnode = parentZnodeName + "/child"; 162 ZKUtil.createNodeIfNotExistsAndWatch(zkWatcher, testZnode, testZnode.getBytes()); 163 ZKUtil.deleteNode(zkWatcher, testZnode); 164 } 165 }); 166 ctx.startThreads(); 167 try { 168 MetaRegionLocationCache metaCache = new MetaRegionLocationCache(zkWatcher); 169 // meta znodes do not exist at this point, cache should be empty. 170 assertTrue(metaCache.getMetaRegionLocations().isEmpty()); 171 // Set the meta locations for a random meta replicas, simulating an active hmaster meta 172 // assignment. 173 for (int i = 0; i < 3; i++) { 174 // Updates the meta znodes. 175 MetaTableLocator.setMetaLocation(zkWatcher, sn, i, RegionState.State.OPEN); 176 } 177 // Wait until the meta cache is populated. 178 int iters = 0; 179 while (iters++ < 10) { 180 if (metaCache.getMetaRegionLocations().size() == 3) { 181 break; 182 } 183 Thread.sleep(1000); 184 } 185 List<HRegionLocation> metaLocations = metaCache.getMetaRegionLocations(); 186 assertEquals(3, metaLocations.size()); 187 for (HRegionLocation location : metaLocations) { 188 assertEquals(sn, location.getServerName()); 189 } 190 } finally { 191 // clean up. 192 ctx.stop(); 193 ZKUtil.deleteChildrenRecursively(zkWatcher, parentZnodeName); 194 } 195 } 196 } 197}