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