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