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.apache.hadoop.hbase.client.RegionReplicaTestHelper.testLocator;
021
022import org.apache.commons.io.IOUtils;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.HRegionLocation;
027import org.apache.hadoop.hbase.RegionLocations;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.RegionReplicaTestHelper.Locator;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038@Category({ MediumTests.class, ClientTests.class })
039public class TestAsyncMetaRegionLocator {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestAsyncMetaRegionLocator.class);
044
045  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
046
047  private static ConnectionRegistry REGISTRY;
048
049  private static AsyncMetaRegionLocator LOCATOR;
050
051  @BeforeClass
052  public static void setUp() throws Exception {
053    TEST_UTIL.getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 3);
054    TEST_UTIL.startMiniCluster(3);
055    TEST_UTIL.waitUntilNoRegionsInTransition();
056    REGISTRY = ConnectionRegistryFactory.getRegistry(TEST_UTIL.getConfiguration());
057    RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, REGISTRY);
058    TEST_UTIL.getAdmin().balancerSwitch(false, true);
059    LOCATOR = new AsyncMetaRegionLocator(REGISTRY);
060  }
061
062  @AfterClass
063  public static void tearDown() throws Exception {
064    IOUtils.closeQuietly(REGISTRY);
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void test() throws Exception {
070    testLocator(TEST_UTIL, TableName.META_TABLE_NAME, new Locator() {
071
072      @Override
073      public void updateCachedLocationOnError(HRegionLocation loc, Throwable error)
074          throws Exception {
075        LOCATOR.updateCachedLocationOnError(loc, error);
076      }
077
078      @Override
079      public RegionLocations getRegionLocations(TableName tableName, int replicaId, boolean reload)
080          throws Exception {
081        return LOCATOR.getRegionLocations(replicaId, reload).get();
082      }
083    });
084  }
085}