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 static org.junit.Assert.assertTrue;
022
023import java.util.Collection;
024import java.util.EnumSet;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.ClusterMetrics.Option;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.testclassification.MiscTests;
033import org.apache.hadoop.hbase.zookeeper.ZKUtil;
034import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
035import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
044
045@Category({ MiscTests.class, MediumTests.class })
046public class TestMetaReplicasAddressChange extends MetaWithReplicasTestBase {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestMetaReplicasAddressChange.class);
051
052  private static final Logger LOG = LoggerFactory.getLogger(TestMetaReplicasAddressChange.class);
053
054  @BeforeClass
055  public static void setUp() throws Exception {
056    startCluster();
057  }
058
059  @Test
060  public void testMetaAddressChange() throws Exception {
061    // checks that even when the meta's location changes, the various
062    // caches update themselves. Uses the master operations to test
063    // this
064    Configuration conf = TEST_UTIL.getConfiguration();
065    ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
066    String baseZNode =
067      conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
068    String primaryMetaZnode =
069      ZNodePaths.joinZNode(baseZNode, conf.get("zookeeper.znode.metaserver", "meta-region-server"));
070    // check that the data in the znode is parseable (this would also mean the znode exists)
071    byte[] data = ZKUtil.getData(zkw, primaryMetaZnode);
072    ServerName currentServer = ProtobufUtil.parseServerNameFrom(data);
073    Collection<ServerName> liveServers = TEST_UTIL.getAdmin()
074      .getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().keySet();
075    ServerName moveToServer =
076      liveServers.stream().filter(s -> !currentServer.equals(s)).findAny().get();
077    final TableName tableName = name.getTableName();
078    TEST_UTIL.createTable(tableName, "f");
079    assertTrue(TEST_UTIL.getAdmin().tableExists(tableName));
080    TEST_UTIL.getAdmin().move(RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedNameAsBytes(),
081      moveToServer);
082    assertNotEquals(currentServer, moveToServer);
083    LOG.debug("CurrentServer={}, moveToServer={}", currentServer, moveToServer);
084    TEST_UTIL.waitFor(60000, () -> {
085      byte[] bytes = ZKUtil.getData(zkw, primaryMetaZnode);
086      ServerName actualServer = ProtobufUtil.parseServerNameFrom(bytes);
087      return moveToServer.equals(actualServer);
088    });
089    TEST_UTIL.getAdmin().disableTable(tableName);
090    assertTrue(TEST_UTIL.getAdmin().isTableDisabled(tableName));
091  }
092}