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.assertNotEquals;
021
022import java.io.IOException;
023import java.util.concurrent.ExecutionException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039// Categorized as a large test so not run as part of general 'test' suite (which is small
040// and mediums). This test fails if networking is odd -- say if you are connected to a
041// VPN... See HBASE-23850
042@Category({ LargeTests.class, ClientTests.class })
043public class TestAsyncTableRSCrashPublish {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestAsyncTableRSCrashPublish.class);
048
049  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
050
051  private static TableName TABLE_NAME = TableName.valueOf("Publish");
052
053  private static byte[] FAMILY = Bytes.toBytes("family");
054
055  @BeforeClass
056  public static void beforeClass() throws Exception {
057    UTIL.getConfiguration().setBoolean(HConstants.STATUS_PUBLISHED, true);
058    /*
059     * Below is code for choosing a NetworkInterface and then setting it into configs so can be
060     * picked up by the client and server. String niName =
061     * UTIL.getConfiguration().get(HConstants.STATUS_MULTICAST_NI_NAME); NetworkInterface ni; if
062     * (niName != null) { ni = NetworkInterface.getByName(niName); } else { String mcAddress =
063     * UTIL.getConfiguration().get(HConstants.STATUS_MULTICAST_ADDRESS,
064     * HConstants.DEFAULT_STATUS_MULTICAST_ADDRESS); InetAddress ina =
065     * InetAddress.getByName(mcAddress); boolean inet6Address = ina instanceof Inet6Address; ni =
066     * NetworkInterface.getByInetAddress(inet6Address? Addressing.getIp6Address():
067     * Addressing.getIp4Address()); }
068     * UTIL.getConfiguration().set(HConstants.STATUS_MULTICAST_NI_NAME, ni.getName());
069     */
070    UTIL.startMiniCluster(2);
071    UTIL.createTable(TABLE_NAME, FAMILY);
072    UTIL.waitTableAvailable(TABLE_NAME);
073  }
074
075  @AfterClass
076  public static void afterClass() throws Exception {
077    UTIL.shutdownMiniCluster();
078  }
079
080  @Test
081  public void test() throws IOException, ExecutionException, InterruptedException {
082    Configuration conf = UTIL.getHBaseCluster().getMaster().getConfiguration();
083    try (AsyncConnection connection = ConnectionFactory.createAsyncConnection(conf).get()) {
084      AsyncNonMetaRegionLocator locator =
085        ((AsyncConnectionImpl) connection).getLocator().getNonMetaRegionLocator();
086      connection.getTable(TABLE_NAME).get(new Get(Bytes.toBytes(0))).join();
087      ServerName serverName =
088        locator.getRegionLocationInCache(TABLE_NAME, HConstants.EMPTY_START_ROW)
089          .getDefaultRegionLocation().getServerName();
090      UTIL.getMiniHBaseCluster().stopRegionServer(serverName);
091      UTIL.waitFor(60000,
092        () -> locator.getRegionLocationInCache(TABLE_NAME, HConstants.EMPTY_START_ROW) == null);
093      connection.getTable(TABLE_NAME).get(new Get(Bytes.toBytes(0))).join();
094      assertNotEquals(serverName,
095        locator.getRegionLocationInCache(TABLE_NAME, HConstants.EMPTY_START_ROW)
096          .getDefaultRegionLocation().getServerName());
097    }
098  }
099}