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