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.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
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.jupiter.api.AfterAll;
030import org.junit.jupiter.api.BeforeAll;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
035
036/**
037 * Fix an infinite loop in {@link AsyncNonMetaRegionLocator}, see the comments on HBASE-21943 for
038 * more details.
039 */
040@Tag(MediumTests.TAG)
041@Tag(ClientTests.TAG)
042public class TestAsyncTableLocateRegionForDeletedTable {
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 byte[] QUALIFIER = Bytes.toBytes("cq");
051
052  private static byte[] VALUE = Bytes.toBytes("value");
053
054  private static AsyncConnection ASYNC_CONN;
055
056  @BeforeAll
057  public static void setUpBeforeClass() throws Exception {
058    TEST_UTIL.startMiniCluster(3);
059    TEST_UTIL.createTable(TABLE_NAME, FAMILY);
060    TEST_UTIL.waitTableAvailable(TABLE_NAME);
061    TEST_UTIL.getAdmin().balancerSwitch(false, true);
062    ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
063    assertFalse(ASYNC_CONN.isClosed());
064  }
065
066  @AfterAll
067  public static void tearDownAfterClass() throws Exception {
068    Closeables.close(ASYNC_CONN, true);
069    assertTrue(ASYNC_CONN.isClosed());
070    TEST_UTIL.shutdownMiniCluster();
071  }
072
073  @Test
074  public void test() throws IOException, InterruptedException {
075    try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
076      for (int i = 0; i < 100; i++) {
077        table.put(new Put(Bytes.toBytes(i)).addColumn(FAMILY, QUALIFIER, VALUE));
078      }
079    }
080    TEST_UTIL.getAdmin().split(TABLE_NAME, Bytes.toBytes(50));
081    TEST_UTIL.waitFor(60000,
082      () -> TEST_UTIL.getMiniHBaseCluster().getRegions(TABLE_NAME).size() == 2);
083    // make sure we can access the split regions
084    try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
085      for (int i = 0; i < 100; i++) {
086        assertFalse(table.get(new Get(Bytes.toBytes(i))).isEmpty());
087      }
088    }
089    // let's cache the two old locations
090    AsyncTableRegionLocator locator = ASYNC_CONN.getRegionLocator(TABLE_NAME);
091    locator.getRegionLocation(Bytes.toBytes(0)).join();
092    locator.getRegionLocation(Bytes.toBytes(99)).join();
093    // recreate the table
094    TEST_UTIL.getAdmin().disableTable(TABLE_NAME);
095    TEST_UTIL.getAdmin().deleteTable(TABLE_NAME);
096    TEST_UTIL.createTable(TABLE_NAME, FAMILY);
097    TEST_UTIL.waitTableAvailable(TABLE_NAME);
098    // confirm that we can still get the correct location
099    assertFalse(ASYNC_CONN.getTable(TABLE_NAME).exists(new Get(Bytes.toBytes(99))).join());
100  }
101}