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