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