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