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 static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertNull; 023 024import java.io.IOException; 025import org.apache.hadoop.hbase.client.RegionInfo; 026import org.apache.hadoop.hbase.regionserver.HRegionServer; 027import org.apache.hadoop.hbase.testclassification.MediumTests; 028import org.apache.hadoop.hbase.testclassification.MiscTests; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 031import org.junit.After; 032import org.junit.Before; 033import org.junit.ClassRule; 034import org.junit.Rule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037import org.junit.rules.TestName; 038 039import org.apache.hbase.thirdparty.com.google.common.collect.Iterables; 040 041/** 042 * Test whether moved region cache is correct 043 */ 044@Category({ MiscTests.class, MediumTests.class }) 045public class TestMovedRegionCache { 046 047 @ClassRule 048 public static final HBaseClassTestRule CLASS_RULE = 049 HBaseClassTestRule.forClass(TestMovedRegionCache.class); 050 051 @Rule 052 public TestName name = new TestName(); 053 054 private HBaseTestingUtil UTIL; 055 private MiniZooKeeperCluster zkCluster; 056 private HRegionServer source; 057 private HRegionServer dest; 058 private RegionInfo movedRegionInfo; 059 060 @Before 061 public void setup() throws Exception { 062 UTIL = new HBaseTestingUtil(); 063 zkCluster = UTIL.startMiniZKCluster(); 064 StartTestingClusterOption option = 065 StartTestingClusterOption.builder().numRegionServers(2).build(); 066 SingleProcessHBaseCluster cluster = UTIL.startMiniHBaseCluster(option); 067 source = cluster.getRegionServer(0); 068 dest = cluster.getRegionServer(1); 069 assertEquals(2, cluster.getRegionServerThreads().size()); 070 TableName tableName = TableName.valueOf(name.getMethodName()); 071 UTIL.createTable(tableName, Bytes.toBytes("cf")); 072 UTIL.waitTableAvailable(tableName, 30_000); 073 movedRegionInfo = Iterables.getOnlyElement(cluster.getRegions(tableName)).getRegionInfo(); 074 UTIL.getAdmin().move(movedRegionInfo.getEncodedNameAsBytes(), source.getServerName()); 075 UTIL.waitFor(2000, new Waiter.Predicate<IOException>() { 076 @Override 077 public boolean evaluate() throws IOException { 078 return source.getOnlineRegion(movedRegionInfo.getRegionName()) != null; 079 } 080 }); 081 } 082 083 @After 084 public void after() throws Exception { 085 UTIL.shutdownMiniCluster(); 086 if (zkCluster != null) { 087 zkCluster.shutdown(); 088 } 089 } 090 091 @Test 092 public void testMovedRegionsCache() throws IOException, InterruptedException { 093 UTIL.getAdmin().move(movedRegionInfo.getEncodedNameAsBytes(), dest.getServerName()); 094 UTIL.waitFor(2000, new Waiter.Predicate<IOException>() { 095 @Override 096 public boolean evaluate() throws IOException { 097 return dest.getOnlineRegion(movedRegionInfo.getRegionName()) != null; 098 } 099 }); 100 assertNotNull("Moved region NOT in the cache!", 101 source.getMovedRegion(movedRegionInfo.getEncodedName())); 102 Thread.sleep(source.movedRegionCacheExpiredTime()); 103 assertNull("Expired moved region exist in the cache!", 104 source.getMovedRegion(movedRegionInfo.getEncodedName())); 105 } 106}