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.Assert.assertNotNull;
021
022import java.util.concurrent.ExecutionException;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.AfterClass;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
036
037@Category({ MediumTests.class, ClientTests.class })
038public class TestAsyncTableLocatePrefetch {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestAsyncTableLocatePrefetch.class);
043
044  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
045
046  private static TableName TABLE_NAME = TableName.valueOf("async");
047
048  private static byte[] FAMILY = Bytes.toBytes("cf");
049
050  private static AsyncConnection CONN;
051
052  private static AsyncNonMetaRegionLocator LOCATOR;
053
054  @BeforeClass
055  public static void setUp() throws Exception {
056    TEST_UTIL.getConfiguration().setInt(AsyncNonMetaRegionLocator.LOCATE_PREFETCH_LIMIT, 100);
057    TEST_UTIL.startMiniCluster(3);
058    TEST_UTIL.createMultiRegionTable(TABLE_NAME, FAMILY);
059    TEST_UTIL.waitTableAvailable(TABLE_NAME);
060    CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
061    LOCATOR = new AsyncNonMetaRegionLocator((AsyncConnectionImpl) CONN);
062  }
063
064  @AfterClass
065  public static void tearDown() throws Exception {
066    Closeables.close(CONN, true);
067    TEST_UTIL.shutdownMiniCluster();
068  }
069
070  @Test
071  public void test() throws InterruptedException, ExecutionException {
072    assertNotNull(LOCATOR.getRegionLocations(TABLE_NAME, Bytes.toBytes("zzz"),
073      RegionReplicaUtil.DEFAULT_REPLICA_ID, RegionLocateType.CURRENT, false).get());
074    // we finish the request before we adding the remaining results to cache so sleep a bit here
075    Thread.sleep(1000);
076    // confirm that the locations of all the regions have been cached.
077    assertNotNull(LOCATOR.getRegionLocationInCache(TABLE_NAME, Bytes.toBytes("aaa")));
078    for (byte[] row : HBaseTestingUtil.KEYS_FOR_HBA_CREATE_TABLE) {
079      assertNotNull(LOCATOR.getRegionLocationInCache(TABLE_NAME, row));
080    }
081  }
082}