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