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