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 HBaseTestingUtility 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 HBaseTestingUtility();
063    zkCluster = UTIL.startMiniZKCluster();
064    StartMiniClusterOption option = StartMiniClusterOption.builder().numRegionServers(2).build();
065    MiniHBaseCluster cluster = UTIL.startMiniHBaseCluster(option);
066    source = cluster.getRegionServer(0);
067    dest = cluster.getRegionServer(1);
068    assertEquals(2, cluster.getRegionServerThreads().size());
069    TableName tableName = TableName.valueOf(name.getMethodName());
070    UTIL.createTable(tableName, Bytes.toBytes("cf"));
071    UTIL.waitTableAvailable(tableName, 30_000);
072    movedRegionInfo = Iterables.getOnlyElement(cluster.getRegions(tableName)).getRegionInfo();
073    UTIL.getAdmin().move(movedRegionInfo.getEncodedNameAsBytes(), source.getServerName());
074    UTIL.waitFor(2000, new Waiter.Predicate<IOException>() {
075      @Override
076      public boolean evaluate() throws IOException {
077        return source.getOnlineRegion(movedRegionInfo.getRegionName()) != null;
078      }
079    });
080  }
081
082  @After
083  public void after() throws Exception {
084    UTIL.shutdownMiniCluster();
085    if (zkCluster != null) {
086      zkCluster.shutdown();
087    }
088  }
089
090  @Test
091  public void testMovedRegionsCache() throws IOException, InterruptedException {
092    UTIL.getAdmin().move(movedRegionInfo.getEncodedNameAsBytes(), dest.getServerName());
093    UTIL.waitFor(2000, new Waiter.Predicate<IOException>() {
094      @Override
095      public boolean evaluate() throws IOException {
096        return dest.getOnlineRegion(movedRegionInfo.getRegionName()) != null;
097      }
098    });
099    assertNotNull("Moved region NOT in the cache!",
100      source.getMovedRegion(movedRegionInfo.getEncodedName()));
101    Thread.sleep(source.movedRegionCacheExpiredTime());
102    assertNull("Expired moved region exist in the cache!",
103      source.getMovedRegion(movedRegionInfo.getEncodedName()));
104  }
105}